Python Code 18.2.1 StopWatch
This code use four step four pat mode to drive the stepping motor forward and reverse direction.
1. Use cd command to enter 16.1.1_SteppingMotor directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/18.2.1_StopWatch
2. Use python command to execute code "StopWatch.py".
python StopWatch.py
After the program is executed, 4-Digit 7-segment start displaying a four-digit number dynamically, and the
will plus 1 in each successive second.
The following is the program code:
im port RPi. GPIO as GPIO
im port time
im port threading
LSBFIRST = 1
MSBFIRST = 2
#define the pins connect to 74HC595
dataPin = 18 #DS Pin of 74HC595(Pin14)
latchPin = 16 #ST_CP Pin of 74HC595(Pin12)
clockPin = 12 #SH_CP Pin of 74HC595(Pin11)
num = (0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90)
digitPin = ( 11, 13,15,19) # Define the pin of 7-segment display common end
counter = 0 # Variable counter, the number will be dislayed by 7-segment display
t = 0 # define the Timer object
def setup():
GPIO. setmode(GPIO.BOARD) # Number GPIOs by its physical location
GPIO. setup(dataPin, GPIO. OUT) # Set pin mode to output
GPIO. setup(latchPin, GPIO. OUT)
GPIO. setup(clockPin, GPIO. OUT)
for pin in digitPin:
GPIO.setup(pin,GPIO.OUT)
def shiftOut(dPin,cPin,order,val):
for i in range(0,8):
GPIO.output(cPin,GPIO.LOW);
if(order == LSBFIRST):
GPIO.output(dPin,(0x01&(val>>i)==0x01) and GPIO.HIGH or GPIO.LOW)
elif(order == MSBFIRST):
GPIO.output(dPin,(0x80&(val<<i)==0x80) and GPIO.HIGH or GPIO.LOW)
GPIO.output(cPin,GPIO.HIGH)
def outData(data): #function used to output data for 74HC595
GPIO. output(latchPin,GPIO.LOW)