[STM32U5] 【NUCLEO-U5A5ZJ-Q测评】硬件SPI驱动LCD屏

[复制链接]
 楼主| lulugl 发表于 2023-11-12 21:28 | 显示全部楼层 |阅读模式
<
#申请原创# @21小跑堂
【目的】单片机的交互信息,通常是用uart输入串口信息,作为STM32U5A5,他的运行频率为160MHz,而且接口信息丰富,可以用LCD屏来做为输出信息,这样比较直观,再也不用开串口助手来观察了。本文就驱动LCD屏来做为此款开发板试用的第一篇。
【实验的硬件】
  • NUCLEO-U5A5ZJ开发板。

b02a4bf5b30572cd99734670954fbdc3
2、3.5寸SPI串口TFT液晶显示模块LCD触摸屏ILI9488 /6 /1驱动320X480
7d84d0970e66fbbc34914a94f7c50de2
【实验的开发环境】
  • 操作系统:win11。
  • 开发IDE工具:STM32CubeIDE,版本为1.13.2:

99500668123a791b2c8458c63f6348a8
【开发步骤】
1、新建NUCLEO-U5A5ZJ开发板工程,这样开发板可以默认的给我们配轩好了串口、LED等常用的外设。
c8c3b5c5a2df4a7522f84e2ee82b60ef
2、打开spi1,配置spi1的IO为MOSI为PA7,SCK为PA5。
0c8b588440d0d60cbed681a39e39dbac
3、配置开发板上的PF2、PD14、PD15为TFT_DC、TFT_CS、TFT_RST,如下图:
9e3a33af6d0fe8b2d1ffbb917148f29d
4、配置三个IO为上拉,输入为High,如下图所示:
998879095204d5af38e7446488080a6b
5、设置spi为单向发送,传输数据为8bit,分频为4,工作频为40M,同时NSS为软摸拟。
63e520561cbeabf55bc81150aa1474f1
6、配置时钟为160MHz,这样spi的时钟可以达到160M。
a852dc54b8a377d1131ede91d35bbfd4
7、因为ili9488要开启大的空间进行数据传输,所以把栈堆的空间调大一些。
f37e0fbdc5162175f241e89a1b58985b
随后按alt+k生成工程。
【代码】
添加ili9488.h,添加代码如下:
  1. /*
  2. * ili9488.h
  3. *
  4. *  Created on: Dec 14, 2021
  5. *      Author: timagr615
  6. */

  7. #ifndef INC_ILI9488_H_
  8. #define INC_ILI9488_H_

  9. #include <math.h>
  10. #include <stdbool.h>

  11. #include "main.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>

  15. #define RST_A() HAL_GPIO_WritePin(TFT_RST_GPIO_Port,TFT_RST_Pin,GPIO_PIN_RESET)
  16. #define RST_D() HAL_GPIO_WritePin(TFT_RST_GPIO_Port,TFT_RST_Pin,GPIO_PIN_SET)
  17. #define CS_A() HAL_GPIO_WritePin(TFT_CS_GPIO_Port,TFT_CS_Pin,GPIO_PIN_RESET)
  18. #define CS_D() HAL_GPIO_WritePin(TFT_CS_GPIO_Port,TFT_CS_Pin,GPIO_PIN_SET)
  19. #define DC_COMMAND() HAL_GPIO_WritePin(TFT_DC_GPIO_Port,TFT_DC_Pin,GPIO_PIN_RESET)
  20. #define DC_DATA() HAL_GPIO_WritePin(TFT_DC_GPIO_Port,TFT_DC_Pin,GPIO_PIN_SET)

  21. #define ILI9488_TFTWIDTH  320
  22. #define ILI9488_TFTHEIGHT 480
  23. #define ILI9488_PIXEL_COUNT        ILI9488_TFTWIDTH * ILI9488_TFTHEIGHT

  24. #define ILI9488_NOP     0x00
  25. #define ILI9488_SWRESET 0x01
  26. #define ILI9488_RDDID   0x04
  27. #define ILI9488_RDDST   0x09

  28. #define ILI9488_SLPIN   0x10
  29. #define ILI9488_SLPOUT  0x11
  30. #define ILI9488_PTLON   0x12
  31. #define ILI9488_NORON   0x13

  32. #define ILI9488_RDMODE  0x0A
  33. #define ILI9488_RDMADCTL  0x0B
  34. #define ILI9488_RDPIXFMT  0x0C
  35. #define ILI9488_RDIMGFMT  0x0D
  36. #define ILI9488_RDSELFDIAG  0x0F

  37. #define ILI9488_INVOFF  0x20
  38. #define ILI9488_INVON   0x21
  39. #define ILI9488_GAMMASET 0x26
  40. #define ILI9488_DISPOFF 0x28
  41. #define ILI9488_DISPON  0x29

  42. #define ILI9488_CASET   0x2A
  43. #define ILI9488_PASET   0x2B
  44. #define ILI9488_RAMWR   0x2C
  45. #define ILI9488_RAMRD   0x2E

  46. #define ILI9488_PTLAR   0x30
  47. #define ILI9488_MADCTL  0x36
  48. #define ILI9488_PIXFMT  0x3A

  49. #define ILI9488_FRMCTR1 0xB1
  50. #define ILI9488_FRMCTR2 0xB2
  51. #define ILI9488_FRMCTR3 0xB3
  52. #define ILI9488_INVCTR  0xB4
  53. #define ILI9488_DFUNCTR 0xB6

  54. #define ILI9488_PWCTR1  0xC0
  55. #define ILI9488_PWCTR2  0xC1
  56. #define ILI9488_PWCTR3  0xC2
  57. #define ILI9488_PWCTR4  0xC3
  58. #define ILI9488_PWCTR5  0xC4
  59. #define ILI9488_VMCTR1  0xC5
  60. #define ILI9488_VMCTR2  0xC7

  61. #define ILI9488_RDID1   0xDA
  62. #define ILI9488_RDID2   0xDB
  63. #define ILI9488_RDID3   0xDC
  64. #define ILI9488_RDID4   0xDD

  65. #define ILI9488_GMCTRP1 0xE0
  66. #define ILI9488_GMCTRN1 0xE1
  67. /*
  68. #define ILI9488_PWCTR6  0xFC
  69. */

  70. // Color definitions
  71. #define ILI9488_BLACK                              0x0000      /*   0,   0,   0 */
  72. #define ILI9488_NAVY                                0x000F      /*   0,   0, 128 */
  73. #define ILI9488_DARKGREEN                   0x03E0      /*   0, 128,   0 */
  74. #define ILI9488_DARKCYAN                    0x03EF      /*   0, 128, 128 */
  75. #define ILI9488_MAROON                      0x7800      /* 128,   0,   0 */
  76. #define ILI9488_PURPLE                      0x780F      /* 128,   0, 128 */
  77. #define ILI9488_OLIVE                               0x7BE0      /* 128, 128,   0 */
  78. #define ILI9488_LIGHTGREY                   0xC618      /* 192, 192, 192 */
  79. #define ILI9488_DARKGREY                    0x7BEF      /* 128, 128, 128 */
  80. #define ILI9488_BLUE                                0x001F      /*   0,   0, 255 */
  81. #define ILI9488_GREEN                       0x07E0      /*   0, 255,   0 */
  82. #define ILI9488_CYAN                                0x07FF      /*   0, 255, 255 */
  83. #define ILI9488_RED                                 0xF800      /* 255,   0,   0 */
  84. #define ILI9488_MAGENTA                     0xF81F      /* 255,   0, 255 */
  85. #define ILI9488_YELLOW                      0xFFE0      /* 255, 255,   0 */
  86. #define ILI9488_WHITE                               0xFFFF      /* 255, 255, 255 */
  87. #define ILI9488_ORANGE                      0xFD20      /* 255, 165,   0 */
  88. #define ILI9488_GREENYELLOW         0xAFE5      /* 173, 255,  47 */
  89. #define ILI9488_PINK                                0xF81F

  90. #define        TFT9341_BLACK   0x0000
  91. #define        TFT9341_BLUE    0x001F
  92. #define        TFT9341_RED     0xF800
  93. #define        TFT9341_GREEN   0x07E0
  94. #define TFT9341_CYAN    0x07FF
  95. #define TFT9341_MAGENTA 0xF81F
  96. #define TFT9341_YELLOW  0xFFE0
  97. #define TFT9341_WHITE   0xFFFF
  98. #define MADCTL_MY  0x80
  99. #define MADCTL_MX  0x40
  100. #define MADCTL_MV  0x20
  101. #define MADCTL_ML  0x10
  102. #define MADCTL_RGB 0x00
  103. #define MADCTL_BGR 0x08
  104. #define MADCTL_MH  0x04


  105. #define swap(a, b) { int16_t t = a; a = b; b = t; }
  106. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  107. #define min(a,b) (((a)<(b))?(a):(b))

  108. //***** Functions prototypes *****//
  109. //1. Write Command to LCD
  110. void ILI9341_SendCommand(uint8_t com);
  111. //2. Write data to LCD
  112. void ILI9341_SendData(uint8_t data);
  113. //2.2 Write multiple/DMA
  114. void ILI9488_SendData_Multi(uint8_t *buff, size_t buff_size);

  115. //void ILI9488_Init(SPI_HandleTypeDef *spiLcdHandle, GPIO_TypeDef *csPORT, uint16_t csPIN, GPIO_TypeDef *dcPORT, uint16_t dcPIN, GPIO_TypeDef *resetPORT, uint16_t resetPIN);
  116. void ILI9488_Init();
  117. void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
  118. void setScrollArea(uint16_t topFixedArea, uint16_t bottomFixedArea);
  119. void scroll(uint16_t pixels);
  120. void pushColor(uint16_t color);
  121. void pushColors(uint16_t *data, uint8_t len, uint8_t first);
  122. void drawImage(const uint8_t* img, uint16_t x, uint16_t y, uint16_t w, uint16_t h);
  123. void fillScreen(uint16_t color);

  124. void drawPixel(int16_t x, int16_t y, uint16_t color);
  125. void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
  126. void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
  127. void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,uint16_t color);
  128. void writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,uint16_t color);
  129. void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);


  130. void setRotation(uint8_t r);
  131. void invertDisplay(uint8_t  i);
  132. uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
  133. void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size);
  134. void ILI9488_printText(char text[], int16_t x, int16_t y, uint16_t color, uint16_t bg, uint8_t size);

  135. void write16BitColor(uint16_t color);

  136. void testLines(uint8_t color);



  137. #endif /* INC_ILI9488_H_ */

