树莓派 Pico + LCD1602 显示个性化字符程序运行时没有报错,但显示屏没有显示任何字符,这是为什么?

PZX_moyinglong 2025-02-097 次点击
以下样例程序本身使用的事Pico ,而我使用的是Pico W!!
运行之后,shell区显示:
Running test_main
Initializing I2C and LCD...
I2C and LCD initialized successfully.
Backlight turned on.
Turning cursor on
Turning cursor off
Blinking cursor on
Blinking cursor off
但是屏幕常亮并且不显示任何字符!!
劳烦Support!
---------------------------运行程序如下---------------------------
import utime
import machine
from machine import I2C
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd

I2C_ADDR = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20

def setup_lcd():
"""
初始化 I2C 和 LCD 模块
:return: 初始化好的 LCD 对象
"""
try:
print("Initializing I2C and LCD...")
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
print("I2C and LCD initialized successfully.")
lcd.backlight_on()
print("Backlight turned on.")
return lcd
except Exception as e:
print(f"Error initializing I2C or LCD: {‌‌‌e}")
return None

def display_time(lcd):
"""
在 LCD 上显示当前时间
:param lcd: LCD 对象
"""
time = utime.localtime()
# 使用 f-string 进行更简洁的字符串格式化
time_str = f"{‌‌‌time[0]:04d}/{‌‌‌time[1]:02d}/{‌‌‌time[2]:02d} {‌‌‌time[3]:02d}:{‌‌‌time[4]:02d}:{‌‌‌time[5]:02d}"
lcd.clear()
lcd.putstr(time_str)

def control_cursor(lcd, count):
"""
根据计数器的值控制 LCD 光标的显示状态
:param lcd: LCD 对象
:param count: 计数器的值
"""
if count % 10 == 0:
print("Turning cursor on")
lcd.show_cursor()
elif count % 10 == 1:
print("Turning cursor off")
lcd.hide_cursor()
elif count % 10 == 2:
print("Blinking cursor on")
lcd.blink_cursor_on()
elif count % 10 == 3:
print("Blinking cursor off")
lcd.blink_cursor_off()

def test_main():
print("Running test_main")
lcd = setup_lcd()
if lcd is None:
return
# 显示初始信息
lcd.putstr("It works!")
utime.sleep(2) # 修正:这里应该是秒数,而不是一个非常大的数字

count = 0
try:
while True:
display_time(lcd)
control_cursor(lcd, count)
utime.sleep(1)
count += 1
except KeyboardInterrupt:
print("Program interrupted by user.")
except Exception as e:
print(f"An unexpected error occurred: {‌‌‌e}")
finally:
if lcd:
lcd.clear()
lcd.backlight_off() # 关闭背光
print("Backlight turned off. LCD cleared.")

if __name__ == "__main__":
test_main()
收藏 ♥ 感谢
Spoony 小组长 8 小时前 
这个 LCD 模块背后有一个电阻用来调节对比度,用个螺丝刀调节一下就可以看清。

登录注册 后可回复。

这里由以下店铺的技术人员在此免费提供能力范围内的技术支持。

NXEZ 创客商店(淘宝)

排障问题发帖注意事项:
1、请尽可能将故障说明清晰,列明例如使用环境和做了哪些操作和配置等。
2、排障中已经做了哪些尝试,如果能在问题中进行描述将极大提高解决问题的效率。
3、对于需要自身完成知识积累才能解决的问题,请自行通过搜索引擎寻找资料学习。