[应用相关] [stm32] 一个简单的stm32vet6驱动2.4寸240X320的8位并口tft屏DEMO

[复制链接]
 楼主| comparison 发表于 2020-1-5 17:04 | 显示全部楼层 |阅读模式
最近在研究用低速、低RAM的单片机来驱动小LCD或TFT彩屏实现动画效果
首先我用一个16MHz晶振的m0内核的8位单片机nRF51822尝试驱动一个1.77寸的4线SPI屏(128X160),
发现,刷一屏大约要0.8s左右的时间,
觉得,如果用72MHz的STM32也许效果会好很多
于是在stm32上做了个类似的版本,
具体收录在《一个简单的stm32vet6驱动的天马4线SPI-1.77寸LCD彩屏DEMO》中
发现刷一屏0.2s左右,
效果是有的,但是还不能达到支持播放流畅动画的效果!
于是,决定将串行数据改成并行数据传输
本节将带来一个用stm32驱动的2.4寸240X320的8位并口tft屏的刷屏效果

 楼主| comparison 发表于 2020-1-5 17:04 | 显示全部楼层
工程结构

369475e11a6acea131.png
 楼主| comparison 发表于 2020-1-5 17:05 | 显示全部楼层
main.c

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x.h"
  3. #include "LCD2.h"


  4. void RCC_Configuration(void);
  5. /****************************************************************************
  6. * 名    称:int main(void)
  7. * 功    能:主函数
  8. * 入口参数:无
  9. * 出口参数:无
  10. * 说    明:
  11. * 调用方法:无
  12. ****************************************************************************/
  13. int main(void)
  14. {
  15.     RCC_Configuration();                   //系统时钟配置
  16.     LCD2_GPIO_Init();
  17.     LCD2_Init();
  18.     while (1)
  19.     {
  20.         Show_RGB(0,240,0,320,0xff0f);
  21.         DELAY_MS(1000);
  22.         Show_RGB(0,240,0,320,0x00fe);
  23.         DELAY_MS(1000);
  24.     }
  25. }

  26. /****************************************************************************
  27. * 名    称:void RCC_Configuration(void)
  28. * 功    能:系统时钟配置为72MHZ
  29. * 入口参数:无
  30. * 出口参数:无
  31. * 说    明:
  32. * 调用方法:无
  33. ****************************************************************************/
  34. void RCC_Configuration(void)
  35. {   
  36.   SystemInit();
  37. }
 楼主| comparison 发表于 2020-1-5 17:05 | 显示全部楼层
