Reading and writing I2C ram and eeprom

Place code snippets and demo code here
Post Reply
User avatar
Electroguard
Posts: 179
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 40 times
Been thanked: 102 times

Reading and writing I2C ram and eeprom

Post by Electroguard »

(from Theo)
'Use this examples to read or write i2C ram or eeprom greater than 2Kbit(256x8)
'The internal adressing (register) is organised with 2 bytes.
'
'These 2 examples are using a 24C32 eeprom (32Kbit/4096x8) on the same board as the RTC.
'The code is writing and reading the whole eeprom in a loop with a certain value.

Code: [Local Link Removed for Guests]

I2C.SETUP 4, 5 , 700  ' set I2C port on pins 4 and 5

'' 87 57 adress I2C eeprom 24C32
i2c_addr = 87

value = 127

html "<br>Writing started.<br>"; 

for register = 1 to 4096
 i2c.begin i2c_addr
  I2c.write ((register/256) AND 0xFF) 'MSB register
  I2c.write (register AND 0xFF)  'LSB register
  i2c.write value
  if i2c.end<>0 then
    html "Write error adress: " + str$(register)+ "<br>"
  endif     
pause 2 'This delay is essential: wait for finishing write cycle
next register
html "<br>Writing finished: "+str$(I2C.END)+ "<br>" 

html "<br>Reading started.<br>"
for register = 1 to 4096
  i2c.begin i2c_addr
  I2c.write(register/256) AND 0xFF 'MSB register
  I2c.write register AND 0xFF  'LSB register
  i2c.end
  i2c.reqfrom i2c_addr,1
  temp = i2c.read
  if temp<>value then
     html "Read error: "+ str$(register) + " " + str$(temp) + "<br>"
  endif
  I2c.end
  if i2c.end <> 0 then
   html "Error adress: "+str$(register)+ "<br>"
  endif
next register
html "<br>Reading finished: "+str$(I2C.END)+ "<br>"

end
Post Reply