所需模块micropython-ssd1306模块
中文下载站:https://www.cnpython.com/pypi/micropython-ssd1306/download
官方下载站:https://pypi.org/project/micropython-ssd1306/
汉字取模说明
- 取模工具:
pctolcd2002
- 取模方式:
- UTF-8字符编码转换网站:
http://www.mytju.com/classcode/tools/encode_utf8.asp
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(1,scl=Pin(18), sda=Pin(23),freq=100000)
OLED= SSD1306_I2C(128, 64, i2c, addr =0x3c)
#中文汉字.0xE6B8A9 0xE5BAA6 0xE6B9BF 是 UTF-8字符编码转16进制
# 16X16点阵取模,阴码,行列式,顺向;
fonts= {
0xE6B8A9:
[0x00,0x23,0x12,0x12,0x83,0x42,0x42,0x13,0x10,0x27,0xE4,0x24,0x24,0x24,0x2F,0x00,
0x00,0xF8,0x08,0x08,0xF8,0x08,0x08,0xF8,0x00,0xFC,0xA4,0xA4,0xA4,0xA4,0xFE,0x00], #/*"温",0*/
0xE5BAA6:
[0x01,0x00,0x3F,0x22,0x22,0x3F,0x22,0x22,0x23,0x20,0x2F,0x24,0x42,0x41,0x86,0x38,
0x00,0x80,0xFE,0x20,0x20,0xFC,0x20,0x20,0xE0,0x00,0xF0,0x10,0x20,0xC0,0x30,0x0E], #/*"度",1*/
0xE6B9BF:
[0x00,0x27,0x14,0x14,0x87,0x44,0x44,0x17,0x11,0x21,0xE9,0x25,0x23,0x21,0x2F,0x00,
0x00,0xF8,0x08,0x08,0xF8,0x08,0x08,0xF8,0x20,0x20,0x24,0x28,0x30,0x20,0xFE,0x00], #/*"湿",0*/
}
#汉字显示遍历
def chinese(ch_str, x_axis, y_axis):
offset_ = 0
for k in ch_str:
code = 0x00 # 将中文转成16进制编码
data_code = k.encode("utf-8")
code |= data_code[0] << 16
code |= data_code[1] << 8
code |= data_code[2]
byte_data = fonts[code]
for y in range(0, 16):
a_ = bin(byte_data[y]).replace('0b', '')
while len(a_) < 8:
a_ = '0'+ a_
b_ = bin(byte_data[y+16]).replace('0b', '')
while len(b_) < 8:
b_ = '0'+ b_
for x in range(0, 8):
OLED.pixel(x_axis + offset_ + x, y+y_axis, int(a_[x]))
OLED.pixel(x_axis + offset_ + x +8, y+y_axis, int(b_[x]))
offset_ += 16
chinese('温度',20,0)
chinese('湿度',20,20)
#OLED.text('Perseverance',15,20)
#OLED.text('Hello world',10,36)
OLED.show()