View Full Version : I2C read and write to 24C01 eeprom
Sabahan
- 20th November 2017, 15:51
I and difficulty reading and write to the eeprom,the reading instruction I used is
I2Cread SDA,SCL,$A0,$00,[data1.......data7]
I2Cwrite SDA,SCL,$A0,$00,[data1......data7]
all data are define as word variable
I'm not sure it have written into the memory correctly or not,but the value of data1 can read into data3 and data2 value can read in data5
I have check with a oscilloscope using I2C analysis,trigger on missing acknowledge signal all data return no acknowledgement
have I did something wrong?
MichelJasmin
- 20th November 2017, 17:34
From the manual, p. 158:
The Address size sent (byte or word) is determined by the size of the variable that is used. If a byte-sized variable is used for the Address, an 8-bit address is sent. If a word-sized variable is used, a 16-bit address is sent. Be sure to use the proper sized variable for the device you wish to communicate with. Constants should not be used for the Address as the size can vary dependent on the size of the constant. Also, expressions should not be used as they can cause an improper Address size to be sent.
Scampy
- 21st November 2017, 09:36
If using bytes then the format would be something like
I2CWRITE SDApin,SCLpin,$A0,$00,[data1,data2,... etc etc]
If using word variables the format would be something like this
I2CWRITE SDApin,SCLpin,$A0,$00,[data1.lowbyte,data1.highbyte, data2.lowbyte,data2.highbyte, etc etc]
IC2READ would have the corresponding values in its statement
richard
- 21st November 2017, 22:23
you don't say what chip/osc you are using , consider this
The timing of the I2c
instructions is set so that standard speed devices(100kHz) will
be accessible at clock speeds up to 8MHz. Fast mode devices (400kHz) may be
used up to 20MHz. If it is desired to access a standard speed device at above
8MHz, the following DEFINE should be added to the program:
DEFINE I2C_SLOW 1
also read the data sheet for your eprom re
1. address command size
2 page size
writes that exceed a page in size or cross a page boundary can go wrong
read the manual and watch out for common myths
Scampy
- 21st November 2017, 23:32
Richard, I was using the OP's example hence the address, and trying to explain that as a word variable he needed to write the high and low bytes. It was not meant as an optimised example...
I used the following to send word variable values to a PCA chip.. it might not be tight code, it may not be "as it should" in the eyes of more experienced programmers, or it may not follow convention in the manual... But it works
pcaChannel = 1
i2cControl = $6 + 4*pcaChannel
I2CWRITE SDApin,SCLpin,i2cWriteAddress,i2cControl,[0,0,CH2_PWM.lowbyte,CH2_PWM.highbyte]
richard
- 22nd November 2017, 02:27
I used the following to send word variable values to a PCA chip..
making a special case where endianness needs to be managed.
the pca chip requires the data sent low byte first, i2cread/write natively send high byte first.
to do that for a eprom is just a typing exercise
Aussie Barry
- 22nd November 2017, 04:03
Hi Richard,
I did a project a while ago which stored word values to a 24LC02 EEPROM.
The only way I could get it to work was to use the lowbyte and highbyte method that Scampy mentioned.
Can you show me a snippet of your code that will successfully write a word value to EEPROM?
Cheers
Barry
VK2XBP
richard
- 22nd November 2017, 05:48
no problem barry, this scans i2c bus reports valid addresses
and then reads and writes a mixed lot of word/byte vars
ps used a 24c08
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.