2、添加ili9488.c代码如下:
  1. /*
  2. * ili9488.c
  3. *
  4. *  Created on: Nov 12, 2023
  5. *      Author: liujianhua
  6. */

  7. #include "ili9488.h"
  8. #include "FONT.H"
  9. extern SPI_HandleTypeDef hspi1;
  10. extern UART_HandleTypeDef huart1;

  11. static uint8_t rotationNum=1;
  12. static bool _cp437    = false;
  13. static SPI_HandleTypeDef lcdSPIhandle;
  14. //Chip Select pin
  15. static GPIO_TypeDef  *tftCS_GPIO;
  16. static uint16_t tftCS_PIN;
  17. //Data Command pin
  18. static GPIO_TypeDef  *tftDC_GPIO;
  19. static uint16_t tftDC_PIN;
  20. //Reset pin
  21. static GPIO_TypeDef  *tftRESET_GPIO;
  22. static uint16_t tftRESET_PIN;
  23. //uint8_t frm_buf[65536] = {0};
  24. uint16_t width;
  25. uint16_t height;

  26. static //Text simple font array (You can your own font)
  27. const unsigned char font1[] = {
  28.         0x00, 0x00, 0x00, 0x00, 0x00,
  29.         0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
  30.         0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
  31.         0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
  32.         0x18, 0x3C, 0x7E, 0x3C, 0x18,
  33.         0x1C, 0x57, 0x7D, 0x57, 0x1C,
  34.         0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
  35.         0x00, 0x18, 0x3C, 0x18, 0x00,
  36.         0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
  37.         0x00, 0x18, 0x24, 0x18, 0x00,
  38.         0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
  39.         0x30, 0x48, 0x3A, 0x06, 0x0E,
  40.         0x26, 0x29, 0x79, 0x29, 0x26,
  41.         0x40, 0x7F, 0x05, 0x05, 0x07,
  42.         0x40, 0x7F, 0x05, 0x25, 0x3F,
  43.         0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
  44.         0x7F, 0x3E, 0x1C, 0x1C, 0x08,
  45.         0x08, 0x1C, 0x1C, 0x3E, 0x7F,
  46.         0x14, 0x22, 0x7F, 0x22, 0x14,
  47.         0x5F, 0x5F, 0x00, 0x5F, 0x5F,
  48.         0x06, 0x09, 0x7F, 0x01, 0x7F,
  49.         0x00, 0x66, 0x89, 0x95, 0x6A,
  50.         0x60, 0x60, 0x60, 0x60, 0x60,
  51.         0x94, 0xA2, 0xFF, 0xA2, 0x94,
  52.         0x08, 0x04, 0x7E, 0x04, 0x08,
  53.         0x10, 0x20, 0x7E, 0x20, 0x10,
  54.         0x08, 0x08, 0x2A, 0x1C, 0x08,
  55.         0x08, 0x1C, 0x2A, 0x08, 0x08,
  56.         0x1E, 0x10, 0x10, 0x10, 0x10,
  57.         0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
  58.         0x30, 0x38, 0x3E, 0x38, 0x30,
  59.         0x06, 0x0E, 0x3E, 0x0E, 0x06,
  60.         0x00, 0x00, 0x00, 0x00, 0x00,
  61.         0x00, 0x00, 0x5F, 0x00, 0x00,
  62.         0x00, 0x07, 0x00, 0x07, 0x00,
  63.         0x14, 0x7F, 0x14, 0x7F, 0x14,
  64.         0x24, 0x2A, 0x7F, 0x2A, 0x12,
  65.         0x23, 0x13, 0x08, 0x64, 0x62,
  66.         0x36, 0x49, 0x56, 0x20, 0x50,
  67.         0x00, 0x08, 0x07, 0x03, 0x00,
  68.         0x00, 0x1C, 0x22, 0x41, 0x00,
  69.         0x00, 0x41, 0x22, 0x1C, 0x00,
  70.         0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
  71.         0x08, 0x08, 0x3E, 0x08, 0x08,
  72.         0x00, 0x80, 0x70, 0x30, 0x00,
  73.         0x08, 0x08, 0x08, 0x08, 0x08,
  74.         0x00, 0x00, 0x60, 0x60, 0x00,
  75.         0x20, 0x10, 0x08, 0x04, 0x02,
  76.         0x3E, 0x51, 0x49, 0x45, 0x3E,
  77.         0x00, 0x42, 0x7F, 0x40, 0x00,
  78.         0x72, 0x49, 0x49, 0x49, 0x46,
  79.         0x21, 0x41, 0x49, 0x4D, 0x33,
  80.         0x18, 0x14, 0x12, 0x7F, 0x10,
  81.         0x27, 0x45, 0x45, 0x45, 0x39,
  82.         0x3C, 0x4A, 0x49, 0x49, 0x31,
  83.         0x41, 0x21, 0x11, 0x09, 0x07,
  84.         0x36, 0x49, 0x49, 0x49, 0x36,
  85.         0x46, 0x49, 0x49, 0x29, 0x1E,
  86.         0x00, 0x00, 0x14, 0x00, 0x00,
  87.         0x00, 0x40, 0x34, 0x00, 0x00,
  88.         0x00, 0x08, 0x14, 0x22, 0x41,
  89.         0x14, 0x14, 0x14, 0x14, 0x14,
  90.         0x00, 0x41, 0x22, 0x14, 0x08,
  91.         0x02, 0x01, 0x59, 0x09, 0x06,
  92.         0x3E, 0x41, 0x5D, 0x59, 0x4E,
  93.         0x7C, 0x12, 0x11, 0x12, 0x7C,
  94.         0x7F, 0x49, 0x49, 0x49, 0x36,
  95.         0x3E, 0x41, 0x41, 0x41, 0x22,
  96.         0x7F, 0x41, 0x41, 0x41, 0x3E,
  97.         0x7F, 0x49, 0x49, 0x49, 0x41,
  98.         0x7F, 0x09, 0x09, 0x09, 0x01,
  99.         0x3E, 0x41, 0x41, 0x51, 0x73,
  100.         0x7F, 0x08, 0x08, 0x08, 0x7F,
  101.         0x00, 0x41, 0x7F, 0x41, 0x00,
  102.         0x20, 0x40, 0x41, 0x3F, 0x01,
  103.         0x7F, 0x08, 0x14, 0x22, 0x41,
  104.         0x7F, 0x40, 0x40, 0x40, 0x40,
  105.         0x7F, 0x02, 0x1C, 0x02, 0x7F,
  106.         0x7F, 0x04, 0x08, 0x10, 0x7F,
  107.         0x3E, 0x41, 0x41, 0x41, 0x3E,
  108.         0x7F, 0x09, 0x09, 0x09, 0x06,
  109.         0x3E, 0x41, 0x51, 0x21, 0x5E,
  110.         0x7F, 0x09, 0x19, 0x29, 0x46,
  111.         0x26, 0x49, 0x49, 0x49, 0x32,
  112.         0x03, 0x01, 0x7F, 0x01, 0x03,
  113.         0x3F, 0x40, 0x40, 0x40, 0x3F,
  114.         0x1F, 0x20, 0x40, 0x20, 0x1F,
  115.         0x3F, 0x40, 0x38, 0x40, 0x3F,
  116.         0x63, 0x14, 0x08, 0x14, 0x63,
  117.         0x03, 0x04, 0x78, 0x04, 0x03,
  118.         0x61, 0x59, 0x49, 0x4D, 0x43,
  119.         0x00, 0x7F, 0x41, 0x41, 0x41,
  120.         0x02, 0x04, 0x08, 0x10, 0x20,
  121.         0x00, 0x41, 0x41, 0x41, 0x7F,
  122.         0x04, 0x02, 0x01, 0x02, 0x04,
  123.         0x40, 0x40, 0x40, 0x40, 0x40,
  124.         0x00, 0x03, 0x07, 0x08, 0x00,
  125.         0x20, 0x54, 0x54, 0x78, 0x40,
  126.         0x7F, 0x28, 0x44, 0x44, 0x38,
  127.         0x38, 0x44, 0x44, 0x44, 0x28,
  128.         0x38, 0x44, 0x44, 0x28, 0x7F,
  129.         0x38, 0x54, 0x54, 0x54, 0x18,
  130.         0x00, 0x08, 0x7E, 0x09, 0x02,
  131.         0x18, 0xA4, 0xA4, 0x9C, 0x78,
  132.         0x7F, 0x08, 0x04, 0x04, 0x78,
  133.         0x00, 0x44, 0x7D, 0x40, 0x00,
  134.         0x20, 0x40, 0x40, 0x3D, 0x00,
  135.         0x7F, 0x10, 0x28, 0x44, 0x00,
  136.         0x00, 0x41, 0x7F, 0x40, 0x00,
  137.         0x7C, 0x04, 0x78, 0x04, 0x78,
  138.         0x7C, 0x08, 0x04, 0x04, 0x78,
  139.         0x38, 0x44, 0x44, 0x44, 0x38,
  140.         0xFC, 0x18, 0x24, 0x24, 0x18,
  141.         0x18, 0x24, 0x24, 0x18, 0xFC,
  142.         0x7C, 0x08, 0x04, 0x04, 0x08,
  143.         0x48, 0x54, 0x54, 0x54, 0x24,
  144.         0x04, 0x04, 0x3F, 0x44, 0x24,
  145.         0x3C, 0x40, 0x40, 0x20, 0x7C,
  146.         0x1C, 0x20, 0x40, 0x20, 0x1C,
  147.         0x3C, 0x40, 0x30, 0x40, 0x3C,
  148.         0x44, 0x28, 0x10, 0x28, 0x44,
  149.         0x4C, 0x90, 0x90, 0x90, 0x7C,
  150.         0x44, 0x64, 0x54, 0x4C, 0x44,
  151.         0x00, 0x08, 0x36, 0x41, 0x00,
  152.         0x00, 0x00, 0x77, 0x00, 0x00,
  153.         0x00, 0x41, 0x36, 0x08, 0x00,
  154.         0x02, 0x01, 0x02, 0x04, 0x02,
  155.         0x3C, 0x26, 0x23, 0x26, 0x3C,
  156.         0x1E, 0xA1, 0xA1, 0x61, 0x12,
  157.         0x3A, 0x40, 0x40, 0x20, 0x7A,
  158.         0x38, 0x54, 0x54, 0x55, 0x59,
  159.         0x21, 0x55, 0x55, 0x79, 0x41,
  160.         0x22, 0x54, 0x54, 0x78, 0x42,
  161.         0x21, 0x55, 0x54, 0x78, 0x40,
  162.         0x20, 0x54, 0x55, 0x79, 0x40,
  163.         0x0C, 0x1E, 0x52, 0x72, 0x12,
  164.         0x39, 0x55, 0x55, 0x55, 0x59,
  165.         0x39, 0x54, 0x54, 0x54, 0x59,
  166.         0x39, 0x55, 0x54, 0x54, 0x58,
  167.         0x00, 0x00, 0x45, 0x7C, 0x41,
  168.         0x00, 0x02, 0x45, 0x7D, 0x42,
  169.         0x00, 0x01, 0x45, 0x7C, 0x40,
  170.         0x7D, 0x12, 0x11, 0x12, 0x7D,
  171.         0xF0, 0x28, 0x25, 0x28, 0xF0,
  172.         0x7C, 0x54, 0x55, 0x45, 0x00,
  173.         0x20, 0x54, 0x54, 0x7C, 0x54,
  174.         0x7C, 0x0A, 0x09, 0x7F, 0x49,
  175.         0x32, 0x49, 0x49, 0x49, 0x32,
  176.         0x3A, 0x44, 0x44, 0x44, 0x3A,
  177.         0x32, 0x4A, 0x48, 0x48, 0x30,
  178.         0x3A, 0x41, 0x41, 0x21, 0x7A,
  179.         0x3A, 0x42, 0x40, 0x20, 0x78,
  180.         0x00, 0x9D, 0xA0, 0xA0, 0x7D,
  181.         0x3D, 0x42, 0x42, 0x42, 0x3D,
  182.         0x3D, 0x40, 0x40, 0x40, 0x3D,
  183.         0x3C, 0x24, 0xFF, 0x24, 0x24,
  184.         0x48, 0x7E, 0x49, 0x43, 0x66,
  185.         0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
  186.         0xFF, 0x09, 0x29, 0xF6, 0x20,
  187.         0xC0, 0x88, 0x7E, 0x09, 0x03,
  188.         0x20, 0x54, 0x54, 0x79, 0x41,
  189.         0x00, 0x00, 0x44, 0x7D, 0x41,
  190.         0x30, 0x48, 0x48, 0x4A, 0x32,
  191.         0x38, 0x40, 0x40, 0x22, 0x7A,
  192.         0x00, 0x7A, 0x0A, 0x0A, 0x72,
  193.         0x7D, 0x0D, 0x19, 0x31, 0x7D,
  194.         0x26, 0x29, 0x29, 0x2F, 0x28,
  195.         0x26, 0x29, 0x29, 0x29, 0x26,
  196.         0x30, 0x48, 0x4D, 0x40, 0x20,
  197.         0x38, 0x08, 0x08, 0x08, 0x08,
  198.         0x08, 0x08, 0x08, 0x08, 0x38,
  199.         0x2F, 0x10, 0xC8, 0xAC, 0xBA,
  200.         0x2F, 0x10, 0x28, 0x34, 0xFA,
  201.         0x00, 0x00, 0x7B, 0x00, 0x00,
  202.         0x08, 0x14, 0x2A, 0x14, 0x22,
  203.         0x22, 0x14, 0x2A, 0x14, 0x08,
  204.         0x55, 0x00, 0x55, 0x00, 0x55,
  205.         0xAA, 0x55, 0xAA, 0x55, 0xAA,
  206.         0xFF, 0x55, 0xFF, 0x55, 0xFF,
  207.         0x00, 0x00, 0x00, 0xFF, 0x00,
  208.         0x10, 0x10, 0x10, 0xFF, 0x00,
  209.         0x14, 0x14, 0x14, 0xFF, 0x00,
  210.         0x10, 0x10, 0xFF, 0x00, 0xFF,
  211.         0x10, 0x10, 0xF0, 0x10, 0xF0,
  212.         0x14, 0x14, 0x14, 0xFC, 0x00,
  213.         0x14, 0x14, 0xF7, 0x00, 0xFF,
  214.         0x00, 0x00, 0xFF, 0x00, 0xFF,
  215.         0x14, 0x14, 0xF4, 0x04, 0xFC,
  216.         0x14, 0x14, 0x17, 0x10, 0x1F,
  217.         0x10, 0x10, 0x1F, 0x10, 0x1F,
  218.         0x14, 0x14, 0x14, 0x1F, 0x00,
  219.         0x10, 0x10, 0x10, 0xF0, 0x00,
  220.         0x00, 0x00, 0x00, 0x1F, 0x10,
  221.         0x10, 0x10, 0x10, 0x1F, 0x10,
  222.         0x10, 0x10, 0x10, 0xF0, 0x10,
  223.         0x00, 0x00, 0x00, 0xFF, 0x10,
  224.         0x10, 0x10, 0x10, 0x10, 0x10,
  225.         0x10, 0x10, 0x10, 0xFF, 0x10,
  226.         0x00, 0x00, 0x00, 0xFF, 0x14,
  227.         0x00, 0x00, 0xFF, 0x00, 0xFF,
  228.         0x00, 0x00, 0x1F, 0x10, 0x17,
  229.         0x00, 0x00, 0xFC, 0x04, 0xF4,
  230.         0x14, 0x14, 0x17, 0x10, 0x17,
  231.         0x14, 0x14, 0xF4, 0x04, 0xF4,
  232.         0x00, 0x00, 0xFF, 0x00, 0xF7,
  233.         0x14, 0x14, 0x14, 0x14, 0x14,
  234.         0x14, 0x14, 0xF7, 0x00, 0xF7,
  235.         0x14, 0x14, 0x14, 0x17, 0x14,
  236.         0x10, 0x10, 0x1F, 0x10, 0x1F,
  237.         0x14, 0x14, 0x14, 0xF4, 0x14,
  238.         0x10, 0x10, 0xF0, 0x10, 0xF0,
  239.         0x00, 0x00, 0x1F, 0x10, 0x1F,
  240.         0x00, 0x00, 0x00, 0x1F, 0x14,
  241.         0x00, 0x00, 0x00, 0xFC, 0x14,
  242.         0x00, 0x00, 0xF0, 0x10, 0xF0,
  243.         0x10, 0x10, 0xFF, 0x10, 0xFF,
  244.         0x14, 0x14, 0x14, 0xFF, 0x14,
  245.         0x10, 0x10, 0x10, 0x1F, 0x00,
  246.         0x00, 0x00, 0x00, 0xF0, 0x10,
  247.         0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  248.         0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  249.         0xFF, 0xFF, 0xFF, 0x00, 0x00,
  250.         0x00, 0x00, 0x00, 0xFF, 0xFF,
  251.         0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
  252.         0x38, 0x44, 0x44, 0x38, 0x44,
  253.         0xFC, 0x4A, 0x4A, 0x4A, 0x34,
  254.         0x7E, 0x02, 0x02, 0x06, 0x06,
  255.         0x02, 0x7E, 0x02, 0x7E, 0x02,
  256.         0x63, 0x55, 0x49, 0x41, 0x63,
  257.         0x38, 0x44, 0x44, 0x3C, 0x04,
  258.         0x40, 0x7E, 0x20, 0x1E, 0x20,
  259.         0x06, 0x02, 0x7E, 0x02, 0x02,
  260.         0x99, 0xA5, 0xE7, 0xA5, 0x99,
  261.         0x1C, 0x2A, 0x49, 0x2A, 0x1C,
  262.         0x4C, 0x72, 0x01, 0x72, 0x4C,
  263.         0x30, 0x4A, 0x4D, 0x4D, 0x30,
  264.         0x30, 0x48, 0x78, 0x48, 0x30,
  265.         0xBC, 0x62, 0x5A, 0x46, 0x3D,
  266.         0x3E, 0x49, 0x49, 0x49, 0x00,
  267.         0x7E, 0x01, 0x01, 0x01, 0x7E,
  268.         0x2A, 0x2A, 0x2A, 0x2A, 0x2A,
  269.         0x44, 0x44, 0x5F, 0x44, 0x44,
  270.         0x40, 0x51, 0x4A, 0x44, 0x40,
  271.         0x40, 0x44, 0x4A, 0x51, 0x40,
  272.         0x00, 0x00, 0xFF, 0x01, 0x03,
  273.         0xE0, 0x80, 0xFF, 0x00, 0x00,
  274.         0x08, 0x08, 0x6B, 0x6B, 0x08,
  275.         0x36, 0x12, 0x36, 0x24, 0x36,
  276.         0x06, 0x0F, 0x09, 0x0F, 0x06,
  277.         0x00, 0x00, 0x18, 0x18, 0x00,
  278.         0x00, 0x00, 0x10, 0x10, 0x00,
  279.         0x30, 0x40, 0xFF, 0x01, 0x01,
  280.         0x00, 0x1F, 0x01, 0x01, 0x1E,
  281.         0x00, 0x19, 0x1D, 0x17, 0x12,
  282.         0x00, 0x3C, 0x3C, 0x3C, 0x3C,
  283.         0x00, 0x00, 0x00, 0x00, 0x00
  284. };
  285. //***** Functions prototypes *****//


  286. //1. Write Command to LCD
  287. void ILI9488_SendCommand(uint8_t com)
  288. {
  289.         //*(__IO uint8_t *)(0x60000000) = com;
  290.         uint8_t tmpCmd = com;

  291.         DC_COMMAND();

  292.         CS_A();
  293.         //Write byte using SPI
  294.         HAL_SPI_Transmit(&hspi1, &tmpCmd, 1, 1);

  295.         CS_D();

  296. }

  297. //2. Write data to LCD
  298. void ILI9488_SendData(uint8_t data)
  299. {
  300.         uint8_t tmpCmd = data;
  301.         DC_DATA();
  302.         CS_A();
  303.         HAL_SPI_Transmit(&hspi1, &tmpCmd, 1, 1);
  304.         CS_D();

  305. }
  306. //2.2 Write multiple/DMA
  307. void ILI9488_SendData_Multi(uint8_t *buff, size_t buff_size){
  308.         DC_DATA();
  309.         CS_A();
  310.         while (buff_size > 0){
  311.                 uint16_t chunk_size = buff_size > 32768 ? 32768 : buff_size;
  312.                 HAL_SPI_Transmit(&hspi1, buff, chunk_size, HAL_MAX_DELAY);
  313.                 buff += chunk_size;
  314.                 buff_size -= chunk_size;
  315.         }
  316.         CS_D();
  317. }


  318. void ILI9488_Init()
  319. {

  320.         CS_D();
  321.         RST_A();
  322.         HAL_Delay(10);
  323.         RST_D();
  324.          width=ILI9488_TFTWIDTH;
  325.          height=ILI9488_TFTHEIGHT;
  326.          //CS_A();
  327.          ILI9488_SendCommand(0xE0);
  328.          ILI9488_SendData(0x00);
  329.          ILI9488_SendData(0x03);
  330.          ILI9488_SendData(0x09);
  331.          ILI9488_SendData(0x08);
  332.          ILI9488_SendData(0x16);
  333.          ILI9488_SendData(0x0A);
  334.          ILI9488_SendData(0x3F);
  335.          ILI9488_SendData(0x78);
  336.          ILI9488_SendData(0x4C);
  337.          ILI9488_SendData(0x09);
  338.          ILI9488_SendData(0x0A);
  339.          ILI9488_SendData(0x08);
  340.          ILI9488_SendData(0x16);
  341.          ILI9488_SendData(0x1A);
  342.          ILI9488_SendData(0x0F);

  343.                  ILI9488_SendCommand(0XE1);
  344.                  ILI9488_SendData(0x00);
  345.                  ILI9488_SendData(0x16);
  346.                  ILI9488_SendData(0x19);
  347.                  ILI9488_SendData(0x03);
  348.                  ILI9488_SendData(0x0F);
  349.                  ILI9488_SendData(0x05);
  350.                  ILI9488_SendData(0x32);
  351.                  ILI9488_SendData(0x45);
  352.                  ILI9488_SendData(0x46);
  353.                  ILI9488_SendData(0x04);
  354.                  ILI9488_SendData(0x0E);
  355.                  ILI9488_SendData(0x0D);
  356.                  ILI9488_SendData(0x35);
  357.                  ILI9488_SendData(0x37);
  358.                  ILI9488_SendData(0x0F);

  359.                  ILI9488_SendCommand(0XC0);      //Power Control 1
  360.                  ILI9488_SendData(0x17);    //Vreg1out
  361.                  ILI9488_SendData(0x15);    //Verg2out

  362.                  ILI9488_SendCommand(0xC1);      //Power Control 2
  363.                  ILI9488_SendData(0x41);    //VGH,VGL

  364.                  ILI9488_SendCommand(0xC5);      //Power Control 3
  365.                  ILI9488_SendData(0x00);
  366.                  ILI9488_SendData(0x12);    //Vcom
  367.                  ILI9488_SendData(0x80);

  368.                  ILI9488_SendCommand(0x36);      //Memory Access
  369.                  ILI9488_SendData(0x48);

  370.                  ILI9488_SendCommand(0x3A);      // Interface Pixel Format
  371.                  ILI9488_SendData(0x66);           //18 bit

  372.                  ILI9488_SendCommand(0XB0);      // Interface Mode Control
  373.                  ILI9488_SendData(0x80);                              //SDO NOT USE

  374.                  ILI9488_SendCommand(0xB1);      //Frame rate
  375.                  ILI9488_SendData(0xA0);    //60Hz

  376.                  ILI9488_SendCommand(0xB4);      //Display Inversion Control
  377.                  ILI9488_SendData(0x02);    //2-dot

  378.                  ILI9488_SendCommand(0XB6); //Display Function Control  RGB/MCU Interface Control

  379.                  ILI9488_SendData(0x02);    //MCU
  380.                  ILI9488_SendData(0x02);    //Source,Gate scan dieection

  381.                  ILI9488_SendCommand(0XE9);      // Set Image Functio
  382.                  ILI9488_SendData(0x00);    // Disable 24 bit data

  383.                  ILI9488_SendCommand(0xF7);      // Adjust Control
  384.                  ILI9488_SendData(0xA9);
  385.                  ILI9488_SendData(0x51);
  386.                  ILI9488_SendData(0x2C);
  387.                  ILI9488_SendData(0x82);    // D7 stream, loose

  388.                  ILI9488_SendCommand(ILI9488_SLPOUT);    //Exit Sleep

  389.                  HAL_Delay(120);

  390.                  ILI9488_SendCommand(ILI9488_DISPON);    //Display on

  391. }

  392. void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
  393. {
  394.         /*ILI9488_SendCommand(ILI9488_CASET); // Column addr set
  395.         ILI9488_SendData(x0 >> 8);
  396.         ILI9488_SendData(x0 & 0xFF);     // XSTART
  397.         ILI9488_SendData(x1 >> 8);
  398.         ILI9488_SendData(x1 & 0xFF);     // XEND
  399.         ILI9488_SendCommand(ILI9488_PASET); // Row addr set
  400.         ILI9488_SendData(y0 >> 8);
  401.         ILI9488_SendData(y0 & 0xff);     // YSTART
  402.         ILI9488_SendData(y1 >> 8);
  403.         ILI9488_SendData(y1 & 0xff);     // YEND
  404.         ILI9488_SendCommand(ILI9488_RAMWR); // write to RAM*/
  405.         ILI9488_SendCommand(ILI9488_CASET); // Column addr set
  406.                 {
  407.                 uint8_t data[] = {(x0 >> 8) & 0xFF, x0 & 0xFF, (x1 >> 8) & 0xFF, x1 & 0xFF};
  408.                 ILI9488_SendData_Multi(data, sizeof(data));
  409.                 }
  410.                 ILI9488_SendCommand(ILI9488_PASET);
  411.                 {
  412.                 uint8_t data[] = {(y0 >> 8) & 0xFF, y0 & 0xFF, (y1 >> 8) & 0xFF, y1 & 0xFF};
  413.                 ILI9488_SendData_Multi(data, sizeof(data));
  414.                 }
  415.                 ILI9488_SendCommand(ILI9488_RAMWR); // write to RAM*/
  416. }

  417. void setScrollArea(uint16_t topFixedArea, uint16_t bottomFixedArea)
  418. {
  419.         ILI9488_SendCommand(0x33); // Vertical scroll definition
  420.         ILI9488_SendData(topFixedArea >> 8);
  421.         ILI9488_SendData(topFixedArea);
  422.         ILI9488_SendData((height - topFixedArea - bottomFixedArea) >> 8);
  423.         ILI9488_SendData(height - topFixedArea - bottomFixedArea);
  424.         ILI9488_SendData(bottomFixedArea >> 8);
  425.         ILI9488_SendData(bottomFixedArea);
  426. }

  427. void scroll(uint16_t pixels)
  428. {
  429.         ILI9488_SendCommand(0x37); // Vertical scrolling start address
  430.         ILI9488_SendData(pixels >> 8);
  431.         ILI9488_SendData(pixels);
  432. }
  433. void pushColor(uint16_t color)
  434. {
  435.         HAL_GPIO_WritePin(tftDC_GPIO, tftDC_PIN, GPIO_PIN_SET);
  436.         HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_RESET);
  437.         write16BitColor(color);
  438.         HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_SET);

  439. }

  440. void pushColors(uint16_t *data, uint8_t len, uint8_t first)
  441. {
  442.         uint16_t color;
  443.         uint8_t buff[len * 3 + 1];
  444.         uint16_t count = 0;
  445.         uint8_t lencount = len;
  446.         HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_RESET);

  447.         if (first == 1) {
  448.                 HAL_GPIO_WritePin(tftDC_GPIO, tftDC_PIN, GPIO_PIN_SET);
  449.         }
  450.         while (lencount--)
  451.         {
  452.                 color = *data++;
  453.                 buff[count] = (((color & 0xF800) >> 11) * 255) / 31;
  454.                 count++;
  455.                 buff[count] = (((color & 0x07E0) >> 5) * 255) / 63;
  456.                 count++;
  457.                 buff[count] = ((color & 0x001F) * 255) / 31;
  458.                 count++;
  459.         }
  460.         HAL_SPI_Transmit(&lcdSPIhandle, buff, len * 3, 100);
  461.         HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_SET);
  462. }

  463. void drawImage(const uint8_t* img, uint16_t x, uint16_t y, uint16_t w, uint16_t h)
  464. {

  465.         if ((x >= width) || (y >= height))
  466.                 return;
  467.         if ((x + w - 1) >= width)
  468.                 w = width - x;
  469.         if ((y + h - 1) >= height)
  470.                 h = height - y;
  471.         setAddrWindow(x, y, x + w - 1, y + h - 1);
  472.         DC_DATA();
  473.         CS_A();
  474.         //HAL_GPIO_WritePin(tftDC_GPIO, tftDC_PIN, GPIO_PIN_SET);
  475.         //HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_RESET);

  476.         uint8_t linebuff[w * 3 + 1];
  477.         uint32_t count = 0;
  478.         for (uint16_t i = 0; i < h; i++) {
  479.                 uint16_t pixcount = 0;
  480.                 for (uint16_t o = 0; o < w; o++) {
  481.                         uint8_t b1 = img[count];
  482.                         count++;
  483.                         uint8_t b2 = img[count];
  484.                         count++;
  485.                         uint16_t color = b1 << 8 | b2;
  486.                         linebuff[pixcount] = (((color & 0xF800) >> 11) * 255)
  487.                                         / 31;
  488.                         pixcount++;
  489.                         linebuff[pixcount] = (((color & 0x07E0) >> 5) * 255)
  490.                                         / 63;
  491.                         pixcount++;
  492.                         linebuff[pixcount] = ((color & 0x001F) * 255) / 31;
  493.                         pixcount++;
  494.                 }
  495.                 HAL_SPI_Transmit(&lcdSPIhandle, linebuff, w * 3, 100);

  496.         }
  497.         CS_D();
  498.         //HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_SET);
  499. }


  500. void drawPixel(int16_t x, int16_t y, uint16_t color)
  501. {
  502.         if ((x < 0) || (x >= width) || (y < 0) || (y >= height))
  503.                 return;

  504.         setAddrWindow(x, y, x + 1, y + 1);
  505.         DC_DATA();
  506.         CS_A();

  507.         write16BitColor(color);
  508.         CS_D();

  509. }

  510. void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
  511. {

  512.         if ((x >= width) || (y >= height))
  513.                 return;

  514.         if ((y + h - 1) >= height)
  515.                 h = height - y;

  516.         setAddrWindow(x, y, x, y + h - 1);
  517.         DC_DATA();
  518.         CS_A();
  519.         //HAL_GPIO_WritePin(tftDC_GPIO, tftDC_PIN, GPIO_PIN_SET);
  520.         //HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_RESET);

  521.         while (h--) {
  522.                 write16BitColor(color);
  523.         }
  524.         CS_D();
  525.         //HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_SET);

  526. }

  527. void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
  528. {

  529.         if ((x >= width) || (y >= height))
  530.                 return;
  531.         if ((x + w - 1) >= width)
  532.                 w = width - x;
  533.         setAddrWindow(x, y, x + w - 1, y);
  534.         DC_DATA();
  535.         CS_A();
  536.         //HAL_GPIO_WritePin(tftDC_GPIO, tftDC_PIN, GPIO_PIN_SET);
  537.         //HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_RESET);
  538.         while (w--)
  539.         {
  540.                 write16BitColor(color);
  541.         }
  542.         CS_D();
  543.         //HAL_GPIO_WritePin(tftCS_GPIO, tftCS_PIN, GPIO_PIN_SET);
  544. }

  545. void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
  546. {
  547.          if (x0 == x1) {
  548.             if (y0 > y1)
  549.               swap(y0, y1);
  550.             drawFastVLine(x0, y0, y1 - y0 + 1, color);
  551.           } else if (y0 == y1) {
  552.             if (x0 > x1)
  553.               swap(x0, x1);
  554.             drawFastHLine(x0, y0, x1 - x0 + 1, color);
  555.           } else {

  556.             writeLine(x0, y0, x1, y1, color);

  557.           }

  558. }

  559. void writeLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color) {

  560.   int16_t steep = abs(y1 - y0) > abs(x1 - x0);
  561.   if (steep) {
  562.     swap(x0, y0);
  563.     swap(x1, y1);
  564.   }

  565.   if (x0 > x1) {
  566.           swap(x0, x1);
  567.           swap(y0, y1);
  568.   }

  569.   int16_t dx, dy;
  570.   dx = x1 - x0;
  571.   dy = abs(y1 - y0);

  572.   int16_t err = dx / 2;
  573.   int16_t ystep;

  574.   if (y0 < y1) {
  575.     ystep = 1;
  576.   } else {
  577.     ystep = -1;
  578.   }

  579.   for (; x0 <= x1; x0++) {
  580.     if (steep) {
  581.       drawPixel(y0, x0, color);
  582.     } else {
  583.       drawPixel(x0, y0, color);
  584.     }
  585.     err -= dy;
  586.     if (err < 0) {
  587.       y0 += ystep;
  588.       err += dx;
  589.     }
  590.   }
  591. }
  592. //6. Fill the entire screen with a background color

  593. void fillScreen(uint16_t color)
  594. {
  595.         fillRect(0, 0,  width, height, color);
  596. }

  597. void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
  598. {

  599.         uint32_t i, n, cnt, buf_size;
  600.         if ((x >= width) || (y >= height))
  601.                 return;
  602.         if ((x + w - 1) >= width)
  603.                 w = width - x;
  604.         if ((y + h - 1) >= height)
  605.                 h = height - y;
  606.         setAddrWindow(x, y, x + w - 1, y + h - 1);
  607.         uint8_t r = (color & 0xF800) >> 11;
  608.         uint8_t g = (color & 0x07E0) >> 5;
  609.         uint8_t b = color & 0x001F;

  610.         r = (r * 255) / 31;
  611.         g = (g * 255) / 63;
  612.         b = (b * 255) / 31;

  613.         n = w*h*3;
  614.         if (n <= 65535){
  615.                 cnt = 1;
  616.                 buf_size = n;
  617.         }
  618.         else {
  619.                 cnt = n/3;
  620.                 buf_size = 3;
  621.                 uint8_t min_cnt = n/65535+1;
  622.                 for (i=min_cnt; i < n/3; i++){
  623.                         if(n%i == 0){
  624.                                 cnt = i;
  625.                                 buf_size = n/i;
  626.                                 break;
  627.                         }
  628.                 }
  629.         }
  630.         uint8_t frm_buf[buf_size];
  631.         for (i=0; i < buf_size/3; i++)
  632.         {
  633.                 frm_buf[i*3] = r;
  634.                 frm_buf[i*3+1] = g;
  635.                 frm_buf[i*3+2] = b;
  636.         }
  637.         DC_DATA();
  638.         CS_A();
  639.                 while(cnt>0)
  640.                 {
  641.                         HAL_SPI_Transmit(&hspi1, frm_buf, buf_size, HAL_MAX_DELAY);

  642.                         cnt -= 1;
  643.                 }
  644.                 CS_D();

  645. }


  646. void setRotation(uint8_t r)
  647. {

  648.         ILI9488_SendCommand(ILI9488_MADCTL);
  649.         uint8_t rotation = r % 4; // can't be higher than 3
  650.         switch (rotation) {
  651.         case 0:
  652.                 ILI9488_SendData(MADCTL_MX | MADCTL_BGR);
  653.                 width = ILI9488_TFTWIDTH;
  654.                 height = ILI9488_TFTHEIGHT;
  655.                 break;
  656.         case 1:
  657.                 ILI9488_SendData(MADCTL_MV | MADCTL_BGR);
  658.                 width = ILI9488_TFTHEIGHT;
  659.                 height = ILI9488_TFTWIDTH;
  660.                 break;
  661.         case 2:
  662.                 ILI9488_SendData(MADCTL_MY | MADCTL_BGR);
  663.                 width = ILI9488_TFTWIDTH;
  664.                 height = ILI9488_TFTHEIGHT;
  665.                 break;
  666.         case 3:
  667.                 ILI9488_SendData(MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
  668.                 width = ILI9488_TFTHEIGHT;
  669.                 height = ILI9488_TFTWIDTH;
  670.                 break;
  671.         }

  672. }

  673. void invertDisplay(uint8_t i)
  674. {

  675.         ILI9488_SendCommand(i ? ILI9488_INVON : ILI9488_INVOFF);

  676. }
  677. uint16_t color565(uint8_t r, uint8_t g, uint8_t b)
  678. {
  679.         return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
  680. }

  681. //11. Text printing functions
  682. void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size)
  683. {
  684.         uint8_t font_size;
  685.         font_size = size;
  686.         if(rotationNum == 1 || rotationNum ==3)
  687.         {
  688.                 if((x >= ILI9488_TFTWIDTH)            || // Clip right
  689.      (y >= ILI9488_TFTHEIGHT)           || // Clip bottom
  690.      ((x + 6 * font_size - 1) < 0) || // Clip left
  691.      ((y + 8 * font_size - 1) < 0))   // Clip top
  692.     return;
  693.         }
  694.         else
  695.         {
  696.                 if((y >= ILI9488_TFTWIDTH)            || // Clip right
  697.      (x >= ILI9488_TFTHEIGHT)           || // Clip bottom
  698.      ((y + 6 * font_size - 1) < 0) || // Clip left
  699.      ((x + 8 * font_size - 1) < 0))   // Clip top
  700.     return;
  701.         }


  702.   if(!_cp437 && (c >= 176)) c++; // Handle 'classic' charset behavior

  703.   for (int8_t i=0; i<6; i++ ) {
  704.     uint8_t line;
  705.     if (i == 5)
  706.       line = 0x0;
  707.     else
  708.       line = pgm_read_byte(font1+(c*5)+i);
  709.     for (int8_t j = 0; j<8; j++) {
  710.       if (line & 0x1) {
  711.         if (font_size == 1) // default size
  712.                 drawPixel(x+i, y+j, color);
  713.         else {  // big size
  714.                 fillRect(x+(i*font_size), y+(j*font_size), font_size, font_size, color);
  715.         }
  716.       } else if (bg != color) {
  717.         if (font_size == 1) // default size
  718.                 drawPixel(x+i, y+j, bg);
  719.         else {  // big size
  720.                 fillRect(x+(i*font_size), y+(j*font_size), font_size, font_size, bg);
  721.         }
  722.       }
  723.       line >>= 1;
  724.     }
  725.   }
  726. }
  727. void ILI9488_printText(char text[], int16_t x, int16_t y, uint16_t color, uint16_t bg, uint8_t size)
  728. {
  729.         int16_t offset;
  730.         offset = size*6;

  731.         for(uint16_t i=0; i<40 && text[i]!=NULL; i++)
  732.         {
  733.                 drawChar(x+(offset*i), y, text[i],color,bg,size);
  734.         }
  735. }
  736. void testLines(uint8_t color)
  737. {

  738.         unsigned long start, t;
  739.         int x1, y1, x2, y2, w = width, h = height;
  740.         fillScreen(ILI9488_BLACK);

  741.         x1 = y1 = 0;
  742.         y2 = h - 1;

  743.         for (x2 = 0; x2 < w; x2 += 6)
  744.                 drawLine(x1, y1, x2, y2, color);
  745.         x2 = w - 1;
  746.         for (y2 = 0; y2 < h; y2 += 6)
  747.                 drawLine(x1, y1, x2, y2, color);
  748.         fillScreen(ILI9488_BLACK);

  749.         x1 = w - 1;
  750.         y1 = 0;
  751.         y2 = h - 1;

  752.         for (x2 = 0; x2 < w; x2 += 6)
  753.                 drawLine(x1, y1, x2, y2, color);
  754.         x2 = 0;
  755.         for (y2 = 0; y2 < h; y2 += 6)
  756.                 drawLine(x1, y1, x2, y2, color);

  757.         fillScreen(ILI9488_BLACK);

  758.         x1 = 0;
  759.         y1 = h - 1;
  760.         y2 = 0;

  761.         for (x2 = 0; x2 < w; x2 += 6)
  762.                 drawLine(x1, y1, x2, y2, color);
  763.         x2 = w - 1;
  764.         for (y2 = 0; y2 < h; y2 += 6)
  765.                 drawLine(x1, y1, x2, y2, color);

  766.         fillScreen(ILI9488_BLACK);

  767.         x1 = w - 1;
  768.         y1 = h - 1;
  769.         y2 = 0;

  770.         for (x2 = 0; x2 < w; x2 += 6)
  771.                 drawLine(x1, y1, x2, y2, color);
  772.         x2 = 0;
  773.         for (y2 = 0; y2 < h; y2 += 6)
  774.                 drawLine(x1, y1, x2, y2, color);
  775. }

  776. void write16BitColor(uint16_t color)
  777. {

  778.           uint8_t r = (color & 0xF800) >> 11;
  779.           uint8_t g = (color & 0x07E0) >> 5;
  780.           uint8_t b = color & 0x001F;

  781.           r = (r * 255) / 31;
  782.           g = (g * 255) / 63;
  783.           b = (b * 255) / 31;
  784.           uint8_t data[3] = {r, g, b};
  785.           ILI9488_SendData_Multi(data, 3);
  786.           //HAL_SPI_Transmit(&hspi1, (uint8_t *)&r, 1, 10);
  787.           //HAL_SPI_Transmit(&hspi1, (uint8_t *)&g, 1, 10);
  788.           //HAL_SPI_Transmit(&hspi1, (uint8_t *)&b, 1, 10);

  789. }




