立即注册

micropython-microbit 中文社区

查看: 419|回复: 1

用 Micro:bit 驱动 Adafruit 4x14 段码显示器

[复制链接]
  • TA的每日心情
    开心
    2019-5-12 11:06
  • 签到天数: 8 天

    [LV.3]偶尔看看II

    发表于 2017-7-2 22:09:34 | 显示全部楼层 |阅读模式

    马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    整理自:http://www.smythe-consulting.com ... 14-segment-i2c.html

    Adafruit 4x14段码显示器使用了 I2C 接口的 HT16K33 控制器,可以方便的用 microbit 的I2C驱动。

    14-seg beer.JPG


    HT16K33 控制器的主要命令有:

    • 0x21  启动振荡器
    • 0x80  清除显示
    • 0x81  发送 RAM 数据到显示器
    • 0x83  显示器以 2Hz 频率闪烁
    • 0xE0  Dim the display
    • 0xEF  设置最大亮度



    显示字符时,我们需要写数据到HT16K33的 RAM,然后将数据发送到显示器。因为我们总是一次发送四个显示器的数据,在切换HT16K33 RAM的起始地址,如0x0。然后是四个双字节对(每个数字的一对),从左到右。每个数字的第二字节是传统的7位模式(除了中间段分成两部分),第一个字节由构成字母表的额外部分(如对角段)。然后将控制字节81发送给数据以“锁定”到显示。

    上面所示的位映射可以表示为两个八位二进制数,其中A是最小有效位,按下面的顺序(第一个位没有使用;您可以将其设为0或1)。字节1和2是反向发送的:

    字节2 字节1
    0 DP N M L K J H G2 G1 F E D C B A

    显示段 A,对应二进制 00000000,00000001, 在取反 -> hex 0x1, 0x0
    显示段 G1,对应二进制 00000000,01000000,取反后 -> hex 0x40, 0x0
    显示 "0",对应段 F E D C B A - 二进制是 00000000,00111111,取反 -> hex 0x3F, 0x0
    显示 "K", 对应段 N K , G1 F E - 二进制 00100100,01110000,取反 -> hex 0x70,0x24


    led_matrix_segments2.png


    MicroPython 代码:
    1. from microbit import i2c, sleep
    2. # address of HT16K33 is 0x70

    3. sleep(1) # allow time for display to settle after power on/reset
    4. # start oscillator
    5. i2c.write(0x70, bytearray([0x21]))


    6. # To display something:
    7. # send     (i2c device address,([RAM start addr,    four pairs of bytes    ]))
    8. # i2c.write(      0x70        ,([0x0           ,digit1,digit2,digit3,digit4]))
    9. # turn on the display


    10. # Clear the display
    11. i2c.write(0x70, bytearray([0x80]))
    12. sleep(1000)


    13. # data for each character
    14. #   byte1,byte2
    15. #0 = 0x3f,0x0
    16. #1 = 0x6,0x0
    17. #2 = 0xdb,0x0
    18. #3 = 0xcf,0x0
    19. #4 = 0xe6,0x0
    20. #5 = 0xed,0x0
    21. #6 = 0xfd,0x0
    22. #7 = 0x1,0xc
    23. #8 = 0xff,0x0
    24. #9 = 0xc7,0x8
    25. #. = 0x0,0x40
    26. #+ = 0xc0,0x12
    27. #- = 0xc0,0x0
    28. #/ = 0x0,0xc
    29. #| = 0x0,0x10
    30. #\ = 0x0,0x21
    31. #0x = 0x8d,0xc
    32. #all segments = 0x3f,0x3f
    33. #A 0xF7,0x0
    34. #B 0x8F,0x12
    35. #C 0x39,0x0
    36. #D 0xF,0x12
    37. #E 0xF9,0x0
    38. #F 0xF1,0x0
    39. #G 0xBD,0x0
    40. #H 0xF6,0x0
    41. #I 0x9,0x12
    42. #J 0x1E,0x0
    43. #K 0x70,0x24
    44. #L 0x38,0x0
    45. #M 0x36,0x5
    46. #N 0x36,0x21
    47. #O 0x3F,0x0
    48. #P 0xF3,0x0
    49. #Q 0x3F,0x20
    50. #R 0xF3,0x20
    51. #S 0xED,0x0
    52. #T 0x0,0x12
    53. #U 0x3E,0x0
    54. #V 0x30,0xC
    55. #W 0x36,0x28
    56. #X 0x0,0x2D
    57. #Y 0x0,0x15
    58. #Z 0x9,0xC
    59. #a 0x58,0x10
    60. #b 0x78,0x20
    61. #c 0xD8,0x0
    62. #d 0x8E,0x8
    63. #e 0x58,0x8
    64. #f 0x71,0x0
    65. #g 0x8E,0x4
    66. #h 0x70,0x10
    67. #i 0x0,0x10
    68. #j 0xE,0x0
    69. #k 0x0,0x36
    70. #l 0x30,0x0
    71. #m 0xD4,0x10
    72. #n 0x50,0x10
    73. #o 0xDC,0x0
    74. #p 0x70,0x1
    75. #q 0x86,0x4
    76. #r 0x50,0x0
    77. #s 0x88,0x20
    78. #t 0x78,0x0
    79. #u 0x1C,0x0
    80. #v 0x4,0x20
    81. #w 0x14,0x28
    82. #x 0xC0,0x28
    83. #y 0xC,0x28
    84. #z 0x48,0x8

    85. while True:

    86. # Turn on all segments
    87.     i2c.write(0x70,bytearray([0x0,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f,0x3f]))
    88.     i2c.write(0x70, bytearray([0x81])) # display on
    89.     sleep(1000)

    90. # print out the character set four at a time
    91. #0 1 2 3
    92.     i2c.write(0x70, bytearray([0x0,0x3f,0x0,0x6,0x0,0xdb,0x0,0xcf,0x0]))
    93.     i2c.write(0x70, bytearray([0x81])) # display on
    94.     sleep (1000)
    95. #4 5 6 7
    96.     i2c.write(0x70, bytearray([0x0,0xe6,0x0,0xed,0x0,0xfd,0x0,0x1,0xc]))
    97.     i2c.write(0x70, bytearray([0x81])) # display on
    98.     sleep (1000)
    99. #8 9 0x all
    100.     i2c.write(0x70, bytearray([0x0,0xff,0x0,0xe7,0x0,0xed,0x12,0x3f,0x3f]))
    101.     i2c.write(0x70, bytearray([0x81])) # display on
    102.     sleep (1000)
    103. #- / . +
    104.     i2c.write(0x70, bytearray([0x0,0x0,0x40,0xc0,0x12,0xc0,0x0,0x0,0xc]))
    105.     i2c.write(0x70, bytearray([0x81])) # display on
    106.     sleep (1000)
    107. #A B C D
    108.     i2c.write(0x70, bytearray([0x0,0xF7,0x0,0x8F,0x12,0x39,0x0,0xF,0x12]))
    109.     i2c.write(0x70, bytearray([0x81])) # display on
    110.     sleep (1000)
    111. #E F G H
    112.     i2c.write(0x70, bytearray([0x0,0xF9,0x0,0xF1,0x0,0xBD,0x0,0xF6,0x0]))
    113.     i2c.write(0x70, bytearray([0x81])) # display on
    114.     sleep (1000)
    115. #I J K L
    116.     i2c.write(0x70, bytearray([0x0,0x9,0x12,0x1E,0x0,0x70,0x24,0x38,0x0]))
    117.     i2c.write(0x70, bytearray([0x81])) # display on
    118.     sleep (1000)
    119. #M N O P
    120.     i2c.write(0x70, bytearray([0x0,0x36,0x5,0x36,0x21,0x3F,0x0,0xF3,0x0]))
    121.     i2c.write(0x70, bytearray([0x81])) # display on
    122.     sleep (1000)
    123. #Q R S T
    124.     i2c.write(0x70, bytearray([0x0,0x3F,0x20,0xF3,0x20,0xED,0x0,0x1,0x12]))
    125.     i2c.write(0x70, bytearray([0x81])) # display on
    126.     sleep (1000)
    127. #U V W X
    128.     i2c.write(0x70, bytearray([0x0,0x3E,0x0,0x30,0xC,0x36,0x28,0x0,0x2D]))
    129.     i2c.write(0x70, bytearray([0x81])) # display on
    130.     sleep (1000)
    131. #Y Z all all
    132.     i2c.write(0x70, bytearray([0x0,0x0,0x15,0x9,0xC,0x3f,0x3f,0x3f,0x3f]))
    133.     i2c.write(0x70, bytearray([0x81])) # display on
    134.     sleep (1000)
    135. #a b c d
    136.     i2c.write(0x70, bytearray([0x0,0x58,0x10,0x78,0x20,0xD8,0x0,0x8E,0x8]))
    137.     i2c.write(0x70, bytearray([0x81])) # display on
    138.     sleep (1000)
    139. #e f g h
    140.     i2c.write(0x70, bytearray([0x0,0x58,0x8,0x71,0x0,0x8E,0x4,0x70,0x10]))
    141.     i2c.write(0x70, bytearray([0x81])) # display on
    142.     sleep (1000)
    143. #i j k l
    144.     i2c.write(0x70, bytearray([0x0,0x0,0x10,0xE,0x0,0x0,0x36,0x30,0x0]))
    145.     i2c.write(0x70, bytearray([0x81])) # display on
    146.     sleep (1000)
    147. #m n o p
    148.     i2c.write(0x70, bytearray([0x0,0xD4,0x10,0x50,0x10,0xDC,0x0,0x70,0x1]))
    149.     i2c.write(0x70, bytearray([0x81])) # display on
    150.     sleep (1000)
    151. #q r s t
    152.     i2c.write(0x70, bytearray([0x0,0x86,0x4,0x50,0x0,0x88,0x20,0x78,0x0]))
    153.     i2c.write(0x70, bytearray([0x81])) # display on
    154.     sleep (1000)
    155. #u v w x
    156.     i2c.write(0x70, bytearray([0x0,0x1C,0x0,0x4,0x20,0x14,0x28,0xC0,0x28]))
    157.     i2c.write(0x70, bytearray([0x81])) # display on
    158.     sleep (1000)
    159. #y z
    160.     i2c.write(0x70, bytearray([0x0,0xC,0x20,0x48,0x8,0x0,0x0,0x0,0x0]))
    161.     i2c.write(0x70, bytearray([0x81])) # display on
    162.     sleep (1000)

    163. # display a message
    164. #                             |   L    |    O   |    V     |   E   
    165.     i2c.write(0x70,bytearray([0x00,0x38,0x0,0x3f,0x0,0x30,0xC,0xf9,0x0]))
    166.     i2c.write(0x70, bytearray([0x81])) # display on
    167.     sleep(1000)

    168. #                             |   B    |    E    |     E   |   R   
    169.     i2c.write(0x70,bytearray([0x00,0x8F,0x12,0xf9,0x00,0xf9,0x00,0xf3,0x20]))
    170.     i2c.write(0x70, bytearray([0x81])) # display on
    171.     sleep(1000)


    172.     i2c.write(0x70, bytearray([0xE0])) # dim the display (range 0xE0 to 0xEF)
    173.     sleep (1000)
    174.     i2c.write(0x70, bytearray([0xEF])) # full brightness again
    175.     sleep (1000)
    176.     i2c.write(0x70, bytearray([0x83])) # blink the display (0x83=2Hz blink, 0x85=1Hz, 0x87=0.5Hz)
    177.     sleep (3000)

    178. # Clear the display
    179.     i2c.write(0x70, bytearray([0x80]))
    180.     sleep(1000)

    181. # make a spinney-round thing!
    182.     for iterations in range(20):
    183.         i2c.write(0x70, bytearray([0x0,0x0,0x12,0x0,0x12,0x0,0x12,0x0,0x12]))
    184.         i2c.write(0x70, bytearray([0x81])) # display on
    185.         sleep (21)
    186.    
    187.         i2c.write(0x70, bytearray([0x0,0x0,0xc,0x0,0xc,0x0,0xc,0x0,0xc]))
    188.         i2c.write(0x70, bytearray([0x81])) # display on
    189.         sleep (20)
    190.    
    191.         i2c.write(0x70, bytearray([0x0,0xc0,0x0,0xc0,0x0,0xc0,0x0,0xc0,0x0]))
    192.         i2c.write(0x70, bytearray([0x81])) # display on
    193.         sleep (20)
    194.    
    195.         i2c.write(0x70, bytearray([0x0,0x0,0x21,0x0,0x21,0x0,0x21,0x0,0x21]))
    196.         i2c.write(0x70, bytearray([0x81])) # display on
    197.         sleep (20)

    198. # turn off the display
    199.     i2c.write(0x70, bytearray([0x80]))
    200.     sleep(1000)
    复制代码



    本帖被以下淘专辑推荐:

    回复

    使用道具 举报

  • TA的每日心情
    开心
    2019-5-12 11:06
  • 签到天数: 8 天

    [LV.3]偶尔看看II

     楼主| 发表于 2017-7-2 22:22:51 | 显示全部楼层
    回复 支持 反对

    使用道具 举报

    游客
    回复
    您需要登录后才可以回帖 登录 | 立即注册

    Archiver|手机版|小黑屋|micropython-microbit 中文社区

    GMT+8, 2019-10-16 03:30 , Processed in 0.206593 second(s), 25 queries .

    Powered by Discuz! X3.4

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表