image.png
Esp8266,刷microPython 1.12版本的固件,测试通过
esp8266_i2c_lcd_test.py
# PCF8574 LCD1602 测试模块 ( 2020.09.25 我开心 )
# 注意:是指一种颜色背光的LCD1602, 不是背光可调多种颜色的那种RGB1602!
# Pin5一般指芯片上的D1针脚、Pin4一般是指D2针脚
from time import sleep_ms, ticks_ms
from machine import I2C, Pin
from esp8266_i2c_lcd import I2cLcd
# The PCF8574 has a jumper selectable address: 0x20 - 0x27
DEFAULT_I2C_ADDR = 0x27
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) # Pin5对应ESP8266芯片的D1针脚、Pin4对应D2针脚
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16) # 指定几行几列
while True:
print("从第几列、第几行开始显示, 最左上角为0,0")
lcd.move_to(0,0)
print("在屏幕上显示内容")
lcd.putstr("1234567890123456")
sleep_ms(2000)
print("在屏幕上显示内容(加\n表示换行)")
lcd.move_to(0,0)
lcd.putstr("LCD1602 Test!\nSecond Line")
sleep_ms(2000)
print("关闭背光")
lcd.backlight_off()
sleep_ms(2000)
print("打开背光")
lcd.backlight_on()
sleep_ms(2000)
print("关闭显示")
lcd.display_off()
sleep_ms(2000)
print("打开显示")
lcd.display_on()
sleep_ms(2000)
print("清除屏幕")
lcd.clear()
sleep_ms(2000)
上面一个代码文件,加下面2个代码文件,共3个,上传到芯片中,文件夹结构如下图所示:
image.png
下面为 lcd_api.py 文件名必须为这个名称,不能修改
import time
class LcdApi:
"""Implements the API for talking with HD44780 compatible character LCDs.
This class only knows what commands to send to the LCD, and not how to get
them to the LCD.
It is expected that a derived class will implement the hal_xxx functions.
"""
# The following constant names were lifted from the avrlib lcd.h
# header file, however, I changed the definitions from bit numbers
# to bit masks.
#
# HD44780 LCD controller command set
LCD_CLR = 0x01 # DB0: clear display
LCD_HOME = 0x02 # DB1: return to home position
LCD_ENTRY_MODE = 0x04 # DB2: set entry mode
LCD_ENTRY_INC = 0x02 # --DB1: increment
LCD_ENTRY