3、添加测试函数如下:
  1.   /* USER CODE BEGIN 2 */
  2.   ILI9488_Init();


  3.     HAL_Delay(1000);
  4.     setRotation(0);
  5.     char badmpu[] = "HELLO 21IC";
  6.           fillScreen(ILI9488_BLACK);
  7.     fillScreen(ILI9488_WHITE);
  8.           ILI9488_printText(badmpu,10,10,ILI9488_BLACK,ILI9488_WHITE,1);
  9.           ILI9488_printText(badmpu,10,20,ILI9488_BLUE,ILI9488_WHITE,2);
  10.           ILI9488_printText(badmpu,10,50,ILI9488_GREEN,ILI9488_WHITE,3);
  11.           ILI9488_printText(badmpu,10,50,ILI9488_RED,ILI9488_WHITE,4);
  12.           testLines(ILI9488_BLUE);
  13.   /* USER CODE END 2 */

【实验效果】
2a4f6c2aaf1811661ad1810c5ae8d89c
【总结】
1.stm32u5系列拥有高频率、大内存,运行速超级快.
2. stm32拥有超级好用的stm32CubeIDE,配置、只要运行在hal库下面的驱动,可以实现少量代码的修改后,轻松移植到不同的芯片上的工程中。
3. 在实现LCD的驱动后,我们运行ADC、PWM、UART等信息,可以实时的显示到LCD屏上,实现交互信息的方便查看等。
【工程源码】
U5A5ZQ_tft.zip (1.53 MB, 下载次数: 12)

