让 LCD 显示屏显示个性化的字符,有点类似于创作一套 emoji 那样有趣。下面我们介绍实现的方法。
用到的东西
– 树莓派 Pico
– LCD1602 显示屏(带 I2C 模块)
– 面包板
– 跳线若干
接线
如图所示接线。
SCL-Pin 2 (GP1)
SDA-Pin 1(GPO)
VCC-Pin 40(VBUS)
GND-Pin 38 (GND)
编程
接线完成下面开始编程。
首先确保你的 Pico 已经安装了 MicroPython 固件。
1、下载 Thonny 编程工具。
2、打开 Thonny 在 Tools> Options> Interpreter 选择 Raspberry Pi Pico 为解释器。
下载 LCD1602 的驱动和库。
https://github.com/T-622/RPI-PICO-I2C-LCD
运行代码之前,需要确认 LCD 1602 的 I2C 地址。下载下面的代码确认 I2C 地址:
https://github.com/gigafide/pico_LCD_16x2
保存为文件之后运行,电脑上会显示一个十进制的数字。需要再通过十进制转十六进制的工具将数字转换成 I2C 地址。
实现个性化字符
使用下面的代码可以显示字符。
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 i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000) lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS) lcd.clear(_) lcd.move_to(0,5) lcd.putstr("Hello") lcd.move_to(0,5) lcd.putstr("World!")
你只需要修改最后四行,就可以让它显示个性化字符。个性化字符的代码可以通过这个工具创建。然后用 Python 的方法定义即可。
# smiley faces happy = bytearray([0x00,0x0A,0x00,0x04,0x00,0x11,0x0E,0x00]) sad = bytearray([0x00,0x0A,0x00,0x04,0x00,0x0E,0x11,0x00]) grin = bytearray([0x00,0x00,0x0A,0x00,0x1F,0x11,0x0E,0x00]) shock = bytearray([0x0A,0x00,0x04,0x00,0x0E,0x11,0x11,0x0E]) meh = bytearray([0x00,0x0A,0x00,0x04,0x00,0x1F,0x00,0x00]) angry = bytearray([0x11,0x0A,0x11,0x04,0x00,0x0E,0x11,0x00]) tongue = bytearray([0x00,0x0A,0x00,0x04,0x00,0x1F,0x05,0x02]) # icons bell = bytearray([0x04,0x0e,0x0e,0x0e,0x1f,0x00,0x04,0x00]) note = bytearray([0x02,0x03,0x02,0x0e,0x1e,0x0c,0x00,0x00]) clock = bytearray([0x00,0x0e,0x15,0x17,0x11,0x0e,0x00,0x00]) heart = bytearray([0x00,0x0a,0x1f,0x1f,0x0e,0x04,0x00,0x00]) duck = bytearray([0x00,0x0c,0x1d,0x0f,0x0f,0x06,0x00,0x00]) check = bytearray([0x00,0x01,0x03,0x16,0x1c,0x08,0x00,0x00]) cross = bytearray([0x00,0x1b,0x0e,0x04,0x0e,0x1b,0x00,0x00]) retarrow = bytearray([0x01,0x01,0x05,0x09,0x1f,0x08,0x04,0x00]) # battery icons battery0 = bytearray([0x0E,0x1B,0x11,0x11,0x11,0x11,0x11,0x1F])) # 0% Empty battery1 = bytearray([0x0E,0x1B,0x11,0x11,0x11,0x11,0x1F,0x1F])) # 16% battery2 = bytearray([0x0E,0x1B,0x11,0x11,0x11,0x1F,0x1F,0x1F])) # 33% battery3 = bytearray([0x0E,0x1B,0x11,0x11,0x1F,0x1F,0x1F,0x1F])) # 50% battery4 = bytearray([0x0E,0x1B,0x11,0x1F,0x1F,0x1F,0x1F,0x1F])) # 66% battery5 = bytearray([0x0E,0x1B,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F])) # 83% battery6 = bytearray([0x0E,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F])) # 100% Full battery7 = bytearray([0x0E,0x1F,0x1B,0x1B,0x1B,0x1F,0x1B,0x1F])) # ! Error lcd.custom_char(0, happy); lcd.putchar(chr(0)) #lcd.putchar(b'\x00')
https://maxpromer.github.io/LCD-Character-Creator/
via