esp32驱动SSD1306的oled显示汉字(micropython)
1.完整源码:
主函数oled.py
from ssd1306 import SSD1306_SPI from ssd1306 import SSD1306 import font import machine def draw_chinese(lcd,ch_str,x_axis,y_axis): offset_=0 y_axis=y_axis*16#中文高度一行占8个 x_axis=(x_axis*16)#中文宽度占16个 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=font.byte2[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): lcd.pixel(x_axis+x-offset_,y+y_axis,int(a_[x]))#文字的上半部分 lcd.pixel(x_axis+x+8-offset_,y+y_axis,int(b_[x]))#文字的下半部分 offset_+=16 WIDTH = const(128) HEIGHT = const (64) pdc = machine.Pin(27, machine.Pin.OUT) pcs = machine.Pin(26, machine.Pin.OUT) prst = machine.Pin(18, machine.Pin.OUT) spi = machine.SPI(baudrate=100000, polarity=1, phase=0, sck=machine.Pin(0), mosi=machine.Pin(2), miso=machine.Pin(4)) spi.init(baudrate=200000) # set the baudrate ssd = SSD1306_SPI(WIDTH, HEIGHT, spi, pdc, prst, pcs) draw_chinese(ssd,'我',0,0) draw_chinese(ssd,'我',1,1) draw_chinese(ssd,'我',2,2) draw_chinese(ssd,'我',3,3) ssd.show()
字体包font.py
byte2={ 0xE997B9: [0x20,0x17,0x00,0x42,0x41,0x5F,0x41,0x41,0x4F,0x49,0x49,0x49,0x49,0x41,0x41,0x40, 0x00,0xFC,0x04,0x04,0x04,0xF4,0x04,0x04,0xE4,0x24,0x24,0xA4,0x44,0x04,0x14,0x08],#闹 0xe68891: [0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10, 0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02],#我 0xE9929F: [0x10,0x10,0x3C,0x20,0x41,0xBD,0x11,0x11,0xFD,0x11,0x11,0x10,0x14,0x18,0x10,0x00, 0x20,0x20,0x20,0x20,0xFC,0x24,0x24,0x24,0x24,0xFC,0x24,0x20,0x20,0x20,0x20,0x20],#钟 0xE697B6: [0x00,0x00,0x7C,0x44,0x45,0x44,0x44,0x7C,0x44,0x44,0x44,0x44,0x7C,0x44,0x00,0x00, 0x08,0x08,0x08,0x08,0xFE,0x08,0x08,0x08,0x88,0x48,0x48,0x08,0x08,0x08,0x28,0x10] }
2. esp硬件spi接线
oled esp32
GND GND
VCC VCC
D0 P0(SCK)
D1 P2(MOSI)
RES P18(RST)
DC P27(DC)
CS P26(CS)
3. 汉字取模
使用工具PCtoLCD2002完美版,参数设置如下图:
生成字节数组:
0x04,0x0E,0x78,0x08,0x08,0xFF,0x08,0x08,0x0A,0x0C,0x18,0x68,0x08,0x08,0x2B,0x10,
0x40,0x50,0x48,0x48,0x40,0xFE,0x40,0x44,0x44,0x48,0x30,0x22,0x52,0x8A,0x06,0x02
4.显示效果