二、UCGUI触摸屏驱动移植

一、例程代码:redis

  1 /*************************************************************************************************************************************
  2 *  
  3 *  文件名称:main.c
  4 *  文件功能:主函数文件
  5 *
  6 ***************************************************************************************************************************************/
  7 
  8 #include "pbdata.h"//调用自定义公共函数库
  9 
 10 
 11 /*********************************************************************************
 12 *
 13 * 初始化操做
 14 *
 15 *********************************************************************************/
 16 void RCC_Configuration(void);//系统时钟初始化函数声明
 17 void GPIO_Configuration(void);//GPIO初始化函数声明
 18 void NVIC_Configuration(void);//中断优先级配置函数声明
 19 void USART_Configuration(void);//串口配置函数声明
 20 
 21 
 22 
 23 
 24 /********************************************************************************
 25 *
 26 *  函数名称:main(void)
 27 *  函数功能:主函数
 28 *  函数说明:不能用void定义主函数
 29 *
 30 ********************************************************************************/
 31 int main(void)//void不能void定义主函数
 32 {
 33         
 34     RCC_Configuration();    //系统时钟初始化
 35     
 36     GPIO_Configuration();//端口初始化
 37     
 38     USART_Configuration();//串口配置
 39     
 40     NVIC_Configuration();//中断优先级配置
 41     
 42     FSMC_Configuration();//FSMC配置
 43     
 44     ILI9325_Init();//LCD配置
 45     
 46     SPI1_Configuration();//SPI1配置
 47     
 48     Touch_GPIO();//触摸屏GPIO配置
 49     
 50     GUI_Init();//GUI初始化
 51   GUI_SetBkColor(GUI_BLUE);//设置背景颜色为蓝色
 52   GUI_Clear();//清屏
 53     
 54     GUI_SetFont(&GUI_Font24_ASCII);//字体设置
 55   GUI_SetColor(GUI_RED);//颜色
 56     
 57     
 58     GUI_DispStringAt("liubo",40,150);//显示字符
 59 
 60 
 61     GUI_CURSOR_Show();//鼠标显示,在触屏上显示鼠标箭头
 62     
 63     
 64     
 65     /*主程序,每次隔1s显示不一样的颜色*/
 66      while(1)
 67      {
 68          if(PEN==0)//若是有触控
 69          {
 70              GUI_TOUCH_Exec();
 71          }
 72          WM_Exec();//刷新整个窗体
 73          delay_ms(20);//延时
 74      }
 75 }
 76 
 77     
 78 
 79 
 80 
 81 /********************************************************************************
 82 *
 83 *  函数名称:RCC_Configuration(void)
 84 *  函数功能:系统时钟高初始化函数
 85 *
 86 ********************************************************************************/
 87     void RCC_Configuration(void)//系统时钟高初始化函数
 88   {
 89         
 90     SystemInit();//系统初始化
 91     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能
 92         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能
 93         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用
 94     
 95         /*LCD使用的引脚配置时钟*/
 96         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
 97       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
 98       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
 99         
100         
101         /*触摸屏使用的引脚时钟配置*/
102         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
103         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
104         RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
105         
106         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE);//FSMC时钟配置
107         
108         
109     }
110     
111     
112 
113 /*******************************************************************************
114 *
115 * 函数名称:GPIO_Configuration(void)
116 * 函数功能:GPIO初始化函数
117 *
118 ********************************************************************************/    
119     
120     void GPIO_Configuration(void)//GPIO初始化函数
121   {
122               
123         
124 /*串口引脚配置*/
125         GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量
126 
127 /*输出引脚配置*/        
128         /*结构体变量赋值*/
129       GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX
130       GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
131         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出
132         /*对应的GPIO初始化*/
133       GPIO_Init(GPIOA,&GPIO_InitStructure);
134         
135     
136 /*输入引脚配置*/        
137         /*结构体变量赋值*/
138       GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX
139         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入
140         /*对应的GPIO初始化*/
141       GPIO_Init(GPIOA,&GPIO_InitStructure);        
142 
143         
144         
145 /*FSMC管脚初始化*/
146   /*结构体变量赋值*/
147     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;//引脚配置
148     GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//频率配置
149     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//输出
150     /*对应的GPIO初始化*/
151     GPIO_Init(GPIOD,&GPIO_InitStructure);//完成初始化
152     
153     GPIO_SetBits(GPIOD,GPIO_Pin_13);//打开背光
154     GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//复位
155     GPIO_Init(GPIOE,&GPIO_InitStructure);    
156     
157 /*启用fsmc复用功能,复用上拉模式*/
158     GPIO_InitStructure.GPIO_Pin =GPIO_Pin_14 //D0
159                                 |GPIO_Pin_15 //D1
160                                 |GPIO_Pin_0     //D2
161                                 |GPIO_Pin_1     //D3
162                                 |GPIO_Pin_8     //D13
163                                 |GPIO_Pin_9     //D14
164                                 |GPIO_Pin_10;//D15
165     GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
166     GPIO_Init(GPIOD,&GPIO_InitStructure);
167 
168     GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7     //D4
169                                 |GPIO_Pin_8     //D5
170                                 |GPIO_Pin_9     //D6
171                                 |GPIO_Pin_10 //D7
172                                 |GPIO_Pin_11 //D8
173                                 |GPIO_Pin_12 //D9
174                                 |GPIO_Pin_13 //D10
175                                 |GPIO_Pin_14 //D11
176                                 |GPIO_Pin_15;//D12
177     GPIO_Init(GPIOE,&GPIO_InitStructure);
178 
179 
180     GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11 //RS
181                                 |GPIO_Pin_4     //nOE
182                                 |GPIO_Pin_5; //nWE
183     GPIO_Init(GPIOD,&GPIO_InitStructure);
184 
185     GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7; //NE1
186     GPIO_Init(GPIOD,&GPIO_InitStructure);
187     
188     
189     
190     
191 }
192         
193         
194 
195     
196 
197 /****************************************************************************
198 *
199 *  函数名称:NVIC_Configuration(void)
200 *  函数功能:配置中断优先级
201 *
202 ****************************************************************************/
203     
204 void NVIC_Configuration(void)
205 {
206   NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量
207     
208   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组
209 
210   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
211   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级
212   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级
213   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能
214     
215   NVIC_Init(&NVIC_InitStructure);//初始化
216 }
217     
218 
219 /*********************************************************************************
220 *
221 *  函数名称:USART_Configuration(void)
222 *  函数功能:串口配置函数
223 *
224 *********************************************************************************/
225 void USART_Configuration(void)
226 {
227 /*定义串口配置结构体变量*/
228         USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量
229     
230     
231 /*结构体变量赋值*/
232     USART_InitStructure.USART_BaudRate = 9600;//波特率9600
233     USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位
234     USART_InitStructure.USART_StopBits = USART_StopBits_1;//中止位1
235     USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验
236     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止
237     USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能
238 
239     
240 /*发送串口配置初始化*/
241     USART_Init(USART1, &USART_InitStructure);
242     
243 
244 /*打开串口接收中断*/
245       USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断
246     
247     
248 /*打开串口*/
249     USART_Cmd(USART1,ENABLE);//串口使能,打开
250         
251 /*清空中断标志位*/
252     USART_ClearFlag(USART1,USART_FLAG_TC);
253 }
254     
255     
256     
257     
258     
259 
260     
261     
262 
263     
264 
265     
266     
267     
268     
269     
270     
271     
272     
273     
274     
275     
276     
277     
278     
279     
280     
281     
282     
283     
284     
285     
286     
287     
288     
289     
View Code
  1 /****************************************************************************************************************
  2 *
  3 * 文件名称:pbdata.c
  4 * 文件功能:自定义函数或者全局变量的初始化
  5 *
  6 ****************************************************************************************************************/
  7 
  8 /*头文件声明*/
  9 #include "pbdata.h"
 10 
 11 
 12 
 13 
 14 /********************************************************************************************
 15 *
 16 * 自定义全局变量
 17 *
 18 ********************************************************************************************/
 19 u8 dt=0;
 20 u16 touch_x=0;
 21 u16 touch_y=0;
 22 
 23 
 24 
 25 
 26 /******************************************************************************************
 27 *
 28 * 自定义函数
 29 *
 30 ******************************************************************************************/
 31 
 32 /*************************************
 33 *
 34 *  时钟配置
 35 *  HSE做为PLL时钟
 36 *  PLL做为SYSCLK
 37 *
 38 **************************************/
 39 void RCC_HSE_Configuration(void) 
 40 { 
 41    RCC_DeInit(); //将外设RCC寄存器重设为缺省值
 42    RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振HSE,HSE晶振打开ON
 43 
 44    if(RCC_WaitForHSEStartUp() == SUCCESS)//等待HSE起震,SUCCESS:HSE晶振稳定且就绪
 45     { 
 46 
 47    RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟
 48    RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速AHB时钟  
 49    RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速AHB时钟    
 50 
 51    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);///设置PLL时钟源及倍频系数 
 52    RCC_PLLCmd(ENABLE); //使能PLL
 53    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) ; ///检查指定的RCC标志位(PLL准备好标志)设置是否
 54     
 55    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);  //设置系统时钟 
 56    while(RCC_GetSYSCLKSource() != 0x08);     //0x08:PLL做为系统时钟
 57     
 58  } 
 59 } 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 /**************************************************
 68 *
 69 *  普通延时函数
 70 *
 71 ***************************************************/
 72 void delay(u32 nCount)
 73 {
 74     for(;nCount!=0;nCount--);
 75 }
 76 
 77 
 78 
 79 
 80 /**************************************************
 81 *
 82 *  函数名称:delay_us(u32 nus)
 83 *  函数功能:微秒延时函数
 84 *  输入参数:输入值为延时us
 85 *
 86 ***************************************************/
 87 void delay_us(u32 nus)
 88 {
 89     u32 temp;
 90     SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,因此是执行9次
 91     SysTick->VAL=0X00;//清空计数器,清空后,就自动设置本身设定的计数器的值
 92     SysTick->CTRL=0X01;//使能,减到零动做(不发生中断),采用外部时钟
 93     
 94     do
 95     {
 96           temp=SysTick->CTRL;//标志位,等到一直减到0
 97          }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达
 98     
 99     SysTick->CTRL=0x00; //关闭计数器
100     SysTick->VAL =0X00; //清空计数器
101 }
102 
103 
104 
105 
106 
107 
108 
109 /***************************************************
110 *
111 * 函数名称:delay_ms(u16 nms)
112 * 函数功能:毫秒级延时
113 * 输入参数:输入值位延时ms
114 *
115 ****************************************************/
116 void delay_ms(u16 nms)
117 {
118     u32 temp;
119     SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,因此是执行9000次
120     SysTick->VAL=0X00;//清空计数器,清空后,就自动设置本身设定的计数器的值
121     SysTick->CTRL=0X01;//使能,减到零动做(不发生中断),采用外部时钟
122     
123     do
124     {
125           temp=SysTick->CTRL;//标志位,等到一直减到0
126          }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达
127     
128     SysTick->CTRL=0x00; //关闭计数器
129     SysTick->VAL =0X00; //清空计数器
130 }
131 
132 
133 
134 /****************************************************
135 *
136 * 重定义printf函数部分
137 *
138 ****************************************************/
139 int fputc(int ch,FILE *F)
140 {
141     
142     USART_SendData(USART1,(u8)ch);
143     
144     while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位
145     
146     return ch;
147 }
View Code
 1 /*pbdata.h*/
 2 /***************************************************************************************************
 3 *
 4 * 文件名称:pbdata.h
 5 * 文件功能:自定义的函数和全局变量的声明头文件
 6 *
 7 ***************************************************************************************************/
 8 
 9 #ifndef _pbdata_H
