def loop():
mcp. output(3,1) # turn on LCD backlight
lcd. begin(16,2) # set number of LCD lines and columns
w hile(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)
def 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.
try:
mcp = PCF8574_GPIO(PCF8574_address)
ex cept:
try:
mcp = PCF8574_GPIO(PCF8574A_address)
e xcept:
print ('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)
if __name__ == '__main__':
p rint ('Program is starting ... ')
try:
loop()
e xcept 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)