• AVR Freaks

Hot!SOLVED - PIC16F628A can't write/read to internal EEPROM

Author
picforthewin
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/05/15 03:48:30
  • Location: 0
  • Status: offline
2019/05/16 17:46:36 (permalink)
0

SOLVED - PIC16F628A can't write/read to internal EEPROM

Hi Guys,
My first post here so please go easy on me :)
 
I have spent a great deal of time trying to write to the internal eeprom in a 16F628A without success. My project is very simple. A single push button advances one of three LEDs eventually lighting all three. When powered down and then back on, I want the LEDs to come back on in the state they were at, at power down. IE: if two LEDs were lit, then the same two should come on at power up.
 
My software advances the LEDs no problem but it either doesn't write to the EEPROM or doesn't read the EEPROM at power up. I have tried pretty much all I can think of, I suspect I have a paging problem but a point in the right direction would be appreciated.
 
I'm using MPLAB IDE V8.92, writing in assembly language and programming with a PICKIT3. Code follows and any pointers gratefully received. Cheers, Mark

INIT
    MOVLW        0X07        ;MAKE PORT PINS DIGITAL
    MOVWF        CMCON
    BSF            RP0
    MOVLW        B'10000000'
    MOVWF        TRISA
    MOVLW        B'00000000'    
    MOVWF        TRISB
    CLRWDT
    MOVLW        B'01010111'    ;ENABLE PORT B PULLUPS
    MOVWF        OPTREG
    
    BCF            RP0
    BCF            RP1
    CLRF        PORTB        ;SET ALL PINS ON PORT B TO LOW (ALL OUTPUTS OFF)
    CLRF        PORTA        ;ALL PORT A PINS ARE OUTPUTS EXCEPT FOR THE SW INPUT - HIGH INPUT = OFF OR NOT ACTIVE - SW INPUT GOES LOW WHEN ACTIVE

    CLRF        LAMPS_REG
    CLRF        ON_TIMER
    CLRW

;**************** MAIN PROGRAM STARTS HERE ******************

TESTING
    BSF            OUT1            ; FLASH A LED TO SHOW POWER UP
    CALL        HALF_SEC
    BCF            OUT1
    CALL        HALF_SEC
    CALL        HALF_SEC
    CALL        HALF_SEC


START                            ; LOAD FROM EEPROM HERE

    CLRW
    BCF            RP0
    MOVLW        0XF1            ; ADDRESS OF FILES IN EEPROM MEMORY - 0X00 IS THE FIRST ADDRESS IN EEPROM
    BSF            RP0
    MOVWF        EEADR
    BSF            EECON1,0            ; SET BIT FOR READ
    BCF            RP0
    MOVF        EEDATA,W
    MOVWF        LAMPS_REG            ; PUT THE MEMORY INTO THE REG

    CALL        LAMPS_CHECK

MAIN
    BTFSS        BUTTON
    GOTO        MOVE        ; SWITCH GOES LOW WHEN PUSHED
    GOTO        MAIN        ; SW NOT PRESSED SO KEEP LOOKING

MOVE                        ; INC ILLUMINATED STACK HERE
    CALL        SHORT_SEC
    BTFSC        BUTTON        ; DOUBLE CHECK NOT A FALSE SW PUSH
    GOTO        MAIN        ; FALSE PRESS SO RETURN TO LOOP LOOKING

; TEST TO SEE WHICH LAMPS ARE ON

    BTFSS        LAMPS_REG,0        ; CHECK LAMPS IN ORDER FOR BEING ON
    GOTO        MAKE_1_ON
    BTFSS        LAMPS_REG,1
    GOTO        MAKE_2_ON
    BTFSS        LAMPS_REG,2
    GOTO        MAKE_3_ON

; ADD THE REST HERE FOR MORE LAMPS INCLUDING TURN ALL OFF IF THEY ARE ALL NOW ON

MAKE_1_ON
    BSF            LAMPS_REG,0        ; SET THE BIT IN THE REG    
    BSF            OUT1            ; TURN ON LAMP
    CALL        WRITE_NEW_LAMPS
    GOTO        MAIN
