物联网开发102 - Micropython ESP32用增强固件驱动st7735显示屏显示汉字

一、目的

        这一节我们来学习如何使用SP32使用加强版本的固件,加载中文字库,驱动st7735显示屏来显示汉字。

二、环境

        ESP32开发板(MicroPython v1.15-13-g530c76f6c-dirty on 2021-04-30)+ 增强固件(esp32_1.15_fb_boost_4M_ULAB.bin) + st7735显示屏 + Win10商业版

        接线方法:

 三、示例代码

from machine import Pin,SPI,PWM
from st7735 import ST7735
import time

BL = PWM(Pin(19),duty = (800),freq = (1000))
spi = SPI(2,baudrate = 80_000_000,polarity = 0,sck = Pin(18),mosi = Pin(23))
tft = ST7735(128,160,spi,dc = Pin(2,Pin.OUT), rst = Pin(4,Pin.OUT), cs = Pin(5,Pin.OUT), rot=2, bgr=0)

tft.font_load("GB2312-24.fon")

def main():
    
    tft.fill(tft.rgb(0,255,0))
    tft.show()
    time.sleep(0.5)
    
    tft.fill(tft.rgb(255,0,0))
    tft.show()
    time.sleep(0.5)
    
    tft.fill(tft.rgb(0,0,255))
    tft.show()
    time.sleep(0.5)
    
    while True:
        for i in range(1000):
            tft.fill(tft.rgb(0,0,0))
            tft.text("欢迎来到上海",32,8,tft.rgb(0,255,0))
            
            tft.hline(0,30,160,tft.rgb(0,0,255))
            tft.hline(0,35,160,tft.rgb(255,0,0))
            
            tft.text("外滩去了没?",10,60,tft.rgb(255,0,255))
            tft.text("你感觉魔都如何?",10,84,tft.rgb(255,255,255))
            
            tft.text("Number = %.4d"%i,50,146,tft.rgb(0,0,255))
            tft.show()
            
            time.sleep(0.1)
            
            
if __name__ == "__main__":
    main()

示例效果:

 注意:部分非官方的ESP32开发板,GPIO2被占用,屏幕会显示花屏,或者造成其他显示不正常,大家可以使用其他IO口。

四、增强固件和字库

        请从下方地址进行下载!

链接: https://pan.baidu.com/s/17N362fTq8XYgW4Xspy8kEQ 提取码: yy42 复制这段内容后打开百度网盘手机App,操作更方便哦

五、st7735屏幕驱动

st7735.py

from time import sleep_ms
from ustruct import pack
from machine import SPI,Pin
from micropython import const
import framebuf
 
#   ST7735V registers definitions

NOP     = const(0x00) # No Operation
SWRESET = const(0x01) # Software reset

SLPIN   = const(0x10) # Sleep in & booster off
SLPOUT  = const(0x11) # Sleep out & booster on
PTLON   = const(0x12) # Partial mode on
NORON   = const(0x13) # Partial off (Normal)

INVOFF  = const(0x20) # Display inversion off
INVON   = const(0x21) # Display inversion on
DISPOFF = const(0x28) # Display off
DISPON  = const(0x29) # Display on
CASET   = const(0x2A) # Column address set
RASET   = const(0x2B) # Row address set
RAMWR   = const(0x2C) # Memory write
RGBSET  = const(0x2D) # Display LUT set

PTLAR   = const(0x30) # Partial start/end address set
COLMOD  = const(0x3A) # Interface pixel format
MADCTL  = const(0x36) # Memory data access control

# panel function commands
FRMCTR1 = const(0xB1) # In normal mode (Full colors)
FRMCTR2 = const(0xB2) # In Idle mode (8-colors)
FRMCTR3 = const(0xB3) # In partial mode + Full colors
INVCTR  = const(0xB4) # Display inversion control

PWCTR1  = const(0xC0) # Power control settings
PWCTR2  = const(0xC1) # Power control settings
PWCTR3  = const(0xC2) # In normal mode (Full colors)
PWCTR4  = const(0xC3) # In Idle mode (8-colors)
PWCTR5  = const(0xC4) # In partial mode + Full colors
VMCTR1  = const(0xC5) # VCOM control

GMCTRP1 = const(0xE0)
GMCTRN1 = const(0xE1)



class ST7735(framebuf.FrameBuffer):
    def __init__(self, width, height, spi, dc, rst, cs, rot=0, bgr=0):
        if dc is None:
            raise RuntimeError('TFT must be initialized with a dc pin number')
        dc.init(dc.OUT, value=0)
        if cs is None:
            raise RuntimeError('TFT must be initialized with a cs pin number')
        cs.init(cs.OUT, value=1)
        if rst is not None:
            rst.init(rst.OUT, value=1)
        else:
            self.rst =None
        self.spi = spi
        self.rot = rot
        self.dc = dc
        self.rst = rst
        self.cs = cs
        self.height = height
        self.width = width
        self.buffer = bytearray(self.height * self.width*2)
        super().__init__(self.buffer, self.width, self.height, framebuf.RGB565, self.width)
        if (self.rot ==0):
            madctl=0x00
        elif (self.rot ==1):
            madctl=0xa0
        elif (self.rot ==2):
            madctl=0xc0
        else :
            madctl=0x60
        if bgr==0:
            madctl|=0x08
        self.madctl = pack('>B', madctl)
        self.reset()

        self._write(SLPOUT)
        sleep_ms(120)
        for command, data in (
            (COLMOD,  b"\x05"),
            (MADCTL,  pack('>B', madctl)),
            ):
            self._write(command, data)
        if self.width==80 or self.height==80:
            self._write(INVON, None)
        else:
            self._write(INVOFF, None)
        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
        self._write(RGBSET, buf)
        #self._write(NORON)
        #sleep_ms(10)
        self.show()
        self._write(DISPON)
        #sleep_ms(100)
    def reset(self):
        if self.rst is None:
            self._write(SWRESET)
            sleep_ms(50)
            return
        self.rst.off()
        sleep_ms(50)
        self.rst.on()
        sleep_ms(50)
    def _write(self, command, data = None):
        self.cs.off()
        self.dc.off()
        self.spi.write(bytearray([command]))
        self.cs.on()
        if data is not None:
            self.cs.off()
            self.dc.on()
            self.spi.write(data)
            self.cs.on()
    def show(self):
        if self.width==80 or self.height==80:
            if self.rot==0 or self.rot==2:
                self._write(CASET,pack(">HH", 26, self.width+26-1))
                self._write(RASET,pack(">HH", 1, self.height+1-1))
            else:
                self._write(CASET,pack(">HH", 1, self.width+1-1))
                self._write(RASET,pack(">HH", 26, self.height+26-1))
        else:
            if self.rot==0 or self.rot==2:
                self._write(CASET,pack(">HH", 0, self.width-1))
                self._write(RASET,pack(">HH", 0, self.height-1))
            else:
                self._write(CASET,pack(">HH", 0, self.width-1))
                self._write(RASET,pack(">HH", 0, self.height-1))
            
        self._write(RAMWR,self.buffer)
    def rgb(self,r,g,b):
        return ((r&0xf8)<<8)|((g&0xfc)<<3)|((b&0xf8)>>3)

六、st7735液晶屏幕购买

https://item.taobao.com/item.htm?spm=a1z09.2.0.0.7bb82e8djoNWJg&id=623738622878&_u=up01rchab7dhttps://item.taobao.com/item.htm?spm=a1z09.2.0.0.7bb82e8djoNWJg&id=623738622878&_u=up01rchab7d                         

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魔都飘雪

您的1毛奖励是我创作的源源动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值