LCD2.c

  1. #include "LCD2.h"



  2. void LCD2_GPIO_Init()
  3. {
  4.     GPIO_InitTypeDef GPIO_InitStructure;

  5.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
  6.    
  7.    
  8.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  9.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  10.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;             //口线翻转速度为50MHz
  11.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  12.    
  13.     //8位数据输出
  14.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  15.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  16.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;             //口线翻转速度为50MHz
  17.     GPIO_Init(GPIOD, &GPIO_InitStructure);            
  18. }

  19. //////////////////////////////////////////////////////////////////
  20. //最底层数据传输函数
  21. //////////////////////////////////////////////////////////////////
  22. //写命令
  23. void Write_Cmd(unsigned char DH,unsigned char DL)
  24. {
  25.     LCD2_CS=0;
  26.     LCD2_RS=0;

  27.     DataPort=DH;
  28.     LCD2_RW=0;
  29.     LCD2_RW=1;

  30.     DataPort=DL;
  31.    
  32.     LCD2_RW=0;
  33.     LCD2_RW=1;
  34.     LCD2_CS=1;
  35. }
  36. //写数据 双8位
  37. void Write_Data(unsigned char DH,unsigned char DL)
  38. {
  39.     LCD2_CS=0;
  40.    
  41.     LCD2_RS=1;
  42.     DataPort=DH;
  43.     LCD2_RW=0;
  44.     LCD2_RW=1;

  45.     DataPort=DL;   
  46.     LCD2_RW=0;
  47.     LCD2_RW=1;
  48.     LCD2_CS=1;
  49. }

  50. //写数据 双8位
  51. void Write_Data2(unsigned char DH,unsigned char DL)
  52. {
  53.     DataPort=DH;
  54.     LCD2_RW=0;
  55.     LCD2_RW=1;

  56.     DataPort=DL;   
  57.     LCD2_RW=0;
  58.     LCD2_RW=1;
  59. }

  60. //////////////////////////////////////////////////////////////////
  61. //调用上面最底层实现稍高层写命令和数据函数
  62. //////////////////////////////////////////////////////////////////
  63. /*----------------------------------------------------------------
  64.                          写命令、写数据
  65. 输入参数:x 需要输入的命令 16位
  66.           y 需要输入的数据 16位
  67. ----------------------------------------------------------------*/
  68. void  Write_Cmd_Data (unsigned char x,unsigned int y)
  69. {
  70.     unsigned char m,n;
  71.     m=y>>8;
  72.     n=y;
  73.     Write_Cmd(0x00,x);
  74.     Write_Data(m,n);
  75. }
  76. /*----------------------------------------------------------------
  77.                          写16位数据
  78. ----------------------------------------------------------------*/
  79. void  Write_Data_U16(unsigned int y)
  80. {
  81.     unsigned char m,n;
  82.     m=y>>8;
  83.     n=y;
  84.     Write_Data2(m,n);
  85. }

  86. /*----------------------------------------------------------------
  87.                             液晶初始化
  88. ----------------------------------------------------------------*/
  89. void LCD2_Init(void)
  90. {
  91.     LCD2_CS=1;
  92.     DELAY_MS(5);
  93.     LCD2_RES=0;
  94.     DELAY_MS(5);
  95.     LCD2_RES=1;
  96.     DELAY_MS(50);
  97.     Write_Cmd_Data(0x0001,0x0100);
  98.     Write_Cmd_Data(0x0002,0x0700);
  99.     Write_Cmd_Data(0x0003,0x1030);
  100.     Write_Cmd_Data(0x0004,0x0000);
  101.     Write_Cmd_Data(0x0008,0x0207);  
  102.     Write_Cmd_Data(0x0009,0x0000);
  103.     Write_Cmd_Data(0x000A,0x0000);
  104.     Write_Cmd_Data(0x000C,0x0000);
  105.     Write_Cmd_Data(0x000D,0x0000);
  106.     Write_Cmd_Data(0x000F,0x0000);
  107.     //power on sequence VGHVGL
  108.     Write_Cmd_Data(0x0010,0x0000);   
  109.     Write_Cmd_Data(0x0011,0x0007);  
  110.     Write_Cmd_Data(0x0012,0x0000);  
  111.     Write_Cmd_Data(0x0013,0x0000);
  112.     //vgh
  113.     Write_Cmd_Data(0x0010,0x1290);   
  114.     Write_Cmd_Data(0x0011,0x0227);
  115.     //DELAY_MS(100);
  116.     //vregiout
  117.     Write_Cmd_Data(0x0012,0x001d); //0x001b
  118.     //DELAY_MS(100);
  119.     //vom amplitude
  120.     Write_Cmd_Data(0x0013,0x1500);
  121.     //DELAY_MS(100);
  122.     //vom H
  123.     Write_Cmd_Data(0x0029,0x0018);
  124.     Write_Cmd_Data(0x002B,0x000D);

  125.     //gamma
  126.     Write_Cmd_Data(0x0030,0x0004);
  127.     Write_Cmd_Data(0x0031,0x0307);
  128.     Write_Cmd_Data(0x0032,0x0002);// 0006
  129.     Write_Cmd_Data(0x0035,0x0206);
  130.     Write_Cmd_Data(0x0036,0x0408);
  131.     Write_Cmd_Data(0x0037,0x0507);
  132.     Write_Cmd_Data(0x0038,0x0204);//0200
  133.     Write_Cmd_Data(0x0039,0x0707);
  134.     Write_Cmd_Data(0x003C,0x0405);// 0504
  135.     Write_Cmd_Data(0x003D,0x0F02);
  136.     //ram
  137.     Write_Cmd_Data(0x0050,0x0000);
  138.     Write_Cmd_Data(0x0051,0x00EF);
  139.     Write_Cmd_Data(0x0052,0x0000);
  140.     Write_Cmd_Data(0x0053,0x013F);  
  141.     Write_Cmd_Data(0x0060,0xA700);
  142.     Write_Cmd_Data(0x0061,0x0001);
  143.     Write_Cmd_Data(0x006A,0x0000);
  144.     //
  145.     Write_Cmd_Data(0x0080,0x0000);
  146.     Write_Cmd_Data(0x0081,0x0000);
  147.     Write_Cmd_Data(0x0082,0x0000);
  148.     Write_Cmd_Data(0x0083,0x0000);
  149.     Write_Cmd_Data(0x0084,0x0000);
  150.     Write_Cmd_Data(0x0085,0x0000);
  151.     //
  152.     Write_Cmd_Data(0x0090,0x0010);
  153.     Write_Cmd_Data(0x0092,0x0600);
  154.     Write_Cmd_Data(0x0093,0x0003);
  155.     Write_Cmd_Data(0x0095,0x0110);
  156.     Write_Cmd_Data(0x0097,0x0000);
  157.     Write_Cmd_Data(0x0098,0x0000);
  158.     Write_Cmd_Data(0x0007,0x0133);
  159.    
  160.     //    Write_Cmd_Data(0x0022);//        
  161. }

  162. /*----------------------------------------------------------------
  163.                          设置坐标
  164. ----------------------------------------------------------------*/
  165. /*----------------------------------------------------------------
  166.                             全局变量
  167. ----------------------------------------------------------------*/
  168. #define WINDOW_XADDR_START    0x0050 // Horizontal Start Address Set
  169. #define WINDOW_XADDR_END    0x0051 // Horizontal End Address Set
  170. #define WINDOW_YADDR_START    0x0052 // Vertical Start Address Set
  171. #define WINDOW_YADDR_END    0x0053 // Vertical End Address Set
  172. #define GRAM_XADDR            0x0020 // GRAM Horizontal Address Set
  173. #define GRAM_YADDR            0x0021 // GRAM Vertical Address Set
  174. #define GRAMWR                 0x0022 // memory write
  175. void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
  176. {
  177.     Write_Cmd_Data(WINDOW_XADDR_START,x0);
  178.     Write_Cmd_Data(WINDOW_XADDR_END,x1);
  179.     Write_Cmd_Data(WINDOW_YADDR_START,y0);
  180.     Write_Cmd_Data(WINDOW_YADDR_END,y1);
  181.     Write_Cmd_Data(GRAM_XADDR,x0);
  182.     Write_Cmd_Data(GRAM_YADDR,y0);
  183.     Write_Cmd (0x00,0x22);//LCD_WriteCMD(GRAMWR);
  184. }

  185. /*----------------------------------------------------------------
  186.                             显示RGB颜色
  187. 输入参数:x0,y0 起始坐标
  188.           x1,y1 结束坐标
  189.           Color  背景颜色
  190. ----------------------------------------------------------------*/
  191. void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
  192. {
  193.     unsigned int i,j;
  194.     LCD_SetPos(x0,x1,y0,y1);
  195.     LCD2_CS=0;
  196.     LCD2_RS=1;
  197. //    for (i=y0;i<=y1;i++)
  198. //    {
  199. //       for (j=x0;j<=x1;j++)
  200. //           Write_Data_U16(Color);
  201. //    }
  202.    
  203.     for (i=0;i<=(y1-y0+1)*(x1-x0+1);i+=32)
  204.     {
  205.          Write_Data_U16(Color);
  206.          Write_Data_U16(Color);
  207.         Write_Data_U16(Color);
  208.          Write_Data_U16(Color);
  209.          Write_Data_U16(Color);
  210.          Write_Data_U16(Color);
  211.         Write_Data_U16(Color);
  212.          Write_Data_U16(Color);
  213.         Write_Data_U16(Color);
  214.          Write_Data_U16(Color);
  215.         Write_Data_U16(Color);
  216.          Write_Data_U16(Color);
  217.          Write_Data_U16(Color);
  218.          Write_Data_U16(Color);
  219.         Write_Data_U16(Color);
  220.          Write_Data_U16(Color);
  221.          Write_Data_U16(Color);
  222.          Write_Data_U16(Color);
  223.         Write_Data_U16(Color);
  224.          Write_Data_U16(Color);
  225.          Write_Data_U16(Color);
  226.          Write_Data_U16(Color);
  227.         Write_Data_U16(Color);
  228.          Write_Data_U16(Color);
  229.         Write_Data_U16(Color);
  230.          Write_Data_U16(Color);
  231.         Write_Data_U16(Color);
  232.          Write_Data_U16(Color);
  233.          Write_Data_U16(Color);
  234.          Write_Data_U16(Color);
  235.         Write_Data_U16(Color);
  236.          Write_Data_U16(Color);
  237.     }
  238.         LCD2_CS=1;
  239. }


  240. void Delay_ms(u16 time)
  241. {
  242.     u16 i=0;  
  243.     while(time--)
  244.     {
  245.         i=12000;
  246.         while(i--);   
  247.     }
  248. }
 楼主| comparison 发表于 2020-1-5 17:06 | 显示全部楼层
