256 loops and then jumps to the DECFSZ command, which
operates on the Delay2 variable. If Delay2 is not zero, then
the GOTO OndelayLoop command line puts us back to
working on the Delay1 variable 256 more times.
I mentioned that all assembly instructions take one
clock count, but actually GOTOs take two. So, that first
loop takes three clock counts (DECFSZ + GOTO). The
second loop also uses three clock counts each time it
jumps us back to the OndelayLoop label. If I multiply all this
out, I get [( 3 256) + 3] 256 = 197,676 instructions. At
the one microsecond per instruction speed, that equates to
197.676 microseconds or close to 200 milliseconds of
delay. That is slow enough for the human eye to see it.
; Waste time.
; The Inner loop takes
; 3 instructions per
; loop 256 loops =
; 768 instructions
; The outer loop takes
; and additional 3
; instructions per loop
; 256 loops
; (768+3) 256 =
; 197376 instructions /
; 1M instructions per
; second = 0.197 sec.,
; call it a two-tenths
; of a second.
The next instruction simply BCFs the PortC pin to put
the LED at a low voltage, and the LED is off.
bcf PORTC,0 ; Turn off LED C0
We add another delay with a different label, to create
the off delay of 200 microseconds.
decfsz ; same delay as above
GETTING STARTED WITH PICs
A simple GOTO command puts the program back at
the top to do it all again, thus creating a blinking LED.
goto MainLoop ; Do it again...
An END command closes out the program.
I can only cover so much territory in a short magazine
article, but hopefully I broke down these simple programs
enough so you can understand that assembly language is not
so scary. Some will say, “why write all that code when a few
lines of PICBASIC PRO will accomplish the same task?” The
reason is precision. There are times when you need to know
exactly how much time it takes to do a certain task, and the
compiler you choose — C or PICBASIC PRO or any other —
may not offer that level of detail or allow you to get down to
the accuracy you need. That is why most compilers allow you
to insert assembly code inside the C or Basic program. Having
the assembly language background makes you far more
prepared to solve your application’s accuracy problems.
The Microchip assembler manual can be downloaded
Doc/33014J.pdf. This will give you all the details you need
on how to write assembly code. Microchip even offers
classes on how to use the MPLAB IDE and assembly at their
Regional Training Centers (RTC). You can find the latest
classes and RTC locations by visiting the website at
www.microchip.com/rtc. Most classes are $49 for a half day
and $99 for a full day. I recommend you check them out.
As you can see, my column has changed to every other
month, which leaves me less time to update you on
everything new. If you have an idea for a future column or
just a question, please send your feedback to chuck@
elproducts.com. I do try to answer every email. Until next
time, keep on programming ... NV
Order online at:
Development Tools for PIC® MCUs
microEngineering Labs, Inc.
Phone: (719) 520-5323
Fax: (719) 520-1867
Colorado Springs, CO 80960
With Accessories for $119.95:
March 2008 17