tm1637.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. count = 0
  13. tm1637.init(1,4)
  14. tm1637.singleShow(0,2)
  15. tm1637.singleShow(1,1,true)
  16. tm1637.singleShow(2,3)
  17. tm1637.singleShow(3,6)
  18. while 1 do
  19. sys.wait(1000)
  20. if count > 7 then
  21. count = 0
  22. end
  23. log.info("调整亮度", count)
  24. tm1637.setLight(count)
  25. count = count + 1
  26. end
  27. end)
  28. ]]
  29. local tm1637 = {}
  30. local sys = require "sys"
  31. local TM1637_SCL
  32. local TM1637_SDA
  33. local function i2c_init(scl,sda)
  34. TM1637_SCL = gpio.setup(scl, 0, gpio.PULLUP)
  35. TM1637_SDA = gpio.setup(sda, 0, gpio.PULLUP)
  36. end
  37. local function i2c_strat()
  38. TM1637_SDA(1)
  39. TM1637_SCL(1)
  40. TM1637_SDA(0)
  41. TM1637_SCL(0)
  42. -- sys.wait(10)
  43. end
  44. local function i2c_send(data)
  45. for i = 0, 7, 1 do
  46. TM1637_SCL(0)
  47. local mbit = bit.isset(data, i) and 1 or 0
  48. TM1637_SDA(mbit)
  49. TM1637_SCL(1)
  50. end
  51. TM1637_SCL(0)
  52. TM1637_SDA(1)
  53. TM1637_SCL(1)
  54. -- mack = TM1637_SDA()
  55. -- if mack == 0 then TM1637_SDA(0) end
  56. TM1637_SDA(0)
  57. TM1637_SCL(0)
  58. end
  59. local function i2c_stop()
  60. TM1637_SCL(0)
  61. TM1637_SDA(0)
  62. TM1637_SCL(1)
  63. TM1637_SDA(1)
  64. end
  65. local function i2c_recv(num)
  66. local revData = i2c.recv(i2cid, i2cslaveaddr, num)
  67. return revData
  68. end
  69. --[[
  70. 0
  71. ---
  72. 5 | | 1 *
  73. -6- 7 (on 2nd segment)
  74. 4 | | 2 *
  75. ---
  76. 3
  77. 76543210
  78. ]]
  79. --[[ 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 - * ]]
  80. local 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};
  81. --[[
  82. TM1637显示清空
  83. @api tm1637.clear()
  84. @usage
  85. tm1637.clear()
  86. ]]
  87. function tm1637.clear()
  88. i2c_strat()
  89. i2c_send(0x40) -- 自加模式
  90. i2c_stop()
  91. i2c_strat()
  92. i2c_send(0xc0)
  93. for i = 1, 4 do i2c_send(0x00) end
  94. i2c_stop()
  95. end
  96. --[[
  97. TM1637显示
  98. @api tm1637.singleShow(com,date,comma)
  99. @number com com端口号
  100. @number date 显示数字,0-9即显示0-9,10会显示a以此类推
  101. @bool comma 是否带逗号或冒号
  102. @usage
  103. tm1637.singleShow(0,2)
  104. tm1637.singleShow(1,1,true)
  105. tm1637.singleShow(2,3)
  106. tm1637.singleShow(3,6)
  107. ]]
  108. function tm1637.singleShow(com,date,comma)
  109. if com then
  110. i2c_strat()
  111. i2c_send(0x44) -- 地址模式
  112. i2c_stop()
  113. i2c_strat()
  114. i2c_send(0xc0+com)
  115. if comma then
  116. i2c_send(bit.bor(hextable[date+1],0x80))
  117. else
  118. i2c_send(hextable[date+1])
  119. end
  120. i2c_stop()
  121. end
  122. end
  123. --[[
  124. TM1637设置亮度
  125. @api tm1637.setLight(light)
  126. @number light 亮度,0-7
  127. @usage
  128. tm1637.setLight(3)
  129. ]]
  130. function tm1637.setLight(light)
  131. i2c_strat()
  132. i2c_send(bit.bor(light,0x88))
  133. i2c_stop()
  134. end
  135. --[[
  136. TM1637初始化
  137. @api tm1637.init(scl,sda)
  138. @number scl i2c_scl
  139. @number sda i2c_sda
  140. @return bool 成功返回true
  141. @usage
  142. tm1637.init(1,4)
  143. ]]
  144. function tm1637.init(scl,sda)
  145. i2c_init(scl,sda)
  146. sys.wait(200)
  147. tm1637.clear()
  148. i2c_strat()
  149. i2c_send(0x8f)
  150. i2c_stop()
  151. return true
  152. end
  153. return tm1637