PIC16x84 Data EEPROM use (source - John Becker's EPE PIC Tutorial)

;TUT33.ASM

;EEPROM USE DETAILS - THIS PROGRAM IS INCAPABLE OF BEING RUN AS IT STANDS IT NEEDS OTHER ROUTINES TO ACCESS IT. It just shows the codes needed for eeprom data write and read

;BASIC INITIALISATION COMMANDS:

#DEFINE PAGE0  bcf $03,5
#DEFINE PAGE1  bsf $03,5

STATUS: .EQU $03
W:      .EQU 0
F:      .EQU 1
STORE1: .EQU $0C        ;or any convenient address
        .ORG $04
        .ORG $05

;obligatory initialisation of variables for eeprom read/write operation:

EEDATA: .EQU $08
EECON1: .EQU $08
EEADR:  .EQU $09
EECON2: .EQU $09
INTCON: .EQU $0B

WR:     .EQU 1  ;eeprom write initiate flag
WREN:   .EQU 2  ;eeprom write enable flag
RD:     .EQU 0  ;eeprom read enable flag

;...............

;WRITE DATA TO EEPROM ROUTINE:

                        ;This routine is entered with W holding
                        ;the eeprom byte address at which data
                        ;is to be stored. The data to be stored
                        ;is held in STORE1.
SETPRM: movwf EEADR     ;Now copy W into EEADR to set eeprom address
        PAGE1
        bsf EECON1,WREN ;enable write flag
        PAGE0
        movf STORE1,W   ;get data value from STORE1 and hold in W
        movwf EEDATA    ;copy W into eeprom data byte register

MANUAL: PAGE1           ;these next 12 lines are according to
        movlw $55       ;Microchip manual dictated factors
        movwf EECON2    ;they cause the action required by
        movlw $AA       ;by the eeprom to store the data in EEDATA
        movwf EECON2    ;at the address held by EEADR.
        bsf EECON1,WR   ;set the ``perform write'' flag

CHKWRT: btfss EECON1,4  ;wait until bit 4 of EECON1 is set
        goto CHKWRT
        bcf EECON1,WREN ;disable write
        bcf EECON1,4    ;clear bit 4 of EECON1
        PAGE0
        bcf INTCON,6    ;clear bit 6 of INTCON
        return          ;and return

;..........

;READ DATA FROM EEPROM ROUTINE:

                        ;This routine is entered with W holding
                        ;the eeprom byte address to be read.
PRMGET: movwf EEADR     ;Now copy W into EEADR to set eeprom address
        PAGE1           ;
        BSF EECON1,RD   ;enable read flag
        PAGE0
        movf EEDATA,W   ;read eeprom data now in EEDATA into W
        return          ;and return


; *********************

Routines used in John Becker's PIC Data Dogger. All variables etc must be Equated prior to use. Most are as stated in PIC data sheet.

;READ DATA FROM EEPROM ROUTINE modified for PIC16F87x devices
; according to data sheet DS30292A page 43
; Routine entered with eeprom byte address preset

PRMGET: bsf STATUS,RP1    ; set for Page 2
        bsf STATUS,RP0    ; set for Page 3
        bcf EECON1,EEPGD  ; point to data memory
        bsf EECON1,RD     ; enable read flag
        bcf STATUS,RP0    ; set for Page 2 
        movf EEDATA,W     ; read eeprom data into W
        incf EEADR,F      ; inc address
        bcf STATUS,RP1    ; set for Page 0
        return

;..........

;WRITE DATA TO EEPROM ROUTINE modified for PIC16F87x devices
;according to data sheet DS30292A page 43

                        ;This routine is entered with W holding
                        ;the eeprom byte address at which data
                        ;is to be stored. The data to be stored
                        ;is held in STORE1.
SETPRM: bsf STATUS,RP1  ;set for Page 2
        bcf STATUS,RP0
        movwf EEADR     ;copy W into EEADR to set eeprom address
        bcf STATUS,RP1  ;set for Page 0
        movf STORE1,W   ;get data value from STORE1 and hold in W
        bsf STATUS,RP1  ;set for Page 2
        movwf EEDATA    ;copy W into eeprom data byte register
        bsf STATUS,RP0  ;set for page 3
        bcf EECON1,EEPGD ;point to Data memory
        bsf EECON1,WREN ;enable write flag

MANUAL: movlw $55       ;these lines cause the action required by
        movwf EECON2    ;by the eeprom to store the data in EEDATA
        movlw $AA       ;at the address held by EEADR.
        movwf EECON2
        bsf EECON1,WR   ;set the ``perform write'' flag
        bcf STATUS,RP1  ;set for Page 0
        bcf STATUS,RP0

CHKWRT: btfss PIR2,EEIF ;wait until bit 4 of PIR2 is set
        goto CHKWRT
        bcf PIR2,EEIF   ;clear bit 4 of PIR2
        return
