Friday, March 24, 2006

PIC: EXAMPLE 12

EXAMPLE 12

Using EEPROM memory

This example demonstrates write to and read from built-in EEPROM memory. The program works as follows. The main loop constantly reads EEPROM memory location at address 5 (decimal). This number is displayed on port D. The same loop tests the state of three push-buttons connected to port A. The push-buttons "INCREMENT" and "DECREMENT" have the same purpose like in example 7 - increment and decrement the variable "cnt" which is thereafter displayed on port B. The push-button "MEMO" enables that variable to be written to EEPROM memory. In order to check it, it is enough to press this push-button and switch off the device. On the next switch on, the program displays the value of the variable on port D (at the moment of writing, this value was displayed on port B).
Example 12 - Using EEPROM memory

Source Code

;********************** 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
       movwf       EEDAT           ; Writes data to temporary register
       banksel     EECON1
       bcf         EECON1,EEPGD
       bsf         EECON1,WREN     ; Write enabled
       
       bcf         INTCON,GIE      ; All interrupts disabled
       btfsc       INTCON,GIE
       goto        $-2
       
       movlw       55h
       movwf       EECON2
       movlw       H'AA'
       movwf       EECON2
       bsf         EECON1,WR

       btfsc       EECON1,WR       ; Wait for write to complete
       goto        $-1
       
       bsf         INTCON,GIE      ; Interrupt enabled
       bcf         EECON1,WREN
       goto        Loop            ; Tests push-buttons again
       end                         ; End of program