Using EEPROM to store variables...
I would like to store some values in EEPROM, and then read these out later when I need them.. The problem is what I I2CWRITE is not what I see when I I2Cread... Where I THINK I store a '1', I see it read back as '127' or '80'...???
Consider this - I want to store a baud rate for SERIN2 - a value of "16572".
So, I create a varable:
WBD var Word ' variable to be stored
LOC_WBD = 20 ' memory location to store it
Then I write the value to EEPROM
Assume: WBD = 16572, then ->
I2CWrite DPIN, CPIN, CHIP1, LOC_WBD, WBD.byte0
pause 10
I2CWrite DPIN, CPIN, CHIP1, LOC_WBD+1, WBD.byte1
pause 10
-> I'd EXPECT I just stored the variable 16572 into memory location 20 and 21 (Word)
THEN to get it back, I'd :
I2Cread DPIN, CPIN, CHIP1, LOC_WBD, WBD.byte0
I2Cread DPIN, CPIN, CHIP1, LOC_WBD+1, WBD.byte1
WBD should be 16572... but it is not. What did I miss?
Same goes for BYTE sized storage of varables..
B0 = 1
LOC_B0 = 22
I2CWrite DPIN, CPIN, CHIP1, LOC_BO, B0
pause 10
( I think I stored '1' in memory location 22)
BUT:
I2Cread DPIN, CPIN, CHIP1, LOC_BO, B0
LCDout "BO is: ",#BO
Displays 127 or 80 - not '1'...
I am missing something FUNDAMENTAL here..
Tom
The EEPROM mystery continues.. forgetting settings
Argg!! I just cannot get those values to stay in EEPROM through a power cycle!!
Here is what I've done:
18F2525
2 x 24LC512 connected as CHIP1, CHIP2
Chip1 con %10100000 ' address of chip1
Chip2 con %10100010 ' address of chip2
resistors in pace.
My program stores data to these chips, and this works. I can store data, then read it back and what I get is valid data (because it contains a checksum, if anything were wrong, the recieving software program would choke)
THEN, I decided I wanted to store the BAUD setting for download and waypoint transmssion, and a flag called PASSTHRU, and Waypoint on/off -- RIGHT IN THE EXTERNAL EEPROM so that I did not have to keep setting it every time I power up the system.
SO,
My program uses the chips memory as follows:
Chip1 address usage:
0-127 = special setting storage
128 - 63872 - logging data
Chip2 address usage:
0 - 63872 - logging data
In the initialization, I have set the following parameters:
' ------------ memory eeprom storage locations --------------------------------------
ADR_loc var word ' location to store the address
CHP_loc var byte ' will only be 1, 2, 3, 4
D_baud_loc var word ' download baud rate
pass_loc var byte ' the flag for passthru - 1 or 0
wpt_loc var byte ' the flag for waypoints - 1 or 0
W_baud_loc var word ' waypoint baud rate
ADR_loc = 100 ' next usable address
CHP_loc = 102 ' the chip we are on
D_baud_loc = 104 ' the download baud value
pass_loc = 108 ' the flag for passthru
wpt_loc = 110 ' the flag for waypoints
w_baud_loc = 112 ' waypoint baud rate
The values actually STORED in these locations are:
address var word ' current memory address
chipselect var byte ' current chip we are on 1 or 2
passthru var byte ' flag - allows data to pass thru to PC
Set_Wpt var byte ' flag allows waypoint data to be sent
wptBD var word ' baudrate of waypoint- ie 16572, etc.
D_baud var word ' baudrate of download speed for PC
SO, my program should ALWAYS know where to look for the various settings. In fact, I have proven that this works - when the program powers up, it confirms that each of these values is correct.
THEN,
I2Cread DPIN, CPIN, Chip1, wpt_loc, set_wpt ' waypoint flag storage
I2Cread DPIN, CPIN, Chip1, Pass_loc, passthru ' passthru flag storage
I2Cread DPIN, CPIN, Chip1, D_baud_loc, D_baud.byte0 ' baud2
I2Cread DPIN, CPIN, Chip1, D_baud_loc+1, D_baud.byte1 'baud2
I2Cread DPIN, CPIN, Chip1, W_baud_loc, wptbd.byte0 ' store waddress 1
I2Cread DPIN, CPIN, Chip1, W_baud_loc+1, wptbd.byte1 ' store waddress 2
I2Cread DPIN, CPIN, Chip1, ADR_loc, address.byte0' get the address 1
I2Cread DPIN, CPIN, Chip1, ADR_loc+1, address.byte1' get the address 2
I2Cread DPIN, CPIN, Chip1, CHP_loc, Chipselect ' get the chipselect
SO - at this point, you'd expect that my program now knows:
1. WHERE to look for the data, and
2. It has read the values from those locations into variables.
The PROBLEM is this.. This works great AS LONG AS I DON'T RESTART. Once I store data using "I2CWRITE" into these locations, I can reliably get them back whenver I want. If I restart, the values are all messed up.
As an example, I wrote a small program to allow me to query these values on a PC screen anytime to troubleshoot. Here is what I see:
Upon first startup:
PARAMETER MEMLOC
Download baud rate unknown: 0 104
Waypoint baud rate unknown: 5837 112
Unknown Streaming setting: 197 108
Unknown Waypoint Setting: 215 110
Last Memory Address: 0 100
Current Chip: 7 102
SO, it is properly looking in the right place, but seeinig garbage values. Once I tell it to store a set of 'Default' values that the program will recognize, I get this response:
PARAMETER MEMLOC
Download baud rate 9600 104
Waypoint baud rate 4800 112
Data WILL NOT stream to GPS-TEAM 108
Waypoint Output ON 110
Last Memory Address: 128 100
Current Chip: 1 102
This response is CORRECT. The program recognizes values and is reporting what the settings are.
If I power cycle, the old garbage values come back - the exact same ones!!
PARAMETER MEMLOC
Download baud rate unknown: 0 104
Waypoint baud rate unknown: 5837 112
Unknown Streaming setting: 197 108
Unknown Waypoint Setting: 215 110
Last Memory Address: 0 100
Current Chip: 7 102
-- so SOMETHING is writing garbage to my memory when I am not looking. Might it be my setup variables for the 18F2525
ADCON0 = %00110000' turn off - select port AN12 (nothing)
ADCON1 = %00001111' turn portA to digital I/O (same as dec 15)
CMCON = $07 ' turn off
HLVDCON = %00000000 ' turn off
CVRCON = $00000000 ' turn off
SSPCON1 = %11011100 ' supposed to be turning on I2C
SSPCON2 = %01111000 ' supposed to be turning on I2C
INTCON = %11110000 ' TG guess at 2525 interrups for all INT pins
INTCON2= %01110100 ' rising edge of INT0,1,2 RB0,1,2
RCON = %10000000 ' no priority interrups
T1CON = %11000000 'Timer1 1:1 prescale.
T0CON = 1
SOMETHING is writing to Chip1 memory locations when I am not looking, I just can't figure out WHAT.. ALL of my loops start at 128, even if I do NO OTHER MEMORY WRITES during a session, the variables STILL get messed up.
HELP!
Tom
EEPROM program again with error trapping
I got to wondering why I was getting nothing in the last build. So I trapped for failures to read and write. I did see that it was just not getting there, so tweaked the port assignments, etc. Here is what I get now:
** MCU started
** EEPROM write skipped
Reading:* Test EEPROM --> OK!
Reading:* Test EEPROM --> OK!
Reading:* Test EEPROM --> OK!
Reading:* Test EEPROM --> OK!
Reading:* Test EEPROM --> OK!
(I added "reading" to it)
The program is as follows:
------------------------------------------------------------------
'--------------------------------------------------------------------------------
'Tom,
'on my DataLoggers I have 16F876 or 18F252 and 2x 24LC512.
'The following code is tested.
'could you give it a try and let us know what the result was?
' -----[ Fuses ]------------------------------------------------
@ __CONFIG _CONFIG1H, _OSC_INTIO67_1H
@ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
@ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H
@ __CONFIG _CONFIG4L, _LVP_OFF_4L & 0bfh ;_XINST_OFF_4L
'
' -----[ Includes/Defines ]---------------------------------------------------------
include "modedefs.bas" 'include serout defines
OSCCON=%01111000
DEFINE OSC 8
While OSCCON.2=0:Wend
SCL var PortC.3 ' I2C Clock
SDA var PortC.4 ' I2C Data
ROM0 con %10100000 ' address of chip1
ADDR VAR word ' ROM Location
MonPort var PortC.6 ' Monitor Port
Mode con 16572 ' 4800 Baud 8N1 for Monitor Port
Test var byte[21] ' Test Array
I2Cdelay con 10 ' Delay after I2Cwrite
Temp var byte
CLEAR
SEROUT2 MonPort,Mode,[13,10,"** MCU started",13,10,13,10]
ADDR=100
I2CREAD SDA,SCL,ROM0,ADDR,[Temp]
IF Temp="*" THEN GOTO SkipWrite
I2CWRITE SDA,SCL,ROM0,ADDR,_
[$2A,$20,$54,$65,$73,$74,$20,$45,$45,$50,$52,$4F,_
$4D,$20,$2D,$2D,$3E,$20,$4F,$4B,$21],failw
PAUSE I2Cdelay
SEROUT2 MonPort,Mode,["** EEPROM written",13,10,13,10]
GOTO Loop
SkipWrite:
SEROUT2 MonPort,Mode,["** EEPROM write skipped",13,10,13,10]
Loop:
I2CREAD SDA,SCL,ROM0,addr,[str test\21],failr
SEROUT2 MonPort,Mode,["Reading:", STR Test\21,13,10]
PAUSE 1000
GOTO Loop
failr:
serout2 monport, mode,["failure to read",10,13]
return
failw:
serout2 monport, mode,["failure to write",10,13]
return
END