前言
平台:windows10,thonny,esp32,1.44寸tft(128*128)st7735s lcd屏幕
接线:BL->3.3v, VCC->3.3v, CS->GPIO5, DC->GPIO2, RES->GPIO15, SDA->GPIO23, SCL->GPIO18
代码
复制即用
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.buffer = bytearray(128*128*2)
super(ST7735,self).__init__(self.buffer,128,128, 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",0x08)), # 显示相对位置(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,127))
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.text('hello world',0,0)
lcd.show()