当前位置:   article > 正文

TFT驱动ST7789使用总结

st7789

最近在使用一款TFT驱动芯片ST7789,在阅读芯片数据手册和液晶屏数据手册时,发现总是对不上,芯片手册中,有好几个引脚,一会儿是这个作用,一会儿又变成另一种作用,实在是让人感到混淆。网上找了好久,也没有人专门针对这款芯片进行细致地讲解,按理说,这款芯片很常用,为啥没有一个专门的教程呢,难道这个确实太简单了,只是我不会看数据手册吗?为此,从网上找了些零碎的内容,做一个简单的汇总整理,并根据我自己的理解和使用情况做一些必要的补充。

ST7789v是Sitronix公司研制的,用于驱动LCD的芯片,在市场上有很多TFT屏使用这款芯片驱动。注意:虽然驱动芯片相同,但不同TFT厂家在设计接口时各有不同

按照《ST7789v Datasheet》的描述,ST7789v的接口有以下几种:

- Parallel 8080-series MCU Interface (8-bit, 9-bit, 16-bit & 18-bit)

- 6/16/18 RGB Interface(VSYNC, HSYNC, DOTCLK, ENABLE, DB[17:0])

- Serial Peripheral Interface(SPI Interface)
- VSYNC Interface

其中Parallel 8080-series MCU Interface、Serial Peripheral Interface两种使用最多,市场上的TFT屏提供的基本是这两中接口或其中之一。

有几点需要注意:

(1)TFT厂家提供的接口描述与《ST7789v Datasheet》里的可能不一致,特别是引脚名称,需要核对清楚;

(2)TFT屏幕内的IM3是连接的高电平还是低电平,这直接约束可以使用哪些接口,IM3,它常常被TFT厂家在内部连好了;

(3)并口引脚的线序必须与厂家核对清楚。

本次,我使用的是SPI接口(4-line 8bit serial I/F Ⅱ下的SDO没有使用)。

端口定义示例如下:

  1. /*MCU连接ST7789V端口定义*/
  2. #define LCD_CS_PORT GpioPortA
  3. #define LCD_CS_PIN GpioPin4 //CS
  4. #define LCD_DCX_PORT GpioPortA
  5. #define LCD_DCX_PIN GpioPin5 //不同显示屏厂家标识不同,如:RS/SPISCL/SCK
  6. #define LCD_SDA_PORT GpioPortA
  7. #define LCD_SDA_PIN GpioPin7 //SDA/MOSI
  8. #define LCD_WRX_PORT GpioPortA
  9. #define LCD_WRX_PIN GpioPin3 //不同显示屏厂家标识不同,如:WR
  10. /*ST7789 Reset控制端口定义*/
  11. #define LCD_RESX_PORT GpioPortA
  12. #define LCD_RESX_PIN GpioPin10 //不同显示屏厂家标识不同,如:RESET/RES

参考:HC32L196 驱动 ST7789v 经验_junzheng的博客-CSDN博客

ST7789V数据手册重点内容摘录

This chip is capable of connecting directly to an external microprocessor, and accepts, 8-bits/9-bits/16-bits/18-bits parallel interface.

基本特征

Display Resolution: 240*RGB (H) *320(V)

Frame Memory Size: 240 x 320 x 18-bit = 1,382,400 bits

MCU Interface

- Parallel 8080-series MCU Interface (8-bit, 9-bit, 16-bit & 18-bit)

- 6/16/18 RGB Interface(VSYNC, HSYNC, DOTCLK, ENABLE, DB[17:0])

- Serial Peripheral Interface(SPI Interface)

- VSYNC Interface

Wide Supply Voltage Range

- I/O Voltage (VDDI to DGND): 1.65V ~ 3.3V (VDDI≦VDD)

- Analog Voltage (VDD to AGND): 2.4V ~ 3.3V

框图

以上注意,DCX/RS和WRX/SCL这两个引脚的功能是可配置的。 

因此,具体厂家生产的液晶屏,需要核对好接口功能。

关键引脚

IM3 IM2 IM1 IM0这几位是决定使用哪种接口的。以及对应的一些位的作用。

复位

并行接口中用作数据/命令的选择位,SPI中用作时钟

 

片选,低电平使能

并口中是写使能,4线SPI中用作数据/命令选择接口

用在并口中,读使能

SPI接口的输入输出或者仅作为输入,数据在时钟的上升沿被锁存,要么是半双工,要么相当于MOSI引脚

SPI输出,在时钟下降沿输出数据,相当于MISO

