Micropython加速物联网开发4 - SPI驱动5110LCD屏


5110是84*48点阵LCD屏,其性价比高、接口简单、速度快、功耗低,非常适合电池供电的便携式终端设备。

本例使用TPYBoard开发板SPI1接口驱动5110LCD屏。

连线图即接口说明

Micropython加速物联网开发4 - SPI驱动5110LCD屏_第1张图片

LCD驱动程序

5110LCD的通信协议是一个没有MISO,只有MOSI的SPI协议,SPI协议可以通过GPIO模拟,本例直接使用SPI1接口驱动。

MicroPython提供了SPI的Python API,本例需要用到SPI初始化、SPI write等API。关于SPI的详细说明可以参考官网:MicroPython SPI API

关于5110LCD有现成的驱动程序upcd8544.py,其代码片段如下:

#upcd8544.py
class PCD8544:
    def __init__(self, spi, rst, ce, dc, light, pwr=None):
        # init the SPI bus and pins
        spi.init(spi.MASTER, baudrate=328125, bits=8, polarity=0, phase=1, firstbit=spi.MSB)
        self.reset()
		......
				
    def position(self, x, y):
		......
		
    def data(self, arr):
        """ send bytes in data mode """
        self.bitmap(arr, 1)

    def bitmap(self, arr, dc):
        ......
        self.spi.write(buf)
		......
		
    def lcd_write_string(self, string, x, y):
        self.position(x,y)
        for i in string:
            self.data(self.lcd_font.get_font6_8(i))
    
    
    def lcd_write_chinese(self,data,x,y):
        #获取 字 的UTF8码
        code = 0x00 #赋初值
        data_code = data.encode("UTF-8")
        code |= data_code[0]<<16
        code |= data_code[1]<<8
        code |= data_code[2]
        #获取 字 的UTF8码 END
        self.position(x,y)
        self.data(self.chinese.get_chinese_utf8(code,0))
        self.position(x,y+1)
        self.data(self.chinese.get_chinese_utf8(code,1))

首先调用MicroPython的SPI init函数进行SPI初始化。然后初始化LCD,当5110上电时,内部寄存器和RAM中的内容是不确定的,因此需要一个RES低电平脉冲复位,其时序图如下:

Micropython加速物联网开发4 - SPI驱动5110LCD屏_第2张图片

8544驱动程序提供了字符显示(lcd_write_string)和汉字显示(lcd_write_chinese)API。它们会调用position函数指定字符在LCD屏显示的位置。5110为84*48点阵屏,横向具有84个像素点,纵向分为了0 - 5 的6个8像素点组成的区域。因此position函数的x参数范围为0 -83,y参数范围为 0 -5。示意图如下:

Micropython加速物联网开发4 - SPI驱动5110LCD屏_第3张图片

除了指定position之外,字符显示API还调用了data函数,data函数调用bitmap函数,bitmap函数通过调用SPI的write() 接口将数据写入屏幕。

字库

本例中定义了普通字符和汉字库,普通字库代码片段如下:

#font.py
class FONT6_8:
    """docstring for FONT6_8"""
    FONTTYPE6_8 = [
        [0x00, 0x7e, 0x11, 0x11, 0x11, 0x7e] # 41 A
        ,[0x00, 0x7f, 0x49, 0x49, 0x49, 0x36] # 42 B
        ,[0x00, 0x3e, 0x41, 0x41, 0x41, 0x22] # 43 C
        ,[0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c] # 44 D
        ......
    ]
    
    def get_font6_8(self, data):
        return self.FONTTYPE6_8[bytearray(data)[0] - 0x20]


汉字库通过取模软件生成。汉字库代码如下:

#chinese.py
class CN_UTF8:
    """docstring for CN_UTF8"""
    UTF8_CHINESE = {
        0xe789a9:[
					[0x40,0x3C,0x10,0xFF,0x10,0x10,0x20,0x10,0x8F,0x78,0x08,0xF8,0x08,0xF8,0x00,0x00],
					[0x02,0x06,0x02,0xFF,0x01,0x01,0x04,0x42,0x21,0x18,0x46,0x81,0x40,0x3F,0x00,0x00]
                ],#物 
        0xe88194:[
					[0x02,0xFE,0x92,0x92,0xFE,0x02,0x00,0x10,0x11,0x16,0xF0,0x14,0x13,0x10,0x00,0x00],
					[0x10,0x1F,0x08,0x08,0xFF,0x04,0x81,0x41,0x31,0x0D,0x03,0x0D,0x31,0x41,0x81,0x00]
                ],#联
        0xe7bd91:[
					[0x00,0xFE,0x02,0x22,0x42,0x82,0x72,0x02,0x22,0x42,0x82,0x72,0x02,0xFE,0x00,0x00],
					[0x00,0xFF,0x10,0x08,0x06,0x01,0x0E,0x10,0x08,0x06,0x01,0x4E,0x80,0x7F,0x00,0x00]
                ],#网                   
    }
    def get_chinese_utf8(self, key,isBottom = 0):
        values = self.UTF8_CHINESE[key]
        return values[isBottom]

汉字UTF-8编码查询

汉字取模工具


主程序

目前有了屏幕驱动程序,也准备好了字符和汉字字库,是时候正式编写主程序了。代码如下:

# main.py
import pyb
import upcd8544
from machine import SPI,Pin 

def main():
    lcd_5110.lcd_write_string('MicroPython',10,1)

    lcd_5110.lcd_write_chinese("物",18,3)
    lcd_5110.lcd_write_chinese("联",34,3)
    lcd_5110.lcd_write_chinese("网",50,3)

    pyb.delay(1000)

if __name__ == '__main__':
    SPI = pyb.SPI(1) #DIN=>X8-MOSI/CLK=>X6-SCK
    RST    = pyb.Pin('X1')
    CE     = pyb.Pin('X2')
    DC     = pyb.Pin('X3')
    LIGHT  = pyb.Pin('X4')
    lcd_5110 = upcd8544.PCD8544(SPI, RST, CE, DC, LIGHT)
    while(1):
     main()

首先导入了驱动程序、MicroPython硬件库中的SPI和Pin,然后定义SPI接口以及其他pin脚,实例化lcd对象lcd_5110,最后显示'MicroPython'字符以及'物联网'三个汉字。

运行效果如下:

Micropython加速物联网开发4 - SPI驱动5110LCD屏_第4张图片

本例完整代码github地址:MicroPython-5110LCD






你可能感兴趣的:(MicroPython)