Specifications

mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix B: Examples
Example 12:
;********************** Header **********************************************
;*********** Defining variables in program **********************************
cblock 0x20 ; Block of variables starts at address 20h
HIcnt
LOcnt
LOOPcnt
cnt
endc ; End of block
;****************************************************************************
ORG 0x000 ; Reset vector
nop
goto main ; Go to start of the program (label "main")
;****************************************************************************
include "pause.inc"
include "button.inc"
;****************************************************************************
main
banksel ANSEL ; Selects bank containing ANSEL
clrf ANSEL
clrf ANSELH ; All pins are digital
banksel TRISB
bsf TRISA, 0 ; Input pin
bsf TRISA, 1 ; Input pin
bsf TRISA, 2 ; Input pin
clrf TRISB ; All port B pins are outputs
clrf TRISD ; All port D pins are outputs
banksel PORTB
clrf PORTB ; PORTB=0
clrf PORTD ; PORTD=0
clrf cnt ; cnt=0
Loop
banksel PORTA
button PORTA,0,0,Increment
button PORTA,1,0,Decrement
button PORTA,2,0,Save
banksel EEADR
movlw .5 ; Reads EEPROM memory location
movwf EEADR ; at address 5
banksel EECON1
bcf EECON1,EEPGD
bsf EECON1,RD ; Reads data from EEPROM memory
banksel EEDATA
movfw EEDATA ; Moves data to W
banksel PORTD
movwf PORTD ; Data is moved from W to PORTD
goto Loop
Increment ; Increments number on port B
incf cnt, f
movf cnt, w
movwf PORTB
goto Loop
Decrement ; Decrements number on port B
decf cnt, f
movf cnt, w
movwf PORTB
goto Loop
Save ; Copies data from port B to EEPROM
banksel EEADR ; memory location at address 5
movlw .5
movwf EEADR ; Writes address
banksel PORTB
movfw PORTB ; Copies port B to register W
banksel EEDAT
http://www.mikroe.com/en/books/picmcubook/appb/ (40 of 54)5/3/2009 11:36:02 AM