-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path11_fourBitSegment.py
executable file
·134 lines (119 loc) · 3.58 KB
/
11_fourBitSegment.py
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
#!/usr/bin/env python
#-----------------------------------------------------------
# File name : 11_fourBitSegment.py
# Description : display 1,2,3,4....
# Author : Jason
# E-mail : [email protected]
# Website : www.adeept.com
# Date : 2015/06/12
#-----------------------------------------------------------
import RPi.GPIO as GPIO
import time
BIT0 = 3
BIT1 = 5
BIT2 = 24
BIT3 = 26
segCode = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f] #0~9
pins = [11,12,13,15,16,18,22,7,3,5,24,26]
bits = [BIT0, BIT1, BIT2, BIT3]
def print_msg():
print 'Program is running...'
print 'Please press Ctrl+C to end the program...'
def digitalWriteByte(val):
GPIO.output(11, val & (0x01 << 0))
GPIO.output(12, val & (0x01 << 1))
GPIO.output(13, val & (0x01 << 2))
GPIO.output(15, val & (0x01 << 3))
GPIO.output(16, val & (0x01 << 4))
GPIO.output(18, val & (0x01 << 5))
GPIO.output(22, val & (0x01 << 6))
GPIO.output(7, val & (0x01 << 7))
def display_1():
GPIO.output(BIT0, GPIO.LOW)
for i in range(10):
digitalWriteByte(segCode[i])
time.sleep(0.5)
def display_2():
for bit in bits:
GPIO.output(bit, GPIO.LOW)
for i in range(10):
digitalWriteByte(segCode[i])
time.sleep(0.5)
def display_3(num):
b0 = num % 10
b1 = num % 100 / 10
b2 = num % 1000 / 100
b3 = num / 1000
if num < 10:
GPIO.output(BIT0, GPIO.LOW)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.HIGH)
GPIO.output(BIT3, GPIO.HIGH)
digitalWriteByte(segCode[b0])
elif num >= 10 and num < 100:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
elif num >= 100 and num < 1000:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.LOW)
digitalWriteByte(segCode[b2])
time.sleep(0.002)
GPIO.output(BIT2, GPIO.HIGH)
elif num >= 1000 and num < 10000:
GPIO.output(BIT0, GPIO.LOW)
digitalWriteByte(segCode[b0])
time.sleep(0.002)
GPIO.output(BIT0, GPIO.HIGH)
GPIO.output(BIT1, GPIO.LOW)
digitalWriteByte(segCode[b1])
time.sleep(0.002)
GPIO.output(BIT1, GPIO.HIGH)
GPIO.output(BIT2, GPIO.LOW)
digitalWriteByte(segCode[b2])
time.sleep(0.002)
GPIO.output(BIT2, GPIO.HIGH)
GPIO.output(BIT3, GPIO.LOW)
digitalWriteByte(segCode[b3])
time.sleep(0.002)
GPIO.output(BIT3, GPIO.HIGH)
else:
print 'Out of range, num should be 0~9999 !'
def setup():
GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location
for pin in pins:
GPIO.setup(pin, GPIO.OUT) #set all pins' mode is output
GPIO.output(pin, GPIO.HIGH) #set all pins are high level(3.3V)
def loop():
while True:
print_msg()
display_1()
time.sleep(1)
display_2()
time.sleep(1)
tmp = int(raw_input('Please input a num(0~9999):'))
for i in range(500):
display_3(tmp)
time.sleep(1)
def destroy(): #When program ending, the function is executed.
for pin in pins:
GPIO.output(pin, GPIO.LOW) #set all pins are low level(0V)
GPIO.setup(pin, GPIO.IN) #set all pins' mode is input
if __name__ == '__main__': #Program starting from here
setup()
try:
loop()
except KeyboardInterrupt:
destroy()