STM32 LCD和触摸屏

Posted qifeng1024

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32 LCD和触摸屏相关的知识,希望对你有一定的参考价值。

显示图片函数:参数:(横坐标,纵坐标,图片像素大小长宽,图片数组指针)

 1 /*
 2 函数功能:显示图片
 3 函数形参:x,y,width,height,*color_buff
 4 */
 5 void lcd_dis_pic(u16 x, u16 y, u16 width, u16 height, u8 *color_buff)
 6 
 7     u32 i = 0;
 8     
 9     LCD_ILI9486_CMD(0x2A);                      //发送0x2A命令,设置X轴坐标
10     LCD_ILI9486_Parameter((x & 0xff00) >> 8);  //发送X轴起始坐标的高8位
11     LCD_ILI9486_Parameter((x & 0x00ff));       //发送X轴起始坐标的低8位
12     LCD_ILI9486_Parameter(((x + width - 1) & 0xff00) >> 8);  //发送X轴结束坐标的高8位
13     LCD_ILI9486_Parameter(((x + width - 1) & 0x00ff));       //发送X轴结束坐标的低8位
14     
15     LCD_ILI9486_CMD(0x2B);                      //发送0x2B命令,设置Y轴坐标
16     LCD_ILI9486_Parameter((y & 0xff00) >> 8);  //发送Y轴起始坐标的高8位
17     LCD_ILI9486_Parameter((y & 0x00ff));       //发送Y轴起始坐标的低8位
18     LCD_ILI9486_Parameter(((y + height - 1) & 0xff00) >> 8);  //发送Y轴结束坐标的高8位
19     LCD_ILI9486_Parameter(((y + height - 1) & 0x00ff));       //发送Y轴结束坐标的低8位
20     
21     LCD_ILI9486_CMD(0x2C);                      //发送0x2A命令,设置LCD显示颜色
22     for(i = 0; i < width * height; i++)  //循环清屏区域的每一个像素点
23     
24         LCD_ILI9486_Parameter((color_buff[2 * i] << 8) + color_buff[2 * i + 1]);
25     
26 