MAKE_2_ON
    BSF            LAMPS_REG,1
    BSF            OUT2
    CALL        WRITE_NEW_LAMPS
    GOTO        MAIN
MAKE_3_ON
    BSF            LAMPS_REG,2
    BSF            OUT3
    CALL        WRITE_NEW_LAMPS
    GOTO        MAIN

CLEAR_ALL                        ; WE GET HERE IF ALL LAMPS ARE ON AND WE WANT TO CLEAR THEM
    CLRF        LAMPS_REG
    CLRF        PORTB
    CLRF        PORTA
    GOTO        MAIN

WAITING_RELEASE
    BTFSS        BUTTON        ; WAIT FOR BUTTON TO BE RELEASED TO AVOID DOUBLE ADVANCE OF LEDS
    GOTO        WAITING_RELEASE
    GOTO        MAIN

;************************** CALLS BELOW HERE ****************************


WRITE_NEW_LAMPS                ; WRITE TO EEPROM
    MOVLW        0XF1
    BSF            RP0
    MOVWF        EEADR
    BSF            WREN

    BCF            RP0
    MOVF        LAMPS_REG,W
    BSF            RP0

    MOVWF        EEDATA
    MOVLW        H'55'
    MOVWF        EECON2
    MOVLW        H'AA'
    MOVWF        EECON2
    BSF            EECON1,1
    BTFSC        EECON1,1
    GOTO        $-1                ; WAIT FOR WRITE TO COMPLETE
    BCF            WREN
    BCF            RP0

    BTFSS        BUTTON
    GOTO        $-1

    RETURN

LAMPS_CHECK

    BTFSC        LAMPS_REG,0            ; CHECK EACH POSITION AND SET THE LAMP AS REQUIRED
    BSF            OUT1
    BTFSC        LAMPS_REG,1
    BSF            OUT2
    BTFSC        LAMPS_REG,2
    BSF            OUT3

    RETURN

; DELAY ROUTINES BELOW HERE

******************************************** END *********************************************

post edited by picforthewin - 2019/05/26 19:54:56
#1

