Chapter 18 74HC595 & 7-segment display.
outData(0xff)
selectDigit(0x02) #Select the second, and display the tens digit
outData(num[dec%100/10])
time. sleep(0.003)
outData(0xff)
selectDigit(0x04) #Select the third, and display the hundreds digit
outData(num[dec%1000/100])
time. sleep(0.003)
outData(0xff)
selectDigit(0x08) #Select the fourth, and display the thousands digit
outData(num[dec%10000/1000])
time. sleep(0.003)
Subfunction timer () is the timer callback function. When the time is up, this function will be executed.
Accompanied by the execution, the variable counter will be added 1, and then reset the time of timer to 1s.
1s later, the function will be executed again.
def timer(): #timer function
g lobal counter
g lobal t
t = threading.Timer(1.0,timer) #reset time of timer to 1s
t. start() #Start timing
counter+=1
p rint ("counter : %d"%counter)
Subfunction setup(), configure all input output modes for the GPIO pin used.
Finally, in loop function, make the digital tube display variable counter value in the while cycle. The value will
change in function timer (), so the content displayed by 7-segment display will change accordingly.
def loop():
g lobal t
g lobal counter
t = threading.Timer(1.0,timer) # set the timer
t. start() #Start timing
w hile Tr ue:
display(counter) #display the number counter
After the program is executed, press "Ctrl+C", then subfunction destroy() will be executed, and GPIO resources
and timers will be released in this subfunction.
def destroy(): # When 'Ctrl+C' is pressed, the function is executed.
g lobal t
GPIO. cleanup()
t. cancel() # cancel the timer