lm92.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. -- ******************************************************
  2. -- LM92 module for ESP8266 with nodeMCU
  3. --
  4. -- Written by Levente Tamas <levente.tamas@navicron.com>
  5. --
  6. -- modify by Garberoglio Leonardo <leogarberoglio@gmail.com>
  7. --
  8. -- GNU LGPL, see https://www.gnu.org/copyleft/lesser.html
  9. -- ******************************************************
  10. -- Module Bits
  11. local moduleName = ...
  12. local M = {}
  13. _G[moduleName] = M
  14. -- Default ID
  15. local id = 0
  16. -- Local vars
  17. local address = 0
  18. -- read regs for len number of bytes
  19. -- return table with data
  20. local function read_reg(reg_addr, len)
  21. local ret={}
  22. local c
  23. local x
  24. i2c.start(id)
  25. i2c.address(id, address ,i2c.TRANSMITTER)
  26. i2c.write(id,reg_addr)
  27. i2c.stop(id)
  28. i2c.start(id)
  29. i2c.address(id, address,i2c.RECEIVER)
  30. c=i2c.read(id,len)
  31. for x=1,len,1 do
  32. tc=string.byte(c,x)
  33. table.insert(ret,tc)
  34. end
  35. i2c.stop(id)
  36. return ret
  37. end
  38. --write reg with data table
  39. local function write_reg(reg_addr, data)
  40. i2c.start(id)
  41. i2c.address(id, address, i2c.TRANSMITTER)
  42. i2c.write(id, reg_addr)
  43. i2c.write(id, data)
  44. i2c.stop(id)
  45. end
  46. --write comparison reg with 2 bytes
  47. local function write_comp_reg(reg_addr, msb, lsb)
  48. i2c.start(id)
  49. i2c.address(id, address, i2c.TRANSMITTER)
  50. i2c.write(id, reg_addr)
  51. i2c.write(id, msb)
  52. i2c.write(id, lsb)
  53. i2c.stop(id)
  54. end
  55. -- initialize i2c
  56. -- a: i2c addr 0x48|A1<<1|A0 (A0-A1: chip pins)
  57. function M.setup(a)
  58. if (a ~= nil) and (a >= 0x48) and (a <= 0x4b ) then
  59. address = a
  60. i2c.start(id)
  61. res = i2c.address(id, address, i2c.TRANSMITTER) --verify that the address is valid
  62. i2c.stop(id)
  63. if (res == false) then
  64. print("device not found")
  65. return nil
  66. end
  67. else
  68. print("wrong i2c address") return nil
  69. end
  70. end
  71. -- Return the temperature data
  72. function M.getTemperature()
  73. local temperature
  74. local tmp=read_reg(0x00,2) --read 2 bytes from the temperature register
  75. temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits
  76. if (temperature>=0x1000) then
  77. temperature= temperature-0x2000 --convert the two's complement
  78. end
  79. return temperature * 0.0625
  80. end
  81. -- Put the LM92 into shutdown mode
  82. function M.shutdown()
  83. write_reg(0x01,0x01)
  84. end
  85. -- Bring the LM92 out of shutdown mode
  86. function M.wakeup()
  87. write_reg(0x01,0x00)
  88. end
  89. -- Write the LM92 Thyst SET POINT
  90. function M.setThyst(data_wr)
  91. local tmp = data_wr / 0.0625
  92. if (tmp>=0x1000) then
  93. tmp= tmp-0x2000 --convert the two's complement
  94. end
  95. tmp = bit.lshift(tmp,3)
  96. local lsb = bit.band(tmp, 0x00FF)
  97. tmp = bit.rshift(bit.band(tmp, 0xFF00),8)
  98. write_comp_reg(0x02,tmp, lsb)
  99. end
  100. -- Write the LM92 T_crit SET POINT
  101. function M.setTcrit(data_wr)
  102. local tmp = data_wr / 0.0625
  103. if (tmp>=0x1000) then
  104. tmp= tmp-0x2000 --convert the two's complement
  105. end
  106. tmp = bit.lshift(tmp,3)
  107. local lsb = bit.band(tmp, 0x00FF)
  108. tmp = bit.rshift(bit.band(tmp, 0xFF00),8)
  109. write_comp_reg(0x03,tmp, lsb)
  110. end
  111. -- Write the LM92 Tlow SET POINT
  112. function M.setTlow(data_wr)
  113. local tmp = data_wr / 0.0625
  114. if (tmp>=0x1000) then
  115. tmp= tmp-0x2000 --convert the two's complement
  116. end
  117. tmp = bit.lshift(tmp,3)
  118. local lsb = bit.band(tmp, 0x00FF)
  119. tmp = bit.rshift(bit.band(tmp, 0xFF00),8)
  120. write_comp_reg(0x04,tmp, lsb)
  121. end
  122. -- Write the LM92 T_high SET POINT
  123. function M.setThigh(data_wr)
  124. local tmp = data_wr / 0.0625
  125. if (tmp>=0x1000) then
  126. tmp= tmp-0x2000 --convert the two's complement
  127. end
  128. tmp = bit.lshift(tmp,3)
  129. local lsb = bit.band(tmp, 0x00FF)
  130. tmp = bit.rshift(bit.band(tmp, 0xFF00),8)
  131. write_comp_reg(0x05,tmp, lsb)
  132. end
  133. -- Read the LM92 Thyst SET POINT
  134. function M.getThyst()
  135. local temperature
  136. local tmp=read_reg(0x02,2) --read 2 bytes from the Hysteresis register
  137. temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits
  138. if (temperature>=0x1000) then
  139. temperature= temperature-0x2000 --convert the two's complement
  140. end
  141. return temperature * 0.0625
  142. end
  143. -- Read the LM92 T_crit SET POINT
  144. function M.getTcrit()
  145. local temperature
  146. local tmp=read_reg(0x03,2) --read 2 bytes from the T Critical register
  147. temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits
  148. if (temperature>=0x1000) then
  149. temperature= temperature-0x2000 --convert the two's complement
  150. end
  151. return temperature * 0.0625
  152. end
  153. -- Read the LM92 Tlow SET POINT
  154. function M.getTlow()
  155. local temperature
  156. local tmp=read_reg(0x04,2) --read 2 bytes from the T Low register
  157. temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits
  158. if (temperature>=0x1000) then
  159. temperature= temperature-0x2000 --convert the two's complement
  160. end
  161. return temperature * 0.0625
  162. end
  163. -- Read the LM92 T_high SET POINT
  164. function M.getThigh()
  165. local temperature
  166. local tmp=read_reg(0x05,2) --read 2 bytes from the T High register
  167. temperature=bit.rshift(tmp[1]*256+tmp[2],3) --lower 3 bits are status bits
  168. if (temperature>=0x1000) then
  169. temperature= temperature-0x2000 --convert the two's complement
  170. end
  171. return temperature * 0.0625
  172. end
  173. return M