Skip to content

Commit ff8a2b7

Browse files
authored
Add files via upload
0 parents  commit ff8a2b7

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

drive_3631as.py

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# 3631AS-1 共阴 数码管 驱动
2+
# 徐奥雯编写 XUAOWEN-ASSETS E-MAIL:[email protected] WECHAT:US-00000
3+
# 使用前:根据NUM_list和引脚图的1-12号(没有6) 在GPIO_list中填写你连接的对应的GPIO
4+
# 使用方法:将本文件使用import drive_3631as导入 在不改变此文件名的前提下
5+
# 使用drive_3631as.start_3631AS(数字)开始显示 使用drive_3631as.stop_3631AS()结束显示 仅支持三位以内的int型或浮点型数据
6+
7+
8+
import RPi.GPIO as GPIO
9+
import time
10+
from multiprocessing import Process
11+
12+
# 根据NUM_list和引脚图的1-12号(没有6) 在GPIO_list中填写你连接的对应的GPIO
13+
NUM_list = [1, 2, 3, 4, 5, None, 7, 8 ,9, 10, 11, 12] # 注意没有第6个 8,9,12 为共阴
14+
GPIO_list = [5, 6, 13, 19, 26, None, 18, 16, 20, 23, 24, 21] #填写你的GPIO 跳过None
15+
16+
# __11_ |
17+
# | | |
18+
# 10 | | 7 | 共三个数字 每个数字8个灯 (包括一个点)
19+
# |__5__| | 三个数字的GND阴级分别为8, 9, 12
20+
# | | | 某个灯要亮 必须其阳极为高电平且阴级为低电平 如果阳极与印记都为高点电平则不亮
21+
# 1 | | 4 |
22+
# |__2__| |
23+
# *3 |
24+
25+
# 数码管的显示分为静态和动态两种。静态就是一个GPIO控制一个LED小灯管。
26+
# 但是随着控制数码管数量的增加,GPIO口就占用太多了,所以多个数码管可以有 共阴 和 共阳 两种共享引脚。
27+
# 这个时候如果采用静态点亮数码管的方式,共享引脚的数码管显示完全一样。
28+
# 所谓动态方式,就是通过GPIO选择引脚,选择要点亮的某个数码管,然后通过共享引脚点亮LED小灯管。
29+
# 然后快速切换点亮其他数码管,由于点亮的切换频率非常快所以感觉上数码管一直亮着。
30+
31+
GPIO.setmode(GPIO.BCM) # 设置使用BCM引脚编号模式
32+
33+
for gpio_num in GPIO_list:
34+
if gpio_num : # gpio_num不为None
35+
GPIO.setup(gpio_num, GPIO.OUT) # 设置出
36+
37+
38+
def num_print_only_one(number, address, point):
39+
'''传入参数(0-9),位置(1-3),是否有小数点(0/1)'''
40+
41+
# 亮灯位置 此时未设置3,6,8,9,10,12 默认为0
42+
# 顺序是1-12 第6位不参与运行 设置为6
43+
number_light_list = [None, '110106100110', '000106100000', '110016100010', '010116100010', '000116100100', '010116000110',
44+
'110116000110', '000106100010', '110116100110', '010116100110']
45+
46+
number_light_coding = number_light_list[number+1] # 获取对应亮灯编码 不含3,6,8,9,10,12
47+
number_light_coding_list = list(number_light_coding) # 转成列表
48+
49+
if address == 3: # 确定第几个数字亮 共三个
50+
number_light_coding_list[8-1] = '0'
51+
number_light_coding_list[9-1] = '1'
52+
number_light_coding_list[12-1] = '1'
53+
elif address == 2:
54+
number_light_coding_list[8-1] = '1'
55+
number_light_coding_list[9-1] = '0'
56+
number_light_coding_list[12-1] = '1'
57+
elif address == 1:
58+
number_light_coding_list[8-1] = '1'
59+
number_light_coding_list[9-1] = '1'
60+
number_light_coding_list[12-1] = '0'
61+
62+
number_light_coding_list[3-1] = str(point) #确定小数点是否亮
63+
64+
for i in range(0, 12): # 循环执行 输出制定亮起的数字
65+
if number_light_coding_list[i] == '1':
66+
GPIO.output(GPIO_list[i], GPIO.HIGH)
67+
elif number_light_coding_list[i] == '0':
68+
GPIO.output(GPIO_list[i], GPIO.LOW)
69+
70+
71+
def number_legal_test(number_01):
72+
'''输入合法检测'''
73+
if type(number_01) == type(1): # 检测是否为int型
74+
if len(str(number_01)) > 3: #检测是否位数过多
75+
print('数据过长,单个3631AS-1 最多显示三位数字')
76+
else:
77+
return 'int' # 输入合法 int
78+
elif type(number_01) == type(1.0): # 检测是否为float型
79+
if len(str(number_01)) > 4: #检测是否位数过多 带小数点共四位
80+
print('数据过长,单个3631AS-1 最多显示三位数字')
81+
else:
82+
return 'float' # 输入合法 float
83+
else:
84+
print('数据类型错误,请收入int型或float型数据')
85+
86+
87+
def int_to_three(number_02):
88+
'''将两个或单个整数数字转换为三个'''
89+
str_number_02 = str(number_02)
90+
if len(str_number_02) == 1:
91+
return '00' + str_number_02
92+
elif len(str_number_02) == 2:
93+
return '0' + str_number_02
94+
else:
95+
return str(number_02)
96+
97+
98+
def float_to_three(number_03):
99+
'''将两个浮点数字转换为三个'''
100+
str_number_03 = str(number_03)
101+
if len(str_number_03) == 3:
102+
return '0' + str_number_03
103+
else:
104+
return str(number_03)
105+
106+
107+
def number_3631AS_start(number_three):
108+
'''显示三个数字'''
109+
if number_legal_test(number_three) == 'int':
110+
str_number_three = int_to_three(number_three)
111+
while 1:
112+
for i in range(1, 3+1):
113+
num_print_only_one(int(str_number_three[i-1]),i,0) # 执行一个数字的显示
114+
time.sleep(1/250)
115+
116+
elif number_legal_test(number_three) == 'float':
117+
str_number_three = float_to_three(number_three)
118+
point_index = str_number_three.find('.') # 找到小数点
119+
str_number_three = str_number_three[:point_index] + str_number_three[point_index+1:] # 去除小数点拼接
120+
if point_index == 2-1: # 根据小数点位置
121+
point_list=[1,0,0]
122+
elif point_index == 3-1:
123+
point_list=[0,1,0]
124+
while 1:
125+
for i in range(1, 3+1):
126+
num_print_only_one(int(str_number_three[i-1]),i,point_list[i-1]) # 执行一个数字的显示
127+
time.sleep(1/250)
128+
129+
130+
# 多进程 Liunx中多进程不必写在if __name__ == '__main__':内
131+
def start_3631AS(number):
132+
global p01 # 全局变量
133+
# target=要开启的子进程的函数 name=进程的名字 args=传递参数(必须是元组类型)
134+
p01 = Process(target=number_3631AS_start, name='进程01', args=(number,)) # 实例化一个进程对象
135+
p01.start() # 开启一个子进程
136+
def stop_3631AS():
137+
p01.terminate() # 手动杀死子进程
138+
139+
for gpio_num in GPIO_list:
140+
if gpio_num : # gpio_num不为None
141+
GPIO.output(gpio_num, False) # 关闭所有针脚
142+
143+
144+
if __name__ == '__main__':
145+
start_3631AS(888) # 开始显示 函数
146+
time.sleep(5)
147+
stop_3631AS() # 结束显示 函数
148+
149+
# xuaowen XUAOWEN 徐奥雯
150+
151+
152+
153+
154+
155+

0 commit comments

Comments
 (0)