PDA

View Full Version : EEPROM Memory



MatthewM
- 24th April 2009, 15:42
I am toying around with a Lab X1 board, trying to make a program that turns on one of two LEDs, (toggled by a switch), then remembers which LED is on when I reset the power. Right now, it always turns on the opposite of the light the was on before I turned it off. Though this does satisfy my goal of using stored memory after power is turned off, I would like it to turn on the correct light, instead of the opposite on, and for the life of me, I can't figure out why. I am sure it is something simple that I am just missing.

If anyone could take a quick look at my code, it would be phenomenal.

Thanks,
Matt

EEPROM 1,[%00000001] 'set initial EEPROM Value

TRISB=%11110000 'set imputs and outputs
PORTB=%11111110
OPTION_REG.7=0
setting Var BYTE 'declare setting as a variable
READ 1,setting 'read EEPROM value to setting
PAUSE 100

LOOP:
IF setting=%00000001 THEN 'determines which LED to turn on
HIGH PORTD.0
LOW PORTD.1
ENDIF
IF setting=%00000010 THEN
LOW PORTD.0
HIGH PORTD.1
ENDIF
IF PORTB.4=1 THEN 'goto subroutine on button press
GOSUB SWITCH
ENDIF
GOTO LOOP

SWITCH:
IF setting=1 THEN 'if setting is 1, write 2 to EEPROM
WRITE 1,%00000010
ENDIF
IF setting=2 THEN 'if setting is 2, write 1 to EEPROM
WRITE 1,%00000001
ENDIF
READ 1,setting 'read EEPROM value to setting
WHILE PORTB.4=1 'wait for button release
PAUSEus 10
WEND
RETURN 'go back to main loop

END

Luckyborg
- 24th April 2009, 16:34
I've been looking over your code for about 10 minutes and nothing is jumping out at me. My only thought is something flaky is going on during the power down stage where PORTB.4 goes high momentarily. You could try adding a debounce or a small pause at the beginning of the SWITCH routine and see if there is any change.

David

MatthewM
- 24th April 2009, 16:41
Just tried that, didn't seem to make much difference...It toggled a bit smoother though.

My guess is somehow the program is causing the EEPROM value to switch at startup or shutdown. No clue how though.

Thank you,
Matt

Bruce
- 24th April 2009, 18:07
Have you tried switching this;


SWITCH:
IF setting=1 THEN 'if setting is 1, write 2 to EEPROM
WRITE 1,%00000010
ENDIF
IF setting=2 THEN 'if setting is 2, write 1 to EEPROM
WRITE 1,%00000001
ENDIF

To this;


SWITCH:
IF setting=1 THEN 'if setting is 1, write 1 to EEPROM
WRITE 1,%00000001
ENDIF
IF setting=2 THEN 'if setting is 2, write 2 to EEPROM
WRITE 1,%00000010
ENDIF