PICkit™ 3 Starter Kit User’s Guide
DS41628B-page 50 2012 Microchip Technology Inc.
The variables, Delay1 and Delay2 will rollover from 0 to 255. This is why it is unnec-
essary to assign a value to the Delay1 and Delay2 variables before decrementing.
3.3.6.2 PIC18
While the Enhanced Core has its 16 bytes of general purpose RAM that is shared
between all banks, the PIC18 has its equivalent at locations 0x00->0x5F. It gives the
user access to 96 bytes, which the user can access without specifying the bank.
EXAMPLE 3-11:
3.3.7 C Language
3.3.7.1 BOTH
Delay loops in ‘C’ that are based solely on a variable counter result in an unpredictable
delay time. The compiler essentially breaks down your code into assembly before
being programmed onto the PIC MCU. Depending on how efficient the compiler is and
how well the program is written will determine the length of time the loop takes. A library
function a for delay loop is the preferred method.
For completion, this lesson includes both ways. The commented out section at the end
of the code in this lesson uses the accurate delay function that is bundled in with the
XC8 compiler. Subsequent lessons will use the built-in delay macro.
EXAMPLE 3-12:
In order to take advantage of this highly accurate routine, the PIC MCU processor
speed must be defined.
EXAMPLE 3-13:
A variable, delay, is created and then decremented before toggling an LED. The ^
XORs the pin with ‘1’ to create the toggling affect. If the optimization of the compiler is
heightened or lowered, the delay will increase or decrease since the compiler produces
different code for each optimization level.
cblock 0x00 ; Access RAM
Delay1 ; Define two file registers for the delay loop in shared memory
Delay2
endc
#define _XTAL_FREQ 500000 //Used by the HI-TECH delay_ms(x) macro
delay = 7500;
while (1) {
while(delay-- != 0)continue; //each instruction is 8us (1/(500KHz/4))
LATCbits.LATC0 ^= 1; //toggle the LED
delay = 7500; //assign a value since it is at 0 from the
delay loop