Chapter 20 LCD1602
228
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
49
d e f
loop
( ):
mcp
.
output
(
3
,
1
)
# turn on LCD backlight
lcd
.
begin
(
16
,
2
)
# set number of LCD lines and columns
w h ile
(
True
):
#lcd.clear()
lcd
.
setCursor
(
0
,
0
)
# set cursor position
lcd
.
message
(
'CPU: '
+
get_cpu_temp
()+
'\n'
)
# display CPU temperature
lcd
.
message
(
get_time_now
()
)
# display the time
sleep
(
1
)
d e f
destroy
():
lcd
.
clear
()
PCF8574_address
=
0x27
# I2C address of the PCF8574 chip.
PCF8574A_address
=
0x3F
# I2C address of the PCF8574A chip.
# Create PCF8574 GPIO adapter.
t r y
:
mcp
=
PCF8574_GPIO
(
PCF8574_address
)
e x cept
:
t r y
:
mcp
=
PCF8574_GPIO
(
PCF8574A_address
)
e x cept
:
p r int
(
'I2C Address Error !'
)
exit
(
1
)
# Create LCD, passing in MCP GPIO adapter.
lcd
=
Adafruit_CharLCD
(
pin_rs
=
0
,
pin_e
=
2
,
pins_db
=[
4
,
5
,
6
,
7
],
GPIO
=
mcp
)
i f
__name__
= =
'__main__'
:
p r int
(
'Program is starting ... '
)
t r y
:
loop
( )
e x cept
KeyboardInterrupt
:
destroy
()
Two modules are used in the code, PCF8574.py and Adafruit_LCD1602.py. These two documents and the
code file are stored in the same directory, and neither of them is dispensable. Please do not delete. PCF8574.py
is used to provide I2C communication mode and operation method of some port for RPi and PCF8574 chip.
Adafruit module Adafruit_LCD1602.py is used to provide some function operation method for LCD1602.
In the code, first get the object used to operate PCF8574 port, then get the object used to operate LCD1602.
address
=
0x27
# I2C address of the PCF8574 chip.
# Create PCF8574 GPIO adapter.
mcp
=
PCF8574_GPIO
(
address
)
# Create LCD, passing in MCP GPIO adapter.
lcd
=
Adafruit_CharLCD
(
pin_rs
=
0
,
pin_e
=
2
,
pins_db
=[
4
,
5
,
6
,
7
],
GPIO
=
mcp
)
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 ...