I spent quite some time trying to be able to write data to EEPROM, but no matter what I did it wouldn't work. Even when I copied the exact same code the book provides, it didn't do anything. I emailed Prof. Emami about it today and this is his response:


Hi Andrew, the reason why it doesn't write to the EEPROM is that you do not disable the EEPROM write mode at the end.Also the EEPROM write interrupt flag must be cleared. (These lines are dropped in the textbook by mistake.) Try the following code and let me know if it works:

BCF         INTCON, GIE          ; Only disable interrupts
                                                ; if already enabled,
                                                ; otherwise discard
BCF         STATUS, RP0          ;
BSF          STATUS, RP1          ; Bank 2
MOVF     ADDR, W                 ; Address to
MOVWF  EEADR                    ; Write to
MOVF     VALUE, W              ; Data to
MOVWF  EEDATA                 ; Write
BSF          STATUS, RP0          ; Bank 3
BCF         EECON1, EEPGD    ; Point to Data memory
BSF          EECON1, WREN     ; Enable writes
MOVLW  0x55                        ; Write 55th to
MOVWF  EECON2                  ; EECON2
MOVLW  0Xaa                        ; Write AAh to
MOVWF  EECON2                  ; EECON2
BSF          EECON1, WR          ; Start write operation
BTFSC     EECON1, WR          ; Wait for
GOTO      $-1                           ; Write to finish
BCF         EECON1, WREN     ; Disable writes
BCF         STATUS, RP0
BCF         STATUS, RP1          ; Bank0
BCF         PIR2, EEIF              ; Clear write interrupt flag
BSF          INTCON, GIE          ; Only enable interrupts
                                                ; if using interrupts,
                                                ; otherwise discard


Also, here is how to check what was written to EEPROM without any code:

-Open PICusb and write your code to the chip.
-Click on "Read" <- Important!!
-View "EEPROM Memory"

Remember that it won't work if you don't read first.

Views: 396


You need to be a member of Personal Mechatronics Lab to add comments!

Join Personal Mechatronics Lab

Comment by Andrew Ferrazzutti on February 19, 2012 at 1:32am

Tip: when using this code, the registers VALUE and ADDR may not work if you declared them with cblock. I just spent 40 minutes debugging, and for some reason, using udata works while cblock 0x20 doesn't do anything. Interestingly, cblock 0x70 does work, but that's a bad idea for other reasons.

Comment by Tatsuhiro Onodera on February 5, 2012 at 11:35am

Hi andrew thanks for this.
It really helped :)

© 2021   Created by PML.   Powered by

Badges  |  Report an Issue  |  Terms of Service