Monday, January 9, 2017

ESPBasic driver example for AT24C32 - I2C EEPROM


Do you remember the widely used DS3231 RTC Module

As you might rememeber we have identified onboard an EEPROM chip, a 32k AT24C32 one and have written some driver examples for LUA 

I was looking a few days ago for a I2C EEPROM to do some tests with ESPBasic and the results...can be seen below :)

1. MAIN Program
cls
i2c.setup(4,5)                    'choose your I2C bus pins

let eeprom_address = 0    'EEPROM memory start address
i2c_address = 87              ' 0x57 - AT24C32 I2C address
let i = 0
stw = "87654321"            'string that we want to save in EEPROM

button "i2c Read",[I2C_read]
button "i2c Write",[I2C_Write]
wait

2. EEPROM READ Function





[I2C_read]
  stwl = len(stw)
  print "Reading from I2C address " & i2c_address
  adrh = eeprom_address >> 8
  adrl = eeprom_address and 255

  i2c.begin(i2c_address)
  i2c.write(adrh)
  i2c.write(adrl)
  i2c.end()

  i2c.begin(i2c_address)
  i2c.requestfrom(i2c_address,stwl)
  tr = i2c.available()
  print "Available bytes :" & tr
  do
      i2c_read = i2c.read()
      readt = i2c_read-48
      print  "Read value : " & readt
      i = i + 1
  loop while i < tr
  i = 0
  i2c.end()
  print "Read complete"
wait


3. EEPPROM WRITE function

[I2C_Write]
 print "Writing to I2C address " & i2c_address

 stwl = len(stw)
 print "String : " & stw
 print "String Lenght : " & stwl

 adrh = eeprom_address >> 8
 adrl = eeprom_address and 255

 i2c.begin(i2c_address)
 i2c.write(adrh)
 i2c.write(adrl)

 i = 1
 do
     stru = mid(stw,i,1)
     print  "Value to Write: " & asc(stru)
     i2c.write(asc(stru))
     i = i + 1
 loop while i < stwl+1
 i = 0
 i2c.end()
 print "Write complete"
wait

Of course, this is just a proof of concept driver example for the I2C 24C32 EEPROM but I think might help you with a good start :)



Happy breadboarding,
TJ.

4 comments:

rambler said...

Hi TJ, thanks for the DS3231 RTC EEPROM example, that may help free up a bit of ESP memory.
I couldn't find any accompanying DS3231 RTC example anywhere, did you not access the RTC with Esp-Basic?

Tracker J said...

Hi rambler, I have done also the RTC driver but was not fully published yet. If is of any interest it can also be posted.

rambler said...

No rush TJ - I was trying to use your BCD routines as gosubs for converting the RTC register contents into displayable decimals, but things are stubbornly refusing to play ball... so I was just putting 2 and 2 together (eeprom + bdc) and guessing you might have already done something with the RTC, and I was half right eh.

rambler said...

I think I've found the problem, so I'll post it (RTC script) on the forum when finally done.

Post a Comment