spl06.lua 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. --[[
  2. @module spl06
  3. @summary spl06_01 气压传感器
  4. @version 1.0
  5. @date 2022.08.01
  6. @author Dozingfiretruck
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. -- 用法实例
  10. local spl06 = require "spl06"
  11. i2cid = 0
  12. i2c_speed = i2c.FAST
  13. sys.taskInit(function()
  14. i2c.setup(i2cid,i2c_speed)
  15. spl06.init(i2cid)--初始化,传入i2cid
  16. while 1 do
  17. local spl06_data = spl06.get_data()
  18. log.info("spl06_data", "spl06_data.P:"..(spl06_data.P*100),"spl06_data.T"..(spl06_data.T))
  19. sys.wait(1000)
  20. end
  21. end)
  22. ]]
  23. local spl06 = {}
  24. local sys = require "sys"
  25. local i2cid
  26. local SPL06_ADDRESS_ADR
  27. local SPL06_ADDRESS_ADR_LOW = 0x76
  28. local SPL06_ADDRESS_ADR_HIGH = 0x77
  29. ---器件所用地址
  30. local SPL06_CHIP_ID_CHECK = 0x0D
  31. local SPL06_CHIP_ID = 0x10
  32. ---i2c读数据
  33. local function i2cRead(i2caddr,regaddr,size)
  34. ---读寄存器
  35. i2c.send(i2cid,i2caddr,regaddr)
  36. ---读数据
  37. --return string.toHex(i2c.recv(i2cId,i2caddr,size))
  38. local _,data = pack.unpack(i2c.recv(i2cid,i2caddr,size),'b1')
  39. return data
  40. end
  41. ---2个数据和3个数据的高低位顺序不一样---
  42. ---i2c读连续地址3个数据
  43. local function i2cReadThreeData(i2caddr,regaddr)
  44. --log.info("i2cReadThreeData","addr:"..i2caddr.." reg:"..regaddr)
  45. local msb = i2cRead(i2caddr,regaddr,1)
  46. local lsb = i2cRead(i2caddr,regaddr+1,1)
  47. local xlsb = i2cRead(i2caddr,regaddr+2,1)
  48. if msb == "" or lsb == "" or xlsb == "" or msb == nil or lsb == nil or xlsb == nil then
  49. msb = 0
  50. lsb = 0
  51. xlsb = 0
  52. log.info("spl06 three data","the data is null")
  53. --return msb4096+lsb16+xlsb/16
  54. return msb<<16 + lsb<<8 + xlsb
  55. else
  56. --log.info("spl06 three data","msb:"..msb.." lsb:"..lsb.." xlsb:"..xlsb)
  57. if regaddr ==0x13 then
  58. return bit.lshift(msb,12) + bit.lshift(lsb,4) + bit.rshift((xlsb&0xF0),4)
  59. elseif regaddr ==0x15 then
  60. return bit.lshift((msb&0x0f),16) + bit.lshift(lsb,8) +xlsb
  61. else
  62. --return msb4096+lsb16+xlsb/16
  63. return bit.lshift(msb,16) + bit.lshift(lsb,8) + xlsb
  64. end
  65. end
  66. end
  67. ---i2c读连续地址2个数据
  68. local function i2cReadTwoData(i2caddr,regaddr)
  69. local msb = i2cRead(i2caddr,regaddr,1)
  70. local lsb = i2cRead(i2caddr,regaddr+1,1)
  71. if lsb == "" or msb == "" or lsb == nil or msb == nil then
  72. msb = 0
  73. lsb = 0
  74. log.info("spl06 two data","the data is null")
  75. return msb256+lsb
  76. else
  77. --log.info("spl06 two data","msb:"..msb.." lsb:"..lsb)
  78. if regaddr ==0x10 then
  79. return msb16 + bit.rshift((lsb&0xF0),4)
  80. elseif regaddr ==0x11 then
  81. return (msb&0x0f)*256 + lsb
  82. else
  83. return msb*256+lsb
  84. end
  85. end
  86. end
  87. --器件ID检测
  88. local function chip_check()
  89. i2c.send(i2cid, SPL06_ADDRESS_ADR_HIGH, SPL06_CHIP_ID_CHECK)--读器件地址
  90. local revData = i2c.recv(i2cid, SPL06_ADDRESS_ADR_HIGH, 1)
  91. if revData:byte() ~= nil then
  92. SPL06_ADDRESS_ADR = SPL06_ADDRESS_ADR_HIGH
  93. else
  94. i2c.send(i2cid, SPL06_ADDRESS_ADR_LOW, SPL06_CHIP_ID_CHECK)--读器件地址
  95. sys.wait(50)
  96. local revData = i2c.recv(i2cid, SPL06_ADDRESS_ADR_LOW, 1)
  97. if revData:byte() ~= nil then
  98. SPL06_ADDRESS_ADR = SPL06_ADDRESS_ADR_LOW
  99. else
  100. log.info("i2c", "Can't find adxl34x device")
  101. return false
  102. end
  103. end
  104. i2c.send(i2cid, SPL06_ADDRESS_ADR, SPL06_CHIP_ID_CHECK)--读器件地址
  105. sys.wait(50)
  106. local revData = i2c.recv(i2cid, SPL06_ADDRESS_ADR, 1)
  107. if revData:byte() == SPL06_CHIP_ID then
  108. log.info("Device i2c id is: SPL06")
  109. else
  110. log.info("i2c", "Can't find SPL06 device")
  111. return false
  112. end
  113. return true
  114. end
  115. --[[
  116. spl06初始化
  117. @api spl06.init(i2cid)
  118. @number 所在的i2c总线id
  119. @return bool 成功返回true
  120. @usage
  121. spl06.init(0)
  122. ]]
  123. function spl06.init(i2cid)
  124. i2cid = i2c_id
  125. sys.wait(20)--20 毫秒等待设备稳定
  126. if chip_check() then
  127. i2c.send(i2cid, SPL06_ADDRESS_ADR,{0x0c,0x89})--reset
  128. log.info("spl06 init_ok")
  129. sys.wait(20)
  130. return true
  131. end
  132. return false
  133. end
  134. --[[
  135. 获取spl06数据
  136. @api spl06.get_data()
  137. @return table spl06数据
  138. @usage
  139. local spl06_data = spl06.get_data()
  140. log.info("spl06_data", "spl06_data.P:"..(spl06_data.P*100),"spl06_data.T"..(spl06_data.T))
  141. ]]
  142. function spl06.get_data()
  143. local spl06_data={P=nil,T=nil}
  144. local Total_Number_24 = 16777216.0
  145. local Total_Number_20 = 1048576.0
  146. local Total_Number_16 = 65535.0
  147. local Total_Number_12 = 4096.0
  148. ---读取压力补偿值
  149. local C0 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x10)
  150. local C1 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x11)
  151. local C00 = i2cReadThreeData(SPL06_ADDRESS_ADR,0x13)
  152. local C10 = i2cReadThreeData(SPL06_ADDRESS_ADR,0x15)
  153. local C01 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x18)
  154. local C11 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x1a)
  155. local C20 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x1c)
  156. local C21 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x1e)
  157. local C30 = i2cReadTwoData(SPL06_ADDRESS_ADR,0x20)
  158. if C0>0x800 then
  159. C0 = C0 - Total_Number_12
  160. end
  161. if C1>0x800 then
  162. C1 = C1 - Total_Number_12
  163. end
  164. if C00>0x80000 then
  165. C00 = C00 - Total_Number_20
  166. end
  167. if C10>0x80000 then
  168. C10 = C10 - Total_Number_20
  169. end
  170. if C01>0x8000 then
  171. C01 = C01 - Total_Number_16
  172. end
  173. if C01>0x8000 then
  174. C11 = C11 - Total_Number_16
  175. end
  176. if C20>0x8000 then
  177. C20 = C20 - Total_Number_16
  178. end
  179. if C21>0x8000 then
  180. C21 = C21 - Total_Number_16
  181. end
  182. if C30>0x8000 then
  183. C30 = C30 - Total_Number_16
  184. end
  185. -- log.info("C0 is:",C0)
  186. -- log.info("C1 is:",C1)
  187. -- log.info("C00 is:",C00)
  188. -- log.info("C10 is:",C10)
  189. -- log.info("C01 is:",C01)
  190. -- log.info("C11 is:",C11)
  191. -- log.info("C20 is:",C20)
  192. -- log.info("C21 is:",C21)
  193. -- log.info("C30 is:",C30)
  194. i2c.send(i2cid, SPL06_ADDRESS_ADR,{0x06,0x73})--PRS_CFG PM_RATE_128,TMP_PRC_8
  195. i2c.send(i2cid, SPL06_ADDRESS_ADR,{0x07,0xF3})--TMP_CFG PM_RATE_128,TMP_PRC_8
  196. -- id = i2cRead(SPL06_ADDRESS_ADR,0x09,1)
  197. -- log.info("spl06 id:",id)
  198. -- i2c.send(i2cid, SPL06_ADDRESS_ADR,{0x09,id|0x08})--oversampling times>8时必须使用
  199. i2c.send(i2cid, SPL06_ADDRESS_ADR,{0x08,0x07})--连续气压温度测量
  200. sys.wait(50)
  201. local adc_P = i2cReadThreeData(SPL06_ADDRESS_ADR,0x00)
  202. local adc_T = i2cReadThreeData(SPL06_ADDRESS_ADR,0x03)
  203. if adc_P>0x800000 then
  204. adc_P = adc_P - Total_Number_24
  205. end
  206. if adc_T>0x800000 then
  207. adc_T = adc_T - Total_Number_24
  208. end
  209. --log.info("adc_P is:",adc_P)
  210. --log.info("adc_T is:",adc_T)
  211. if adc_P == 0 then
  212. log.info("adc_P is 0")
  213. return toint(9996),toint(9996)
  214. end
  215. if not adc_P then
  216. log.info("adc_P is nil")
  217. return toint(9996),toint(9996)
  218. end
  219. local kP = 7864320
  220. local kT = 7864320
  221. local Praw_src = adc_P / kP
  222. local Traw_src = adc_T / kT
  223. --log.info("Traw_src is:",Traw_src)
  224. --log.info("Praw_src is:",Praw_src)
  225. --计算气压
  226. local qua2 = C10 + Praw_src * (C20 + Praw_src* C30)
  227. local qua3 = Traw_src * Praw_src * (C11 + Praw_src * C21)
  228. spl06_data.P = C00 + Praw_src * qua2 + Traw_src * C01 + qua3
  229. --计算温度
  230. spl06_data.T = C0*0.5 + Traw_src * C1
  231. return spl06_data or 0
  232. end
  233. return spl06