programing eeprom of a pic16f684

Discussion in 'Programmer's Corner' started by DHEN, Jan 26, 2010.

  1. DHEN

    Thread Starter New Member

    Dec 7, 2009
    9
    0
    Working on a project with the PIC16F684 MU, where i need to store a value (30H) in the EEPROM. i followed the instructions in the manual for writing to the eeprom.

    Link To Manual: http://ww1.microchip.com/downloads/en/DeviceDoc/41202F-print.pdf

    This is my code:
    ------------------------------------------------
    BSF 03H,5 ; BANK 1

    MOVLW B'1'
    MOVWF 9BH;EEADR
    BCF 03H,5 ; BANK 0
    MOVLW 30H ; data i want stored in address 1 of the eeprom
    BSF 03H,5 ; BANK 1
    MOVWF 9AH;EEDAT

    BSF 03H,5 ; BANK 1
    BSF 9CH,2;WREN
    BCF 8BH,7;GIE
    BTFSC 8BH,7
    GOTO $-2
    MOVLW B'01010101'
    MOVWF 9DH ;EECON2
    MOVLW B'10101010'
    MOVWF 9DH ; EECON2
    BSF 9CH,1;WR
    BSF 8BH,7
    BCF 03H,5 ; BANK 0
    RETURN
    ------------------------------------------
    when i run this code in MPLAB and ISIS the eeprom stays at FF regardless of the value in register 30H :confused:.

    Any help would be greatly appreciated!!!!
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I do not work with your chip, but I have worked with the 16f690. Your write sequence to ECON2 must be checked I think it is 0x55, then 0xAA, and so set the WR bit in ECON1
     
  3. DHEN

    Thread Starter New Member

    Dec 7, 2009
    9
    0
    In my efforts to troubleshoot the problem i converted the HEX number to Binary.
     
  4. eblc1388

    Senior Member

    Nov 28, 2008
    1,542
    102
    EEPROM write takes many clock cycles to complete, and the program uses interrupts to wait for the write to finish. Because the PIC simulator model also implements this delayed writing timing behavior, the simulation might seem to run very slowly - please be patient. Don't expect an immediate change of value in the EEPROM.

    Just place a breakpoint after the whole EEPROM writing routine. Then go and have a cup of coffee, relax.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Sorry I forget to mention this in my last mail. Then using the MPLAB SIM tool, do not run at max speed. In the MPLAB toolbar select Debugger->setting and the select the OSC/TRace tab. Set your CPU speed to 4MHz. This work on my computer, and I hope it will work on your.
    Here is a correct code. You code was somewhat messy. Please post code with better comments next time. Even if you are frustrated and struggle big time like now.
    Ps in my code I did not include any interrupt configuration. But the data sheet recommend turning off interrupt before starting the EEPROM write cycle
    Code ( (Unknown Language)):
    1.  
    2. BANKSEL EEADR ;Select Memory Bank 2
    3. MOVLW 1;
    4. MOVWF EEADR ;Data Memory Address to write
    5. MOVLW 0x30
    6. MOVWF EEDAT ;Data Memory Value to write
    7. BANKSEL EECON1 ;Select Memory Bank 3
    8. BCF EECON1, EEPGD ;Point to DATA memory
    9. BSF EECON1, WREN ;Enable writes
    10. MOVLW 0x55 ;
    11. MOVWF EECON2 ;Write 55h
    12. MOVLW 0xAA ;
    13. MOVWF EECON2 ;Write AAh
    14. BSF EECON1, WR ;Set WR bit to begin write
    15. BANKSEL 0x00 ;Select Memory Bank 0
    16.  
     
  6. DHEN

    Thread Starter New Member

    Dec 7, 2009
    9
    0
    Sorry about the messy code. I do appreciate your help!

    on line 7 of your code:
    BCF EECON1, EEPGD ;Point to DATA memory

    i don't understand what EEPGD Means. the manual shows the EECON1 regisiter as:
    |address| Name | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
    | 9Ch | EECON1| —| —| —| —| WRERR| WREN| WR| RD|
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Sorry for that. I used 16f690 and that bit is PIC16F685/PIC16F689/PIC16F690 only.
     
Loading...