树莓派入门之4位数码管

数码管长这个样子:

数码管分为静态数码管和动态数码管,如果是静态数码管的话针脚数量不敢想象,我这个是动态的数码管,所以只有12个针脚。

可能大家还不是很懂,可是解释啥的感觉自己也不太会,就放上一个链接供大家参考:

https://www.cnblogs.com/badnewfish/p/5908423.html

然后放上驱动的代码(显示当前时间):

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
# encoding: utf-8

import RPi.GPIO
import time

# abcdefg 从上边开始顺时针编号, dp 表示小数点 dotpoint

# 定义单个数码管各段led对应的GPIO口
LED_A = 26
LED_B = 19
LED_C = 13
LED_D = 6
LED_E = 5
LED_F = 11
LED_G = 9
LED_DP = 10

# 定义1到4号数码管阳极对应的GPIO口
DIGIT1 = 12
DIGIT2 = 16
DIGIT3 = 20
DIGIT4 = 21

# 定义按钮输入的GPIO口
# btn = 27

RPi.GPIO.setmode(RPi.GPIO.BCM)

RPi.GPIO.setup(LED_A, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_B, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_C, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_D, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_E, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_F, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_G, RPi.GPIO.OUT)
RPi.GPIO.setup(LED_DP, RPi.GPIO.OUT)
RPi.GPIO.setup(DIGIT1, RPi.GPIO.OUT)
RPi.GPIO.setup(DIGIT2, RPi.GPIO.OUT)
RPi.GPIO.setup(DIGIT3, RPi.GPIO.OUT)
RPi.GPIO.setup(DIGIT4, RPi.GPIO.OUT)

RPi.GPIO.output(DIGIT1, True)
RPi.GPIO.output(DIGIT2, True)
RPi.GPIO.output(DIGIT3, True)
RPi.GPIO.output(DIGIT4, True)

# RPi.GPIO.setup(btn, RPi.GPIO.IN, pull_up_down=RPi.GPIO.PUD_UP)

# 指定no(1-4)号数码管显示数字num(0-9),第三个参数是显示不显示小数点(true/false)
def showDigit(no, num, showDotPoint):
# 先将正极拉低,关掉显示
RPi.GPIO.output(DIGIT1, False)
RPi.GPIO.output(DIGIT2, False)
RPi.GPIO.output(DIGIT3, False)
RPi.GPIO.output(DIGIT4, False)

if (num == 0) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, False)
RPi.GPIO.output(LED_F, False)
RPi.GPIO.output(LED_G, True)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 1) :
RPi.GPIO.output(LED_A, True)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, True)
RPi.GPIO.output(LED_E, True)
RPi.GPIO.output(LED_F, True)
RPi.GPIO.output(LED_G, True)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 2) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, True)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, False)
RPi.GPIO.output(LED_F, True)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 3) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, True)
RPi.GPIO.output(LED_F, True)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 4) :
RPi.GPIO.output(LED_A, True)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, True)
RPi.GPIO.output(LED_E, True)
RPi.GPIO.output(LED_F, False)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 5) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, True)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, True)
RPi.GPIO.output(LED_F, False)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 6) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, True)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, False)
RPi.GPIO.output(LED_F, False)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 7) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, True)
RPi.GPIO.output(LED_E, True)
RPi.GPIO.output(LED_F, True)
RPi.GPIO.output(LED_G, True)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 8) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, False)
RPi.GPIO.output(LED_F, False)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)
elif (num == 9) :
RPi.GPIO.output(LED_A, False)
RPi.GPIO.output(LED_B, False)
RPi.GPIO.output(LED_C, False)
RPi.GPIO.output(LED_D, False)
RPi.GPIO.output(LED_E, True)
RPi.GPIO.output(LED_F, False)
RPi.GPIO.output(LED_G, False)
RPi.GPIO.output(LED_DP, not showDotPoint)

if (no == 1) :
RPi.GPIO.output(DIGIT1, True)
elif (no == 2) :
RPi.GPIO.output(DIGIT2, True)
elif (no == 3) :
RPi.GPIO.output(DIGIT3, True)
elif (no == 4) :
RPi.GPIO.output(DIGIT4, True)

try:
t=0.005
i = 0
while True:
# 按钮按下时显示日期,否则显示时间
# 为了区别左右的数字,让第二个数码管的小数点显示出来
#(本来应该是一个冒号,我们这个数码管没有,就用小数点代替了
currentTime = time.localtime(time.time())
currentH = int(time.strftime("%H", currentTime))
currentM = int(time.strftime("%M", currentTime))

time.sleep(t)
showDigit(1, int(currentH / 10), False)
time.sleep(t)
showDigit(2, int(currentH % 10), True)
time.sleep(t)
showDigit(3, int(currentM / 10), False)
time.sleep(t)
showDigit(4, int(currentM % 10), False)
except KeyboardInterrupt:
pass

# 最后清理GPIO口(不做也可以,建议每次程序结束时清理一下,好习惯)
RPi.GPIO.cleanup()

这里着重要说的就是接线,接线的话上面代码里定义的GPIO编号不要改,接线时刚好能连在一块。

首先拿出12根线,连到数码管上,连的时候注意四根控制四个数字发光的线的颜色最好类似,方便我们识别,我这里用的是白色的线。

连上之后就要参照代码中定义的A对应哪条线,这条线又对应哪个GPIO引脚这样的思路去接线,接完之后就可以运行程序了。

然后就是见证奇迹的时刻:

Godbobo wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!