I need help with EEPROM Read/Write Procedures...

johnmobley

New Member
I would like some help writing to EEPROM. Datalogging is new to me and I have never worked with EEPROM before. When I looked at the code it seemed pretty simple. It would take a data "byte" stored in a symbol labeled info (b4) and through the sub-procedures read/write to EEPROM.

As I began writing my program I began to run into a problem that is now becoming important. One of the things I want to record are temperature values. To work with temperature I had to use data "word" because a byte was too small. To use the word I also created a symbol called info2 (w1). All of the EEPROM read/write sub-procedures are only looking for the info symbol (which is a "byte" value). How can I resolve these conflicts so that I can also read/write the temperature values to EEPROM? If more information is neccesary to help me, please let me know. Below is the basic code that I am using:



symbol EE_D_I = pin0 'EEPROM data pin (input 0)
symbol EE_D_O = 0 'EEPROM data pin (output 0)
symbol EE_CLK = 1 'EEPROM clock pin (output 1)
symbol EE_CS = 2 'EEPROM chip select pin (output 2)
symbol info = b4 'Data byte for heater status
symbol i = b5 'Scratchpad counter
symbol ShifReg = w3 'Scratchpad shift register
symbol address = b8 'EEPROM address
symbol page = b9 'EEPROM page
symbol clocks = b11 'Scratchpad clock counter
symbol info2 = w1 'Data word for temperature

for address = 0 to 59
readadc 1, info
If info = 255 Then
goto heaton
Else
goto heatoff
endif

CONTINUE:
readadc10 2, info2 'This reads the temperature of the heater box.
let page = 1
gosub eewrite
let info2 = info2 * 100 - 50508 / 43
serout 7, T2400, (254, 192, "Temp. = ", #info2, " ", %11011111, "F")
for b0 = 1 to 2 '1 x 60 second delay
pause 60000
next b0
next address
end

heaton:
serout 7, T2400, (254, 128, "Heaters are: ON ") 'This reads the state of the heater circuit.
let page = 0
gosub eewrite
goto CONTINUE

heatoff:
serout 7, T2400, (254, 128, "Heaters are: OFF") 'This reads the state of the heater circuit.
let page = 0
gosub eewrite
goto CONTINUE

' ***Below are the standard sub-procedures to read/write to EEPROM.
' ***There will be a comment above each procedure to designate it’s purpose.

' This sub-procedure writes a byte to the EEPROM
' ‘Data’ is written to ‘address’ on ‘page’

eewrite:
gosub eenabl 'Enable.
let ShifReg = $A00 'Get the write opcode.
let ShifReg = ShifReg | w4 'OR in the address bits.
let clocks = 12 'Send 12 bits to EEPROM.
high EE_CS 'Select EEPROM.
gosub eeout 'Send the opcode/address.
let ShifReg = info * 16 'Move bit 7 to bit 11.
let clocks = 8 'Eight data bits.
gosub eeout 'Send the data.
low EE_CS 'Deselect EEPROM.
gosub edisbl 'Write Protect.
return

' This sub-procedure reads a byte from the EEPROM.
' ‘Data’ is read from ‘address’ on ‘page’

eeread:
let ShifReg = $C00 'Get the read opcode.
let ShifReg = ShifReg | w4 'OR in the address bits.
let clocks = 12 'Send 12 bits to the EEPROM.
high EE_CS 'Chip select on.
gosub eeout 'Send the opcode/address.
gosub eein 'Receive the byte.
low EE_CS 'Deselect the EEPROM.
return

' Here are the internal sub-procedures that are called for by the eewrite and eeread subprocedures.

eenabl:
let ShifReg = $980 'Get the write-enable opcode.
high EE_CS 'Chip select on.
let clocks = 12 'Send 12 bits to EEPROM.
gosub eeout 'Send the opcode.
low EE_CS 'Deselect the EEPROM.
return

edisbl:
let ShifReg = $800 'Get the write-disable opcode.
high EE_CS 'Chip select on.
let clocks = 12 'Send 12 bits to EEPROM.
gosub eeout 'Send the opcode.
low EE_CS 'Deselect the EEPROM.
return

eein:
let info = 0 'Clear the data byte.
for i = 1 to 8 'Prepare to get 8 bits.
let info = info * 2 'Shift EEdata to the left
high EE_CLK 'Data valid on rising edge.
let info = info + EE_D_I 'Move data to 1st of variable.
low EE_CLK 'End of clock pulse.
next I 'Get another bit.
return

eeout:
for i = 1 to clocks 'Number of bits to shift out.
let EE_D_I = ShifReg / $800 'Get bit 12 of ShifReg.
pulsout EE_CLK, 10 'Output a brief clock pulse.
let ShifReg = ShifReg * 2 'Shift register to the left.
next i 'Send another bit.
return
 

MPep

Senior Member
Why not use W6? Seem to be the simplest solution.
It does not appear as though you are using B12 and B13 yet. :)
What PICAXE are you using?

