液晶ST7302图像缓存结构
简 介: 本文分析了液晶ST7302图像缓存结构,并给出了对字节缓存进行点阵设置的子程序。基于此,可以进一步开发出相应的应用程序。
关键词
: ST7302,液晶,MicroPython
§01 ST7302图像缓存
在 仿电子墨水屏ST7302 给出了对 ST7302 利用 ESP32 MicroPython SPI端口进行访问的实验。当ST7302初始化之后,可以将内存的缓冲区写入ST7302内存,用于显示图像和文字。 下面就缓存与ST7302显示屏之间的显示关系进行介绍。
1.1 ST7302液晶显示结构
ST7302显示板的像素为 250×118 个像素。如下图所示。
▲ 图1.1.1 ST7302液晶像素分布
如果将一块内存数据通过下面程序写入它的显示屏,内存对于液晶像素之间的关系比较特殊。
def LCDFill(xsta, ysta, xend, yend, color):
LCDAddressSet(xsta, ysta, xend-1, yend-1)
lcd_cs.off()
lcd_spi.write(color)
lcd_cs.on
- 1
- 2
- 3
- 4
- 5
- 6
首先,字节缓存逻辑结构为 0x21×125 结构,每一个字节对应液晶中的 2×4的一个矩形内的像素。比如第一个字节,就对应左上角的 2×4的像素矩阵。其中 0 ~ 7 位对应的像素排列如下图所示。
▲ 图1.1.2 内存与液晶像素之间的关系
因此,每个字节对应了四行、两列像素。 0x21(33)个字节应该对应132行像素,但只有前面118行被显示出来。 125列个字节则对应了250列液晶像素。
1.2 内存点阵
根据上述逻辑结构,下面给出了对于字节缓存中设置任意坐标(x,y)点阵的子程序。
lcdbmp = bytearray(0x21*125)
def LCDSetPixel(x, y):
x2 = x//2
y4 = y//4
y2 = y-y4*4
mask = 4**(3-y2)
if x-x2*2 == 0: mask *= 2
lcdbmp[x2*0x21+y4] |= mask
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
1.2.1 测试绘制曲线
下面程序绘制出从 (0,0) 到 (99,99)的一条直线。
LCDInit()
for i in range(100):
LCDSetPixel(i, i)
LCDFill(0,0,11,127,lcdbmp)
- 1
- 2
- 3
- 4
▲ 图1.2.1 绘制直线的显示结果
下面是绘制一条sin 曲线的测试程序。
LCDInit()
for i in range(250):
angle = i * 4 * math.pi / 250
sina = -math.sin(angle) * 50 + 59
v = int(sina)
LCDSetPixel(i, v)
LCDFill(0,0,11,127,lcdbmp)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
▲ 图1.2.2 绘制sin曲线显示结果
下图是动态平移sin曲线的效果。
startangle = 0
while True:
for i in range(len(lcdbmp)):
lcdbmp[i] = 0x0
for i in range(250):
angle = i * 4 * math.pi / 250 + startangle
sina = -math.sin(angle) * 50 + 59
v = int(sina)
LCDSetPixel(i, v)
LCDFill(0,0,11,127,lcdbmp)
startangle -= 0.1
time.sleep_ms(10)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
▲ 图1.2.4 动态显示sin曲线的效果
※ 总 结 ※
本文分析了液晶ST7302图像缓存结构,并给出了对字节缓存进行点阵设置的子程序。基于此,可以进一步开发出相应的应用程序。
2.1 实验程序
from machine import Pin,SPI
import time
import math
lcd_spi = SPI(1, 16000000, polarity=1, phase=1)
lcd_dc = Pin(15, Pin.OUT)
lcd_cs = Pin(2, Pin.OUT)
lcd_res = Pin(27, Pin.OUT)
lcd_cs.on()
lcd_dc.on()
lcd_res.on()
def LCDWriteBus(dat):
lcd_cs.off()
lcd_spi.write(bytes([dat]))
lcd_cs.on()
def LCDWriteData8(dat):
LCDWriteBus(dat)
def LCDWriteData8Dim(dat):
lcd_cs.off()
lcd_spi.write(bytes(dat))
lcd_cs.on()
def LCDWriteData(dat):
lcd_cs.off()
lcd.spi.write(dat.to_bytes(2,1))
lcd_cs.on()
def LCDWriteReg(dat):
lcd_dc.off()
LCDWriteBus(dat)
lcd_dc.on()
def LCDAddressSet(x1,y1,x2,y2):
lcd_cs.off()
lcd_dc.off()
lcd_spi.write(bytes([0x2a]))
lcd_dc.on()
lcd_spi.write(bytes([x1+0x19,x2+0x19]))
lcd_dc.off()
lcd_spi.write(bytes([0x2b]))
lcd_dc.on()
lcd_spi.write(bytes([y1,y2]))
lcd_dc.off()
lcd_spi.write(bytes([0x2c]))
lcd_dc.on()
lcd_cs.on()
def LCDInit():
lcd_res.off()
time.sleep_ms(100)
lcd_res.on()
time.sleep_ms(400)
LCDWriteReg(0x38)
LCDWriteReg(0xeb)
LCDWriteData8(0x02)
LCDWriteReg(0xd7)
LCDWriteData8(0x68)
LCDWriteReg(0xd1)
LCDWriteData8(0x01)
LCDWriteReg(0xc0)
LCDWriteData8(0x80)
LCDWriteReg(0xc1)
LCDWriteData8Dim([0x28,0x28,0x28,0x28,0x14,0x00])
LCDWriteReg(0xc2)
LCDWriteData8Dim([0x00,0x00,0x00,0x00])
LCDWriteReg(0xcb)
LCDWriteData8(0x14)
LCDWriteReg(0xb4)
LCDWriteData8Dim([0xe5,0x77,0xf1,0xff,0xff,0x4f,0xf1,0xff,0xff,0x4f])
LCDWriteReg(0x11)
time.sleep_ms(100)
LCDWriteReg(0xc7)
LCDWriteData8(0xa6)
LCDWriteData8(0xe9)
LCDWriteReg(0xb0)
LCDWriteData8(0x64)
LCDWriteReg(0x36)
LCDWriteData8(0x00)
LCDWriteReg(0x3a)
LCDWriteData8(0x11)
LCDWriteReg(0xb9)
LCDWriteData8(0x23)
LCDWriteReg(0xb8)
LCDWriteData8(0x09)
LCDWriteReg(0x2a)
LCDWriteData8(0x05)
LCDWriteData8(0x36)
LCDWriteReg(0x2b)
LCDWriteData8(0x00)
LCDWriteData8(0xc7)
LCDWriteReg(0xd0)
LCDWriteData8(0x1f)
LCDWriteReg(0x29)
LCDWriteReg(0xb9)
LCDWriteData8(0xe3)
time.sleep_ms(100)
LCDWriteReg(0xb9)
LCDWriteData8(0x23)
LCDWriteReg(0x72)
LCDWriteData8(0x00)
LCDWriteReg(0x39)
LCDWriteReg(0x2a)
LCDWriteReg(0x19)
LCDWriteData8(0x23)
LCDWriteReg(0x2b)
LCDWriteData8Dim([0x0,0x7c,0x2c])
time.sleep_ms(120)
def LCDFill(xsta, ysta, xend, yend, color):
LCDAddressSet(xsta, ysta, xend-1, yend-1)
lcd_cs.off()
lcd_spi.write(color)
lcd_cs.on
lcdbmp = bytearray(0x21*125)
def LCDSetPixel(x, y):
x2 = x//2
y4 = y//4
y2 = y-y4*4
mask = 4**(3-y2)
if x-x2*2 == 0: mask *= 2
lcdbmp[x2*0x21+y4] |= mask
LCDInit()
for i in range(250):
angle = i * 4 * math.pi / 250
sina = -math.sin(angle) * 50 + 59
v = int(sina)
LCDSetPixel(i, v)
LCDFill(0,0,11,127,lcdbmp)
startangle = 0
while True:
for i in range(len(lcdbmp)):
lcdbmp[i] = 0x0
for i in range(250):
angle = i * 4 * math.pi / 250 + startangle
sina = -math.sin(angle) * 50 + 59
v = int(sina)
LCDSetPixel(i, v)
LCDFill(0,0,11,127,lcdbmp)
startangle -= 0.1
time.sleep_ms(10)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
■ 相关文献链接:
● 相关图表链接:
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/125880583
- 点赞
- 收藏
- 关注作者
评论(0)