micropython lcd_Esp8266+PCF8574 LCD1602液晶显示屏+MicroPython测试(2020-09-25)

这篇博客介绍了如何利用MicroPython 1.12版本固件在Esp8266上通过I2C与PCF8574连接的LCD1602液晶显示屏进行交互。作者提供了`esp8266_i2c_lcd_test.py`代码示例,展示了如何显示文本、控制背光、切换显示等基本操作。同时,还包含了`lcd_api.py`和`esp8266_i2c_lcd.py`两个辅助类,用于实现与LCD的底层通信。
摘要由CSDN通过智能技术生成

7798d0c06c69

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个,上传到芯片中,文件夹结构如下图所示:

7798d0c06c69

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值