Day3_数码管

基础知识

数码管分类

  1. 按照显示数字的个数分为1位,3位,4位。。。
  2. 按连接方式分为共阳极(阳极接到一起形成公共阳极)和共阴极(阴极接到一起形成公共阴极) image

SM410364引脚图,共阴极4位

74位数码管没有公共端,有4个位选线和公共端相连。

  1. 段选线
    image
  2. 位选线1234
    image

模块化

共阴极四位数码管公共类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
共阴极数码管公共类
"""
from machine import Pin
import time

class Seg:
def __init__(self,a,b,c,d,e,f,g,dp,seg1,seg2,seg3,seg4): # 在调用的时候填写段选线和位选线对应的GPIO引脚
# 定义段选线引脚对象
self.a = Pin(a,Pin.OUT)
self.b = Pin(b,Pin.OUT)
self.c = Pin(c,Pin.OUT)
self.d = Pin(d,Pin.OUT)
self.e = Pin(e,Pin.OUT)
self.f = Pin(f,Pin.OUT)
self.g = Pin(g,Pin.OUT)
self.dp = Pin(dp,Pin.OUT)
# 存放所有段选线对象
self.led_list = [self.a,self.b,self.c,self.d,self.e,self.f,self.g,self.dp]
# 定义位选线引脚对象
self.seg1 = Pin(seg1,Pin.OUT)
self.seg2 = Pin(seg2,Pin.OUT)
self.seg3 = Pin(seg3,Pin.OUT)
self.seg4 = Pin(seg4,Pin.OUT)
# 存放所有位选线对象
self.seg_list = [self.seg1,self.seg2,self.seg3,self.seg4]
# 共阴极数码管不同数字对应的电平
self.number_dict = {
#:[a,b,c,d,e,f,g,dp]
0:[1,1,1,1,1,1,0,0],
1:[0,1,1,0,0,0,0,0],
2:[1,1,0,1,1,0,1,0],
3:[1,1,1,1,0,0,1,0],
4:[0,1,1,0,0,1,1,0],
5:[1,0,1,1,0,1,1,0],
6:[1,0,1,1,1,1,1,0],
7:[1,1,1,0,0,0,0,0],
8:[1,1,1,1,1,1,1,0],
9:[1,1,1,1,0,1,1,0],
}

# 清零操作
def clear(self):
# 初始化位选线
for seg in self.seg_list:
seg.on()
# 初始化段选线
for led in self.led_list:
led.off()

# display_number(第几位,显示什么数字)
def display_number(self,order,number):
# 定义当前数字对应的段选线电平列表
logic_list = self.number_dict.get(number)
if logic_list and 0<=order<4:
# 清屏
self.clear()
# 显示位拉低电平
self.seg_list[order].off()
# 显示数字
for i in range(len(logic_list)):
self.led_list[i].value(logic_list[i])

# display_4_number(1234) 四位数字动态扫描显示
def display_4_number(self,number):
if number <=9999:
number_list = []
for i in range(4):
number_list.insert(0, number % 10)
number //= 10
for i in range(4):
self.display_number(i,number_list[i])
time.sleep_ms(5)

if __name__ == "__main__":
seg_object = Seg(a=32,b=25,c=27,d=12,e=13,f=33,g=26,dp=14,seg1=5,seg2=18,seg3=19,seg4=15)

seg_object.display_number(3,8)
while True :
seg_object.display_4_number(1274)


  1. 调用
    image
    1
    2
    3
    4
    5
    from common.seg import Seg

    seg_object = Seg(a=32,b=25,c=27,d=12,e=13,f=33,g=26,dp=14,seg1=5,seg2=18,seg3=19,seg4=15)

    seg_object.display_number(3,8)

实验

循环所有数字和位

  1. 接线
    每个位选线串一个1k的电阻
    image
  2. 代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    import time
    from machine import Pin

    # 定义位选线引脚对象
    seg1 = Pin(5,Pin.OUT)
    seg2 = Pin(18,Pin.OUT)
    seg3 = Pin(19,Pin.OUT)
    seg4 = Pin(15,Pin.OUT)

    # 定义位选线引脚对象的列表
    seg_list = [seg1,seg2,seg3,seg4]

    # 定义段选线引脚对象
    a = Pin(32,Pin.OUT)
    b = Pin(25,Pin.OUT)
    c = Pin(27,Pin.OUT)
    d = Pin(12,Pin.OUT)
    e = Pin(13,Pin.OUT)
    f = Pin(33,Pin.OUT)
    g = Pin(26,Pin.OUT)
    dp = Pin(14,Pin.OUT)

    # 定义短选线引脚对象的列表
    led_list = [a,b,c,d,e,f,g,dp]

    # 共阴极数码管不同数字对应的电平
    number_dict = {
    #:[a,b,c,d,e,f,g,dp]
    0:[1,1,1,1,1,1,0,0],
    1:[0,1,1,0,0,0,0,0],
    2:[1,1,0,1,1,0,1,0],
    3:[1,1,1,1,0,0,1,0],
    4:[0,1,1,0,0,1,1,0],
    5:[1,0,1,1,0,1,1,0],
    6:[1,0,1,1,1,1,1,0],
    7:[1,1,1,0,0,0,0,0],
    8:[1,1,1,1,1,1,1,0],
    9:[1,1,1,1,0,1,1,0],
    }

    # 清零操作
    def clear():
    # 初始化位选线
    for seg in seg_list:
    seg.on()
    # 初始化段选线
    for led in led_list:
    led.off()

    # display_number(第几位,显示什么数字)
    def display_number(order,number):
    # 定义当前数字对应的段选线电平列表
    logic_list = number_dict.get(number)
    if logic_list and 0<=order<4:
    # 清屏
    clear()
    # 显示位拉低电平
    seg_list[order].off()
    # 显示数字
    for i in range(len(logic_list)):
    led_list[i].value(logic_list[i])

    if __name__ == "__main__":
    for i in range(4):
    for j in range(10):
    display_number(i,j)
    time.sleep(0.2)