Question

I'm working on a project in which i need to be able to store a value to the PIC's EEPROM, I have managed to get the read to work but so far i am unable to get the PIC to write to the memory using PIC assembly. This is the code that i have been using , it is derived from the example write code from the data sheet, I am using the 16D630 PIC, and using the PIC assembly language.

MOVLW 0x02
MOVWF EEADR
MOVLW 0x01
MOVWF EEDATA
BSF STATUS,RP0 ;BANK 1
BSF EECON1,WREN ;ENABLE WRITE
BCF INTCON,GIE ;DISABLE INTS
MOVLW 0x55 ;UNLOCK WRITE
MOVWF EECON2 ;
MOVLW 0xAA ;
MOVWF EECON2 ;
BSF EECON1,WR ;START THE WRITE
BSF INTCON,GIE ;ENABLE INTS
Was it helpful?

Solution

Set appropriate memory bank before your program load EEADR and EEDATA values.

Use "Watch" window to control values in registers EEADR and EEDATA when you are stepping (F7) over program lines in debbug mode!

BSF STATUS,RP0       ;Set BANK 1 first !!!
MOVLW 0x02
MOVWF EEADR
MOVLW 0x01
MOVWF EEDATA

BSF EECON1,WREN ;ENABLE WRITE
BCF INTCON,GIE ;DISABLE INTS
MOVLW 0x55 ;UNLOCK WRITE
MOVWF EECON2 ;
MOVLW 0xAA ;
MOVWF EECON2 ;
BSF EECON1,WR ;START THE WRITE
BSF INTCON,GIE ;ENABLE INTS

OTHER TIPS

Before you enable the write WREN flag you should indicate whether you intend to write into program memory (flash) or into eeprom by setting/clearing EEPGD flag in EECON1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top