SophiaOP 发表于 2023-12-13 14:32 来自手机 | 显示全部楼层
s
这屏幕从哪买的啊?
yangjiaxu 发表于 2023-12-13 14:33 | 显示全部楼层
s
其实可以移植一下lvgl的或者其他的GUI,比如GFX这种,都是很不错的选择
埃娃 发表于 2023-12-13 14:42 | 显示全部楼层
s
堆栈调大有什么好处啊?
亚瑟 发表于 2023-12-13 15:00 | 显示全部楼层
s
刷新率怎么样啊?
 楼主| lulugl 发表于 2023-12-13 20:07 | 显示全部楼层
s
埃娃 发表于 2023-12-13 14:42
堆栈调大有什么好处啊?

太小了的话,如果申请不到内容,就会挂机。
 楼主| lulugl 发表于 2023-12-13 20:08 | 显示全部楼层
s
SophiaOP 发表于 2023-12-13 14:32
这屏幕从哪买的啊?

很久以前的了,某宝有,好象也不贵吧。
 楼主| lulugl 发表于 2023-12-13 20:08 | 显示全部楼层
s
yangjiaxu 发表于 2023-12-13 14:33
其实可以移植一下lvgl的或者其他的GUI,比如GFX这种,都是很不错的选择

后面的帖子已经移植好了LVGL了,touchGFX的话还没有动手。
 楼主| lulugl 发表于 2023-12-13 20:09 | 显示全部楼层
s
亚瑟 发表于 2023-12-13 15:00
刷新率怎么样啊?

没有做全屏的刷新,如果处理好,应该30FPS没有问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

180

主题

830

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部