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

micro:bit编程、教学、展示
STEM
回复
头像
shaoziyang
帖子: 3951
注册时间: 2019年 10月 21日 13:48

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

#1

帖子 shaoziyang »

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

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

14-seg beer.JPG
14-seg beer.JPG (58.99 KiB) 查看 2667 次
 


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 HG2 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
led_matrix_segments2.png (19.2 KiB) 查看 2667 次

MicroPython 代码:

代码: 全选

from microbit import i2c, sleep
# address of HT16K33 is 0x70

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


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


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


# data for each character
#   byte1,byte2
#0 = 0x3f,0x0
#1 = 0x6,0x0
#2 = 0xdb,0x0
#3 = 0xcf,0x0
#4 = 0xe6,0x0
#5 = 0xed,0x0
#6 = 0xfd,0x0
#7 = 0x1,0xc
#8 = 0xff,0x0
#9 = 0xc7,0x8
#. = 0x0,0x40
#+ = 0xc0,0x12
#- = 0xc0,0x0
#/ = 0x0,0xc
#| = 0x0,0x10
#\ = 0x0,0x21
#0x = 0x8d,0xc
#all segments = 0x3f,0x3f
#A 0xF7,0x0
#B 0x8F,0x12
#C 0x39,0x0
#D 0xF,0x12
#E 0xF9,0x0
#F 0xF1,0x0
#G 0xBD,0x0
#H 0xF6,0x0
#I 0x9,0x12
#J 0x1E,0x0
#K 0x70,0x24
#L 0x38,0x0
#M 0x36,0x5
#N 0x36,0x21
#O 0x3F,0x0
#P 0xF3,0x0
#Q 0x3F,0x20
#R 0xF3,0x20
#S 0xED,0x0
#T 0x0,0x12
#U 0x3E,0x0
#V 0x30,0xC
#W 0x36,0x28
#X 0x0,0x2D
#Y 0x0,0x15
#Z 0x9,0xC
#a 0x58,0x10
#b 0x78,0x20
#c 0xD8,0x0
#d 0x8E,0x8
#e 0x58,0x8
#f 0x71,0x0
#g 0x8E,0x4
#h 0x70,0x10
#i 0x0,0x10
#j 0xE,0x0
#k 0x0,0x36
#l 0x30,0x0
#m 0xD4,0x10
#n 0x50,0x10
#o 0xDC,0x0
#p 0x70,0x1
#q 0x86,0x4
#r 0x50,0x0
#s 0x88,0x20
#t 0x78,0x0
#u 0x1C,0x0
#v 0x4,0x20
#w 0x14,0x28
#x 0xC0,0x28
#y 0xC,0x28
#z 0x48,0x8

while True:

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

# print out the character set four at a time
#0 1 2 3
    i2c.write(0x70, bytearray([0x0,0x3f,0x0,0x6,0x0,0xdb,0x0,0xcf,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#4 5 6 7
    i2c.write(0x70, bytearray([0x0,0xe6,0x0,0xed,0x0,0xfd,0x0,0x1,0xc]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#8 9 0x all
    i2c.write(0x70, bytearray([0x0,0xff,0x0,0xe7,0x0,0xed,0x12,0x3f,0x3f]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#- / . +
    i2c.write(0x70, bytearray([0x0,0x0,0x40,0xc0,0x12,0xc0,0x0,0x0,0xc]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#A B C D
    i2c.write(0x70, bytearray([0x0,0xF7,0x0,0x8F,0x12,0x39,0x0,0xF,0x12]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#E F G H
    i2c.write(0x70, bytearray([0x0,0xF9,0x0,0xF1,0x0,0xBD,0x0,0xF6,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#I J K L
    i2c.write(0x70, bytearray([0x0,0x9,0x12,0x1E,0x0,0x70,0x24,0x38,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#M N O P
    i2c.write(0x70, bytearray([0x0,0x36,0x5,0x36,0x21,0x3F,0x0,0xF3,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#Q R S T
    i2c.write(0x70, bytearray([0x0,0x3F,0x20,0xF3,0x20,0xED,0x0,0x1,0x12]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#U V W X
    i2c.write(0x70, bytearray([0x0,0x3E,0x0,0x30,0xC,0x36,0x28,0x0,0x2D]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#Y Z all all
    i2c.write(0x70, bytearray([0x0,0x0,0x15,0x9,0xC,0x3f,0x3f,0x3f,0x3f]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#a b c d
    i2c.write(0x70, bytearray([0x0,0x58,0x10,0x78,0x20,0xD8,0x0,0x8E,0x8]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#e f g h
    i2c.write(0x70, bytearray([0x0,0x58,0x8,0x71,0x0,0x8E,0x4,0x70,0x10]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#i j k l
    i2c.write(0x70, bytearray([0x0,0x0,0x10,0xE,0x0,0x0,0x36,0x30,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#m n o p
    i2c.write(0x70, bytearray([0x0,0xD4,0x10,0x50,0x10,0xDC,0x0,0x70,0x1]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#q r s t
    i2c.write(0x70, bytearray([0x0,0x86,0x4,0x50,0x0,0x88,0x20,0x78,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#u v w x
    i2c.write(0x70, bytearray([0x0,0x1C,0x0,0x4,0x20,0x14,0x28,0xC0,0x28]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)
#y z
    i2c.write(0x70, bytearray([0x0,0xC,0x20,0x48,0x8,0x0,0x0,0x0,0x0]))
    i2c.write(0x70, bytearray([0x81])) # display on
    sleep (1000)

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

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


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

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

# make a spinney-round thing!
    for iterations in range(20):
        i2c.write(0x70, bytearray([0x0,0x0,0x12,0x0,0x12,0x0,0x12,0x0,0x12]))
        i2c.write(0x70, bytearray([0x81])) # display on
        sleep (21)
   
        i2c.write(0x70, bytearray([0x0,0x0,0xc,0x0,0xc,0x0,0xc,0x0,0xc]))
        i2c.write(0x70, bytearray([0x81])) # display on
        sleep (20)
   
        i2c.write(0x70, bytearray([0x0,0xc0,0x0,0xc0,0x0,0xc0,0x0,0xc0,0x0]))
        i2c.write(0x70, bytearray([0x81])) # display on
        sleep (20)
   
        i2c.write(0x70, bytearray([0x0,0x0,0x21,0x0,0x21,0x0,0x21,0x0,0x21]))
        i2c.write(0x70, bytearray([0x81])) # display on
        sleep (20)

# turn off the display
    i2c.write(0x70, bytearray([0x80]))
    sleep(1000)
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章