Writing to EEPROM on PIC

10,779

Solution 1

On page 122 of the 16F690 datasheet, it details the proper way to write to EEPROM:

BANKSEL EEADR                   ;
MOVF    DATA_EE_ADDR, W;
MOVWF   EEADR          ;Data Memory Address to write
MOVF    DATA_EE_DATA, W;
MOVWF   EEDAT                   ;Data Memory Value to write
BANKSEL EECON1                  ;
BCF     EECON1, EEPGD ;Point to DATA memory
BSF     EECON1, WREN   ;Enable writes
BCF     INTCON, GIE             ;Disable INTs.
BTFSC   INTCON, GIE             ;SEE AN576
GOTO    $-2
; BEGIN REQUIRED SEQUENCE
MOVLW   55h            ;       
MOVWF   EECON2         ;Write 55h
MOVLW   AAh                     ;
MOVWF   EECON2                  ;Write AAh
BSF     EECON1, WR              ;Set WR bit to begin write
BSF     INTCON, GIE             ;Enable INTs.
SLEEP                  ;Wait for interrupt to signal write complete
; END REQUIRED SEQUENCE
BCF     EECON1, WREN   ;Disable writes
BANKSEL 0x00           ;Bank 0

I noticed that you are specifically missing this line:

 BCF     EECON1, EEPGD ;Point to DATA memory

If EEPGD is always set, then you'll try to write to program memory (aka overwrite the flash program memory) which should always fail unless you've gone out of your way to specifically enable that.

Aside from that, as far as I can tell from reading your code, everything else looks fine. It's okay that you're polling EECON1.WR instead of setting an interrupt. It will cost you more power than putting the device to sleep, but of course you should just worry about one thing at a time.

Solution 2

If you use the high tech compiler, writing to EEPROM is as simple as
#include <pic.h>
and then in main,
EEPROM_WRITE(0, 0x00); or really;

EEPROM_WRITE(location to write to, value to write to it);

I actually haven't been brave enough to write my PIC in assembly.

Share:
10,779
jcoder
Author by

jcoder

Updated on June 04, 2022

Comments

  • jcoder
    jcoder about 2 years

    Are there any PIC microcontroller programmers here?

    I'm learning some PIC microcontroller programming using a pickit2 and the 16F690 chip that came with it. I'm working through trying out the various facilities at the moment. I can sucessfully read a byte from the EEPROM in code if I set the EEPROM vaklue in MPLAB but I don't seem to be able to modify the value using the PIC itsself. Simply nothing happens and I don't read back the modified value, I always get the original which implies to me that the write isn't working?

    This is my code for that section, am I missing something? I know I'm doing a lot of unnecessary bank switches, I added most of them to ensure that being on the wrong bank wasn't the issue.

            ; ------------------------------------------------------
            ; Now SET the EEPROM location ZERO to 0x08
            ; ------------------------------------------------------
    
            BANKSEL EEADR
            CLRF    EEADR           ; Set EE Address to zero
    
            BANKSEL EEDAT
            MOVLW   0x08            ; Store the value 0x08 in the EEPROM
            MOVWF   EEDAT
    
            BANKSEL EECON1
            BSF     EECON1, WREN    ; Enable writes to the EEPROM
    
            BANKSEL EECON2
            MOVLW   0x55            ; Do the thing we have to do so
            MOVWF   EECON2          ; that writes can work
            MOVLW   0xAA
            MOVWF   EECON2
    
            BANKSEL EECON1      
            BSF     EECON1, WR      ; And finally perform the write
    
    WAIT
            BTFSC   EECON1, WR      ; Wait for write to finish
            GOTO    WAIT
    
            BANKSEL PORTC           ; Just to make sure we are on the right bank
    
  • jcoder
    jcoder about 14 years
    I'm clearin EEPGD in the previous code that sucessfully reads from the EEPROM and forgot to do it again here, but it should still be set to 0. Just in case I added and it still isn't working. Is there anything I need to set in my control flags or anything like that that I might have missed to allow writing?
  • jcoder
    jcoder about 14 years
    And yes, I'm still learning and I've not got as far as learning interrupts yet, that and timers are next on my list.
  • jcoder
    jcoder about 14 years
    Accepted this answer because it's correct and points out a small mistake in my code. I've got this to work by removing the unneeded back select statements. It seems that doing that in the middle of the programming sequence actually stops it working
  • John Slegers
    John Slegers about 14 years
    Section 10.1.3 of the datasheet also says "Interrupts should be disabled during this code segment.", referring to the "required sequence" in the code sample. Have you tried that?
  • jcoder
    jcoder about 14 years
    I can't edit my comment above. I meant "bank" select, not "back" select. I don't have interrupts enabled in my program so no need to disable them. I have it working now anyway but thanks for the suggestion