AT24C on NodeMCU Lua

AT24C on NodeMCU Lua

This page is to Setup and Config AT24C on NodeMCU Lua

Hardware and Software

Hardware

AT24C on NodeMCU Lua

Software

ESplorer

Sanki Note

    • SDA : D3 (GPIO 0 ) / SCL D4 (GPIO 2)

Examples

This example is AT24C Library (Lua) -- AT24CXXX_Lib.lua

Examples

This example is AT24C Library (Lua) -- AT24CXXX_Test.lua

            1. -- I2C Must be Sda:D3 and cls:D4

            2. --

            3. -- AT24C01 --> 1Kb = 1024 Byte(Char) [ Page 8 * 128 ]

            4. -- AT24C32 --> 32Kb = 4096 Byte(Char) [ Page 32 * 128 ]

            5. -- AT24Cbitpage --> bitpageKb = 32768 Byte(Char) [ Page 64 * 512 ]

            6. -- AT24C512 --> 512Kb = 65536 Byte(Char) [ Page 128 * 512 ]

            7. --

            8. -- Address 0x50 -- 0x57 based on A1, A2, A3 On and Off

            9. --

            10. -- E.g. AT24C32

            11. -- Pos from 0 to 4096

            12. -- write len page 32 words only

            13. local id = 0

            14. local eesize = 4096 -- default 32K

            15. local page = 32 -- degault 32k

            16. local bitpage = page * 8

            17. local Nom_du_module = ...

            18. local M = {}

            19. _G[Nom_du_module] = M

            20. -- c = id : EEPROM ID e.g. 0

            21. -- s = eesize : Byte e.g. 4096 (32K)

            22. -- d = sda (Pin 3)

            23. -- l = scl (Pin 4)

            24. -- a = device : 0x50 - 0x57

            25. function M.init(c, d, l, a, s)

            26. id = c

              1. -- I2C Must be Sda:D3 and cls:D4

            27. eesize = (s * 1024) / 8

              1. --

            1. sda = d

              1. -- AT24C01 --> 1Kb = 1024 Byte(Char) [ Page 8 * 128 ]

            1. scl = l

              1. -- AT24C32 --> 32Kb = 4096 Byte(Char) [ Page 32 * 128 ]

            1. device = a

              1. -- AT24C256 --> 256Kb = 32768 Byte(Char) [ Page 64 * 512 ]

            1. if eesize == 512 then page = 16 end -- 4K

              1. -- AT24C512 --> 512Kb = 65536 Byte(Char) [ Page 128 * 512 ]

            1. if eesize == 16384 then page = 64 end -- 128K

              1. --

            1. if eesize == 32768 then page = 64 end -- 256 Kb

              1. -- Address 0x50 -- 0x57 based on A1, A2, A3 On and Off

            1. if eesize == 65536 then page = 128 end -- 512Kb

              1. --

            1. bitpage = 256

              1. -- E.g. AT24C32

              1. -- Pos from 0 to 4096

            1. if (i2c.setup(id, sda, scl, i2c.SLOW)) ~= 0 then

              1. -- write len page 32 words only

            1. print("------ I2C EEPROM Connected")

            2. print("ID : " .. id)

              1. require("AT24CXXX_Lib")

            1. print("EEPROM : " .. s .. "Kb / " .. eesize .. " Bytes (Words)")

            2. print("Bit / Page : " .. bitpage .. " Bits")

              1. -- c = id : EEPROM ID e.g. 0

            1. print("Byte / Page : " .. page .. " Bytes")

              1. -- s = eesize : 32k = (32 * 1024) / 8 = 4096 (32K)

            1. print("Total Page : " .. eesize / page .. " Pages")

              1. -- d = sda (Pin 3)

            1. print("Device : " .. string.format("0x" .. "%x", a))

              1. -- l = scl (Pin 4)

            1. print("SDA : pin " .. d)

              1. -- a = device : 0x50 - 0x57

            1. print("SCL : pin " .. l)

            2. print("---------------------------")

              1. AT24CXXX_Lib.init(0, 3, 4, 0x50, 256)

            1. elseif (i2c.setup(id, sda, scl, i2c.SLOW)) == 0 then print("Error in the configuration of the I2C Port!") return nil

              1. print("---------------- Format -------------")

            1. end

              1. --AT24CXXX_Lib.format(4096)

            1. end

              1. print("\n---------------- Char Pos -------------")

            1. function M.getInfo()

              1. aaPos = 896

            1. return id, eesize, page, bitpage

              1. aByte = string.format("Position : " .. "%s", aaPos)

            1. end

              1. rrPos = aaPos

              2. AT24CXXX_Lib.write_byte(aaPos, aByte)

            1. function M.read_octet(haute, basse)

              1. print("\nWrite : " .. aByte .. " to " .. aaPos)

              2. print("\nRead : " .. aaPos .. " --> [" .. AT24CXXX_Lib.read_byte(rrPos) .. "]")

            1. i2c.start(id)

            2. i2c.address(id, device, i2c.TRANSMITTER)

            3. i2c.write(id, haute)

            4. i2c.write(id, basse)

              1. print("\n---------------- a Words -------------")

            1. i2c.stop(id)

              1. aWords = "感冒其實不必看醫生";

            1. i2c.start(id)

              1. aWordsLen = string.len(aWords);

            1. i2c.address(id, device, i2c.RECEIVER)

              1. wPos = 0

            1. c=i2c.read(id, 1)

            2. i2c.stop(id)

              1. AT24CXXX_Lib.write_sequence(wPos, aWords)

            1. return c

              1. print("\nWrite : Pos [" .. wPos .. "] < -- [" .. aWords .. "] (" .. aWordsLen .. ")")

              2. print("\nRead : Pos [" .. wPos .. "] --> [" .. AT24CXXX_Lib.read_sequence(wPos, aWordsLen ) .. "]")

            1. end

            2. function M.read_byte(charpos)

              1. print("\nCheck : [" .. AT24CXXX_Lib.checkPos(wPos, aWords) .. "]")

            1. i2c.start(id)

            2. i2c.address(id, device, i2c.TRANSMITTER)

              1. print("\n---------------- DUMP -------------")

            1. i2c.write(id, charpos / bitpage % bitpage )

            2. i2c.write(id, charpos % bitpage)

              1. id, eesize, page, bitpage = AT24CXXX_Lib.getInfo

            1. i2c.stop(id)

              1. for i=0, 1024, 32 do

            1. i2c.start(id)

              1. print(string.format("%4s",i) .. " : " .. AT24CXXX_Lib.dump(i, i + 31))

            1. i2c.address(id, device, i2c.RECEIVER)

              1. end

            1. c=i2c.read(id, 1)

            2. i2c.stop(id)

              1. AT24CXXX_Lib.dumpList(0, 1024)

            1. return c

              1. print("\n---------------- Find -------------")

            1. end

              1. sPos = 0

            1. function M.write_byte(charpos, valeur)

              1. ePos = 100

              2. findStr = "看醫"

            1. i2c.start(id)

              1. retPos = AT24CXXX_Lib.find(sPos, ePos, findStr)

            1. i2c.address(id, device, i2c.TRANSMITTER)

              1. if retPos ~= nil then

            1. i2c.write(id, charpos / bitpage % bitpage)

              1. print("\nFound : [" .. findStr .. "] on Byte Pos [" .. retPos .. "] in Range [" .. sPos .. ":" .. ePos .. "]")

            1. i2c.write(id, charpos % bitpage)

            2. i2c.write(id, valeur)

              1. else

            1. i2c.stop(id)

              1. print("\nNot Found [" .. findStr .. "] in Byte Range [" .. sPos .. ":" .. ePos .. "]")

            1. tmr.delay(750)

              1. end

              1. print("\n")

            1. end

            2. function M.write_octet(haute, basse, valeur)

              1. AT24CXXX_Lib = nil

              2. package.loaded["AT24CXXX_Lib"]=nil

            1. i2c.start(id)

            2. i2c.address(id, device, i2c.TRANSMITTER)

            3. i2c.write(id, haute)

            4. i2c.write(id, basse)

            5. i2c.write(id, valeur)

            6. i2c.stop(id)

            7. tmr.delay(750)

            8. end

            9. function M.write_sequenceHEX(haute, basse, valeur)

            10. fin_bas = bit.band(string.len(valeur),0xff)

            11. fin_haut =bit.rshift(string.len(valeur),8)

            12. i2c.start(id)

            13. i2c.address(id, device, i2c.TRANSMITTER)

            14. i2c.write(id, haute)

            15. i2c.write(id, basse)

            16. i2c.write(id, valeur)

            17. i2c.stop(id)

            18. tmr.delay(750)

            19. end

            20. function M.read_sequenceHEX(haute, basse, longueur)

            21. i2c.start(id)

            22. i2c.address(id, device, i2c.TRANSMITTER)

            23. i2c.write(id, haute)

            24. i2c.write(id, basse)

            25. i2c.stop(id)

            26. i2c.start(id)

            27. i2c.address(id, device, i2c.RECEIVER)

            28. c = i2c.read(id, longueur)

            29. print(c)

            30. i2c.stop(id)

            31. return c

            32. end

            33. function M.write_sequence(charpos, valeur)

            34. if (string.len(valeur) > page) then

            35. print("[Warning : Write Size " .. string.len(valeur) .. " over Page Size " .. page .."]")

            36. end

            37. fin_bas = bit.band(string.len(valeur),0xff)

            38. fin_haut =bit.rshift(string.len(valeur),8)

            39. i2c.start(id)

            40. i2c.address(id, device, i2c.TRANSMITTER)

            41. i2c.write(id, charpos / bitpage % bitpage)

            42. i2c.write(id, charpos % bitpage)

            43. i2c.write(id, valeur)

            44. i2c.stop(id)

            45. tmr.delay(750)

            46. end

            47. function M.read_sequence(charpos, longueur)

            48. i2c.start(id)

            49. i2c.address(id, device, i2c.TRANSMITTER)

            50. i2c.write(id, charpos / bitpage % bitpage)

            51. --print("charpos : " .. charpos)

            52. --print("bitpage : " .. bitpage)

            53. i2c.write(id, charpos % bitpage)

            54. i2c.stop(id)

            55. i2c.start(id)

            56. i2c.address(id, device, i2c.RECEIVER)

            57. c = i2c.read(id, longueur)

            58. --print(c)

            59. i2c.stop(id)

            60. return c

            61. end

            62. -- fill up all the EEPROM with 0xFF

            63. function M.format(_eesize)

            64. --local page = 32

            65. local words = _eesize / 8

            66. --if _eesize >= 16384 then page = 64 end

            67. --if _eesize >= 65536 then page = 128 end

            68. local s = string.rep(string.char(0xFF), page)

            69. for a=0, _eesize-page, page do

            70. M.write_sequence(a, s)

            71. tmr.wdclr()

            72. --print("Formated Page " .. a .. " / " .. _eesize)

            73. end

            74. --_ee = 0

            75. s=nil

            76. print("Formated Size : " .. _eesize .. " bits / " .. words .. " Bytes");

            77. end

            78. -- get address of first 0xFF data in EE at addr/8

            79. function M.find(sPos, ePos, str)

            80. local r = nil

            81. for a=sPos, ePos, 1 do

            82. --print("Pos : " .. a .. " --> " .. M.read_sequence(a, string.len(str)))

            83. if M.read_sequence(a, string.len(str)) == str then

            84. r = a

            85. break

            86. end

            87. tmr.wdclr()

            88. end

            89. return r

            90. end

            91. function M.dump(sPos, ePos)

            92. local r = ""

            93. for a=sPos, ePos, 4 do

            94. r = r .. M.read_sequence(a, 4) .. " "

            95. tmr.wdclr()

            96. end

            97. return r

            98. end

            99. function M.dumpList(sPos, ePos)

            100. local r = string.format("%3s","")

            101. if page == 64 then r = r .. " 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60" end

            102. 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

            103. if page == 32 then r = r .. " 0 4 8 12 16 20 24 28" end

            104. print("\n" .. r)

            105. for i=sPos, ePos, page do

            106. --for a=0, page, 4 do

            107. print(string.format("%4s",i) .. " : " .. AT24CXXX_Lib.dump(i, i + (page -1)))

            108. tmr.wdclr()

            109. -- end

            110. end

            111. end

            112. function M.checkPos(aPos, str)

            113. begin = aPos % page

            114. strLen = string.len(str)

            115. if (begin + strLen > 32) then

            116. return "String too long"

            117. end

            118. return "OK"

            119. end