lm92.lua 4.9 KB

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