ds3231.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. --- 模块功能:DS3231
  2. -- @module DS3231
  3. -- @author Dozingfiretruck
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2021.03.14
  7. module(...,package.seeall)
  8. require"utils"
  9. require"bit"
  10. pm.wake("ds3231")
  11. local i2cid = 2 --i2cid
  12. local DS3231_ADDRESS = 0x68 -- address pin low (GND), default for InvenSense evaluation board
  13. local i2cslaveaddr = DS3231_ADDRESS --slave address
  14. ---器件通讯地址
  15. local DS3231_CHIP_ID_ADDR = 0xD0
  16. local DS3231_ID = 0x58
  17. ---DS3231所用地址
  18. local REG_SEC = 0x00
  19. local REG_MIN = 0x01
  20. local REG_HOUR = 0x02
  21. local REG_DAY = 0x03
  22. local REG_WEEK = 0x04
  23. local REG_MON = 0x05
  24. local REG_YEAR = 0x06
  25. local REG_ALM1_SEC = 0x07
  26. local REG_ALM1_MIN = 0x08
  27. local REG_ALM1_HOUR = 0x09
  28. local REG_ALM1_DAY_DATE = 0x0A
  29. local REG_ALM2_MIN = 0x0B
  30. local REG_ALM2_HOUR = 0x0C
  31. local REG_ALM2_DAY_DATE = 0x0D
  32. local REG_CONTROL = 0x0E
  33. local REG_STATUS = 0x0F
  34. local REG_AGING_OFFSET = 0x10
  35. local REG_TEMP_MSB = 0x11
  36. local REG_TEMP_LSB = 0x12
  37. local function i2c_send(data)
  38. i2c.send(i2cid, i2cslaveaddr, data)
  39. end
  40. local function i2c_recv(data,num)
  41. i2c.send(i2cid, i2cslaveaddr, data)
  42. local revData = i2c.recv(i2cid, i2cslaveaddr, num)
  43. return revData
  44. end
  45. --器件初始化
  46. local function DS3231_init()
  47. i2c_send({REG_CONTROL, 0x04})--close clock out
  48. log.info("i2c init_ok")
  49. end
  50. local function ds3231_get_temperature()
  51. local temp
  52. local T = i2c_recv(REG_TEMP_MSB,2)
  53. if bit.band(T:byte(1),0x80) then
  54. --negative temperature
  55. temp = T:byte(1)
  56. temp = temp - (bit.rshift(T:byte(2),6)*0.25)--0.25C resolution
  57. else
  58. --positive temperature
  59. temp = T:byte(1)
  60. temp = temp + (bit.band(bit.rshift(T:byte(2),6),0x03)*0.25)
  61. end
  62. return temp;
  63. end
  64. local function bcd_to_hex(data)
  65. local hex = bit.rshift(data,4)*10+bit.band(data,0x0f)
  66. return hex;
  67. end
  68. local function hex_to_bcd(data)
  69. local hex = bit.lshift(data/10,4)+data%10
  70. return hex;
  71. end
  72. local function ds3231_read_time()
  73. -- read time
  74. local time_data = {}
  75. local data = i2c_recv(REG_SEC,7)
  76. time_data.tm_year = bcd_to_hex(data:byte(7)) + 2000
  77. time_data.tm_mon = bcd_to_hex(bit.band(data:byte(6),0x7f)) - 1
  78. time_data.tm_mday = bcd_to_hex(data:byte(5))
  79. time_data.tm_hour = bcd_to_hex(data:byte(3))
  80. time_data.tm_min = bcd_to_hex(data:byte(2))
  81. time_data.tm_sec = bcd_to_hex(data:byte(1))
  82. return time_data
  83. end
  84. local function ds3231_set_time(time)
  85. -- set time
  86. local data7 = hex_to_bcd(time.tm_year + 2000)
  87. local data6 = hex_to_bcd(time.tm_mon + 1)
  88. local data5 = hex_to_bcd(time.tm_mday)
  89. local data4 = hex_to_bcd(time.tm_wday+1)
  90. local data3 = hex_to_bcd(time.tm_hour)
  91. local data2 = hex_to_bcd(time.tm_min)
  92. local data1 = hex_to_bcd(time.tm_sec)
  93. i2c_send({REG_SEC, data1,data2,data3,data4,data5,data6,data7})
  94. end
  95. local function DS3231()
  96. sys.wait(4000)
  97. if i2c.setup(i2cid,i2c.SLOW) ~= i2c.SLOW then
  98. log.error("I2c.init","fail")
  99. return
  100. end
  101. DS3231_init()
  102. while true do
  103. log.info("ds3231_get_temperature", ds3231_get_temperature())
  104. local time = ds3231_read_time()
  105. log.info("ds3231_read_time",time.tm_year,time.tm_mon,time.tm_mday,time.tm_hour,time.tm_min,time.tm_sec)
  106. local set_time = {tm_year=2021,tm_mon=3,tm_mday=0,tm_wday=0,tm_hour=0,tm_min=0,tm_sec=0}
  107. ds3231_set_time(set_time)
  108. log.info("ds3231_read_time",time.tm_year,time.tm_mon,time.tm_mday,time.tm_hour,time.tm_min,time.tm_sec)
  109. sys.wait(1000)
  110. end
  111. end
  112. sys.taskInit(DS3231)