DB[17:0] 并行口时用到的数据位。 

注意,上面DCX/RS和WRX/SCL这两个引脚的RS和SCL功能,并未提及,不知道具体是什么含义。

针对具体的液晶屏ASL024HLCGB00-210

只实现了两种接口,IM3和IM0固定为0 

当IM2和IM1均为1时,使用的就是4线8位SPI

片选

时钟

IM3固化为低电平,因此是数据输入/输出口

这里只能实现半双工,其实,可以将SDA作为输入(上升沿),然后还有个单独的SDO作为输出(下降沿),以此实现全双工。

命令/参数选择

又回到ST7789V数据手册

直接关注四线八位SPI,4-line serial interface

引脚定义

片选

The write mode of the interface means the micro controller writes commands and data to the LCD driver.

In 4-lines serial interface, data packet contains just transmission byte and control bit WRX is transferred by the WRX pin. If WRX is “low”, the transmission byte is interpreted as a command byte. If WRX is “high”, the transmission byte is stored in the display data RAM (memory write command), or command register as parameter.

Any instruction can be sent in any order to the driver. The MSB is transmitted first. The serial interface is initialized when CSX is high. In this state, SCL clock pulse or SDA data have no effect. A falling edge on CSX enables the serial interface and indicates the start of data transmission.

写时序

读时序

The read mode of the interface means that the micro controller reads register value from the driver. To achieve read function, the micro controller first has to send a command (read ID or register command) and then the following byte is transmitted in the opposite direction. After that CSX is required to go to high before a new command is send (see the below figure). The driver samples the SDA (input data) at rising edge of SCL, but shifts SDA (output data) at the falling edge of SCL. Thus the micro controller is supported to read at the

rising edge of SCL. After the read status command has been sent, the SDA line must be set to tri-state no later than at the falling edge of SCL of the last bit.

更多内容,详见数据手册。其他具体内容在实践中再总结补充吧。 

相关命令参考数据手册9 COMMAND 