10 #define _pbdata_H
11 
12 
13 
14 
15 
16 /********************************************************************
17 *
18 *  调用的头文件放在这里
19 *
20 ********************************************************************/
21 #include "stm32f10x.h"
22 
23 
24 #include "stm32f10x_rcc.h"
25 #include "stm32f10x_gpio.h"
26 #include "misc.h"
27 #include "stm32f10x_adc.h"
28 #include "stm32f10x_bkp.h"
29 #include "stm32f10x_can.h"
30 #include "stm32f10x_cec.h"
31 #include "stm32f10x_dac.h"
32 #include "stm32f10x_dbgmcu.h"
33 #include "stm32f10x_dma.h"
34 #include "stm32f10x_exti.h"
35 #include "stm32f10x_flash.h"
36 #include "stm32f10x_fsmc.h"
37 #include "stm32f10x_i2c.h"
38 #include "stm32f10x_iwdg.h"
39 #include "stm32f10x_pwr.h"
40 #include "stm32f10x_rtc.h"
41 #include "stm32f10x_sdio.h"
42 #include "stm32f10x_spi.h"
43 #include "stm32f10x_tim.h"
44 #include "stm32f10x_usart.h"
45 #include "stm32f10x_wwdg.h"
46 
47 
48 #include "lcd_ILI9325.h"//lcd配置
49 #include "stm32_fsmc.h"//fsmc通讯
50 
51 #include "stdio.h"
52 
53 #include "GUI.h"
54 
55 
56 #include "stm32_spi.h"
57 #include "stm32_touch.h"
58 
59 
60 
61 /********************************************************************
62 *
63 *  自定义全局变量声明
64 *
65 ********************************************************************/
66 extern u8 dt;
67 extern u16 touch_x;
68 extern u16 touch_y;
69 
70 
71 
72 
73 /********************************************************************
74 *
75 *  自定义全函数声明
76 *
77 ********************************************************************/
78 void delay(u32 nCount);
79 void delay_us(u32 nus);
80 void delay_ms(u16 nms);
81 int fputc(int ch,FILE *F);
82 
83 
84 
85 #endif
View Code
  1 /*************************************************************************************************************************************
  2 *  
  3 *  文件名称:stm32_spi.c
  4 *  文件功能:SPI相关函数
  5 *
  6 ***************************************************************************************************************************************/
  7 
  8 
  9 
 10 #include "pbdata.h"
 11 
 12 
 13 
 14 
 15 /*********************************************************************************
 16 *
 17 * 函数名称:void  SPI_Configuration(void)
 18 * 函数功能:SPI配置函数
 19 *
 20 *********************************************************************************/
 21 
 22 void  SPI1_Configuration(void)
 23 {
 24 
 25 /*定义一个SPI配置结构体变量*/
 26    SPI_InitTypeDef     SPI_InitStructure;
 27   
 28 /*定义一个SPI的GPIO配置结构体变量*/
 29    GPIO_InitTypeDef       GPIO_InitStructure;
 30 
 31 /*配置GPIO*/
 32   /*GPIO配置结构体变量赋值*/
 33    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;////引脚配置
 34    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
 35    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出
 36    /*对应的GPIO初始化*/
 37    GPIO_Init(GPIOA,&GPIO_InitStructure);
 38 
 39 
 40 /*以太网片选配置*/
 41    /*以太网GPIO配置结构体变量赋值*/
 42    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;//以太网引脚配置
 43    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
 44    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//发送配置
 45    /*对应的GPIO初始化*/
 46    GPIO_Init(GPIOB,&GPIO_InitStructure);
 47 
 48 
 49 /*触摸屏片选配置*/
 50    /*以太网GPIO配置结构体变量赋值*/
 51    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;//触摸屏引脚配置
 52    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
 53    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//发送配置
 54    /*对应的GPIO初始化*/
 55    GPIO_Init(GPIOC,&GPIO_InitStructure);
 56 
 57 
 58 
 59 /*Flash片选配置*/
 60    /*FlashGPIO配置结构体变量赋值*/
 61    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;//flash引脚配置
 62    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
 63    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//发送配置
 64    /*对应的GPIO初始化*/
 65    GPIO_Init(GPIOE,&GPIO_InitStructure);
 66 
 67 
 68 
 69 /*SPI1配置*/
 70    /*SPI1配置结构体变量赋值*/
 71    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//双线双向全双工
 72    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//CPU主模式
 73    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//8位发送
 74    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//悬空时为低
 75    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//第一个时钟沿捕获
 76    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//内部管理
 77    SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_64;//864分频
 78    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//高位开始发送
 79    SPI_InitStructure.SPI_CRCPolynomial = 7;
 80    /*SPI1初始化*/
 81    SPI_Init(SPI1, &SPI_InitStructure);
 82 
 83    SPI_Cmd(SPI1,ENABLE); //使能SPI
 84 
 85 
 86 
 87 /*禁止片选*/
 88     GPIO_SetBits(GPIOB,GPIO_Pin_7);//禁止以太网
 89     GPIO_SetBits(GPIOC,GPIO_Pin_4);//禁止触摸屏
 90     GPIO_SetBits(GPIOE,GPIO_Pin_6);//禁止flash
 91 
 92 }
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 /*********************************************************************************
102 *
103 * 函数名称:u8  SPI_SendByte(u8  byte)
104 * 函数功能:SPI发送数据函数
105 *
106 *********************************************************************************/
107 
108 u8  SPI1_SendByte(u8  byte)
109 {
110     while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);//判断是否忙
111     
112     SPI_I2S_SendData(SPI1,byte);//发送
113 
114     while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);//判断是否忙
115 
116     return  SPI_I2S_ReceiveData(SPI1);
117 
118 }
View Code
1 #ifndef _STM32_SPI_H
2 #define _STM32_SPI_H
3 
4 void SPI1_Configuration(void);//SPIÅäÖú¯Êý
5 u8 SPI1_SendByte(u8 byte);//SPI·¢ËÍÊý¾Ýº¯Êý
6 
7 #endif
View Code
 1 /*************************************************************************************************************************************
 2 *  
 3 *  文件名称:stm32_touch.c
 4 *  文件功能:触摸屏函数
 5 *
 6 ***************************************************************************************************************************************/
 7 #include "pbdata.h"
 8 
 9 