LCD初始化:

  1 /******************** LCD引脚初始化函数 *******************
  2 LCD显示屏硬件管脚对应关系:
  3 数据线:
  4 DB0---PD14    DB1---PD15    DB2---PD0    DB3---PD1
  5 DB4---PE7    DB5---PE8    DB6---PE9    DB7---PE10
  6 DB8---PE11    DB9---PE12    DB10---PE13    DB11---PE14
  7 DB12---PE15    DB13---PD8    DB14---PD9    DB15---PD10
  8 控制线:
  9 WR---PD5    RD---PD4    CS---PG12    RS---PF12    BL--PB15
 10 **********************************************************/
 11 void LCD_Port_Init(void)
 12 
 13     /* 使能LCD硬件管脚时钟 */
 14     RCC->AHB1ENR |= (1<<3);//开PD
 15     RCC->AHB1ENR |= (1<<4);//开PE
 16     RCC->AHB1ENR |= (1<<6);//开PG
 17     RCC->AHB1ENR |= (1<<1);//开PB
 18     RCC->AHB1ENR |= (1<<5);//开PF
 19     
 20     
 21     /* 设置LCD硬件管脚模式 */
 22     /* DB0~DB15数据线:推挽输出  时钟50M */
 23     GPIOD->MODER |=(0X1<<(2*14))|(0X1<<(2*15));   //配置成通用输出  
 24     GPIOD->OSPEEDR |=(0X2<<(2*14))|(0X2ul<<(2*15)); //配置成输出速率50M         
 25     
 26     GPIOD->MODER |=(0X1<<(2*0))|(0X1<<(2*1));     //配置成通用输出  
 27     GPIOD->OSPEEDR |=(0X2<<(2*0))|(0X2<<(2*1));   //配置成输出速率50M
 28     
 29     GPIOE->MODER |=0x55554000;                    //配置成通用输出  
 30     GPIOE->OTYPER |= 0;                           //配置成输出推挽
 31     GPIOE->OSPEEDR |=0x55554000;                  //配置成输出速率50M
 32 
 33     GPIOD->MODER |=(0X1<<(2*8))|(0X1<<(2*9))|(0X1<<(2*10));  //配置成通用输出  
 34     GPIOD->OSPEEDR |=(0X2<<(2*1))|(0X2<<(2*1))|(0X2<<(2*1));   //配置成输出速率50M
 35     
 36     /* 控制线:推挽输出  时钟50M 空闲为高电平 */
 37     GPIOD->MODER |=(0X1<<(2*4))|(0X1<<(2*5));  //配置成通用输出  
 38     GPIOD->OSPEEDR |=(0X2<<(2*4))|(0X2<<(2*5));
 39     GPIOD->ODR |= (1<<4)|(1<<5);
 40     
 41     GPIOG->MODER |=(0X1<<(2*12));              //配置成通用输出  
 42     GPIOG->OSPEEDR |=(0X2<<(2*12));
 43     GPIOG->ODR |= (1<<12);
 44 
 45     GPIOF->MODER |=(0X1<<(2*12));              //配置成通用输出  
 46     GPIOF->OSPEEDR |=(0X2<<(2*12));
 47     GPIOF->ODR |= (1<<12);
 48     
 49     GPIOB->MODER |=(0X1<<(2*15));               //配置成通用输出  
 50     GPIOB->OSPEEDR |=(0X2ul<<(2*15));
 51     GPIOB->ODR &= ~(1<<15);                     //关闭背光,在LCD初始化完后再打开
 52 
 53 
 54 /******************* LCD写命令函数 *********************************
 55 参数:需要输入的命令
 56 ********************************************************************/
 57 void LCD_ILI9486_CMD(u16 common)
 58 
 59     GPIOG->ODR &= ~(1 << 12);    //CS拉低
 60     GPIOF->ODR &= ~(1 << 12);    //D/C拉低
 61     GPIOD->ODR &= ~(1 << 5);     //WR拉低
 62     //LCD_Output_Data(cmd);        //输出命令数据
 63     /* 把命令发送到数据总线 */
 64     if(common&0x01)   GPIOD->BSRRL = (1<<14); else GPIOD->BSRRH = (1<<14);
 65     if(common&0x02)   GPIOD->BSRRL = (1<<15); else GPIOD->BSRRH = (1<<15);
 66     if(common&0x04)   GPIOD->BSRRL = (1<<0);  else GPIOD->BSRRH = (1<<0);
 67     if(common&0x08)   GPIOD->BSRRL = (1<<1);  else GPIOD->BSRRH = (1<<1);
 68     if(common&0x10)   GPIOE->BSRRL = (1<<7);  else GPIOE->BSRRH = (1<<7);
 69     if(common&0x20)   GPIOE->BSRRL = (1<<8);  else GPIOE->BSRRH = (1<<8);
 70     if(common&0x40)   GPIOE->BSRRL = (1<<9);  else GPIOE->BSRRH = (1<<9);
 71     if(common&0x80)   GPIOE->BSRRL = (1<<10); else GPIOE->BSRRH = (1<<10);
 72     if(common&0x0100) GPIOE->BSRRL = (1<<11); else GPIOE->BSRRH = (1<<11);
 73     if(common&0x0200) GPIOE->BSRRL = (1<<12); else GPIOE->BSRRH = (1<<12);
 74     if(common&0x0400) GPIOE->BSRRL = (1<<13); else GPIOE->BSRRH = (1<<13);
 75     if(common&0x0800) GPIOE->BSRRL = (1<<14); else GPIOE->BSRRH = (1<<14);
 76     if(common&0x1000) GPIOE->BSRRL = (1<<15); else GPIOE->BSRRH = (1<<15);
 77     if(common&0x2000) GPIOD->BSRRL = (1<<8);  else GPIOD->BSRRH = (1<<8);
 78     if(common&0x4000) GPIOD->BSRRL = (1<<9);  else GPIOD->BSRRH = (1<<9);
 79     if(common&0x8000) GPIOD->BSRRL = (1<<10); else GPIOD->BSRRH = (1<<10);
 80     GPIOD->ODR |= 1 << 5;        //WR拉高
 81     GPIOG->ODR |= 1 << 12;       //拉高CS
 82 
 83 
 84 /******************* LCD写数据函数 *********************************
 85 参数:需要输入的命令
 86 ********************************************************************/
 87 void LCD_ILI9486_Parameter(u16 data)
 88 
 89     GPIOG->ODR &= ~(1 << 12);    //CS拉低
 90     GPIOF->ODR |= 1 << 12;       //D/C拉高
 91     GPIOD->ODR &= ~(1 << 5);     //WR拉低
 92     //LCD_Output_Data(data);        //输出命令数据
 93     /* 把数据发送到数据总线 */
 94     if(data&0x01)   GPIOD->BSRRL = (1<<14); else GPIOD->BSRRH = (1<<14);
 95     if(data&0x02)   GPIOD->BSRRL = (1<<15); else GPIOD->BSRRH = (1<<15);
 96     if(data&0x04)   GPIOD->BSRRL = (1<<0);  else GPIOD->BSRRH = (1<<0);
 97     if(data&0x08)   GPIOD->BSRRL = (1<<1);  else GPIOD->BSRRH = (1<<1);
 98     if(data&0x10)   GPIOE->BSRRL = (1<<7);  else GPIOE->BSRRH = (1<<7);
 99     if(data&0x20)   GPIOE->BSRRL = (1<<8);  else GPIOE->BSRRH = (1<<8);
