前言
参考大佬用CH341驱动OLED,链接如下:GitHub - jimjiang2/ch341dll_wrap_typical_app: A ch341dll Wrap is for using in Python 32bits windows to access I2C SPI and MDIO (by GPIO), and Demo with display PC sreen on OLED by i2c or SPI .
本文主要实现了CH341驱动LCD显示汉字。
一、源码
import random
import time
import numpy as np
from matplotlib import pyplot as plt
import os
import random
import numpy as np
from fractions import Fraction
import pyautogui
from PIL import Image
import binascii
import struct
import codecs
import binascii## call ch341dll_wrap
from ch341dll_32bits_wrap.ch341dll_wrap import *###1. init ch341 device
hd = CH341DEV(0)
hd.ch341_i2c_speed(3)
#hd.ch341_close()###2. init oled ----
#原驱动是1306,这里用的是UC1601,函数名没改
hd.spi_oled1306_3w(0,0xe2)
hd.spi_oled1306_3w(0,0x2f) #Set Power Control loading 15-24nF
hd.spi_oled1306_3w(0,0xa6) # Set 0xa6正常 Display;0xa7 反显
hd.spi_oled1306_3w(0,0xc4)
hd.spi_oled1306_3w(0,0xa0)
hd.spi_oled1306_3w(0,0x89)
hd.spi_oled1306_3w(0,0xe8)
hd.spi_oled1306_3w(0,0x81)
hd.spi_oled1306_3w(0,0xdf) #0xaf Set Vbias : 7.99V 0xff
hd.spi_oled1306_3w(0,0xf1)
hd.spi_oled1306_3w(0,0x3f)
hd.spi_oled1306_3w(0,0xf2)
hd.spi_oled1306_3w(0,0x00)
hd.spi_oled1306_3w(0,0xf3)
hd.spi_oled1306_3w(0,0x3f)
hd.spi_oled1306_3w(0,0x85)
hd.spi_oled1306_3w(0,0xaf)
### initial donedef lcd_address(page,column):column=column-1;page=page-1;din = [];b1=0xB0 + page;b2=column&0x0f;b3=((column>>4)&0x0f)+0x10;hd.spi_oled1306_3w(0,b1);hd.spi_oled1306_3w(0,b2);hd.spi_oled1306_3w(0,b3);def lcd_clr():for i in range(9):lcd_address(i+1,1);for j in range(132):hd.spi_oled1306_3w(1,0x00);def display_graphic_16x16(page,column,data):din = [];for ii in range(32):din.append(int(data[ii]))for i in range(2):lcd_address(page+i,column);for j in range(16):hd.spi_oled1306_3w(1,din[i*16+j]);def display_graphic_16x16_1(page,column,data):din = [];for ii in range(32):din.append(int(data[ii]))for i in range(2):lcd_address(page+i,column);for j in range(16):hd.spi_oled1306_3w(1,din[(1-i)*16+15-j]);arr=[0x02,0xE2,0x22,0x22,0x22,0xFE,0x22,0x22,
0x22,0xFE,0x22,0x22,0x22,0xE2,0x02,0x00,
0x00,0xFF,0x00,0x08,0x06,0x01,0x16,0x08,
0x06,0x01,0x02,0x4C,0x80,0x7F,0x00,0x00,0x80,0x80,0x40,0x20,0x10,0x08,0x04,0xC3,
0x04,0x08,0x10,0x20,0x40,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x12,0xD2,0x52,0x5F,0x52,0xF2,
0x52,0x5F,0x52,0xD2,0x12,0x10,0x10,0x00,
0x00,0x00,0x00,0x9F,0x52,0x32,0x12,0x1F,
0x12,0x32,0x52,0x9F,0x00,0x00,0x00,0x00,0xF2,0x12,0x12,0xF2,0x02,0xF2,0x12,0x12,
0xF2,0x00,0xFC,0x16,0x25,0x84,0xFC,0x00,
0xFF,0x01,0x82,0xFF,0x00,0xFF,0x01,0x82,
0xFF,0x10,0x13,0x12,0x52,0x82,0x7E,0x00,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00,0xFC,
0x04,0x16,0x65,0x04,0x84,0xFC,0x00,0x00,
0x00,0x0F,0x04,0x04,0x0F,0x00,0x10,0x13,
0x12,0x12,0x12,0x52,0x92,0x42,0x3E,0x00,0x00,0x22,0x26,0x1A,0x12,0x02,0x7E,0x80,
0x22,0x26,0x1A,0x12,0x02,0x7E,0x00,0x00,
0x20,0x21,0x29,0x25,0x23,0x25,0x29,0xF1,
0x29,0x25,0x23,0x25,0x29,0x21,0x20,0x00,0x10,0x10,0xD0,0xFF,0x90,0x10,0xFC,0x04,
0x02,0xF8,0x00,0xFC,0x04,0x04,0xFC,0x00,
0x04,0x03,0x00,0xFF,0x00,0x01,0x8F,0x44,
0x32,0x0F,0x00,0xFF,0x04,0x08,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#/*"一",8*/0x00,0x10,0x88,0xC4,0x33,0x00,0x40,0x42,
0x42,0x42,0xC2,0x42,0x42,0x42,0x40,0x00,
0x02,0x01,0x00,0xFF,0x00,0x00,0x00,0x00,
0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,#/*"行",9*/0x00,0x00,0xF8,0x08,0x08,0x0C,0x0A,0x09,
0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,
0x00,0x00,0xFF,0x41,0x41,0x41,0x41,0x41,
0x41,0x41,0x41,0x41,0xFF,0x00,0x00,0x00,#/*"白",10*/0x00,0x80,0xEE,0x8A,0xFA,0xAA,0xAE,0x14,
0x12,0xF7,0xAA,0xAA,0xF6,0x12,0x10,0x00,
0x00,0x40,0x40,0x5E,0x52,0x53,0x56,0x5A,
0x52,0x5A,0x1E,0x50,0x90,0x70,0x00,0x00,#/*"鹭",11*/0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x40,
0x40,0x40,0x40,0x40,0x40,0x00,0x00,0x00,
0x40,0x40,0x40,0x40,0x40,0x40,0x7F,0x40,
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,#/*"上",12*/0x40,0x44,0x54,0x54,0x54,0x54,0x54,0x7F,
0x54,0x54,0x54,0x54,0x54,0x44,0x40,0x00,
0x00,0x00,0x00,0xFF,0x15,0x15,0x15,0x15,
0x15,0x55,0x95,0x7F,0x00,0x00,0x00,0x00,#/*"青",13*/0x40,0x40,0x42,0x42,0x42,0x42,0x42,0xFE,
0x42,0x42,0x42,0x42,0x42,0x40,0x40,0x00,
0x80,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,
0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00,#/*"天",14*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#/*"。",15*/0x10,0x0C,0xC4,0x54,0x4C,0x64,0xD5,0x46,
0x44,0x44,0x4C,0x54,0xC4,0x14,0x0C,0x00,
0x00,0x00,0xFF,0x40,0x44,0x62,0x55,0x49,
0x55,0x43,0x40,0x40,0xFF,0x00,0x00,0x00,#/*"窗",0*/0x40,0x40,0x20,0x20,0x90,0x88,0x94,0xE3,
0x84,0x88,0x90,0x20,0x20,0x40,0x40,0x00,
0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x44,
0x46,0x45,0x44,0xFC,0x00,0x00,0x00,0x00,#/*"含",1*/0x02,0x02,0xE2,0x22,0x22,0xFE,0x22,0x22,
0x22,0xFE,0x22,0x22,0xE2,0x02,0x02,0x00,
0x00,0x00,0xFF,0x48,0x44,0x43,0x40,0x40,
0x40,0x43,0x44,0x44,0xFF,0x00,0x00,0x00,#/*"西",2*/0x00,0xF0,0x00,0xFF,0x00,0xF0,0x40,0x20,
0x10,0x0C,0x23,0xCC,0x10,0x20,0x40,0x00,
0x00,0x1F,0x10,0x0F,0x08,0x1F,0x00,0x01,
0x09,0x11,0x21,0xD1,0x0D,0x03,0x00,0x00,#/*"岭",3*/0x80,0x80,0x84,0x84,0x84,0x84,0x84,0xFC,
0x82,0x82,0x82,0x83,0x82,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#/*"千",4*/0x24,0x24,0xA4,0xFE,0x23,0x22,0x80,0x70,
0x00,0xFF,0x00,0x80,0x40,0x30,0x00,0x00,
0x08,0x06,0x01,0xFF,0x01,0x86,0x40,0x30,
0x0E,0x01,0x06,0x18,0x20,0x40,0x80,0x00,#/*"秋",5*/0x10,0x0C,0x05,0x55,0x55,0x55,0x05,0x7F,
0x05,0x55,0x55,0x55,0x05,0x14,0x0C,0x00,
0x00,0x00,0x41,0x49,0x49,0x49,0x49,0x49,
0x49,0x49,0x49,0x49,0xFF,0x00,0x00,0x00,#/*"雪",6*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#/*",",7*/0x00,0xF8,0x01,0x06,0x00,0x00,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0xFE,0x00,0x00,
0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,#/*"门",8*/0x10,0x60,0x02,0x0C,0xC0,0x00,0xF8,0x08,
0x0C,0x0B,0x08,0x08,0x08,0xF8,0x00,0x00,
0x04,0x04,0x7C,0x03,0x00,0x00,0xFF,0x41,
0x41,0x41,0x41,0x41,0x41,0xFF,0x00,0x00,#/*"泊",9*/0x00,0x08,0x88,0x48,0x28,0x18,0x0F,0xE8,
0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,
0x00,0x20,0x11,0x09,0x05,0x41,0x81,0x7F,
0x01,0x01,0x05,0x09,0x11,0x20,0x00,0x00,#/*"东",10*/0x00,0x00,0x80,0x9E,0x92,0x92,0x92,0x92,
0x92,0x92,0x92,0x9E,0x80,0x00,0x00,0x00,
0x84,0x84,0x44,0x44,0x24,0x14,0x0C,0x07,
0x0C,0x14,0x24,0x44,0x44,0x84,0x84,0x00,#/*"吴",11*/0x04,0x04,0x04,0x04,0x04,0xFC,0x44,0x44,
0x44,0x44,0x44,0xC4,0x04,0x04,0x04,0x00,
0x80,0x40,0x20,0x18,0x06,0x01,0x00,0x00,
0x40,0x80,0x40,0x3F,0x00,0x00,0x00,0x00,#/*"万",12*/0x00,0x00,0xFE,0x92,0x92,0x92,0x92,0xFE,
0x92,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,
0x40,0x40,0x44,0x44,0x44,0x44,0x44,0x7F,
0x44,0x44,0x44,0x44,0x44,0x40,0x40,0x00,#/*"里",13*/0x80,0x80,0xFC,0x96,0xE5,0x84,0xFC,0x80,
0x40,0x3E,0x02,0x02,0x7E,0x40,0x40,0x00,
0x80,0x60,0x1F,0x02,0x4C,0x80,0x7F,0x00,
0xFE,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,#/*"船",14*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,#/*"。",15*/
]lcd_clr();for k in range(4):for i in range(8):din = [];for ii in range(i*32,(i+1)*32):din.append(int(arr[ii+k*32*8]))display_graphic_16x16(1+(int)(i/8)*2+k*2, i*16%128+1,din)### 2.def get_gb2312_bitmap(HZ):# 定义变量gbh = 0 # 汉字机器内码的高位gbl = 0 # 汉字机器内码的低位HzPt = 0 # 指向HZ字符串的指针gbbl = 0 # 根据GB2312编码计算出的点阵信息偏移量bitmaps_combined = b''# 打开文件,文件路径为 "gb2312_80.bin"fp = open("gb2312_80.bin", "rb")if fp is None: # 如果无法打开文件print("无法打开字体文件。")return 1 # 退出函数# 循环遍历HZ字符串中的每个字符HZ = HZ.encode("gb2312_80")while HzPt < len(HZ):# 将HZ字符串中的每个字符转换为GB2312编码的高位和低位gbh = HZ[HzPt]print(gbh)gbl = HZ[HzPt+1]# 打印GB2312编码的高位和低位#print("高位:0x{:02X}".format(gbh))#print("低位:0x{:02X}".format(gbl))# 根据GB2312编码计算点阵信息的偏移量if gbh >= 0xB0: # 如果高位大于等于0xB0(这是GB2312编码的起始部分)gbbl = ((gbh - 0xA7) * 94 + (gbl - 0xA1)) * 32 # 根据偏移量计算公式计算偏移量else: # 如果高位小于0xB0gbbl = ((gbh - 0xA1) * 94 + (gbl - 0xA1)) * 32 # 根据偏移量计算公式计算偏移量print("偏移量:{:06d}".format(gbbl)) # 打印偏移量# 使用计算出的偏移量在文件中定位点阵信息,并读取32个字节的点阵信息fp.seek(gbbl)bitmap = fp.read(32);bitmaps_combined=bitmaps_combined+bitmap# 将HzPt指针向前移动两个位置,准备处理下一个汉字的编码HzPt += 2# 关闭文件和释放内存(这部分在原代码中没有明确指出,我在这里添加了)fp.close()del bitmap # 释放内存return bitmaps_combinedHZ ="春江潮水连海平,海上明月共潮生。滟滟随波千万里,何处春江无月明!江流宛转绕芳甸,月照花林皆似霰;空里流霜不觉飞,汀上白沙看不见。江天一色无纤尘,皎皎空中孤月轮。江畔何人初见月?江月何年初照人?人生代代无穷已,江月年年望相似。"
b=get_gb2312_bitmap(HZ)
hex_data = binascii.hexlify(b)
hex_string = hex_data.decode('utf-8')
byte_string = bytes.fromhex(hex_string)#lst = b.decode('utf-8').split(',')
arr=list(byte_string);
#print(byte_string)
lcd_clr();
#time.sleep(4);for m in range((int)(len(HZ)/32)):for k in range(4):for i in range(8):din = [];for jj in range(32):din.append(int(arr[32-jj+i*32+k*8*32+m*4*8*32]));display_graphic_16x16_1(1+(int)(i/8)*2+k*2, i*16%128+1,din)time.sleep(5);hd.ch341_close();
二、总结
1、CH341有IIC、SPI这些MCU常用的接口,可以和拥有这些接口标准的设备通讯。
2、CH341通过USB和电脑通讯,可以理解为电脑的外围设备。用电脑编程向CH341发送指令,可以使CH341驱动其他原本需要单片机驱动的设备,比如LCD。
3、电脑的性能远远高于普通的MCU。所以用这种方式可以很容易提高性能,还省了MCU。
4、python实现了汉字->取字库字模->驱动CH341->LCD。关于驱动CH341,请参见大佬的github的源码。本文代码只是import了大佬的CH341 python封装。本源码依赖ch341dll_wrap.py。
5、所用字库为标准GB2312-80 16*16点阵字库。
6、源码需在电脑端执行,CH341硬件支持。
演示用程序执行效果