10 
11 
12 /********************************************************************************
13 *
14 *  函数名称:Touch_GPIO
15 *  函数功能:触摸屏GPIO初始化
16 *
17 ********************************************************************************/
18 void  Touch_GPIO(void)
19 {
20    GPIO_InitTypeDef  GPIO_InitStructure;//结构体变量定义
21 
22    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;//引脚配置
23    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
24 
25    GPIO_Init(GPIOB,&GPIO_InitStructure);//完成初始化
26 }
27 
28 
29 
30 
31 
32 
33 /********************************************************************************
34 *
35 *  函数名称:TPReadX
36 *  函数功能:读取X轴
37 *
38 ********************************************************************************/
39 u16  TPReadX(void)
40 {
41    u16  x=0;
42 
43    TP_CS();//拉低
44    delay_us(5);//延时
45    SPI1_SendByte(0xD0);//发送命令
46    delay_us(5);//延时
47    x=SPI1_SendByte(0x00);//读数据,8位到x,高8位
48    x<<=8;//移动到高8位
49    x+=SPI1_SendByte(0x00);//读数据,低8位读了后加到高位
50    delay_us(5);//延时
51    TP_DCS();//拉高
52      x>>=3;//16位,只有12位数,去除低3位
53      return x;
54 }
55 
56 
57 
58 
59 
60 
61 
62 
63 /********************************************************************************
64 *
65 *  函数名称:TPReadY
66 *  函数功能:读取Y轴
67 *
68 ********************************************************************************/
69 u16  TPReadY(void)
70 {
71    u16  y=0;
72 
73    TP_CS();//拉低
74    delay_us(5);//延时
75    SPI1_SendByte(0x90);//发送命令
76    delay_us(5);//延时
77    y=SPI1_SendByte(0x00);//读数据,8位到x,高8位
78    y<<=8;//移动到高8位
79    y+=SPI1_SendByte(0x00);//读数据,低8位读了后加到高位
80    delay_us(5);//延时
81    TP_DCS();//拉高
82    y>>=3;//16位,只有12位数,去除低3位
83    return y;
84 }
View Code
 1 /*************************************************************************************************************************************
 2 *  
 3 *  文件名称:stm32_touch.h
 4 *  文件功能:触摸屏函数声明
 5 *
 6 ***************************************************************************************************************************************/
 7 
 8 #ifndef  _STM32_TOUCH_H
 9 #define  _STM32_TOUCH_H
