This page is to Setup and Config AT24C on NodeMCU Lua
Hardware and Software
Hardware
AT24C on NodeMCU Lua
Software
ESplorer
SDA : D3 (GPIO 0 ) / SCL D4 (GPIO 2)
This example is AT24C Library (Lua) -- AT24CXXX_Lib.lua
This example is AT24C Library (Lua) -- AT24CXXX_Test.lua
-- I2C Must be Sda:D3 and cls:D4
--
-- AT24C01 --> 1Kb = 1024 Byte(Char) [ Page 8 * 128 ]
-- AT24C32 --> 32Kb = 4096 Byte(Char) [ Page 32 * 128 ]
-- AT24Cbitpage --> bitpageKb = 32768 Byte(Char) [ Page 64 * 512 ]
-- AT24C512 --> 512Kb = 65536 Byte(Char) [ Page 128 * 512 ]
--
-- Address 0x50 -- 0x57 based on A1, A2, A3 On and Off
--
-- E.g. AT24C32
-- Pos from 0 to 4096
-- write len page 32 words only
local id = 0
local eesize = 4096 -- default 32K
local page = 32 -- degault 32k
local bitpage = page * 8
local Nom_du_module = ...
local M = {}
_G[Nom_du_module] = M
-- c = id : EEPROM ID e.g. 0
-- s = eesize : Byte e.g. 4096 (32K)
-- d = sda (Pin 3)
-- l = scl (Pin 4)
-- a = device : 0x50 - 0x57
function M.init(c, d, l, a, s)
id = c
-- I2C Must be Sda:D3 and cls:D4
eesize = (s * 1024) / 8
--
sda = d
-- AT24C01 --> 1Kb = 1024 Byte(Char) [ Page 8 * 128 ]
scl = l
-- AT24C32 --> 32Kb = 4096 Byte(Char) [ Page 32 * 128 ]
device = a
-- AT24C256 --> 256Kb = 32768 Byte(Char) [ Page 64 * 512 ]
if eesize == 512 then page = 16 end -- 4K
-- AT24C512 --> 512Kb = 65536 Byte(Char) [ Page 128 * 512 ]
if eesize == 16384 then page = 64 end -- 128K
--
if eesize == 32768 then page = 64 end -- 256 Kb
-- Address 0x50 -- 0x57 based on A1, A2, A3 On and Off
if eesize == 65536 then page = 128 end -- 512Kb
--
bitpage = 256
-- E.g. AT24C32
-- Pos from 0 to 4096
if (i2c.setup(id, sda, scl, i2c.SLOW)) ~= 0 then
-- write len page 32 words only
print("------ I2C EEPROM Connected")
print("ID : " .. id)
require("AT24CXXX_Lib")
print("EEPROM : " .. s .. "Kb / " .. eesize .. " Bytes (Words)")
print("Bit / Page : " .. bitpage .. " Bits")
-- c = id : EEPROM ID e.g. 0
print("Byte / Page : " .. page .. " Bytes")
-- s = eesize : 32k = (32 * 1024) / 8 = 4096 (32K)
print("Total Page : " .. eesize / page .. " Pages")
-- d = sda (Pin 3)
print("Device : " .. string.format("0x" .. "%x", a))
-- l = scl (Pin 4)
print("SDA : pin " .. d)
-- a = device : 0x50 - 0x57
print("SCL : pin " .. l)
print("---------------------------")
AT24CXXX_Lib.init(0, 3, 4, 0x50, 256)
elseif (i2c.setup(id, sda, scl, i2c.SLOW)) == 0 then print("Error in the configuration of the I2C Port!") return nil
print("---------------- Format -------------")
end
--AT24CXXX_Lib.format(4096)
end
print("\n---------------- Char Pos -------------")
function M.getInfo()
aaPos = 896
return id, eesize, page, bitpage
aByte = string.format("Position : " .. "%s", aaPos)
end
rrPos = aaPos
AT24CXXX_Lib.write_byte(aaPos, aByte)
function M.read_octet(haute, basse)
print("\nWrite : " .. aByte .. " to " .. aaPos)
print("\nRead : " .. aaPos .. " --> [" .. AT24CXXX_Lib.read_byte(rrPos) .. "]")
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, haute)
i2c.write(id, basse)
print("\n---------------- a Words -------------")
i2c.stop(id)
aWords = "感冒其實不必看醫生";
i2c.start(id)
aWordsLen = string.len(aWords);
i2c.address(id, device, i2c.RECEIVER)
wPos = 0
c=i2c.read(id, 1)
i2c.stop(id)
AT24CXXX_Lib.write_sequence(wPos, aWords)
return c
print("\nWrite : Pos [" .. wPos .. "] < -- [" .. aWords .. "] (" .. aWordsLen .. ")")
print("\nRead : Pos [" .. wPos .. "] --> [" .. AT24CXXX_Lib.read_sequence(wPos, aWordsLen ) .. "]")
end
function M.read_byte(charpos)
print("\nCheck : [" .. AT24CXXX_Lib.checkPos(wPos, aWords) .. "]")
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
print("\n---------------- DUMP -------------")
i2c.write(id, charpos / bitpage % bitpage )
i2c.write(id, charpos % bitpage)
id, eesize, page, bitpage = AT24CXXX_Lib.getInfo
i2c.stop(id)
for i=0, 1024, 32 do
i2c.start(id)
print(string.format("%4s",i) .. " : " .. AT24CXXX_Lib.dump(i, i + 31))
i2c.address(id, device, i2c.RECEIVER)
end
c=i2c.read(id, 1)
i2c.stop(id)
AT24CXXX_Lib.dumpList(0, 1024)
return c
print("\n---------------- Find -------------")
end
sPos = 0
function M.write_byte(charpos, valeur)
ePos = 100
findStr = "看醫"
i2c.start(id)
retPos = AT24CXXX_Lib.find(sPos, ePos, findStr)
i2c.address(id, device, i2c.TRANSMITTER)
if retPos ~= nil then
i2c.write(id, charpos / bitpage % bitpage)
print("\nFound : [" .. findStr .. "] on Byte Pos [" .. retPos .. "] in Range [" .. sPos .. ":" .. ePos .. "]")
i2c.write(id, charpos % bitpage)
i2c.write(id, valeur)
else
i2c.stop(id)
print("\nNot Found [" .. findStr .. "] in Byte Range [" .. sPos .. ":" .. ePos .. "]")
tmr.delay(750)
end
print("\n")
end
function M.write_octet(haute, basse, valeur)
AT24CXXX_Lib = nil
package.loaded["AT24CXXX_Lib"]=nil
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, haute)
i2c.write(id, basse)
i2c.write(id, valeur)
i2c.stop(id)
tmr.delay(750)
end
function M.write_sequenceHEX(haute, basse, valeur)
fin_bas = bit.band(string.len(valeur),0xff)
fin_haut =bit.rshift(string.len(valeur),8)
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, haute)
i2c.write(id, basse)
i2c.write(id, valeur)
i2c.stop(id)
tmr.delay(750)
end
function M.read_sequenceHEX(haute, basse, longueur)
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, haute)
i2c.write(id, basse)
i2c.stop(id)
i2c.start(id)
i2c.address(id, device, i2c.RECEIVER)
c = i2c.read(id, longueur)
print(c)
i2c.stop(id)
return c
end
function M.write_sequence(charpos, valeur)
if (string.len(valeur) > page) then
print("[Warning : Write Size " .. string.len(valeur) .. " over Page Size " .. page .."]")
end
fin_bas = bit.band(string.len(valeur),0xff)
fin_haut =bit.rshift(string.len(valeur),8)
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, charpos / bitpage % bitpage)
i2c.write(id, charpos % bitpage)
i2c.write(id, valeur)
i2c.stop(id)
tmr.delay(750)
end
function M.read_sequence(charpos, longueur)
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, charpos / bitpage % bitpage)
--print("charpos : " .. charpos)
--print("bitpage : " .. bitpage)
i2c.write(id, charpos % bitpage)
i2c.stop(id)
i2c.start(id)
i2c.address(id, device, i2c.RECEIVER)
c = i2c.read(id, longueur)
--print(c)
i2c.stop(id)
return c
end
-- fill up all the EEPROM with 0xFF
function M.format(_eesize)
--local page = 32
local words = _eesize / 8
--if _eesize >= 16384 then page = 64 end
--if _eesize >= 65536 then page = 128 end
local s = string.rep(string.char(0xFF), page)
for a=0, _eesize-page, page do
M.write_sequence(a, s)
tmr.wdclr()
--print("Formated Page " .. a .. " / " .. _eesize)
end
--_ee = 0
s=nil
print("Formated Size : " .. _eesize .. " bits / " .. words .. " Bytes");
end
-- get address of first 0xFF data in EE at addr/8
function M.find(sPos, ePos, str)
local r = nil
for a=sPos, ePos, 1 do
--print("Pos : " .. a .. " --> " .. M.read_sequence(a, string.len(str)))
if M.read_sequence(a, string.len(str)) == str then
r = a
break
end
tmr.wdclr()
end
return r
end
function M.dump(sPos, ePos)
local r = ""
for a=sPos, ePos, 4 do
r = r .. M.read_sequence(a, 4) .. " "
tmr.wdclr()
end
return r
end
function M.dumpList(sPos, ePos)
local r = string.format("%3s","")
if page == 64 then r = r .. " 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60" end
if page == 128 then r = r .. " 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120 124" end
if page == 32 then r = r .. " 0 4 8 12 16 20 24 28" end
print("\n" .. r)
for i=sPos, ePos, page do
--for a=0, page, 4 do
print(string.format("%4s",i) .. " : " .. AT24CXXX_Lib.dump(i, i + (page -1)))
tmr.wdclr()
-- end
end
end
function M.checkPos(aPos, str)
begin = aPos % page
strLen = string.len(str)
if (begin + strLen > 32) then
return "String too long"
end
return "OK"
end