I have a task to write eeprom read/write subroutines for PIC16F84 microcontroller. I have written such code:
EEPROM_Read
clrw
bsf STATUS, RP0 ; Bank0
movlw DATA_EE_ADDR ; Any location in Data EEPROM memory space
movwf EEADR ; Address to read
bsf EECON1, RD ; EE Read
movlw EEDATA
bcf STATUS, RP0 ; Bank0
return
EEPROM_Write
bcf INTCON,GIE ;disable interrupts for EEPROM write
banksel EEADR ;bank 2
movfw DATA_EE_ADDR ;load write address to EEPROM address pointer
movwf EEADR
movfw DATA_EE_DATA ;load write data to EEPROM data buffer
movwf EEDATA
banksel EECON1 ;bank 3
bsf EECON1,WREN ;enable EEPROM write
movlw 0x55 ;unlock codes
movwf EECON2
movlw 0xAA
movwf EECON2
bsf EECON1,WR ;enable EEPROM write
btfsc EECON1,WR ;wait here until write completes
goto $-1
bcf EECON1,WREN ;write complete, disable write mode
banksel 0 ;bank 0
movlw 0 ;are interrupts being used?
xorwf INTCON,W
btfss STATUS,Z
bsf INTCON,GIE ;yes,enable interrupts
return
Is it right? I have problems with EEProm_Read. It seems that it doesn't work right.
According to datasheet information your EEPROM read code is wrong, because EEADR register is in memory bank 0 and not in bank 1 as in your code case:
Correct code is:
BCF STATUS, RP0 ; Bank 0
MOVLW DATA_EE_ADDR
MOVWF EEADR ; Address to read
BSF STATUS, RP0 ; Bank 1
BSF EECON1, RD ; EE Read
BCF STATUS, RP0 ; Bank 0
MOVF EEDATA, W ; W = EEDATA