tm1637.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. --[[
  2. @module tm1637
  3. @summary tm1637 驱动
  4. @version 1.0
  5. @date 2022.04.06
  6. @author Dozingfiretruck
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. -- 用法实例
  10. local tm1637 = require "tm1637"
  11. sys.taskInit(function()
  12. tm1637.init(1,4)
  13. tm1637.singleShow(0,2)
  14. tm1637.singleShow(1,1,true)
  15. tm1637.singleShow(2,3)
  16. tm1637.singleShow(3,6)
  17. while 1 do
  18. sys.wait(1000)
  19. end
  20. end)
  21. ]]
  22. local tm1637 = {}
  23. local sys = require "sys"
  24. local TM1637_SCL
  25. local TM1637_SDA
  26. local function i2c_init(scl,sda)
  27. TM1637_SCL = gpio.setup(scl, 0, gpio.PULLUP)
  28. TM1637_SDA = gpio.setup(sda, 0, gpio.PULLUP)
  29. end
  30. local function i2c_strat()
  31. TM1637_SDA(1)
  32. TM1637_SCL(1)
  33. TM1637_SDA(0)
  34. TM1637_SCL(0)
  35. -- sys.wait(10)
  36. end
  37. local function i2c_send(data)
  38. for i = 0, 7, 1 do
  39. TM1637_SCL(0)
  40. local mbit = bit.isset(data, i) and 1 or 0
  41. TM1637_SDA(mbit)
  42. TM1637_SCL(1)
  43. end
  44. TM1637_SCL(0)
  45. TM1637_SDA(1)
  46. TM1637_SCL(1)
  47. -- mack = TM1637_SDA()
  48. -- if mack == 0 then TM1637_SDA(0) end
  49. TM1637_SDA(0)
  50. TM1637_SCL(0)
  51. end
  52. local function i2c_stop()
  53. TM1637_SCL(0)
  54. TM1637_SDA(0)
  55. TM1637_SCL(1)
  56. TM1637_SDA(1)
  57. end
  58. local function i2c_recv(num)
  59. local revData = i2c.recv(i2cid, i2cslaveaddr, num)
  60. return revData
  61. end
  62. --[[
  63. 0
  64. ---
  65. 5 | | 1 *
  66. -6- 7 (on 2nd segment)
  67. 4 | | 2 *
  68. ---
  69. 3
  70. 76543210
  71. ]]
  72. --[[ 0 1 2 3 4 5 6 7 8 9 A b C d E F G H I J K L M n O P q r S t U v W X y Z - * ]]
  73. hextable = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x3d,0x76,0x06,0x1e,0x76,0x38,0x55,0x54,0x3f,0x73,0x67,0x50,0x6d,0x78,0x3e,0x1c,0x2a,0x76,0x6e,0x5b,0x00,0x40,0x63};
  74. --[[
  75. TM1637显示清空
  76. @api tm1637.clear()
  77. @usage
  78. tm1637.clear()
  79. ]]
  80. function tm1637.clear()
  81. i2c_strat()
  82. i2c_send(0x40) -- 自加模式
  83. i2c_stop()
  84. i2c_strat()
  85. i2c_send(0xc0)
  86. for i = 1, 4 do i2c_send(0x00) end
  87. i2c_stop()
  88. end
  89. --[[
  90. TM1637显示
  91. @api tm1637.singleShow(com,date,comma)
  92. @number com com端口号
  93. @number date 显示数字,0-9即显示0-9,10会显示a以此类推
  94. @bool comma 是否带逗号或冒号
  95. @usage
  96. tm1637.singleShow(0,2)
  97. tm1637.singleShow(1,1,true)
  98. tm1637.singleShow(2,3)
  99. tm1637.singleShow(3,6)
  100. ]]
  101. function tm1637.singleShow(com,date,comma)
  102. if com then
  103. i2c_strat()
  104. i2c_send(0x44) -- 地址模式
  105. i2c_stop()
  106. i2c_strat()
  107. i2c_send(0xc0+com)
  108. if comma then
  109. i2c_send(bit.bor(hextable[date+1],0x80))
  110. else
  111. i2c_send(hextable[date+1])
  112. end
  113. i2c_stop()
  114. end
  115. end
  116. --[[
  117. TM1637初始化
  118. @api tm1637.init(scl,sda)
  119. @number scl i2c_scl
  120. @number sda i2c_sda
  121. @return bool 成功返回true
  122. @usage
  123. tm1637.init(1,4)
  124. ]]
  125. function tm1637.init(scl,sda)
  126. i2c_init(scl,sda)
  127. sys.wait(200)
  128. tm1637.clear()
  129. i2c_strat()
  130. i2c_send(0x8f)
  131. i2c_stop()
  132. return true
  133. end
  134. return tm1637