8 Replies Related Threads

    DeutcheN
    Super Member
    • Total Posts : 120
    • Reward points : 0
    • Joined: 2010/09/01 12:35:40
    • Location: DM43BH
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/23 14:22:47 (permalink)
    0
     
    Try changing the BSF and BCF payloads.
     
    For example change:
     

     
        MOVLW        0XF1
        BSF            RP0
        MOVWF        EEADR
        BSF            WREN

        BCF            RP0
        MOVF        LAMPS_REG,W
        BSF            RP0
     

     
    To:

     
        MOVLW        0XF1
        BSF            STATUS,RP0
        MOVWF        EEADR
        BSF            EECON1,WREN

        BCF           STATUS, RP0
        MOVF        LAMPS_REG,W
        BSF            STATUS,RP0
     

     
     
    #2
    picforthewin
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/05/15 03:48:30
    • Location: 0
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/23 19:05:26 (permalink)
    0
    Thanks for the comments DeutcheN. I had already specified the status bits as equates but I changed things at your suggestion. Unfortunately no change. I am unsure if I cannot write or just can't read? Here is a snippet of the code that I changed at your suggestion.
     


    WRITE_NEW_LAMPS                ; WRITE TO EEPROM
        BSF            STATUS,RP0
        MOVLW        0XF1

        MOVWF        EEADR
        BSF            EECON1,WREN

        BCF            STATUS,RP0
        MOVF        LAMPS_REG,W
        BSF            STATUS,RP0

        MOVWF        EEDATA
        MOVLW        H'55'
        MOVWF        EECON2
        MOVLW        H'AA'
        MOVWF        EECON2
        BSF            EECON1,1
        BTFSC        EECON1,1
        GOTO        $-1                ; WAIT FOR WRITE TO COMPLETE
        BCF            EECON1,WREN
        BCF            STATUS,RP0

    #3
    1and0
    Access is Denied
    • Total Posts : 9720
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/23 19:21:23 (permalink)
    +2 (2)
    picforthewin
    I have spent a great deal of time trying to write to the internal eeprom in a 16F628A without success. 
    ...
       MOVLW        0XF1            ; ADDRESS OF FILES IN EEPROM MEMORY - 0X00 IS THE FIRST ADDRESS IN EEPROM
       BSF            RP0
       MOVWF        EEADR
     ...
    WRITE_NEW_LAMPS                ; WRITE TO EEPROM
       MOVLW        0XF1
       BSF            RP0
       MOVWF        EEADR

    Your PIC device has only 128 bytes of EEPROM data.  What value is 0xF1 ?
    #4
    1and0
    Access is Denied
    • Total Posts : 9720
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/23 19:28:10 (permalink)
    +2 (2)
    picforthewin
     
       BCF            RP0
       MOVLW        0XF1            ; ADDRESS OF FILES IN EEPROM MEMORY - 0X00 IS THE FIRST ADDRESS IN EEPROM
       BSF            RP0
       MOVWF        EEADR
       BSF            EECON1,0            ; SET BIT FOR READ
       BCF            RP0
       MOVF        EEDATA,W
       MOVWF        LAMPS_REG            ; PUT THE MEMORY INTO THE REG

    Also, you got a banking error here -- EEDATA is _not_ located in Bank0.
    #5
    picforthewin
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/05/15 03:48:30
    • Location: 0
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/24 17:54:16 (permalink)
    0
    Hello 1and0, thanks for your valuable input. I have read the datasheet again (for the 2K time) and I now see the addresses for EEPROM do indeed start at 0X00. I have corrected this and also corrected. I think? my banking error. Still no read or write although I'm still unsure if I am not reading or nor writing or both???
     
    Is there some way to narrow this down? Thanks again for the valuable pointers. My updated code here:
     


    START                            ; LOAD FROM EEPROM HERE

        CLRW
        MOVLW        0X10            ; ADDRESS OF FILES IN EEPROM MEMORY - 0X00 IS THE FIRST ADDRESS IN EEPROM
        BSF            STATUS,RP0
        MOVWF        EEADR
        BSF            EECON1,0            ; SET BIT FOR READ
        MOVF        EEDATA,W
        BCF            STATUS,RP0

        MOVWF        LAMPS_REG            ; PUT THE MEMORY INTO THE REG
        CALL        LAMPS_CHECK

    MAIN
        BTFSS        BUTTON
        GOTO        MOVE        ; SWITCH GOES LOW WHEN PUSHED
        GOTO        MAIN        ; SW NOT PRESSED SO KEEP LOOKING

    MOVE                        ; INC ILLUMINATED STACK HERE
        CALL        SHORT_SEC
        BTFSC        BUTTON        ; DOUBLE CHECK NOT A FALSE SW PUSH
        GOTO        MAIN        ; FALSE PRESS SO RETURN TO LOOP LOOKING

    ; TEST TO SEE WHICH LAMPS ARE ON

        BTFSS        LAMPS_REG,0        ; CHECK LAMPS IN ORDER FOR BEING ON
        GOTO        MAKE_1_ON
        BTFSS        LAMPS_REG,1
        GOTO        MAKE_2_ON
        BTFSS        LAMPS_REG,2
        GOTO        MAKE_3_ON
        GOTO        CLEAR_ALL

    ; ADD THE REST HERE FOR MORE LAMPS INCLUDING TURN ALL OFF IF THEY ARE ALL NOW ON

    MAKE_1_ON
        BSF            LAMPS_REG,0        ; SET THE BIT IN THE REG    
        BSF            OUT1            ; TURN ON LAMP
        CALL        WRITE_NEW_LAMPS
        GOTO        MAIN
    MAKE_2_ON
        BSF            LAMPS_REG,1
        BSF            OUT2
        CALL        WRITE_NEW_LAMPS
        GOTO        MAIN
    MAKE_3_ON
        BSF            LAMPS_REG,2
        BSF            OUT3
        CALL        WRITE_NEW_LAMPS
        GOTO        MAIN

    CLEAR_ALL                        ; WE GET HERE IF ALL LAMPS ARE ON AND WE WANT TO CLEAR THEM
        CLRF        LAMPS_REG
        CALL        WRITE_NEW_LAMPS
        CLRF        PORTB
        CLRF        PORTA
        GOTO        MAIN

    WAITING_RELEASE
        BTFSS        BUTTON        ; WAIT FOR BUTTON TO BE RELEASED TO AVOID DOUBLE ADVANCE OF LEDS
        GOTO        WAITING_RELEASE
        GOTO        MAIN

    ;************************** CALLS BELOW HERE ****************************


    WRITE_NEW_LAMPS                ; WRITE TO EEPROM

        MOVF        LAMPS_REG,W

        BSF            STATUS,RP0

        MOVLW        0X10
        MOVWF        EEADR
        BSF            EECON1,WREN
        MOVWF        EEDATA
        MOVLW        H'55'
        MOVWF        EECON2
        MOVLW        H'AA'
        MOVWF        EECON2
        BSF            EECON1,1
        BTFSC        EECON1,1
        GOTO        $-1                ; WAIT FOR WRITE TO COMPLETE
        BCF            EECON1,WREN

        BCF            STATUS,RP0

        BTFSS        BUTTON
        GOTO        $-1

        RETURN

    LAMPS_CHECK

        BTFSC        LAMPS_REG,0            ; CHECK EACH POSITION AND SET THE LAMP AS REQUIRED
        BSF            OUT1
        BTFSC        LAMPS_REG,1
        BSF            OUT2
        BTFSC        LAMPS_REG,2
        BSF            OUT3

        RETURN

    ; DELAY ROUTINES BELOW HERE

     
    #6
    1and0
    Access is Denied
    • Total Posts : 9720
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/24 19:03:58 (permalink)
    +2 (2)
    picforthewin
    Hello 1and0, thanks for your valuable input. I have read the datasheet again (for the 2K time) and I now see the addresses for EEPROM do indeed start at 0X00. I have corrected this and also corrected. I think? my banking error. Still no read or write although I'm still unsure if I am not reading or nor writing or both???
     
    Is there some way to narrow this down? Thanks again for the valuable pointers. My updated code here:

    WRITE_NEW_LAMPS                ; WRITE TO EEPROM

        MOVF        LAMPS_REG,W

        BSF            STATUS,RP0

        MOVLW        0X10
        MOVWF        EEADR
        BSF            EECON1,WREN
        MOVWF        EEDATA


    It is your write routine, where LAMPS_REG is not stored into EEDATA to be written to the EEPROM.  Anyway, you have to learn to go thru your code line by line to find error, etc.
    #7
    mpgmike
    Super Member
    • Total Posts : 278
    • Reward points : 0
    • Joined: 2014/01/23 17:27:06
    • Location: NJ
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/26 10:09:34 (permalink)
    0
    You still have your EEADR in the W Register when you write to EEDATA.

    I don't need the world to know my name, but I want to live a life so all my great-grandchildren proudly remember me.
    #8
    picforthewin
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/05/15 03:48:30
    • Location: 0
    • Status: offline
    Re: Help with PIC16F628A can't write/read to internal EEPROM 2019/05/26 19:52:43 (permalink)
    +3 (3)
    Thanks to all for your positive contributions to my problem, which is now solved I'm pleased to say!
     
    To close off, and leave a crumb for those that come looking later, apart from the issues listed above, the final thing that stumped me was loading the W reg with the data AFTER switching banks when it should be done prior to setting up for the write BUT then load the data as the first thing after bank switching so as not to corrupt the data in W.
     
    Thanks again to all, here is the snippet (that works a treat).

     

    WRITE_NEW_LAMPS                ; WRITE TO EEPROM

        MOVF        LAMPS_REG,W        ; MOVE DATA INTO THE W REG BEFORE CHANGING BANKS THEN PUT IN EEDATA BEFORE DOING ANYTHING ELSE
     

        BSF            STATUS,RP0
     

        MOVWF        EEDATA
        MOVLW        0X05
        MOVWF        EEADR
        BSF            EECON1,WREN
        MOVLW        H'55'
        MOVWF        EECON2
        MOVLW        H'AA'
        MOVWF        EECON2
        BSF            EECON1,WR
        BTFSC        EECON1,WR
        GOTO        $-1                ; WAIT FOR WRITE TO COMPLETE
        BCF            EECON1,WREN

        BCF            STATUS,RP0

     
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5