找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1368|回复: 4
收起左侧

STM32F401CCU6使用HAL库点亮2.2寸ILI9341屏幕软件SPI

[复制链接]
ID:818537 发表于 2023-6-20 20:19 | 显示全部楼层 |阅读模式
先上图
51hei.jpg

移植全动电子,软件SPI刷屏速度慢

  1. #include "tft9341.h"                  // Device header


  2. //管理LCD重要参数
  3. //默认为竖屏
  4. _lcd_dev lcddev;

  5. //画笔颜色,背景颜色
  6. u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;  
  7. u16 DeviceCode;         




  8. void  SPIv_WriteData(u8 Data)
  9. {
  10.         unsigned char i=0;
  11.         for(i=8;i>0;i--)
  12.         {
  13.           if(Data&0x80)        
  14.           SPI_MOSI_SET; //输出数据
  15.       else SPI_MOSI_CLR;
  16.            
  17.       SPI_SCLK_CLR;      
  18.       SPI_SCLK_SET;
  19.       Data<<=1;
  20.         }
  21. }

  22. /*****************************************************************************
  23. * @name       :void LCD_WR_REG(u8 data)
  24. * @date       :2018-08-09
  25. * @function   :Write an 8-bit command to the LCD screen
  26. * @parameters :data:Command value to be written
  27. * @retvalue   :None
  28. ******************************************************************************/
  29. void LCD_WR_REG(u8 data)
  30. {
  31.    LCD_CS_CLR;     
  32.          LCD_RS_CLR;         
  33.    SPIv_WriteData(data);
  34.    LCD_CS_SET;        
  35. }

  36. /*****************************************************************************
  37. * @name       :void LCD_WR_DATA(u8 data)
  38. * @date       :2018-08-09
  39. * @function   :Write an 8-bit data to the LCD screen
  40. * @parameters :data:data value to be written
  41. * @retvalue   :None
  42. ******************************************************************************/
  43. void LCD_WR_DATA(u8 data)
  44. {
  45.    LCD_CS_CLR;
  46.          LCD_RS_SET;
  47.    SPIv_WriteData(data);
  48.    LCD_CS_SET;
  49. }

  50. /*****************************************************************************
  51. * @name       :void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
  52. * @date       :2018-08-09
  53. * @function   :Write data into registers
  54. * @parameters :LCD_Reg:Register address
  55.                 LCD_RegValue:Data to be written
  56. * @retvalue   :None
  57. ******************************************************************************/
  58. void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
  59. {        
  60.         LCD_WR_REG(LCD_Reg);  
  61.         LCD_WR_DATA(LCD_RegValue);                             
  62. }           

  63. /*****************************************************************************
  64. * @name       :void LCD_WriteRAM_Prepare(void)
  65. * @date       :2018-08-09
  66. * @function   :Write GRAM
  67. * @parameters :None
  68. * @retvalue   :None
  69. ******************************************************************************/         
  70. void LCD_WriteRAM_Prepare(void)
  71. {
  72.         LCD_WR_REG(lcddev.wramcmd);
  73. }         

  74. /*****************************************************************************
  75. * @name       :void Lcd_WriteData_16Bit(u16 Data)
  76. * @date       :2018-08-09
  77. * @function   :Write an 16-bit command to the LCD screen
  78. * @parameters :Data:Data to be written
  79. * @retvalue   :None
  80. ******************************************************************************/         
  81. void Lcd_WriteData_16Bit(u16 Data)
  82. {        
  83.    LCD_CS_CLR;
  84.    LCD_RS_SET;  
  85.    SPIv_WriteData(Data>>8);
  86.          SPIv_WriteData(Data);
  87.    LCD_CS_SET;
  88. }

  89. /*****************************************************************************
  90. * @name       :void LCD_DrawPoint(u16 x,u16 y)
  91. * @date       :2018-08-09
  92. * @function   :Write a pixel data at a specified location
  93. * @parameters :x:the x coordinate of the pixel
  94.                 y:the y coordinate of the pixel
  95. * @retvalue   :None
  96. ******************************************************************************/        
  97. void LCD_DrawPoint(u16 x,u16 y)
  98. {
  99.         LCD_SetCursor(x,y);//设置光标位置
  100.         Lcd_WriteData_16Bit(POINT_COLOR);
  101. }

  102. /*****************************************************************************
  103. * @name       :void LCD_Clear(u16 Color)
  104. * @date       :2018-08-09
  105. * @function   :Full screen filled LCD screen
  106. * @parameters :color:Filled color
  107. * @retvalue   :None
  108. ******************************************************************************/        
  109. void LCD_Clear(u16 Color)
  110. {
  111.   unsigned int i,m;  
  112.         LCD_SetWindows(0,0,lcddev.width-1,lcddev.height-1);   
  113.         LCD_CS_CLR;
  114.         LCD_RS_SET;
  115.         for(i=0;i<lcddev.height;i++)
  116.         {
  117.     for(m=0;m<lcddev.width;m++)
  118.     {        
  119.                         Lcd_WriteData_16Bit(Color);
  120.                 }
  121.         }
  122.          LCD_CS_SET;
  123. }

  124. /*****************************************************************************
  125. * @name       :void LCD_GPIOInit(void)
  126. * @date       :2018-08-09
  127. * @function   :Initialization LCD screen GPIO
  128. * @parameters :None
  129. * @retvalue   :None
  130. ******************************************************************************/        
  131. void LCD_GPIOInit(void)
  132. {
  133.         
  134.          __HAL_RCC_GPIOA_CLK_ENABLE();   

  135. }

  136. /*****************************************************************************
  137. * @name       :void LCD_RESET(void)
  138. * @date       :2018-08-09
  139. * @function   :Reset LCD screen
  140. * @parameters :None
  141. * @retvalue   :None
  142. ******************************************************************************/        
  143. void LCD_RESET(void)
  144. {
  145.         LCD_RST_CLR;
  146.         HAL_Delay(100);        
  147.         LCD_RST_SET;
  148.         HAL_Delay(50);
  149. }

  150. /*****************************************************************************
  151. * @name       :void LCD_Init(void)
  152. * @date       :2018-08-09
  153. * @function   :Initialization LCD screen
  154. * @parameters :None
  155. * @retvalue   :None
  156. ******************************************************************************/                  
  157. void LCD_Init(void)
  158. {  
  159.         LCD_GPIOInit();//LCD GPIO初始化        
  160.          LCD_RESET(); //LCD 复位
  161. //*************2.2inch ILI9341初始化**********//
  162.         LCD_WR_REG(0xCF);  
  163.         LCD_WR_DATA(0x00);
  164.         LCD_WR_DATA(0xD9); //C1
  165.         LCD_WR_DATA(0X30);
  166.         LCD_WR_REG(0xED);  
  167.         LCD_WR_DATA(0x64);
  168.         LCD_WR_DATA(0x03);
  169.         LCD_WR_DATA(0X12);
  170.         LCD_WR_DATA(0X81);
  171.         LCD_WR_REG(0xE8);  
  172.         LCD_WR_DATA(0x85);
  173.         LCD_WR_DATA(0x10);
  174.         LCD_WR_DATA(0x7A);
  175.         LCD_WR_REG(0xCB);  
  176.         LCD_WR_DATA(0x39);
  177.         LCD_WR_DATA(0x2C);
  178.         LCD_WR_DATA(0x00);
  179.         LCD_WR_DATA(0x34);
  180.         LCD_WR_DATA(0x02);
  181.         LCD_WR_REG(0xF7);  
  182.         LCD_WR_DATA(0x20);
  183.         LCD_WR_REG(0xEA);  
  184.         LCD_WR_DATA(0x00);
  185.         LCD_WR_DATA(0x00);
  186.         LCD_WR_REG(0xC0);    //Power control
  187.         LCD_WR_DATA(0x21);   //VRH[5:0]  //1B
  188.         LCD_WR_REG(0xC1);    //Power control
  189.         LCD_WR_DATA(0x12);   //SAP[2:0];BT[3:0] //01
  190.         LCD_WR_REG(0xC5);    //VCM control
  191.         LCD_WR_DATA(0x39);          //3F
  192.         LCD_WR_DATA(0x37);          //3C
  193.         LCD_WR_REG(0xC7);    //VCM control2
  194.         LCD_WR_DATA(0XAB);   //B0
  195.         LCD_WR_REG(0x36);    // Memory Access Control
  196.         LCD_WR_DATA(0x48);
  197.         LCD_WR_REG(0x3A);   
  198.         LCD_WR_DATA(0x55);
  199.         LCD_WR_REG(0xB1);   
  200.         LCD_WR_DATA(0x00);   
  201.         LCD_WR_DATA(0x1B);  //1A
  202.         LCD_WR_REG(0xB6);    // Display Function Control
  203.         LCD_WR_DATA(0x0A);
  204.         LCD_WR_DATA(0xA2);
  205.         LCD_WR_REG(0xF2);    // 3Gamma Function Disable
  206.         LCD_WR_DATA(0x00);
  207.         LCD_WR_REG(0x26);    //Gamma curve selected
  208.         LCD_WR_DATA(0x01);

  209.         LCD_WR_REG(0xE0); //Set Gamma
  210.         LCD_WR_DATA(0x0F);
  211.         LCD_WR_DATA(0x23);
  212.         LCD_WR_DATA(0x1F);
  213.         LCD_WR_DATA(0x0B);
  214.         LCD_WR_DATA(0x0E);
  215.         LCD_WR_DATA(0x08);
  216.         LCD_WR_DATA(0x4B);
  217.         LCD_WR_DATA(0XA8);
  218.         LCD_WR_DATA(0x3B);
  219.         LCD_WR_DATA(0x0A);
  220.         LCD_WR_DATA(0x14);
  221.         LCD_WR_DATA(0x06);
  222.         LCD_WR_DATA(0x10);
  223.         LCD_WR_DATA(0x09);
  224.         LCD_WR_DATA(0x00);
  225.         LCD_WR_REG(0XE1); //Set Gamma
  226.         LCD_WR_DATA(0x00);
  227.         LCD_WR_DATA(0x1C);
  228.         LCD_WR_DATA(0x20);
  229.         LCD_WR_DATA(0x04);
  230.         LCD_WR_DATA(0x10);
  231.         LCD_WR_DATA(0x08);
  232.         LCD_WR_DATA(0x34);
  233.         LCD_WR_DATA(0x47);
  234.         LCD_WR_DATA(0x44);
  235.         LCD_WR_DATA(0x05);
  236.         LCD_WR_DATA(0x0B);
  237.         LCD_WR_DATA(0x09);
  238.         LCD_WR_DATA(0x2F);
  239.         LCD_WR_DATA(0x36);
  240.         LCD_WR_DATA(0x0F);
  241.         LCD_WR_REG(0x2B);
  242.         LCD_WR_DATA(0x00);
  243.         LCD_WR_DATA(0x00);
  244.         LCD_WR_DATA(0x01);
  245.         LCD_WR_DATA(0x3f);
  246.         LCD_WR_REG(0x2A);
  247.         LCD_WR_DATA(0x00);
  248.         LCD_WR_DATA(0x00);
  249.         LCD_WR_DATA(0x00);
  250.         LCD_WR_DATA(0xef);         
  251.         LCD_WR_REG(0x11); //Exit Sleep
  252.         HAL_Delay(120);
  253.         LCD_WR_REG(0x29); //display on               

  254.   LCD_direction(USE_HORIZONTAL);//设置LCD显示方向
  255.         //LCD_LED=1;//点亮背光         
  256.         LCD_Clear(WHITE);//清全屏白色
  257. }

  258. /*****************************************************************************
  259. * @name       :void LCD_SetWindows(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd)
  260. * @date       :2018-08-09
  261. * @function   :Setting LCD display window
  262. * @parameters :xStar:the bebinning x coordinate of the LCD display window
  263.                                                                 yStar:the bebinning y coordinate of the LCD display window
  264.                                                                 xEnd:the endning x coordinate of the LCD display window
  265.                                                                 yEnd:the endning y coordinate of the LCD display window
  266. * @retvalue   :None
  267. ******************************************************************************/
  268. void LCD_SetWindows(u16 xStar, u16 yStar,u16 xEnd,u16 yEnd)
  269. {        
  270.         LCD_WR_REG(lcddev.setxcmd);        
  271.         LCD_WR_DATA(xStar>>8);
  272.         LCD_WR_DATA(0x00FF&xStar);               
  273.         LCD_WR_DATA(xEnd>>8);
  274.         LCD_WR_DATA(0x00FF&xEnd);

  275.         LCD_WR_REG(lcddev.setycmd);        
  276.         LCD_WR_DATA(yStar>>8);
  277.         LCD_WR_DATA(0x00FF&yStar);               
  278.         LCD_WR_DATA(yEnd>>8);
  279.         LCD_WR_DATA(0x00FF&yEnd);

  280.         LCD_WriteRAM_Prepare();        //开始写入GRAM                        
  281. }   

  282. /*****************************************************************************
  283. * @name       :void LCD_SetCursor(u16 Xpos, u16 Ypos)
  284. * @date       :2018-08-09
  285. * @function   :Set coordinate value
  286. * @parameters :Xpos:the  x coordinate of the pixel
  287.                                                                 Ypos:the  y coordinate of the pixel
  288. * @retvalue   :None
  289. ******************************************************************************/
  290. void LCD_SetCursor(u16 Xpos, u16 Ypos)
  291. {                                             
  292.         LCD_SetWindows(Xpos,Ypos,Xpos,Ypos);        
  293. }

  294. /*****************************************************************************
  295. * @name       :void LCD_direction(u8 direction)
  296. * @date       :2018-08-09
  297. * @function   :Setting the display direction of LCD screen
  298. * @parameters :direction:0-0 degree
  299.                           1-90 degree
  300.                                                                                                         2-180 degree
  301.                                                                                                         3-270 degree
  302. * @retvalue   :None
  303. ******************************************************************************/
  304. void LCD_direction(u8 direction)
  305. {
  306.                         lcddev.setxcmd=0x2A;
  307.                         lcddev.setycmd=0x2B;
  308.                         lcddev.wramcmd=0x2C;
  309.         switch(direction){                  
  310.                 case 0:                                                                          
  311.                         lcddev.width=LCD_W;
  312.                         lcddev.height=LCD_H;               
  313.                         LCD_WriteReg(0x36,(1<<3)|(0<<6)|(0<<7));//BGR==1,MY==0,MX==0,MV==0
  314.                 break;
  315.                 case 1:
  316.                         lcddev.width=LCD_H;
  317.                         lcddev.height=LCD_W;
  318.                         LCD_WriteReg(0x36,(1<<3)|(0<<7)|(1<<6)|(1<<5));//BGR==1,MY==1,MX==0,MV==1
  319.                 break;
  320.                 case 2:                                                                          
  321.                         lcddev.width=LCD_W;
  322.                         lcddev.height=LCD_H;        
  323.                         LCD_WriteReg(0x36,(1<<3)|(1<<6)|(1<<7));//BGR==1,MY==0,MX==0,MV==0
  324.                 break;
  325.                 case 3:
  326.                         lcddev.width=LCD_H;
  327.                         lcddev.height=LCD_W;
  328.                         LCD_WriteReg(0x36,(1<<3)|(1<<7)|(1<<5));//BGR==1,MY==1,MX==0,MV==1
  329.                 break;        
  330.                 default:break;
  331.         }               
  332. }         

复制代码

51hei.png
由于程序太大 无法上传,所以只上传了核心屏幕驱动(上图8个文件),大家可以自习移植:
HARDWARE.7z (24.31 KB, 下载次数: 17)
回复

使用道具 举报

ID:973741 发表于 2023-6-21 08:55 来自手机 | 显示全部楼层
芯片有硬件spi,却要去用软件,并且驱动的还是tft,有实际意义吗
回复

使用道具 举报

ID:101305 发表于 2023-6-26 11:36 | 显示全部楼层
问下这屏幕刷新率高吗?能显示动画吗?
回复

使用道具 举报

ID:336834 发表于 2023-6-26 12:18 | 显示全部楼层
能用硬件SPI和DMA驱动这个屏幕吗?
回复

使用道具 举报

ID:336834 发表于 2023-6-26 12:19 | 显示全部楼层
问一下楼里的大佬们,这个STM32F401CU6单片机能使用lvgl吗?怎么使用呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表