199
Chapter 18 74HC595 & 7-segment display.
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:
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
#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
;
f o r
(
i
=
0
;
i
<
8
;
i
+ + ){
digitalWrite
(
cPin
,
LOW
);
i f
(
order
= =
LSBFIRST
){
digitalWrite
(
dPin
,((
0x01
&(
val
>>
i
))
= =
0x01
)
?
HIGH
:
LOW
) ;
delayMicroseconds
(
1
);
Содержание Ultimate Starter Kit
Страница 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Страница 116: ...Chapter 9 Potentiometer RGBLED 116 www freenove com support freenove com Circuit Schematic diagram ...
Страница 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Страница 136: ...Chapter 12 Joystick 136 www freenove com support freenove com Circuit Schematic diagram Hardware connection ...
Страница 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Страница 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Страница 182: ...Chapter 17 74HC595 LEDBar Graph 182 www freenove com support freenove com Circuit Schematic diagram Hardware connection ...
Страница 197: ...197 Chapter 18 74HC595 7 segment display www freenove com support freenove com Circuit Schematic diagram ...
Страница 198: ...Chapter 18 74HC595 7 segment display 198 www freenove com support freenove com Hardware connection ...
Страница 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Страница 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...
Страница 270: ...Chapter 26 WebIOPi IOT 270 www freenove com support freenove com Circuit Schematic diagram Hardware connection ...