[Company Logo Image]        Annex A 

Home Up Contents Search Articles Caveat Purpose Notice                                                           

This is the Assembler code written in pseudo-English.

Note that the compiler ignores anything following a semi-colon.  This makes it possible to "comment the code" on a line-by-line basis.  You are encouraged to document EVERY step.

;*****Set up the Constants**** 

STATUS equ 03h ;Address of the STATUS register
TRISA equ 85h ;Address of the tristate register for port A
PORTA equ 05h ;Address of Port A
COUNT1 equ 08h ;First counter for our delay loops
COUNT2 equ 09h ;Second counter for our delay loops 

;****Set up the port**** 

bsf STATUS,5 ;Switch to Bank 1
movlw 00h ;Set the Port A pins
movwf TRISA ;to output.
bcf STATUS,5 ;Switch back to Bank 0 

;****Turn the LED on**** 
Start movlw 02h ;Turn the LED on by first putting
movwf PORTA ;it into the w register and then
;on the port

;****Start of the delay loop 1****

Loop1 decfsz COUNT1,1 ;Subtract 1 from 255
goto Loop1 ;If COUNT is zero, carry on.
decfsz COUNT2,1 ;Subtract 1 from 255
goto Loop1 ;Go back to the start of our
;This delay
;255 to zero, 255 times

;****Delay finished, now turn the LED off**** 

movlw 00h ;Turn the LED off by first putting
movwf PORTA ;it into the w register and then on
;the port 

;****Add another delay**** 

Loop2 decfsz COUNT1,1 ;This second loop keeps the
goto Loop2 ;LED turned off long enough for
decfsz COUNT2,1 ;us to see it turned off
goto Loop2 ; 

;****Now go back to the start of the program

goto Start ;go back to Start and turn
;on again

;****End of the program**** 

end ;Needed by some compilers, ;and also just in case we miss
;the goto instruction.

CatsEyes suggests the following modifications:

          list      p=16F84A            ; list directive to define processor
 
          #include <p16F84A.inc>        ; processor specific variable definitions
 
          errorlevel  -302              ; suppress message 302 from list file
 
          __CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
 
The first line specifies the processor you are using, so you don't have to set it up in MPLAB (or a command line argument if you're using the command line to assemble your code).
 
The second line will set up all the processor-specific variable names for things like Register Files, specific bits within the register files and configuration bits. In the LED flasher sample code, for example, you can eliminate the EQUates for the STATUS, TRISA and PORTA registers, as they're already defined in the include file. In addition, bits within the registers can be named instead of using the bit number, e.g.
 
          bsf       STATUS, RP0            ;Switch to Bank 1
 
The bank status bit is named "RP0" (which is EQUated to "5" in the include file). This corresponds more closely to the code samples given in the processor datasheets.
 
The third line suppresses the annoying "Register in operand not in bank 0." warnings. (Of course the bank bits are correct!) Oddly, this line was omitted from the 16F84A code template.
 
Finally, the fourth line specifies the configuration word (so it is saved with the code, rather than you having to remember to specify it in MPLAB and/or in the programmer software). The configuration word specifies some fixed operating parameters of the chip such as oscillator modes, code protection etc. Refer to the section "SPECIAL FEATURES OF THE CPU" of the datasheet, under "Configuration Bits."
 

Home ] Up ]                                                                                                                                               

Send mail to ironsidz@hotmail.com with questions or comments about this web site.
Copyright © 2003 
Last modified: March 18, 2004