100     if(data&0x40)   GPIOE->BSRRL = (1<<9);  else GPIOE->BSRRH = (1<<9);
101     if(data&0x80)   GPIOE->BSRRL = (1<<10); else GPIOE->BSRRH = (1<<10);
102     if(data&0x0100) GPIOE->BSRRL = (1<<11); else GPIOE->BSRRH = (1<<11);
103     if(data&0x0200) GPIOE->BSRRL = (1<<12); else GPIOE->BSRRH = (1<<12);
104     if(data&0x0400) GPIOE->BSRRL = (1<<13); else GPIOE->BSRRH = (1<<13);
105     if(data&0x0800) GPIOE->BSRRL = (1<<14); else GPIOE->BSRRH = (1<<14);
106     if(data&0x1000) GPIOE->BSRRL = (1<<15); else GPIOE->BSRRH = (1<<15);
107     if(data&0x2000) GPIOD->BSRRL = (1<<8);  else GPIOD->BSRRH = (1<<8);
108     if(data&0x4000) GPIOD->BSRRL = (1<<9);  else GPIOD->BSRRH = (1<<9);
109     if(data&0x8000) GPIOD->BSRRL = (1<<10); else GPIOD->BSRRH = (1<<10);
110     GPIOD->ODR |= 1 << 5;        //WR拉高
111     GPIOG->ODR |= 1 << 12;       //拉高CS
112 
113 
114 /********************** LCD清屏函数 ******************************************
115 参数:
116 SX、EX:X轴的起始坐标和结束坐标
117 SY、EY:Y轴的起始坐标和结束坐标
118 Color: 屏幕颜色
119 ******************************************************************************/
120 void LCD_Clear(u16 SX, u16 EX, u16 SY, u16 EY, u16 Color)
121 
122     u32 i;
123     
124     LCD_ILI9486_CMD(0x2A);                      //发送0x2A命令,设置X轴坐标
125     LCD_ILI9486_Parameter((SX & 0xff00) >> 8);  //发送X轴起始坐标的高8位
126     LCD_ILI9486_Parameter((SX & 0x00ff));       //发送X轴起始坐标的低8位
127     LCD_ILI9486_Parameter((EX & 0xff00) >> 8);  //发送X轴结束坐标的高8位
128     LCD_ILI9486_Parameter((EX & 0x00ff));       //发送X轴结束坐标的低8位
129     
130     LCD_ILI9486_CMD(0x2B);                      //发送0x2B命令,设置Y轴坐标
131     LCD_ILI9486_Parameter((SY & 0xff00) >> 8);  //发送Y轴起始坐标的高8位
132     LCD_ILI9486_Parameter((SY & 0x00ff));       //发送Y轴起始坐标的低8位
133     LCD_ILI9486_Parameter((EY & 0xff00) >> 8);  //发送Y轴结束坐标的高8位
134     LCD_ILI9486_Parameter((EY & 0x00ff));       //发送Y轴结束坐标的低8位
135     
136     LCD_ILI9486_CMD(0x2C);                      //发送0x2A命令,设置LCD显示颜色
137     for(i = 0; i < (EX - SX + 1) * (EY - SY + 1); i++)  //循环清屏区域的每一个像素点
138     
139         LCD_ILI9486_Parameter(Color);
140     
141 
142 
143 /********************** LCD初始化函数 *******************************************/
144 void LCD_Init(void)
145 
146     LCD_Port_Init();
147     //************* Reset LCD Driver ****************//
148     systick_delay_ms(120); // Delay 120 ms
149     //************* Start Initial Sequence **********//
150     LCD_ILI9486_CMD(0XF2);
151     LCD_ILI9486_Parameter(0x18);
152     LCD_ILI9486_Parameter(0xA3);
153     LCD_ILI9486_Parameter(0x12);
154     LCD_ILI9486_Parameter(0x02);
155     LCD_ILI9486_Parameter(0XB2);
156     LCD_ILI9486_Parameter(0x12);
157     LCD_ILI9486_Parameter(0xFF);
158     LCD_ILI9486_Parameter(0x10);
159     LCD_ILI9486_Parameter(0x00);
160     LCD_ILI9486_CMD(0XF8);
161     LCD_ILI9486_Parameter(0x21);
162     LCD_ILI9486_Parameter(0x04);
163     LCD_ILI9486_CMD(0XF9);
164     LCD_ILI9486_Parameter(0x00);
165     LCD_ILI9486_Parameter(0x08);
166     LCD_ILI9486_CMD(0x36);
167     LCD_ILI9486_Parameter(0x08);   //设置RGB,含排线的屏
168 //    LCD_ILI9486_Parameter(0x00);   //设置RGB,不含排线的屏BGR
169     LCD_ILI9486_CMD(0x3A);
170     LCD_ILI9486_Parameter(0x05);   //设置16位BPP
171     LCD_ILI9486_CMD(0xB4);
172     LCD_ILI9486_Parameter(0x01);//0x00
173     LCD_ILI9486_CMD(0xB6);
174     LCD_ILI9486_Parameter(0x02);
175     LCD_ILI9486_Parameter(0x22);
176     LCD_ILI9486_CMD(0xC1);
177     LCD_ILI9486_Parameter(0x41);
178     LCD_ILI9486_CMD(0xC5);
179     LCD_ILI9486_Parameter(0x00);
180     LCD_ILI9486_Parameter(0x07);//0X18
181     LCD_ILI9486_CMD(0xE0);
182     LCD_ILI9486_Parameter(0x0F);
183     LCD_ILI9486_Parameter(0x1F);
184     LCD_ILI9486_Parameter(0x1C);
185     LCD_ILI9486_Parameter(0x0C);
186     LCD_ILI9486_Parameter(0x0F);
187     LCD_ILI9486_Parameter(0x08);
188     LCD_ILI9486_Parameter(0x48);
189     LCD_ILI9486_Parameter(0x98);
190     LCD_ILI9486_Parameter(0x37);
191     LCD_ILI9486_Parameter(0x0A);
192     LCD_ILI9486_Parameter(0x13);
193     LCD_ILI9486_Parameter(0x04);
194     LCD_ILI9486_Parameter(0x11);
195     LCD_ILI9486_Parameter(0x0D);
196     LCD_ILI9486_Parameter(0x00);
197     LCD_ILI9486_CMD(0xE1);
198     LCD_ILI9486_Parameter(0x0F);
199     LCD_ILI9486_Parameter(0x32);
200     LCD_ILI9486_Parameter(0x2E);
201     LCD_ILI9486_Parameter(0x0B);
202     LCD_ILI9486_Parameter(0x0D);
203     LCD_ILI9486_Parameter(0x05);
204     LCD_ILI9486_Parameter(0x47);
205     LCD_ILI9486_Parameter(0x75);
206     LCD_ILI9486_Parameter(0x37);
207     LCD_ILI9486_Parameter(0x06);
208     LCD_ILI9486_Parameter(0x10);
209     LCD_ILI9486_Parameter(0x03);
210     LCD_ILI9486_Parameter(0x24);
211     LCD_ILI9486_Parameter(0x20);
212     LCD_ILI9486_Parameter(0x00);
213     LCD_ILI9486_CMD(0x11);   //退出睡眠
214     systick_delay_ms(120);
215     LCD_ILI9486_CMD(0x29);   //开启显示
216     
217     /* 以下由用户按实际编写 */
218     LCD_Clear(0,319,0,479,0xffff);  //把屏幕清成白色
219     GPIOB->ODR |= 1 << 15;          //开LCD背光灯
220 

 

以上是关于STM32 LCD和触摸屏的主要内容,如果未能解决你的问题,请参考以下文章

基于STM32设计的计算器(实现基本运算)

基于STM32设计的计算器(实现基本运算)

基于STM32设计的指针式电子钟与万年历

如何用stm32cubemx 设置 FLASH读写配置

正点原子4.3寸屏

STM32f103的电阻触摸屏的五点校正算法