基于micropython的st7735s驱动

前言

合宙tft1.8寸屏幕,esp32,thonny,windows10

分为两部分,驱动所有代码,复制即用;代码解说

引脚接口情况:SCL P18,SDA P23,RES P15,DC P2,CS P5

所有代码

from time import sleep_ms
from ustruct import pack
from machine import SPI,Pin
from micropython import const
import framebuf


class ST7735(framebuf.FrameBuffer):
    def __init__(self,spi):
        self.spi = spi
        self.dc=Pin(2,Pin.OUT,value=0)
        self.rst=Pin(15,Pin.OUT,value=1)
        self.cs=Pin(5,Pin.OUT,value=1)
        self.height = 160
        self.width = 128
        self.buffer = bytearray(160*128*2)
        super(ST7735,self).__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.reset()
        self._write(0x11)
        sleep_ms(100)
        
        buf=bytearray(128)
        for i in range(32):
            buf[i]=i*2
            buf[i+96]=i*2
        for i in range(64):
            buf[i+32]=i
        
        for command, data in (
            (0x3A, b"x05"), # 选择16位颜色模式
            (0x36, pack(">B",0xC0)), # 显示相对位置
            (0x20, None), #关闭反转
            (0x29, None),#开启显示
            (0x2d, buf),# RGB
            ):
            self._write(command, data)
        self.clear()
        
    def clear(self):
        self.fill(0)
        self.show()
    
    def reset(self):
        self._write(0x01)

    def _write(self, command, data=None):
        self.cs.off() # 开启片选信号
        self.dc.off() # 低电平写命令
        self.spi.write(bytearray([command]))
        if data is not None:
            self.cs.on() # 释放片选信号
            self.cs.off() # 开启片选信号
            self.dc.on() # 高电平写参数
            self.spi.write(data)
            self.cs.on() # 释放片选信号

    def show(self):
        self._write(0x2a,pack(">HH", 0, 127))
        self._write(0x2b,pack(">HH", 0, 159))
        self._write(0x2c,self.buffer)
    
    def pictrue(self,x,y,width,height,data):
        for i in range(height):
            for j in range(width):
                self.pixel(x+j,y+i,data[j+i*width])

    def sleep(self):
        self._write(0x28)
        self._write(0x10)


lcd= ST7735(SPI(2, baudrate=60000000))
#lcd.rect(50,50,50,60,500)矩形
#lcd.vline(0,0,50,500)竖线
#lcd.hline(0,50,50,500)横线
#lcd.text("hello",50,50,500)显示英语
#lcd.ellipse(50,50,50,50,500,1)#椭圆
#lcd.line(0,0,40,50,500)直线
#lcd.pixel(50,50,500)画点
lcd.text("hello world",0,0)
#lcd.text("I love you",0,10)
#lcd.text("thank you",0,20)
#lcd.text("I am fine",0,30)
#lcd.text("good morning",0,40)
#lcd.text("good afternoon",0,50)
#lcd.text("good evening",0,60)
#lcd.text("Todat is sunny day",0,70)
#lcd.text("wow,is good",0,80)
#lcd.text("that is good",0,90)
#lcd.text("good luck",0,100)
#lcd.text("you are my dog",0,110)
#lcd.text("it is fortunate",0,120)
#lcd.text("where si my pen",0,130)
#lcd.text("hello world",0,140)
#lcd.text("i am good at studing",0,150)
lcd.show() # 显示出来

代码分解

导包

from time import sleep_ms
from ustruct import pack
from machine import SPI,Pin
from micropython import const
import framebuf

st7735s类 

类初始化

定义dc rst cs引脚;lcd宽高;缓冲区大小:128*160*2字节

复位,唤醒,进行相应命令配置,具体命令见手册

def __init__(self,spi):
        self.spi = spi
        self.dc=Pin(2,Pin.OUT,value=0)
        self.rst=Pin(15,Pin.OUT,value=1)
        self.cs=Pin(5,Pin.OUT,value=1)
        self.height = 160
        self.width = 128
        self.buffer = bytearray(160*128*2)
        super(ST7735,self).__init__(self.buffer, self.width, self.height, framebuf.RGB565)
        self.reset()
        self._write(0x11)
        sleep_ms(100)
        
        buf=bytearray(128)
        for i in range(32):
            buf[i]=i*2
            buf[i+96]=i*2
        for i in range(64):
            buf[i+32]=i
        
        for command, data in (
            (0x3A, b"x05"), # 选择16位颜色模式
            (0x36, pack(">B",0xC0)), # 显示相对位置
            (0x20, None), #关闭反转
            (0x29, None),#开启显示
            (0x2d, buf),# RGB
            ):
            self._write(command, data)
        self.clear()

清屏函数

def clear(self):
    self.fill(0)
    self.show()

复位函数

软件复位

def reset(self):
    self._write(0x01)

写数据函数

st7735s的命令有2种:命令后无参数;命令后有参数

def _write(self, command, data=None):
    self.cs.off() # 开启片选信号
    self.dc.off() # 低电平写命令
    self.spi.write(bytearray([command]))
    if data is not None:
        self.cs.on() # 释放片选信号
        self.cs.off() # 开启片选信号
        self.dc.on() # 高电平写参数
        self.spi.write(data)
        self.cs.on() # 释放片选信号

刷新函数

采用全局刷新,128*160,一次性将缓冲区内所有数据写入st7735s内

def show(self):
    self._write(0x2a,pack(">HH", 0, 127))
    self._write(0x2b,pack(">HH", 0, 159))
    self._write(0x2c,self.buffer)

画图函数

x,图片右上角横坐标;y,图片右上角纵坐标;width,图片宽;height,图片高;data,颜色数据

def pictrue(self,x,y,width,height,data):
    for i in range(height):
        for j in range(width):
            self.pixel(x+j,y+i,data[j+i*width])

进入睡眠函数

def sleep(self):
    self._write(0x28)
    self._write(0x10)