LPC Demo Board Lessons
© 2005 Microchip Technology Inc. DS51556A-page 15
3.2.2 Lesson 2: Delay Loop (Blink a LED)
The first lesson showed how to turn on a LED, this lesson shows how to make it blink.
While this might seem a trivial change from Lesson 1, the reasons will soon become
apparent.
New Instructions
CLRF Clear file register
INCF Increment file register
DECF Decrement file register
INCFSZ Increment file register, Skip next instruction if zero
DECFSZ Decrement file register, Skip next instruction if zero
GOTO Jump to a new location in the program
EXAMPLE 3-2: PICkit 2, LESSON 2: BLINK
While adding a BCF instruction and making it loop will make it blink, it will blink so fast
you won’t see it. It will only look dim. That loop requires 4 instruction times to execute.
The first instruction turns it on. The second one turns it off. The GOTO takes two instruc-
tion times, which means it will be on for 25% of the time.
As configured, the PICmicro executes 1 million instructions per second. At this rate, the
blinking needs to be slowed down so that the blinking can be seen, which can be done
by using a delay loop.
Loop
BSF PORTC,0 ;turn on LED C0
BCF PORTC,0 ;turn off LED C0
GOTO Loop ;do it again
Note: Counting cycles – Relating clock speed to instruction speed. The processor
requires 4 clocks to execute an instruction. Since the internal oscillator as
used in these lessons runs at 4 MHz, the instruction rate is 1 MHz.
Low Pin Count Demo Board User’s Guide
DS51556A-page 16 © 2005 Microchip Technology Inc.
Increment or Decrement a File Register
The INCFSZ and DECFSZ instructions add or subtract one from the contents of
the file register and skips the next instruction when the result is zero. One use is
in the delay loop as shown in Example 3-3.
CLRF Clears the counter location.
DECFSZ Decrements the location, and if the result is zero, the next
instruction is skipped.
EXAMPLE 3-3: DELAY LOOP
The GOTO Loop (in Example 3-3) backs up and does it again. This loop takes 3
instruction times; one for the decrement and two for the GOTO (see note) and the
counter will force it to go around 256 times, which takes it a total of 768 instruction times
(768 μs) to execute.
Even that is still too fast for the eye to see. It can be slowed down even more by adding
a second loop around this one.
The inner loop still takes 768 μs plus 3 for the outer loop, but now it’s executed another
256 times, 771 * 256 = 197376 μs = 0.197s.
Open Blink.asm and build the lesson. Next, import the hex file into the PICkit 2 and
program the device. Note the LED now flashes at about a 2 Hz rate.
Short Loop
CLRF Delay
Loop
DECFSZ Delay,f
GOTO Loop
Long Loop
CLRF Delay1
CLRF Delay2
Loop
DECFSZ Delay1,f
GOTO Loop
DECFSZ Delay2,f
GOTO Loop
Note: GOTO instructions take two instructions due to the pipelined design of the
processor. The processor fetches the next instruction while executing the
current instruction. When a program branch occurs, the fetched instruction
is not executed.
LPC Demo Board Lessons
© 2005 Microchip Technology Inc. DS51556A-page 17
3.2.3 Lesson 3: Rotate (Move the LED)
Building on Lessons 1 and 2, which showed how to light up a LED and then make it
blink with a delay loop, this lesson adds rotation. It will light up DS4 and then shift it to
DS3, then DS2, then DS1 and back to DS4.
New Instructions
MOVLW Loads Wreg with a literal value
MOVWF Moves the contents of Wreg to a file register
MOVF Moves the contents of a file register, either to Wreg or back
into the file register (see note)
RRF Rotate file register right
RLF Rotate file register left
Rotate Program Flow
First, initialize the I/O port and the Display,
Copy the Display variable to the I/O Port, then
Delay for a little while
Rotate the display
FIGURE 3-1: ROTATE PROGRAM FLOW
Note: Moving a file register to itself looks like a NOP at first. However, it has a
useful side effect in that the Z flag is set to reflect the value. In other words,
MOVF fileregister,f is a convenient way to test whether or not the
value is zero without affecting the contents of the Wreg.
Initialize I/O Port
Put Up Display
Delay
Rotate Display
Reset Display
Did it overflow?
No
Yes

DM164120-1

Mfr. #:
Manufacturer:
Microchip Technology
Description:
Development Boards & Kits - PIC / DSPIC PICkit 2 Low Pin Cnt Demo Board
Lifecycle:
New from this manufacturer.
Delivery:
DHL FedEx Ups TNT EMS
Payment:
T/T Paypal Visa MoneyGram Western Union

Products related to this Datasheet