For future reference, please try using the [ code ] tags.
 

westaust55

Moderator
help with EEPROM Read/Write Procedures

Why not use W6? Seem to be the simplest solution.
It does not appear as though you are using B12 and B13 yet. :)
What PICAXE are you using?

For future reference, please try using the [ code ] tags.
Also what EEPROM are you using. At a glance seems like an SPI not i2c EEPROM - not that that is an issue of the PICAXE does not handle i2c comms.

All you need to do is the write the two separate bytes from info2 (w1)
so the first/most significant byte is b3 and the second/least significant byte is b2 (w1 + b3:b2)

Make two calls to the EEPORM read/write routine and preload the appropriate byte for each call.
 

johnmobley

New Member
Ok first to answer the question about the EEPROM. I am trying to work with the Serial EEPROM chip 93LC66A from Microchip.

Below is a modified section of my code to see if I understand what is being said, the added code should be in bold, italicised text:

Code:
CONTINUE:
readadc10 2, info2 'This reads the temperature of the heater box.
let page = 1
[B][I]let info = b3[/I][/B]
gosub eewrite
[B][I]let info = b2
gosub eewrite[/I][/B]
let info2 = info2 * 100 - 50508 / 43
serout 7, T2400, (254, 192, "Temp. = ", #info2, " ", %11011111, "F")
for b0 = 1 to 2 '1 x 60 second delay
pause 60000
next b0
next address
end
By doing it this way will the second byte overwrite the first byte? Or will they both be stored for later retrival? If this is not the way, then again I am still lost...

Please let me know what you think?
 

hippy

Ex-Staff (retired)
I'm not familiar with the 93LC66A and haven't studied your code but, two consecutive writes will write to different Eeprom addresses providing the address of the Eeepron to be written is incremented before each write.

If you are using the "FOR address = 0 TO 59" to indicate where to write, what you have will not work, both calls to 'eewrite' will rite to the same location. You will need to change to something like ...

address = 0
Do
let info = b3
gosub eewrite
let address = address+1
let info = b2
gosub eewrite
let address = address+1
Loop While address <= 59
 

johnmobley

New Member
Ok I was afraid of that. I was looking at the original code and I noticed that they used the same address for the data set but they changed the "page" where the information was stored for each value in the set. So with that in mind can I use the following code? The modifications to the original code are noted in bold italics. The original program only incremented the address after ALL data for that set was recorded. All that they did was increment the "page" from 0 to 1. Is there space for more pages? (such as Page 2.) I looked at the datasheet for the EEPROM chip but I did not find anything related to avalible pages. Like I stated earlier, I have never worked with EEPROM before so this is new to me.

Code:
CONTINUE:
readadc10 2, info2 'This reads the temperature of the heater box.
let page = 1
[B][I]let info = b3[/B][/I]
gosub eewrite
[B][I]let page = 2
let info = b2
gosub eewrite[/B][/I]
let info2 = info2 * 100 - 50508 / 43
serout 7, T2400, (254, 192, "Temp. = ", #info2, " ", %11011111, "F")
for b0 = 1 to 2 '1 x 60 second delay
pause 60000
next b0
next address
end
I got the "original" code to read/write to my EEPROM from the following file which is provided when you install the programming editor.
It's location is:

C:\Program Files\Programming Editor\projects\Datalogger.pdf
(or similar, depending on where you installed your program.)

I then modified the program to match my circuit design and intentions. The code found above is from my program. I am now just trying to figure out how to work/modify the EEPROM do do what I would like...
 
Last edited:

hippy

Ex-Staff (retired)
I was looking at the original code and I noticed that they used the same address for the data set but they changed the "page" where the information was stored for each value in the set.
The 'page' (b9) and 'address' (b8) are concatenated to form w4 which is the full address used to write to eeprom. One set of bytes gets written to 0..255, the others to 256..511.

So with that in mind can I use the following code?
I'm not sure. In the original code the two pages were 'page=0' and 'page=1'. I haven't looked at the eeprom data sheet but there may only be page 0 and 1.

I then modified the program to match my circuit design and intentions. The code found above is from my program. I am now just trying to figure out how to work/modify the EEPROM do do what I would like...
I'm a little unclear as to what you would like your program to do. Set 'page=' ( 0 or 1, possibly higher ) and set 'address=' (0 to 255) and that will write a byte to eeprom. Providing 'page' and 'address' are different those bytes will be written to diferent locations.
 
Top