PDA

View Full Version : How to Read / Write I2c 24LC16B EEprom



odie52
- 3rd May 2010, 04:37
:confused:This is my first ever post...
I read a lot but not enough...
I am using an 18F4520 pic with a 24LC16B external EEprom with proper pullups ect.
Circuit works great using the I2CRead and Write commands with Word addresses and Byte Variables for the data.

I am having lots of difficulty getting it to work with word size Variables in the data to read and write.

I understand from the spec sheet that the 16B is a byte size variable and a word size address and using this I2Cwrite command
addsout var word
myvariable var byte

I2CWRITE DPIN,CPIN,$A0,addsout,[myvariable]

This works great for numbers <=255

I need numbers stored <= 65535 which requires a WORD size stored variable.

I have tried so many different ways to Highbyte Lowbyte the data - I have lost count.

Please Help!

Bill Legge
- 3rd May 2010, 10:28
Odie52,

Here is an example of writing 16 bytes, in one go, to external EEPROM. Some of the variables are byte sized:



' ************************************************** ***************************
' * *
' * EEPROM WRITE. 512k EEPROM used. Control Byte for this one is $A8 *
' * Stores weather readings, 16 bytes/Record. 4096 Records/Chip *
' * At one Record every 6 minutes this holds data for 17 days *
' * Write address 'Eaddress' stored in PIC EEPROM at addresses 1 and 2 *
' * *
' ************************************************** ***************************

EEPROM_write:
read 1,Eaddress.Byte0 ' Next free EEPROM address for chip $A8
read 2,Eaddress.Byte1 ' TempInt is not stored

i2cwrite Eprom_D, Eprom_C, $A8, Eaddress,_' Write to $A8 on ETT board
[Mont,Days,Hour,Mins,_
Pressure.lowbyte,Pressure.highbyte,_
Humidity,_
TempExt.byte0,TempExt.byte1,_
Wind_S,_
Wind_S_Ave,_
Wind_D,_
Wind_D_Ave,_
Rain,_
Voltage,_ ' Spare
Voltage] ' Spare
pause 10

Eaddress = Eaddress+16 ' 16 bytes per record, must be an integer of 128/No of rcds
write 1,Eaddress.Byte0 ' Write new Eaddress to PIC EEPROM
write 2,Eaddress.Byte1
return


And reading it back:



' ************************************************** ***************************
' * *
' * DATA DUMP. Read EEPROM $A8 from Address 0 to Address Eaddres *
' * and send it to the PC *
' * *
' ************************************************** ***************************

Data_dump:
RCSTA=$90:TXSTA=$24:SPBRG=21 ' Set up for 115200 Baud when oscillator is at 40 MHz
pause 10

read 1,Eaddress.Byte0
read 2,Eaddress.Byte1

hserout [13,13,"DUMP OF DATA FROM EEPROM CHIP $A8",13]' Print column headings with TAB command
hserout [_
"Recd" ,9,_
"Month",9,_
"Date ",9,_
"Hours",9,_
"Mins ",9,_
"Press",9,_
"ReHum",9,_
"TeExt",9,_
"WinSp",9,_
"WinSA",9,_
"WinDr",9,_
"WinDA",9,_
"Rain ",9,_
"Volts",9,_
"Volts",13]

for Address=0 to Eaddress-16 step 16' 16 bytes per record
i2cread Eprom_D, Eprom_C, $A8, Address,_' Read from %1010100X on ETT board
[Mont,_
Days,_
Hour,_
Mins,_
Pressure.lowbyte,Pressure.highbyte,_
Humidity,_
TempExt.lowbyte,TempExt.highbyte,_
Wind_S,_
Wind_S_Ave,_
Wind_D,_
Wind_D_Ave,_
Rain,_
Voltage,_
Voltage]

hserout[_' Print weather data with TAB command
dec5 Address,9,_
dec2 Mont,9,_' Read from EEPROM $A8
dec2 Days,9,_' Read from EEPROM $A8
dec2 Hour,9,_' Read from EEPROM $A8
dec2 Mins,9,_' Read from EEPROM $A8
dec4 Pressure/10,".",dec1 Pressure,9,_ ' Read from EEPROM $A8
dec2 Humidity,9,_ ' Read from EEPROM $A8
dec2 TempExt/10,".", dec1 TempExt,9,_' Read from EEPROM $A8
dec3 Wind_S,9,_' Read from EEPROM $A8
dec3 Wind_S_Ave,9,_' Read from EEPROM $A8
dec3 Wind_D,9,_' Read from EEPROM $A8
dec3 Wind_D_Ave,9,_ ' Read from EEPROM $A8
dec2 Rain/10,".", dec1 Rain,9,_ ' Read from EEPROM $A8
dec2 Voltage/10,".", dec1 Voltage,9,_' Read from EEPROM $A8
dec2 Voltage/10,".", dec1 Voltage,13]' Read from EEPROM $A8
pause 10

next Address
hserout [13,13]
return


Note the 'WRITE' command to stores the address on the internal PIC EEPROM and the 'I2CWRITE' stores the variables on the external EEPROM

hope this helps?

Regards Bill Legge

odie52
- 5th May 2010, 03:00
Well well, read.. read.. and read some more... then go back and read it again and again...
The spec sheet & syntax that is....

If you have tried everything... go back and read the spec sheet & syntax of the command one more time ...

Yep that is exactly what I did .. still couldn't make it work so I asked for my first time ever on this forum a question and the answer was given right there in front of me.. there it was in the spec sheet. the external eeprom 24LC16B uses a byte as the address not a word. You then I2Cwrite to an address using a byte of information 0 to 256. You can use ether a byte or word of information sent to eeprom up to 1 page depending on the eeprom size for the information to store. Reading it back is just the oposite...

By the way THANKS!

Next time before I ask a question on this forum I promise you I will read Just one more time the spec sheet and the syntax of the promblem..

THANKS again... Odie52