用micpython写一个ILI9342C驱动程序
160浏览 · 2023-11-16 12:03:52
ILI9342C是一款常见的TFT LCD[控制器](https://geek.csdn.net/educolumn/39901e2ac3fc8f7138ffb4b585690028?spm=1055.2569.3001.10083)芯片,它可以通过SPI接口与微[控制器](https://geek.csdn.net/educolumn/39901e2ac3fc8f7138ffb4b585690028?spm=1055.2569.3001.10083)通信。以下是一个基本的Micropython ILI9342C驱动程序的示例:
```python
from micropython import const
import framebuf
import time
# ILI9342C命令
ILI_NOP = const(0x00)
ILI_SWRESET = const(0x01)
ILI_RDDID = const(0x04)
ILI_RDDST = const(0x09)
# 控制寄存器
ILI_SLPIN = const(0x10)
ILI_SLPOUT = const(0x11)
ILI_PTLON = const(0x12)
ILI_NORON = const(0x13)
# [显示](https://geek.csdn.net/educolumn/103ca72607c717122e068b9f06a24df6?spm=1055.2569.3001.10083)[窗口](https://geek.csdn.net/educolumn/00d3d1b467be187bf63895f811603770?spm=1055.2569.3001.10083)
ILI_CASET = const(0x2A)
ILI_PASET = const(0x2B)
ILI_RAMWR = const(0x2C)
# 颜色格式
ILI_COLMOD = const(0x3A)
class ILI9342C(framebuf.FrameBuffer):
def __init__(self, spi, cs, dc, rst=None, width=240, height=320):
self.spi = spi
self.cs = cs
self.dc = dc
self.rst = rst
self.width = width
self.height = height
self.buffer = bytearray(width * height * 2)
su[per](https://geek.csdn.net/educolumn/2c6ed1be7edac16b3c9a0c3228eaff0c?spm=1055.2569.3001.10083)().__init__(self.buffer, width, height, framebuf.RGB565)
self.reset()
self.init()
def reset(self):
if self.rst != None:
self.rst.value(0)
time.sleep_ms(50)
self.rst.value(1)
time.sleep_ms(50)
def init(self):
# 初始化步骤1
self.write_cmd(ILI_SWRESET)
time.sleep_ms(120)
# 初始化步骤2
self.write_cmd(0xEF)
self.write_data(0x03)
self.write_data(0x80)
self.write_data(0x02)
# 初始化步骤3
self.write_cmd(0xCF)
self.write_data(0x00)
self.write_data(0xC1)
self.write_data(0x30)
# 初始化步骤4
self.write_cmd(0xED)
self.write_data(0x64)
self.write_data(0x03)
self.write_data(0x12)
self.write_data(0x81)
# 初始化步骤5
self.write_cmd(0xE8)
self.write_data(0x85)
self.write_data(0x00)
self.write_data(0x78)
# 初始化步骤6
self.write_cmd(0xCB)
self.write_data(0x39)
self.write_data(0x2C)
self.write_data(0x00)
self.write_data(0x34)
self.write_data(0x02)
# 初始化步骤7
self.write_cmd(0xF7)
self.write_data(0x20)
# 初始化步骤8
self.write_cmd(0xEA)
self.write_data(0x00)
self.write_data(0x00)
# 初始化步骤9
self.write_cmd(ILI_COLMOD)
self.write_data(0x55)
# 初始化步骤10
self.write_cmd(ILI_MADCTL)
self.write_data(0x48)
# 初始化步骤11
self.write_cmd(ILI_SLPOUT)
time.sleep_ms(120)
# 初始化步骤12
self.write_cmd(ILI_DISPON)
def write_cmd(self, cmd):
self.dc.value(0) # 发送命令
self.cs.value(0) # 选中芯片
self.spi.write(bytearray([cmd]))
self.cs.value(1) # 取消片选
def write_data(self, data):
self.dc.value(1) # 发送数据
self.cs.value(0) # 选中芯片
self.spi.write(bytearray([data]))
self.cs.value(1) # 取消片选
def show(self):
self.write_cmd(ILI_CASET)
self.write_data(0)
self.write_data(0)
self.write_data(self.width >> 8)
self.write_data(self.width & 0xFF)
self.write_cmd(ILI_PASET)
self.write_data(0)
self.write_data(0)
self.write_data(self.height >> 8)
self.write_data(self.height & 0xFF)
self.write_cmd(ILI_RAMWR)
self.cs.value(1)
self.dc.value(1)
self.cs.value(0)
self.spi.write(self.buffer)
self.cs.value(1)