Code
In this code, we use 74HC595 to control 4-Digit 7-segment display, and use dynamic scanning way to show
the changing numbers.
C Code 18.2.1 StopWatch
First observe the project result, and then analyze the code.
1. Use cd command to enter 16.1.1_SteppingMotor directory of C code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/C_Code/18.2.1_StopWatch
2. Use following command to compile "StopWatch.c" and generate executable file "StopWatch".
gcc StopWatch.c -o StopWatch -lwiringPi
3. Run the generated file "SteppingMotor".
sudo ./StopWatch
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:
#include <wiringPi.h>
#include <stdio.h>
#include <wiringShift.h>
#include <signal.h>
#include <unistd.h>
#define dataPin 5 //DS Pin of 74HC595(Pin14)
#define latchPin 4 //ST_CP Pin of 74HC595(Pin12)
#define clockPin 1 //CH_CP Pin of 74HC595(Pin11)
const int digitPin[]={0,2,3,12}; // Define 7-segment display common pin
// character 0-9 code of common anode 7-segment display
unsigned char num[ ] ={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int counter = 0; //variable counter,the number will be displayed by 7-segment display
//Open one of the 7-segment display and close the remaining three, the parameter digit is
optional for 1,2,4,8
void selectDigit(int digit){
digitalWrite(digitPin[0],((digit&0x08) == 0x08) ? LOW : HIGH);
digitalWrite(digitPin[1],((digit&0x04) == 0x04) ? LOW : HIGH);
digitalWrite(digitPin[2],((digit&0x02) == 0x02) ? LOW : HIGH);
digitalWrite(digitPin[3],((digit&0x01) == 0x01) ? LOW : HIGH);
}
void _shiftOut(int dPin,int cPin,int order,int val){
int i;
for(i = 0; i < 8; i++ ){
digitalWrite(cPin,LOW);
if(order == LSBFIRST){
digitalWrite(dPin,((0x01&(val>>i)) == 0x01) ? HIGH : LOW);
delayMicroseconds(1);