Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Help with PIC16F628A can't write/read to internal EEPROM

1 post / 0 new
Mark
Mark's picture
Offline
Last seen: 1 month 3 weeks ago
Joined: 15/05/2019 - 06:20
Help with 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 *********************************************