Chapter 18 74HC595 & 7-segment display.
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
GPIO.output(latchPin,GPIO.HIGH)
time.sleep(0.5)
for i in range(0,len(num)):
GPIO.output(latchPin,GPIO.LOW)
shiftOut(dataPin,clockPin,MSBFIRST,num[i]&0x7f)#Use "&0x7f"to display the
decimal point.
GPIO.output(latchPin,GPIO.HIGH)
time.sleep(0.5)
def destroy(): # When 'Ctrl+C' is pressed, the function is executed.
GPIO. cleanup()
if __name__ == '__main__': # Program starting from here
p rint ('Program is starting...' )
setup()
try:
loop()
e xcept KeyboardInterrupt:
destroy()
First, put encoding of “0”-“F” into the array.
num = [0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e]
In the “for” cycle of loop() function, use 74HC595 to output contents of array “num” successively.
SevenSegmentDisplay can correctly display the corresponding characters. Pay attention to that in shiftOut
function, the transmission bit, flag bit highest bit will be transmitted preferentially.
for i in range(0,len(num)):
GPIO.output(latchPin,GPIO.LOW)
shiftOut(dataPin,clockPin,MSBFIRST,num[i])#Output the figures and the highest
level is transfered preferentially.
GPIO.output(latchPin,GPIO.HIGH)
time.sleep(0.5)
If you want to display the decimal point, make the highest bit of each array become 0, which can be
implemented easily by num[i]&0x7f.
shiftOut(dataPin,clockPin,MSBFIRST,num[i]&0x7f)# Use “&0x7f”to display the decimal
point.