10 
11 #include "pbdata.h"
12 
13 #define  TP_CS()   GPIO_ResetBits(GPIOC,GPIO_Pin_4)//低电平片选
14 #define  TP_DCS()  GPIO_ResetBits(GPIOC,GPIO_Pin_4)//高电平释放
15 
16 #define PEN GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)
17 
18 
19 void  Touch_GPIO(void);//触摸屏GPIO初始化
20 u16  TPReadX(void);//读取X轴
21 u16  TPReadY(void);//读取Y轴
22 
23 
24 
25 #endif
View Code
 1 /*************************************************************************************************************************************
 2 *  
 3 *  文件名称:LCDConf.h
 4 *  文件功能:GUI配置文件
 5 *
 6 ***************************************************************************************************************************************/
 7 
 8 #ifndef LCDCONF_H
 9 #define LCDCONF_H
10 
11 
12 
13 
14 
15 
16 
17 
18 /*********************************************************************
19 *
20 *  LCD基本配置
21 *
22 **********************************************************************/
23 
24 /*触摸屏大小设置,3.2寸:240*320*/
25 #define LCD_XSIZE      (240)   
26 #define LCD_YSIZE      (320)   
27 
28 #define LCD_BITSPERPIXEL (16)//像素
29 #define LCD_SWAP_RB   (1) //红蓝转换
30 #define LCD_CONTROLLER  9325//驱动
31 
32 
33 #endif /* LCDCONF_H */
View Code
 1 /*************************************************************************************************************************************
 2 *  
 3 *  文件名称:GUIConf.h
 4 *  文件功能:GUI配置文件
 5 *
 6 ***************************************************************************************************************************************/
 7 
 8 /*********************************************************************************
 9 *
10 * 宏定义部分
11 *
12 *********************************************************************************/
13 
14 #ifndef GUICONF_H
15 #define GUICONF_H
16 
17 #define GUI_OS                    (0)  //UCOS操做系统支持定义,1打开,0关闭
18 #define GUI_SUPPORT_TOUCH         (1)  //触摸屏支持定义,1打开,0关闭
19 #define GUI_SUPPORT_MOUSE         (1)  //鼠标支持,1打开,0关闭
20 #define GUI_SUPPORT_UNICODE       (0)  //字符显示支持,1打开,0关闭
21 
22 
23 
24 #define GUI_DEFAULT_FONT          &GUI_Font6x8   //字体设置
25 #define GUI_ALLOC_SIZE            12500  //动态内存管理
26 
27 
28 
29 #define GUI_WINSUPPORT            1  //视窗管理,1打开,0关闭
30 #define GUI_SUPPORT_MEMDEV        1  //内存管理,1打开,0关闭
31 #define GUI_SUPPORT_AA            1  //抗锯齿,1打开,0关闭
32 
33 
34 
35 
36 #undef GUIDEMO_EN
37 #undef APPLICATION_EN
38 #define GUIDEMO_EN                   1 /* DemoTask */
39 #define APPLICATION_EN   !(GUIDEMO_EN) /* AppTask */ 
40 
41 
42 
43 #endif  /* Avoid multiple inclusion */
44 
45 
46                                                                                                                                                                                                                                                                                                                                                                                                                                                              
View Code
 1 /*
 2 *********************************************************************************************************
 3 *                                             uC/GUI V3.98
 4 *                        Universal graphic software for embedded applications
 5 *
 6 *                       (c) Copyright 2002, Micrium Inc., Weston, FL
 7 *                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
 8 *
 9 *              µC/GUI is protected by international copyright laws. Knowledge of the
10 *              source code may not be used to write a similar product. This file may
11 *              only be used in accordance with a license and should not be redistributed
12 *              in any way. We appreciate your understanding and fairness.
13 *
14 ----------------------------------------------------------------------
15 File        : GUITouch.Conf.h
16 Purpose     : Configures touch screen module
17 ----------------------------------------------------------------------
18 */
19 
20 
21 #ifndef GUITOUCH_CONF_H
22 #define GUITOUCH_CONF_H
23 
24 /*触摸AD*/
25 #define GUI_TOUCH_AD_LEFT    0   
26 #define GUI_TOUCH_AD_RIGHT   4095
27 #define GUI_TOUCH_AD_TOP     0   
28 #define GUI_TOUCH_AD_BOTTOM  4095
29 
30 
31 /*镜像,如果方向不对,可修改*/
32 #define GUI_TOUCH_SWAP_XY    0
33 #define GUI_TOUCH_MIRROR_X   0
34 #define GUI_TOUCH_MIRROR_Y   1
35 
36 #endif /* GUITOUCH_CONF_H */
37                                                                                                                                                                                                                                                                                                                                                                                                                                                              
View Code
  1 /*************************************************************************************************************************************
  2 *  
  3 *  文件名称:GUI_X_Touch.c
  4 *  文件功能:主函数文件
  5 *
  6 ***************************************************************************************************************************************/
  7 #include "pbdata.h"
  8 
  9 
 10 
 11 
 12 
 13 /*********************************************************************************
 14 *
 15 * 函数名称:GUI_TOUCH_X_ActivateX
 16 * 函数功能:
 17 * 输入参数:
 18 * 输出参数:
 19 * 函数说明:
 20 *
 21 *********************************************************************************/
 22 /*void GUI_TOUCH_X_ActivateX(void)
 23 {
 24 }
 25 */
 26 
 27 void GUI_TOUCH_X_ActivateX(void)
 28 {
 29 
 30 }
 31 
 32 
 33 
 34 
 35 
 36 /*********************************************************************************
 37 *
 38 * 函数名称:GUI_TOUCH_X_ActivateY
 39 * 函数功能:
 40 * 输入参数:
 41 * 输出参数:
 42 * 函数说明:
 43 *
 44 *********************************************************************************/
 45 /*void GUI_TOUCH_X_ActivateY(void)
 46 {
 47 }*/
 48 
 49 
 50 void GUI_TOUCH_X_ActivateY(void)
 51 {
 52 
 53 }
 54 
 55 
 56 
 57 
 58 
 59 /*********************************************************************************
 60 *
 61 * 函数名称:GUI_TOUCH_X_MeasureX
 62 * 函数功能:
 63 * 输入参数:
 64 * 输出参数:
 65 * 函数说明:
 66 *
 67 *********************************************************************************/
 68 /*
 69 int  GUI_TOUCH_X_MeasureX(void)
 70 {
 71     u16 count=0;
 72     u16 buffer[10]={0};
 73     u32 tp_x=0;
 74       
 75   do
 76   {
 77         buffer[count]=TPReadX();//保存X轴的值
 78     count++;
 79   } while((PEN==0)&&count<10);
 80 
 81    if(count==10)
 82    {
 83       tp_x=0;
 84       for(count=0;count<10;count++)
 85       {
 86         tp_x=tp_x+buffer[count];//X轴值累加
 87       }
 88 
 89        /这个是AD出来的值/
 90        touch_x=tp_x/10;
 91         }
 92         
 93         return touch_x;
 94             
 95  }
 96 */
 97 int GUI_TOUCH_X_MeasureX(void)
 98 {
 99    u16 count=0;
100    u16 buffer[10]={0};
101    u32 tp_x=0;
102    
103    do
104    {
105          buffer[count]=TPReadX();
106       count++;
107    }while((PEN==0)&&count<10);
108 
109    if(count==10)
110    {
111           tp_x=0;
112        for(count=0;count<10;count++)
113        {
114                tp_x=tp_x+buffer[count];            
115        }
116 
117        touch_x=tp_x/10;      
118    }
119      
120      return touch_x;    
121  }
122  
123 
124 
125 
126 
127 
128 
129 
130 /*********************************************************************************
131 *
132 * 函数名称:GUI_TOUCH_X_MeasureY
133 * 函数功能:
134 * 输入参数:
135 * 输出参数:
136 * 函数说明:
137 *
138 *********************************************************************************/
139 
140 /*
141 int  GUI_TOUCH_X_MeasureY(void)
142 {
143     u16 count=0;
144     u16 buffer[10]={0};
145     u32 tp_y=0;
146       
147   do
148   {
149         buffer[count]=TPReadY();//保存X轴的值
150     count++;
151   } while((PEN==0)&&count<10);
152 
153    if(count==10)
154    {
155       tp_y=0;
156       for(count=0;count<10;count++)
157       {
158         tp_y=tp_y+buffer[count];//X轴值累加
159       }
160 
161        /这个是AD出来的值/
162        touch_y=tp_y/10;
163         }
164         
165         return touch_y;
166             
167  }
168 
169 
170 */
171 
172 
173 
174 int GUI_TOUCH_X_MeasureY(void)
175 {
176    u16 count=0;
177    u16 buffer[10]={0};
178    u32 tp_y=0;
179    
180    do
181    {
182          buffer[count]=TPReadY();
183       count++;
184    }while((PEN==0)&&count<10);
185 
186    if(count==10)
187    {
188           tp_y=0;
189        for(count=0;count<10;count++)
190        {
191                tp_y=tp_y+buffer[count];            
192        }
193 
194        touch_y=tp_y/10;      
195    }
196 
197    return touch_y;
198 }
View Code

 

 

 

二、工程下载:app

 http://yunpan.cn/cQD6Jw956PV48 (提取码:8b1e)ide