Chapter 17 74HC595 & LEDBar Graph
186
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
MSBFIRST
=
2
#define the pins connect to 74HC595
dataPin
=
11
#DS Pin of 74HC595(Pin14)
latchPin
=
13
#ST_CP Pin of 74HC595(Pin12)
clockPin
=
15
#SH_CP Pin of 74HC595(Pin11)
d e f
setup
( ):
GPIO
.
setmode
(
GPIO
.
BOARD
)
# Number GPIOs by its physical location
GPIO
.
setup
(
dataPin
,
GPIO
.
OUT
)
GPIO
.
setup
(
latchPin
,
GPIO
.
OUT
)
GPIO
.
setup
(
clockPin
,
GPIO
.
OUT
)
# shiftOut function, use bit serial transmission.
d e f
shiftOut
(
dPin
,
cPin
,
order
,
val
):
f o r
i
i n
range
(
0
,
8
):
GPIO
.
output
(
cPin
,
GPIO
.
LOW
);
i f
(
order
= =
LSBFIRST
):
GPIO
.
output
(
dPin
,(
0x01
&(
val
>>
i
)==
0x01
)
a n d
GPIO
.
HIGH
o r
GPIO
.
LOW
)
e l if
(
order
= =
MSBFIRST
):
GPIO
.
output
(
dPin
,(
0x80
&(
val
<<
i
)==
0x80
)
a n d
GPIO
.
HIGH
o r
GPIO
.
LOW
)
GPIO
.
output
(
cPin
,
GPIO
.
HIGH
);
d e f
loop
( ):
w h ile
T r ue
:
x
=
0x01
f o r
i
i n
range
(
0
,
8
):
GPIO
.
output
(
latchPin
,
GPIO
.
LOW
)
#Output low level to latchPin
shiftOut
(
dataPin
,
clockPin
,
LSBFIRST
,
x
)
#Send serial data to 74HC595
GPIO
.
output
(
latchPin
,
GPIO
.
HIGH
)
#Output high level to latchPin, and 74HC595
will update the data to the parallel output port.
x
< < =
1
# make the variable move one bit to left once, then the bright LED move
one step to the left once.
time
.
sleep
(
0.1
)
x
=
0x80
f o r
i
i n
range
(
0
,
8
):
GPIO
.
output
(
latchPin
,
GPIO
.
LOW
)
shiftOut
(
dataPin
,
clockPin
,
LSBFIRST
,
x
)
GPIO
.
output
(
latchPin
,
GPIO
.
HIGH
)
x
> > =
1
time
.
sleep
(
0.1
)
d e f
destroy
():
# When 'Ctrl+C' is pressed, the function is executed.
GPIO
.
cleanup
()
i f
__name__
= =
'__main__'
:
# Program starting from here
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...