另外补上这个小实验的连线图:

1、这个是本节的并行接口与屏幕的连接方式:
251205e11a6f9f0b85.png


2、这是上两节串行接口的连线,上面对应的引脚连接是与nRF51822的(第一次试验),下面对应的连接是与stm32的(第二次试验)
507365e11a6ff274d1.png
 楼主| comparison 发表于 2020-1-5 17:06 | 显示全部楼层
小结

从效果图上看,即使采用stm32的8位并行来驱动屏幕速度还是达不到刷新动画的效果~

之后我也在传输数据的函数上做了些优化,可效果还是不明显——

如第一点:优化前RS等引脚的定义要通过宏展开,每次计算BitBand后面的式子~

#define BitBand(Addr, Bit) *((volatile int*)(((int)(Addr) & 0x60000000) + 0x02000000 + (int)(Addr) * 0x20 + (Bit) * 4))
#define LCD2_CS         BitBand(&GPIOB->ODR, 8)
#define LCD2_RES        BitBand(&GPIOB->ODR, 9)
#define LCD2_RS         BitBand(&GPIOB->ODR, 7)
#define LCD2_RW         BitBand(&GPIOB->ODR, 6)
#define DataPort         GPIOD->ODR
优化后采用直接把值赋给对应的引脚来减少运算量

