<<Previous

Next>>

 NAP

NAP Period

Places the microcontroller into low power mode for short periods of time. During this NAP, power consumption is reduced to minimum. The listed periods are only approximate because the timing is derived from the Watchdog Timer which is R/C driven and can vary greatly from chip to chip and over temperature. Since NAP uses the Watchdog Timer, its timing is independent of the oscillator frequency.

Period

Delay (Approx.)

0

18 milliseconds

1

36 milliseconds

2

72 milliseconds

3

144 milliseconds

4

288 milliseconds

5

576 milliseconds

6

1.152 seconds

7

2.304 seconds

	NAP 7	' Low power pause for about 2.3 seconds

 ON DEBUG

ON DEBUG GOTO Label

ON DEBUG allows a debug monitor program to be executed between each PICBASIC PRO™ instruction

The method by which this happens is similar to the method used by ON INTERRUPT GOTO. Once ON DEBUG GOTO is encountered, a call to the specified debug label is inserted before each PICBASIC PRO™ instruction in the program. DISABLE DEBUG prevents the insertion of this call while ENABLE DEBUG resumes the insertion of the call.

A monitor routine may be written that is activated before each instruction. This routine can send data to an LCD or to a serial comm program. Any program information may be displayed or even altered in this manner. A small monitor program example is posted on our web site.

A word-sized system variable that resides in BANK0 is required to provide place to store the address the program was at before the monitor routine was called by ON DEBUG GOTO:

DEBUG_ADDRESS VAR WORD BANK0 SYSTEM

An Additional byte-sized variable may be used to return the level of the current program stack:

DEBUG_STACK VAR BYTE BANK0 SYSTEM

This level should never be greater than 4 for 12- and 14-bit core PICmicro MCUs, 12 for 17Cxxx devices or 27 for 18Cxxx devices in a PICBASIC PRO™ program.  The supplied variable will be incremented at each GOSUB and decremented at each RETURN. This variable should be set to 0 at the beginning of the program.

Adding this variable to a program does add overhead in that the value of the variable must be incremented and decremented at each GOSUB and RETURN.

 ON INTERRUPT

ON INTERRUPT GOTO Label

ON INTERRUPT allows the handling of microcontroller interrupts by a PICBASIC PRO™ subroutine.

There are 2 ways to handle interrupts using the PICBASIC PRO™ Compiler. The first is to write an assembly language interrupt routine. This is the way to handle interrupts with the shortest latency and lowest overhead. This method is discussed under advanced topics in a later section.

The second method is to write a PICBASIC PRO™ interrupt handler. This looks just like a PICBASIC PRO™ subroutine but ends with a RESUME.

When an interrupt occurs, it is flagged. As soon as the current PICBASIC PRO™ statement=s execution is complete, the program jumps to the BASIC interrupt handler at Label. Once the interrupt handler is complete, a RESUME statement sends the program back to where it was when the interrupt occurred, picking up where it left off.

DISABLE and ENABLE allow different sections of a PICBASIC PRO™ program to execute without the possibility of being interrupted. The most notable place to use DISABLE is right before the actual interrupt handler. Or the interrupt handler may be placed before the ON INTERRUPT statement as the interrupt flag is not checked before the first ON INTERRUPT in a program.

Latency is the time it takes from the time of the actual interrupt to the time the interrupt handler is entered. Since PICBASIC PRO™ statements are not re-entrant (i.e. you cannot execute another PICBASIC PRO™ statement while one is being executed), there can be considerable latency before the interrupt routine is entered.

PBP will not enter the BASIC interrupt handler until it has finished executing the current statement. If the statement is a PAUSE or SERIN, it could be quite a while before the interrupt is acknowledged. The program must be designed with this latency in mind. If it is unacceptable and the interrupts must be handled more quickly, an assembly language interrupt routine must be used.

Overhead is another issue. ON INTERRUPT will add an instruction after every statement to check whether or not an interrupt has occurred. DISABLE turns off the addition of this instruction. ENABLE turns it back on again. Usually the additional instruction will not be much of a problem, but long programs in small microcontrollers could suffer.

More than one ON INTERRUPT may be used in a program.

	ON INTERRUPT GOTO myint	' Interrupt handler is myint
	INTCON = %10010000	' Enable RB0 interrupt

	. . .

	DISABLE		' Disable interrupts in handler
myint: 	led = 1		' Turn on LED when interrupted
	RESUME		' Return to main program
	ENABLE		' Enable interrupts after handler

To turn off interrupts permanently (or until needed again) once ON INTERRUPT has been used, set INTCON to $80:

INTCON = $80

 OUTPUT

OUTPUT Pin

Make the specified Pin an output. Pin may be a constant, 0 - 15, or a variable that contains a number 0 - 15 (e.g. B0) or a pin name (e.g. PORTA.0).

	OUTPUT 0		' Make Pin0 an output
	OUTPUT PORTA.0		' Make PORTA, pin 0 an output

Alternatively, the pin may be set to an output in a much quicker and shorter way (from a generated code standpoint):

	TRISB.0 = 0		' Set PORTB, pin 0 to an output

All of the pins on a port may be set to outputs by setting the whole TRIS register at once:

	TRISB = %00000000	' Set all of PORTB to outputs
<<Previous

Next>>