ds3231.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. --[[
  2. @module ds3231
  3. @summary ds3231 实时时钟传感器
  4. @version 1.0
  5. @date 2022.03.16
  6. @author Dozingfiretruck
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. -- 用法实例
  10. local ds3231 = require "ds3231"
  11. i2cid = 0
  12. i2c_speed = i2c.FAST
  13. sys.taskInit(function()
  14. i2c.setup(i2cid,i2c_speed)
  15. ds3231.init(i2cid)--初始化,传入i2c_id
  16. while 1 do
  17. log.info("ds3231.get_temperature", ds3231.get_temperature())
  18. local time = ds3231.read_time()
  19. log.info("ds3231.read_time",time.tm_year,time.tm_mon,time.tm_mday,time.tm_hour,time.tm_min,time.tm_sec)
  20. sys.wait(5000)
  21. local set_time = {tm_year=2021,tm_mon=3,tm_mday=0,tm_wday=0,tm_hour=0,tm_min=0,tm_sec=0}
  22. ds3231.set_time(set_time)
  23. time = ds3231.read_time()
  24. log.info("ds3231_read_time",time.tm_year,time.tm_mon,time.tm_mday,time.tm_hour,time.tm_min,time.tm_sec)
  25. sys.wait(1000)
  26. end
  27. end)
  28. ]]
  29. local ds3231 = {}
  30. local sys = require "sys"
  31. local i2cid
  32. local DS3231_ADDRESS = 0x68 -- address pin low (GND), default for InvenSense evaluation board
  33. local i2cslaveaddr = DS3231_ADDRESS --slave address
  34. ---DS3231所用地址
  35. local REG_SEC = 0x00
  36. local REG_MIN = 0x01
  37. local REG_HOUR = 0x02
  38. local REG_DAY = 0x03
  39. local REG_WEEK = 0x04
  40. local REG_MON = 0x05
  41. local REG_YEAR = 0x06
  42. local REG_ALM1_SEC = 0x07
  43. local REG_ALM1_MIN = 0x08
  44. local REG_ALM1_HOUR = 0x09
  45. local REG_ALM1_DAY_DATE = 0x0A
  46. local REG_ALM2_MIN = 0x0B
  47. local REG_ALM2_HOUR = 0x0C
  48. local REG_ALM2_DAY_DATE = 0x0D
  49. local REG_CONTROL = 0x0E
  50. local REG_STATUS = 0x0F
  51. local REG_AGING_OFFSET = 0x10
  52. local REG_TEMP_MSB = 0x11
  53. local REG_TEMP_LSB = 0x12
  54. local function i2c_send(data)
  55. i2c.send(i2cid, i2cslaveaddr, data)
  56. end
  57. local function i2c_recv(data,num)
  58. i2c.send(i2cid, i2cslaveaddr, data)
  59. local revData = i2c.recv(i2cid, i2cslaveaddr, num)
  60. return revData
  61. end
  62. local function bcd_to_hex(data)
  63. local hex = bit.rshift(data,4)*10+bit.band(data,0x0f)
  64. return hex;
  65. end
  66. local function hex_to_bcd(data)
  67. local bcd = bit.lshift(math.floor(data/10),4)+data%10
  68. return bcd;
  69. end
  70. --[[
  71. ds3231初始化
  72. @api ds3231.init(i2c_id)
  73. @number 所在的i2c总线id
  74. @return bool 成功返回true
  75. @usage
  76. ds3231.init(0)
  77. ]]
  78. function ds3231.init(i2c_id)
  79. i2cid = i2c_id
  80. i2c_send({REG_CONTROL, 0x04})--close clock out
  81. log.info("ds3231 init_ok")
  82. return true
  83. end
  84. --[[
  85. 获取温度数据
  86. @api ds3231.get_temperature()
  87. @return number 温度数据
  88. @usage
  89. log.info("ds3231.get_temperature", ds3231.get_temperature())
  90. ]]
  91. function ds3231.get_temperature()
  92. local temp
  93. local T = i2c_recv(REG_TEMP_MSB,2)
  94. if bit.band(T:byte(1),0x80) then
  95. --negative temperature
  96. temp = T:byte(1)
  97. temp = temp - (bit.rshift(T:byte(2),6)*0.25)--0.25C resolution
  98. else
  99. --positive temperature
  100. temp = T:byte(1)
  101. temp = temp + (bit.band(bit.rshift(T:byte(2),6),0x03)*0.25)
  102. end
  103. return temp;
  104. end
  105. --[[
  106. 获取时间
  107. @api ds3231.read_time()
  108. @return table 时间表
  109. @usage
  110. local time = ds3231.read_time()
  111. log.info("ds3231.read_time",time.tm_year,time.tm_mon,time.tm_mday,time.tm_hour,time.tm_min,time.tm_sec)
  112. ]]
  113. function ds3231.read_time()
  114. -- read time
  115. local time_data = {}
  116. local data = i2c_recv(REG_SEC,7)
  117. time_data.tm_year = bcd_to_hex(data:byte(7)) + 2000
  118. time_data.tm_mon = bcd_to_hex(bit.band(data:byte(6),0x7f)) - 1
  119. time_data.tm_mday = bcd_to_hex(data:byte(5))
  120. time_data.tm_hour = bcd_to_hex(data:byte(3))
  121. time_data.tm_min = bcd_to_hex(data:byte(2))
  122. time_data.tm_sec = bcd_to_hex(data:byte(1))
  123. return time_data
  124. end
  125. --[[
  126. 设置时间
  127. @api ds3231.set_time(time)
  128. @table time 时间表
  129. @usage
  130. local set_time = {tm_year=2021,tm_mon=3,tm_mday=0,tm_wday=0,tm_hour=0,tm_min=0,tm_sec=0}
  131. ds3231.set_time(set_time)
  132. ]]
  133. function ds3231.set_time(time)
  134. -- set time
  135. local data7 = hex_to_bcd(time.tm_year + 2000)
  136. local data6 = hex_to_bcd(time.tm_mon + 1)
  137. local data5 = hex_to_bcd(time.tm_mday)
  138. local data4 = hex_to_bcd(time.tm_wday+1)
  139. local data3 = hex_to_bcd(time.tm_hour)
  140. local data2 = hex_to_bcd(time.tm_min)
  141. local data1 = hex_to_bcd(time.tm_sec)
  142. i2c_send({REG_SEC, data1,data2,data3,data4,data5,data6,data7})
  143. end
  144. return ds3231