#define BitBand(Addr, Bit) *((volatile int*)(((int)(Addr) & 0x60000000) + 0x02000000 + (int)(Addr) * 0x20 + (Bit) * 4))
#define LCD2_CS         (*((volatile int*)0x422181A0))   //BitBand(&GPIOB->ODR, 8)
#define LCD2_RES        (*((volatile int*)0x422181A4))   //BitBand(&GPIOB->ODR, 9)
#define LCD2_RS         (*((volatile int*)0x4221819C))   //BitBand(&GPIOB->ODR, 7)
#define LCD2_RW         (*((volatile int*)0x42218198))   //BitBand(&GPIOB->ODR, 6)
#define DataPort         (*((volatile int*)0x4001140C)) //GPIOD->ODR
 楼主| comparison 发表于 2020-1-5 17:07 | 显示全部楼层
如第二点:为了减少Show_RGB函数中循环中的Write_Data_U16的调用,直接将Write_Data_U16计算拆到最细放到循环内

762865e11a737d8e1c.png
 楼主| comparison 发表于 2020-1-5 17:07 | 显示全部楼层
如第三点:为了排除Write_Data_U16中4、5两行移位运算和类型转换所带来的时间花销,直接采用上图中全局变量color1、color2来直接赋值,查看效果有没有提升~

  1. void  Write_Data_U16(unsigned int y)
  2. {
  3.     unsigned char m,n;
  4.     m=y>>8;
  5.     n=y;
  6.     Write_Data2(m,n);
  7. }
 楼主| comparison 发表于 2020-1-5 17:07 | 显示全部楼层
本篇中资源链接:http://pan.baidu.com/s/1bnjw1Fh

241395e11a767e8baf.png

注:其中未优化版工程比较简洁方便理解学习,优化测试版是为了提升传输速率做的几点优化(效果不大,代码稍乱)
renzheshengui 发表于 2020-2-3 11:29 | 显示全部楼层
非常感谢楼主分享
wakayi 发表于 2020-2-3 11:33 | 显示全部楼层
非常感谢楼主分享
wowu 发表于 2020-2-3 11:47 | 显示全部楼层
非常感谢楼主分享
xiaoqizi 发表于 2020-2-3 12:03 | 显示全部楼层
非常感谢楼主分享
木木guainv 发表于 2020-2-3 12:23 | 显示全部楼层
非常感谢楼主分享
磨砂 发表于 2020-2-3 16:34 | 显示全部楼层
非常感谢楼主分享
晓伍 发表于 2020-2-3 16:35 | 显示全部楼层
非常感谢楼主分享
八层楼 发表于 2020-2-3 16:37 | 显示全部楼层
非常感谢楼主分享
观海 发表于 2020-2-3 16:41 | 显示全部楼层
非常感谢楼主分享
gygp 发表于 2020-2-5 20:40 | 显示全部楼层
用STM32驱动2.4寸TFT可以读取ID可以吗  
chenci2013 发表于 2020-2-5 20:40 | 显示全部楼层
STM32f429通过RGB直接驱动TFT-LCD可以吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

25

主题

417

帖子

0

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