ST7789驱动

  1. /********************************************************************
  2. * name :
  3. * description : STM32F407 LCD
  4. PB3 SPI1_SCK - DC
  5. PB4 SPI1_MISO - SD0
  6. PB5 SPI1_MOSI - SDA
  7. * Input :
  8. * Return :
  9. ********************************************************************/
  10. void Spi1Init(void)
  11. {
  12. GPIO_InitTypeDef GPIO_InitStruct;
  13. SPI_InitTypeDef SPI_InitStruct;
  14. RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE );
  15. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE ); //使能SPI1时钟
  16. /* GPIO配置 */
  17. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  18. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; //配置为复用模式
  19. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  20. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  21. /* MISO */
  22. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
  23. GPIO_Init(GPIOB, &GPIO_InitStruct);
  24. /* MOSI */
  25. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
  26. GPIO_Init(GPIOB, &GPIO_InitStruct);
  27. GPIO_SetBits(GPIOB, GPIO_Pin_5);
  28. /* SCK */
  29. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
  30. GPIO_Init(GPIOB, &GPIO_InitStruct);
  31. GPIO_SetBits(GPIOB, GPIO_Pin_3);
  32. // 复用
  33. GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1); /* PB3复用为 SPI1 */
  34. GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1); /* PB4复用为 SPI1 */
  35. GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1); /* PB5复用为 SPI1 */
  36. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); /* 复位 SPI1 */
  37. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); /* 停止复位 SPI1 */
  38. SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工模式;
  39. SPI_InitStruct.SPI_Mode = SPI_Mode_Master; //主机模式
  40. SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; //SPI发送接收8位帧结构
  41. SPI_InitStruct.SPI_CPOL = SPI_CPOL_High; //串行同步时钟的空闲状态为高电平
  42. SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge; //第二个跳变沿数据被采样
  43. SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; //NSS信号由软件控制
  44. SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //传输速度为 84M/256=328.125KHz
  45. SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从MSB位开始
  46. SPI_InitStruct.SPI_CRCPolynomial = 7; //CRC值计算的多项式
  47. SPI_Init(SPI1, &SPI_InitStruct);
  48. SPI_Cmd(SPI1, ENABLE);
  49. }
  50. /********************************************************************
  51. * name :
  52. * description : SPI1 读写一个字节
  53. * Input : 要写入的字节
  54. * Return : 读取到的字节
  55. ********************************************************************/
  56. uint8_t SPI1ReadWriteByte(uint8_t TxData)
  57. {
  58. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}//等待发送区空
  59. SPI_I2S_SendData(SPI1, TxData); //通过外设SPIx发送一个byte数据
  60. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} //等待接收完一个byte
  61. return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
  62. }
  63. /********************************************************************
  64. * name :
  65. * description : SPI 发送数据
  66. * Input :
  67. * Return :
  68. ********************************************************************/
  69. static void SendValue(uint8_t value)
  70. {
  71. uint8_t i;
  72. OUT_LCD_SCL_LO;
  73. OUT_LCD_CS_ON;
  74. for(i = 0; i < 8; i++)
  75. {
  76. if(value & 0x80)
  77. OUT_LCD_SDA_HI;
  78. else
  79. OUT_LCD_SDA_LO;
  80. OUT_LCD_SCL_LO;
  81. value <<= 1;
  82. OUT_LCD_SCL_HI;
  83. }
  84. OUT_LCD_CS_OFF;
  85. }
  86. /********************************************************************
  87. * name :
  88. * description : 写屏命令
  89. * Input :
  90. * Return :
  91. ********************************************************************/
  92. static void WriteCommand(uint8_t value)
  93. {
  94. OUT_LCD_WR_CMD;
  95. SendValue(value);
  96. }
  97. /********************************************************************
  98. * name :
  99. * description : 写屏数据
  100. * Input :
  101. * Return :
  102. ********************************************************************/
  103. static void WriteData(uint8_t value)
  104. {
  105. OUT_LCD_WR_DAT;
  106. SendValue(value);
  107. }
  108. /********************************************************************
  109. * name :
  110. * description : 写16位数据
  111. * Input :
  112. * Return :
  113. ********************************************************************/
  114. static void Write16Bits(uint16_t value)
  115. {
  116. OUT_LCD_WR_DAT;
  117. SendValue(value >> 8);
  118. SendValue(value);
  119. }
  120. /********************************************************************
  121. * name :
  122. * description : 显示屏驱动芯片初始化
  123. * Input :
  124. * Return :
  125. ********************************************************************/
  126. void St7789Init(void)
  127. {
  128. OUT_LCD_RESET_START;
  129. delay_ms(200);
  130. OUT_LCD_RESET_STOP;
  131. delay_ms(100);
  132. WriteCommand(0x11);
  133. delay_ms(10);
  134. WriteCommand(0x36);
  135. WriteData (0xa0);
  136. WriteCommand(0x3a);
  137. WriteData (0x05); //像素点的数据格式设置为 RGB:565
  138. //与屏的型号有关
  139. WriteCommand(0x20); // 颜色值需按位取反
  140. WriteCommand(0xb2);
  141. WriteData (0x0c);
  142. WriteData (0x0c);
  143. WriteData (0x00);
  144. WriteData (0x33);
  145. WriteData (0x33);
  146. WriteCommand(0xb7);
  147. WriteData (0x35);
  148. WriteCommand(0xbb);
  149. WriteData (0x35);
  150. WriteCommand(0xc0);
  151. WriteData (0x2c);
  152. WriteCommand(0xc2);
  153. WriteData (0x01);
  154. WriteCommand(0xc3);
  155. WriteData (0x0b);
  156. WriteCommand(0xc4);
  157. WriteData (0x20);
  158. WriteCommand(0xc6);
  159. WriteData (0x1e);
  160. WriteCommand(0xd0);
  161. WriteData (0xa4);
  162. WriteData (0xa1);
  163. WriteCommand(0xE0); //Set Gamma
  164. WriteData (0xd0);
  165. WriteData (0x00);
  166. WriteData (0x02);
  167. WriteData (0x07);
  168. WriteData (0x0b);
  169. WriteData (0x1a);
  170. WriteData (0x31);
  171. WriteData (0x54);
  172. WriteData (0x40);
  173. WriteData (0x29);
  174. WriteData (0x12);
  175. WriteData (0x12);
  176. WriteData (0x12);
  177. WriteData (0x17);
  178. WriteCommand(0XE1); //Set Gamma
  179. WriteData (0xd0);
  180. WriteData (0x00);
  181. WriteData (0x02);
  182. WriteData (0x07);
  183. WriteData (0x05);
  184. WriteData (0x25);
  185. WriteData (0x2d);
  186. WriteData (0x44);
  187. WriteData (0x45);
  188. WriteData (0x1c);
  189. WriteData (0x18);
  190. WriteData (0x16);
  191. WriteData (0x1c);
  192. WriteData (0x1d);
  193. WriteCommand(0x29);
  194. }
  195. /********************************************************************
  196. * name :
  197. * description : 画点函数,在坐标为(xStart,yStart)的位置,画一个点
  198. * Input :
  199. * Return :
  200. ********************************************************************/
  201. void LcdDrawPoint(uint32_t xStart, uint32_t yStart, uint32_t color)
  202. {
  203. WriteCommand(0x2a);
  204. Write16Bits(xStart);
  205. WriteCommand(0x2b);
  206. Write16Bits(yStart);
  207. WriteCommand(0x2c);
  208. Write16Bits(color);
  209. }
  210. /********************************************************************
  211. * name :
  212. * description : 窗口设置函数
  213. * Input :
  214. * Return :
  215. ********************************************************************/
  216. void SetWindow(uint32_t xStart, uint32_t yStart, uint32_t xEnd, uint32_t yEnd)
  217. {
  218. WriteCommand(0x2a);
  219. Write16Bits(xStart);
  220. Write16Bits(xEnd);
  221. WriteCommand(0x2b);
  222. Write16Bits(yStart);
  223. Write16Bits(yEnd);
  224. WriteCommand(0x2c);
  225. }
  226. /********************************************************************
  227. * name :
  228. * description : 设置背景色
  229. * Input :
  230. * Return :
  231. ********************************************************************/
  232. void St7789SetBGColor(uint32_t color)
  233. {
  234. uint32_t i,j;
  235. SetWindow(0,0,(LCD_WIDTH-1),(LCD_HEIGHT-1));
  236. for(i=0;i<LCD_HEIGHT;i++)
  237. {
  238. for(j=0;j<LCD_WIDTH;j++)
  239. {
  240. Write16Bits(color);
  241. }
  242. }
  243. }
  244. /********************************************************************
  245. * name :
  246. * description : 填充块
  247. * Input :
  248. * Return :
  249. ********************************************************************/
  250. void St7789FillBlock(uint32_t xStart, uint32_t yStart, uint32_t block_width, uint32_t block_height, uint16_t color)
  251. {
  252. uint32_t i,j;
  253. SetWindow( xStart, yStart, (xStart+block_width-1), (yStart+block_height-1) );
  254. for(i=0;i<block_width;i++)
  255. {
  256. for(j=0;j<block_height;j++)
  257. {
  258. Write16Bits(color);
  259. }
  260. }
  261. }
  262. /********************************************************************
  263. * name :
  264. * description : 在坐标为(xStart,yStart)的位置,
  265. 填充一个宽和高分别为block_width、block_height的图片
  266. * Input : block_data为指向图片点阵数据的指针,
  267. 图片点阵数据格式为 RGB:565
  268. * Return :
  269. ********************************************************************/
  270. void St7789DrawPic(uint32_t xStart, uint32_t yStart, uint32_t block_width, uint32_t block_height, const uint8_t *block_data)
  271. {
  272. uint32_t i,j;
  273. SetWindow( xStart, yStart, (xStart+block_width-1), (yStart+block_height-1) );
  274. for(i=0;i<block_width;i++)
  275. {
  276. for(j=0;j<block_height;j++)
  277. {
  278. WriteData(*(block_data+1));
  279. WriteData(*block_data);
  280. block_data += 2;
  281. }
  282. }
  283. }
  284. /********************************************************************
  285. * name :
  286. * description : 二进制画图
  287. * Input :
  288. * Return :
  289. ********************************************************************/
  290. void St7789DrawImageBin(uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t *image_mask, uint32_t color, bool reverse)
  291. {
  292. uint32_t i, j;
  293. uint32_t x0, y0;
  294. uint8_t bit_mask;
  295. for(i = 0; i < h; i++)
  296. {
  297. y0 = y + i;
  298. bit_mask = 0x80;
  299. for(j = 0; j < w; j++)
  300. {
  301. if(reverse)
  302. x0 = x + w - 1 - j;
  303. else
  304. x0 = x + j;
  305. if(*image_mask & bit_mask)
  306. {
  307. LcdDrawPoint(x0, y0, color);
  308. }
  309. bit_mask >>= 1;
  310. if(bit_mask == 0)
  311. {
  312. bit_mask = 0x80;
  313. image_mask++;
  314. }
  315. }
  316. if(bit_mask != 0x80)
  317. image_mask++;
  318. }
  319. }
  320. /********************************************************************
  321. * name :
  322. * description : 绘制字符串,可设置前景色和背景色
  323. * Input :
  324. * Return :
  325. ********************************************************************/
  326. void St7789DrawChar(uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t *str, uint32_t forecolor, uint32_t backcolor)
  327. {
  328. uint32_t i, j;
  329. uint8_t mask;
  330. SetWindow( x, y, x + w - 1, y + h -1);
  331. for(i = 0; i < h; i++)
  332. {
  333. mask = 0x80;//取模时:阴码,高位在前
  334. for(j = 0; j < w; j++)
  335. {
  336. if(*str & mask)
  337. Write16Bits(forecolor);
  338. else
  339. Write16Bits(backcolor);
  340. mask >>= 1;
  341. if(mask == 0)
  342. {
  343. mask = 0x80;
  344. str++;
  345. }
  346. }
  347. if(mask != 0x80)
  348. str++;
  349. }
  350. }
  351. /********************************************************************
  352. * description : 画一个圆形(无填充)
  353. * Input : x0,y0 圆心坐标
  354. r 圆半径
  355. mask_quadrant 象限选择
  356. * Return :
  357. ********************************************************************/
  358. void St7789DrawCycle(uint32_t x0, uint32_t y0, uint32_t r, uint32_t forecolor, uint8_t mask_quadrant)
  359. {
  360. int a, b;
  361. int di;
  362. a = 0;
  363. b = r;
  364. di = 3 - (r << 1);
  365. while(a <= b)
  366. {
  367. //1 象限
  368. if(mask_quadrant & 0x01)
  369. {
  370. LcdDrawPoint(x0 + b, y0 - a, forecolor);
  371. LcdDrawPoint(x0 + a, y0 - b, forecolor);
  372. }
  373. //2 象限
  374. if(mask_quadrant & 0x02)
  375. {
  376. LcdDrawPoint(x0 - b, y0 - a, forecolor);
  377. LcdDrawPoint(x0 - a, y0 - b, forecolor);
  378. }
  379. //3 象限
  380. if(mask_quadrant & 0x04)
  381. {
  382. LcdDrawPoint(x0 - a, y0 + b, forecolor);
  383. LcdDrawPoint(x0 - b, y0 + a, forecolor);
  384. }
  385. //4 象限
  386. if(mask_quadrant & 0x08)
  387. {
  388. LcdDrawPoint(x0 + b, y0 + a, forecolor);
  389. LcdDrawPoint(x0 + a, y0 + b, forecolor);
  390. }
  391. a++;
  392. if(di < 0)
  393. {
  394. di += 4 * a + 6;
  395. }
  396. else
  397. {
  398. di += 10 + 4 * (a - b);
  399. b--;
  400. }
  401. }
  402. }
  403. /********************************************************************
  404. * description : 画一个圆块
  405. * Input : x0,y0 圆心坐标
  406. r 圆半径
  407. color 颜色
  408. * Return :
  409. ********************************************************************/
  410. void St7789DrawFilledCycle(uint32_t x0, uint32_t y0, uint32_t r, uint32_t color)
  411. {
  412. int16_t sCurrentX, sCurrentY;
  413. int16_t sError;
  414. sCurrentX = 0;
  415. sCurrentY = r;
  416. sError = 3- (r<<1); //判断下个点位置的标志
  417. while(sCurrentX <= sCurrentY)
  418. {
  419. int16_t sCountY;
  420. for(sCountY=sCurrentX;sCountY<=sCurrentY;sCountY++)
  421. {
  422. LcdDrawPoint(x0 + sCurrentX, y0 + sCountY, color);
  423. LcdDrawPoint(x0 - sCurrentX, y0 + sCountY, color);
  424. LcdDrawPoint(x0 - sCountY, y0 + sCurrentX, color);
  425. LcdDrawPoint(x0 - sCountY, y0 - sCurrentX, color);
  426. LcdDrawPoint(x0 - sCurrentX, y0 - sCountY, color);
  427. LcdDrawPoint(x0 + sCurrentX, y0 - sCountY, color);
  428. LcdDrawPoint(x0 + sCountY, y0 - sCurrentX, color);
  429. LcdDrawPoint(x0 + sCountY, y0 + sCurrentX, color);
  430. }
  431. sCurrentX++;
  432. if(sError < 0)
  433. {
  434. sError += (4*sCurrentX + 6);
  435. }
  436. else
  437. {
  438. sError += (10 + 4*(sCurrentX - sCurrentY));
  439. sCurrentY--;
  440. }
  441. }
  442. }
  443. /*************************** END OF FILE ***************************/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/143861
推荐阅读
相关标签
  

闽ICP备14008679号