20、LCD触摸屏
一、LCD彩色液晶屏工作原理
1、STM32与触摸屏连接方法
PD7-LCD-CS:LCD片选信号。
PE1-LCD-RST:LCD复位信号。
PD5-nWE:写使能,连接LCD的RW脚。
PD4-nOE:输出使能连接LCD的RD脚。
PD11-A16-RS:命令/数据标志
(0,读写命令;1,读写数据)。
D[15:0]:16位双向数据线。
PD13-LIGHT-PWM:LCD背光控制。
2、触摸屏颜色设置
24位转成16位格式公式:
#define RGB565(r, g, b) ((r >> 3) << 11 | (g >> 2) << 5 | (b >> 3))
3、ILI9320常用命令
(R0),这个命令,有两个功能,如果对它写,则最低位为OSC,用于开启或关闭振荡器。而如果对它读操作,则返回的是控制器的型号。
(R32、R33),设置GRAM的行地址和列地址。R32用于设置列地址(X坐标,0~239),R33用于设置行地址(Y坐标,0~319)。当我们要在某个指定点写入一个颜色的时候,先通过这两个命令设置到该点,然后写入颜色值就可以了。
(R80~R83),行列GRAM地址位置设置。这几个命令用于设定你显示区域的大小,我们整个屏的大小为240*320,但是有时候我们只需要在其中的一部分区域写入数据,如果用先写坐标,后写数据这样的方式来实现,则速度大打折扣。此时我们就可以通过这几个命令,在其中开辟一个区域,然后不停的丢数据,地址计数器就会根据R3的设置自动增加/减少,这样就不需要频繁的写地址了,大大提高了刷新的速度。
(R34),写数据到GRAM命令,当写入了这个命令之后,地址计数器才会自动的增加和减少。该命令是我们要介绍的这一组命令里面唯一的单个操作的命令,只需要写入该值就可以了,其他的都是要先写入命令编号,然后写入操作数。
R3,入口模式命令。我们重点关注的是I/D0、I/D1、AM这3个位,因为这3个位控制了屏幕的显示方向。AM:控制GRAM更新方向。当AM=0的时候,地址以行方向更新。当AM=1的时候,地址以列方向更新。I/D[1:0]:当更新了一个数据之后,根据这两个位的设置来控制地址计数器自动增加/减少1。
R7,显示控制命令。该命令CL位用来控制是8或16位彩色。当CL=1时是8位色,当CL=0时是16位色。D1、D0、BASEE这3个位用来控制屏幕显示状态。当全部设置成1时显示开启,全部设置成0时显示关闭。我们一般通过该命令来设置液晶屏在空闲时显示状态,以降低功耗。
4、触摸屏操作步骤
TFTLCD显示需要的相关设置步骤如下:
1)设置STM32与TFTLCD模块相连接的IO。 这一步,先将我们与TFTLCD模块相连的IO口进行初始化,以便驱动LCD。
2)初始化TFTLCD模块。 通过向TFTLCD写入一系列的设置,来启动TFTLCD的显示。为后 续显示字符和数字做准备。
3)通过函数将字符和数字显示到TFTLCD模块上。 这里就是通过我们设计的程序,将要显示的字符送到TFTLCD模 块就可以了。
二、STM32 FSMC工作原理 ( FSMC全称“静态存储器控制器”)
1、FSMC功能介绍
● 将AHB传输信号转换到适当的外部设备协议
● 满足访问外部设备的时序要求
所有的外部存储器共享控制器输出的地址、数据和控制信号,每个外部设备可以通过一个唯一的片选信号加以区分。FSMC在任一时刻只访问一个外部设备。
● 具有静态存储器接口的器件包括:
─静态随机存储器(SRAM)
─只读存储器(ROM)
─NOR闪存
─PSRAM(4个存储器块)
● 两个NAND闪存块。
● 16位的PC卡兼容设备
● 8或16位数据总线
● 每一个存储器块都有独立的片选控制
● 每一个存储器块都可以独立配置
● 时序可编程以支持各种不同的器件:
─等待周期可编程(多达15个周期)
─总线恢复周期可编程(多达15个周期)
─输出使能和写使能延迟可编程(多达15周期)
─独立的读写时序和协议,可支持宽范围的存储器和时序
● PSRAM和SRAM器件使用的写使能和字节选择输出
● 将32位的AHB访问请求,转换到连续的16位或8位,对外部16位或8位器件的访问
2、
3、FSMC框图
FSMC 包含以下四个模块:
(1)AHB 接口(包含 FSMC 配置寄存器)
(2)NOR 闪存和 PSRAM 控制器
(3)NAND 闪存和 PC 卡控制器
(4)外部设备接口
要注意的是,FSMC 可以请求 AHB 进行数据宽度的操作。如果 AHB 操作的数据宽度大于外部设备(NOR 或 NAND 或 LCD)的宽度,此时 FSMC 将 AHB操作分割成几个连续的较小的数据宽度,以适应外部设备的数据宽度。
4、FSMC 对外部设备的地址映像
从FSMC的角度看,可以把外部存储器划分为固定大小为256M字节的四个存储块,见下图。
● 存储块1用于访问最多4个NOR闪存或PSRAM存储设备。4个NOR/PSRAM区有4个专用的片选。
● 存储块2和3用于访问NAND闪存设备,每个存储块连接一个NAND闪存。
● 存储块4用于访问PC卡设备每一个存储块上的存储器类型是由用户在配置寄存器中定义的。
5、NOR和PSRAM地址映像
从上图可以看出,FSMC 对外部设备的地址映像从 0x6000 0000 开始,到0x9FFF FFFF 结束,共分 4 个地址块,每个地址块 256M 字节。可以看出,每个
地址块又分为 4 个分地址块,大小 64M。对 NOR 的地址映像来说,我们可以通过选择 HADDR[27:26]来确定当前使用的是哪个 64M 的分地址块,如下页表格。
而这四个分存储块的片选,则使用 NE[4:1]来选择。数据线/地址线/控制线是共享的。
NE1-NE4,对应的引脚为:PD7—NE1,PG9—NE2,PG10-NE3,PG12—NE4
6、HADDR与FSMC_A对应关系
HADDR是需要转换到外部存储器的内部AHB地址线。
HADDR[25:0]包含外部存储器地址。HADDR是字节地址,而存储器访问不都是按字节访问,因此接到存储器的地址线依存储器的数据宽度有所不同,如下表:
对于16位宽度的外部存储器,FSMC将在内部使用HADDR[25:1]产生外部存储器的地址FSMC_A[24:0]。不论外部存储器的宽度是多少(16位或8位),
FSMC_A[0]始终应该连到外部存储器的地址线A[0]。
7、STM32与触摸屏连接方法
PD7-LCD-CS:LCD片选信号。
PE1-LCD-RST:LCD复位信号。
PD5-nWE:写使能,连接LCD的RW脚。
PD4-nOE:输出使能连接LCD的RD脚。
PD11-A16-RS:命令/数据标志
(0,读写命令;1,读写数据)。
D[15:0]:16位双向数据线。
PD13-LIGHT-PWM:LCD背光控制。
8、把TFTLCD当成SRAM设备用
这里我们介绍下为什么可以把TFTLCD当成SRAM设备用:首先我们了解下外部SRAM的连接。 外部SRAM的控制一般有:地址线(如A0-A18)、数据线(如D0-D15)、写信号(WE)、读信号(OE)、片选信号(CS),如果SRAM支持字节控制,那么还有UB/LB信号。 TFTLCD信号包括:RS、D0-D15、WR、RD、CS、RST和BL等,其中真正在操作LCD的时候需要用到的就只有:RS、D0-D15、WR、RD和CS。其操作时序和SRAM的控制完全类似,唯一不同就是TFTLCD有RS信号,但是没有地址信号。
TFTLCD通过RS信号来决定传送的数据是数据还是命令,本质上可以理解为一个地址信号,比如我们把RS接在A0上面,那么当FSMC控制器写地址0的时候,会使得A0变为0,对TFTLCD来说,就是写命令。而FSMC写地址1的时候,A0将会变为1,对TFTLCD来说,就是写数据了。这样,就把数据和命令分开了,他们其实就是对应SRAM操作的两个连续地址。当然RS也可以连接在其他地址线上,我们众想科技的LY-STM32开发板是把RS连接在A16上面的。
三、点亮LCD彩色液晶屏
1、点亮LCD彩色液晶屏的步骤
(1).打开FSMC外设时钟
(2).配置FSMC的GPIO管脚状态
(3).FSMC初始化配置
(4).调用LCD彩色液晶屏初始化函数
(5).编写LCD彩色液晶屏读写函数(命令/数据)
(6).发送数据到LCD彩色液晶屏的缓存里并显示
2、ILI9320常用命令
3、代码例程:
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 47 /*主程序,每次隔1s显示不同的颜色*/ 48 while(1) 49 { 50 delay_ms(1000); 51 ILI_9325_CLEAR(RED); 52 delay_ms(1000); 53 ILI_9325_CLEAR(GREEN); 54 delay_ms(1000); 55 ILI_9325_CLEAR(BLUE); 56 delay_ms(1000); 57 ILI_9325_CLEAR(BLACK); 58 delay_ms(1000); 59 ILI_9325_CLEAR(WHITE); 60 } 61 62 } 63 64 65 66 67 /******************************************************************************** 68 * 69 * 函数名称:RCC_Configuration(void) 70 * 函数功能:系统时钟高初始化函数 71 * 72 ********************************************************************************/ 73 void RCC_Configuration(void)//系统时钟高初始化函数 74 { 75 76 SystemInit();//系统初始化 77 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能 78 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能 79 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用 80 81 /*LCD使用的引脚配置时钟*/ 82 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 83 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); 84 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); 85 86 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE);//FSMC时钟配置 87 88 89 } 90 91 92 93 /******************************************************************************* 94 * 95 * 函数名称:GPIO_Configuration(void) 96 * 函数功能:GPIO初始化函数 97 * 98 ********************************************************************************/ 99 100 void GPIO_Configuration(void)//GPIO初始化函数 101 { 102 103 104 /*串口引脚配置*/ 105 GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 106 107 /*输出引脚配置*/ 108 /*结构体变量赋值*/ 109 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX 110 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率 111 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出 112 /*对应的GPIO初始化*/ 113 GPIO_Init(GPIOA,&GPIO_InitStructure); 114 115 116 /*输入引脚配置*/ 117 /*结构体变量赋值*/ 118 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX 119 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入 120 /*对应的GPIO初始化*/ 121 GPIO_Init(GPIOA,&GPIO_InitStructure); 122 123 124 125 /*FSMC管脚初始化*/ 126 /*结构体变量赋值*/ 127 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;//引脚配置 128 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//频率配置 129 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//输出 130 /*对应的GPIO初始化*/ 131 GPIO_Init(GPIOD,&GPIO_InitStructure);//完成初始化 132 133 GPIO_SetBits(GPIOD,GPIO_Pin_13);//打开背光 134 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//复位 135 GPIO_Init(GPIOE,&GPIO_InitStructure); 136 137 /*启用fsmc复用功能,复用上拉模式*/ 138 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_14 //D0 139 |GPIO_Pin_15 //D1 140 |GPIO_Pin_0 //D2 141 |GPIO_Pin_1 //D3 142 |GPIO_Pin_8 //D13 143 |GPIO_Pin_9 //D14 144 |GPIO_Pin_10;//D15 145 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; 146 GPIO_Init(GPIOD,&GPIO_InitStructure); 147 148 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7 //D4 149 |GPIO_Pin_8 //D5 150 |GPIO_Pin_9 //D6 151 |GPIO_Pin_10 //D7 152 |GPIO_Pin_11 //D8 153 |GPIO_Pin_12 //D9 154 |GPIO_Pin_13 //D10 155 |GPIO_Pin_14 //D11 156 |GPIO_Pin_15;//D12 157 GPIO_Init(GPIOE,&GPIO_InitStructure); 158 159 160 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11 //RS 161 |GPIO_Pin_4 //nOE 162 |GPIO_Pin_5; //nWE 163 GPIO_Init(GPIOD,&GPIO_InitStructure); 164 165 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7; //NE1 166 GPIO_Init(GPIOD,&GPIO_InitStructure); 167 168 169 170 171 } 172 173 174 175 176 177 /**************************************************************************** 178 * 179 * 函数名称:NVIC_Configuration(void) 180 * 函数功能:配置中断优先级 181 * 182 ****************************************************************************/ 183 184 void NVIC_Configuration(void) 185 { 186 NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量 187 188 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组 189 190 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 191 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 192 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级 193 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 194 195 NVIC_Init(&NVIC_InitStructure);//初始化 196 } 197 198 199 /********************************************************************************* 200 * 201 * 函数名称:USART_Configuration(void) 202 * 函数功能:串口配置函数 203 * 204 *********************************************************************************/ 205 void USART_Configuration(void) 206 { 207 /*定义串口配置结构体变量*/ 208 USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量 209 210 211 /*结构体变量赋值*/ 212 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 213 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位 214 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 215 USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验 216 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止 217 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能 218 219 220 /*发送串口配置初始化*/ 221 USART_Init(USART1, &USART_InitStructure); 222 223 224 /*打开串口接收中断*/ 225 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断 226 227 228 /*打开串口*/ 229 USART_Cmd(USART1,ENABLE);//串口使能,打开 230 231 /*清空中断标志位*/ 232 USART_ClearFlag(USART1,USART_FLAG_TC); 233 } 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
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 21 22 23 24 25 /****************************************************************************************** 26 * 27 * 自定义函数 28 * 29 ******************************************************************************************/ 30 31 /************************************* 32 * 33 * 时钟配置 34 * HSE作为PLL时钟 35 * PLL作为SYSCLK 36 * 37 **************************************/ 38 void RCC_HSE_Configuration(void) 39 { 40 RCC_DeInit(); //将外设RCC寄存器重设为缺省值 41 RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振HSE,HSE晶振打开ON 42 43 if(RCC_WaitForHSEStartUp() == SUCCESS)//等待HSE起震,SUCCESS:HSE晶振稳定且就绪 44 { 45 46 RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟 47 RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速AHB时钟 48 RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速AHB时钟 49 50 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);///设置PLL时钟源及倍频系数 51 RCC_PLLCmd(ENABLE); //使能PLL 52 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) ; ///检查指定的RCC标志位(PLL准备好标志)设置是否 53 54 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置系统时钟 55 while(RCC_GetSYSCLKSource() != 0x08); //0x08:PLL作为系统时钟 56 57 } 58 } 59 60 61 62 63 64 65 66 /************************************************** 67 * 68 * 普通延时函数 69 * 70 ***************************************************/ 71 void delay(u32 nCount) 72 { 73 for(;nCount!=0;nCount--); 74 } 75 76 77 78 79 /************************************************** 80 * 81 * 函数名称:delay_us(u32 nus) 82 * 函数功能:微秒延时函数 83 * 输入参数:输入值为延时us 84 * 85 ***************************************************/ 86 void delay_us(u32 nus) 87 { 88 u32 temp; 89 SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次 90 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 91 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 92 93 do 94 { 95 temp=SysTick->CTRL;//标志位,等到一直减到0 96 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 97 98 SysTick->CTRL=0x00; //关闭计数器 99 SysTick->VAL =0X00; //清空计数器 100 } 101 102 103 104 105 106 107 108 /*************************************************** 109 * 110 * 函数名称:delay_ms(u16 nms) 111 * 函数功能:毫秒级延时 112 * 输入参数:输入值位延时ms 113 * 114 ****************************************************/ 115 void delay_ms(u16 nms) 116 { 117 u32 temp; 118 SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次 119 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 120 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 121 122 do 123 { 124 temp=SysTick->CTRL;//标志位,等到一直减到0 125 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 126 127 SysTick->CTRL=0x00; //关闭计数器 128 SysTick->VAL =0X00; //清空计数器 129 } 130 131 132 133 /**************************************************** 134 * 135 * 重定义printf函数部分 136 * 137 ****************************************************/ 138 int fputc(int ch,FILE *F) 139 { 140 141 USART_SendData(USART1,(u8)ch); 142 143 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 144 145 return ch; 146 }
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 54 /******************************************************************** 55 * 56 * 自定义全局变量声明 57 * 58 ********************************************************************/ 59 extern u8 dt; 60 61 62 63 64 65 /******************************************************************** 66 * 67 * 自定义全函数声明 68 * 69 ********************************************************************/ 70 void delay(u32 nCount); 71 void delay_us(u32 nus); 72 void delay_ms(u16 nms); 73 int fputc(int ch,FILE *F); 74 75 76 77 #endif
View Code
1 /** 2 ****************************************************************************** 3 * @file GPIO/IOToggle/stm32f10x_it.c 4 * @author MCD Application Team 5 * @version V3.0.0 6 * @date 04/06/2009 7 * @brief Main Interrupt Service Routines. 8 * This file provides template for all exceptions handler and 9 * peripherals interrupt service routine. 10 ****************************************************************************** 11 * @copy 12 * 13 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 14 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 15 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 16 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 17 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 18 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 19 * 20 * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2> 21 */ 22 23 /* Includes ------------------------------------------------------------------*/ 24 #include "stm32f10x_it.h" 25 #include "stm32f10x_exti.h" 26 #include "stm32f10x_rcc.h" 27 #include "stm32f10x_tim.h" 28 #include "misc.h" 29 #include "pbdata.h" 30 31 void NMI_Handler(void) 32 { 33 } 34 35 36 37 /** 38 * @brief This function handles Hard Fault exception. 39 * @param None 40 * @retval : None 41 */ 42 void HardFault_Handler(void) 43 { 44 /* Go to infinite loop when Hard Fault exception occurs */ 45 while (1) 46 { 47 } 48 } 49 50 /** 51 * @brief This function handles Memory Manage exception. 52 * @param None 53 * @retval : None 54 */ 55 void MemManage_Handler(void) 56 { 57 /* Go to infinite loop when Memory Manage exception occurs */ 58 while (1) 59 { 60 } 61 } 62 63 /** 64 * @brief This function handles Bus Fault exception. 65 * @param None 66 * @retval : None 67 */ 68 void BusFault_Handler(void) 69 { 70 /* Go to infinite loop when Bus Fault exception occurs */ 71 while (1) 72 { 73 } 74 } 75 76 /** 77 * @brief This function handles Usage Fault exception. 78 * @param None 79 * @retval : None 80 */ 81 void UsageFault_Handler(void) 82 { 83 /* Go to infinite loop when Usage Fault exception occurs */ 84 while (1) 85 { 86 } 87 } 88 89 /** 90 * @brief This function handles SVCall exception. 91 * @param None 92 * @retval : None 93 */ 94 void SVC_Handler(void) 95 { 96 } 97 98 /** 99 * @brief This function handles Debug Monitor exception. 100 * @param None 101 * @retval : None 102 */ 103 void DebugMon_Handler(void) 104 { 105 } 106 107 /** 108 * @brief This function handles PendSV_Handler exception. 109 * @param None 110 * @retval : None 111 */ 112 void PendSV_Handler(void) 113 { 114 } 115 116 /** 117 * @brief This function handles SysTick Handler. 118 * @param None 119 * @retval : None 120 */ 121 void SysTick_Handler(void) 122 { 123 } 124 125 126 127 /**************************************************************************** 128 * 129 * 函数名称:void TIM3_IRQHandler(void) 130 * 外部中断处理函数 131 * 132 ****************************************************************************/ 133 134 135 void TIM3_IRQHandler(void) 136 { 137 TIM_ClearITPendingBit(TIM3,TIM_IT_Update); 138 139 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) 140 { 141 if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET) 142 { 143 //LED 熄灭 144 GPIO_SetBits(GPIOB,GPIO_Pin_5); 145 } 146 else 147 { 148 //LED 发光 149 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 150 } 151 } 152 153 } 154 155 156 157 158 /**************************************************************************** 159 * 160 * 函数名称:void EXTI9_5_IRQHandler(void) 161 * 外部中断处理函数 162 * 163 ****************************************************************************/ 164 void EXTI9_5_IRQHandler(void) 165 { 166 if(EXTI_GetITStatus(EXTI_Line5)==SET) //判断是否产生了中断 167 { 168 EXTI_ClearITPendingBit(EXTI_Line5);//中断清除 169 170 delay_ms(10);//延时消抖 171 172 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) //再次判断,消抖 173 { 174 if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)//如果LED是亮 175 { 176 GPIO_SetBits(GPIOB,GPIO_Pin_5); //则熄灭 177 } 178 else 179 { 180 GPIO_ResetBits(GPIOB,GPIO_Pin_5);//如果是熄灭的,就亮 181 } 182 } 183 } 184 } 185 186 187 188 189 190 191 192 193 194 /**************************************************************************** 195 * Ļ Ԇúvoid EXTI1_IRQHandler(void) 196 * ٦ ŜúEXTI2אԦmԌѲ 197 * ɫࠚӎ˽úϞ 198 * Զࠚӎ˽úϞ 199 * ˵ ķú 200 * ַԃרúϞ 201 ****************************************************************************/ 202 void EXTI1_IRQHandler(void) 203 { 204 205 } 206 207 /**************************************************************************** 208 * Ļ Ԇúvoid EXTI2_IRQHandler(void) 209 * ٦ ŜúEXTI2אԦmԌѲ 210 * ɫࠚӎ˽úϞ 211 * Զࠚӎ˽úϞ 212 * ˵ ķú 213 * ַԃרúϞ 214 ****************************************************************************/ 215 void EXTI2_IRQHandler(void) 216 { 217 218 } 219 220 /**************************************************************************** 221 * 222 * 函数名称:void EXTI3_IRQHandler(void) 223 * 函数功能:外部中断XTI3中断处理函数 224 * 225 ****************************************************************************/ 226 void EXTI3_IRQHandler(void) 227 { 228 229 230 } 231 232 233 234 235 /**************************************************************************** 236 * 237 * 函数名称:USART1_IRQHandler(void) 238 * 函数功能:串口中断1处理函数 239 * 240 ****************************************************************************/ 241 void USART1_IRQHandler(void) 242 { 243 244 if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断是否发生了串口中断 245 { 246 247 USART_SendData(USART1,USART_ReceiveData(USART1));//从串口中读取数据,然后发送出去 248 249 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 250 251 } 252 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:lcd_ILI9325.h 4 * 文件功能:LCD初始化头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #include "pbdata.h" 10 11 12 13 14 15 /********************************************* 16 * 17 * 写命令子函数 18 * 19 **********************************************/ 20 void LCD_WR_REG(u16 index) 21 { 22 *(__IO u16 *)(Bank1_LCD_C)=index;//把我们定义的数据读写首地址转化为指针,指向LCD真实的扩展地址 23 } 24 25 26 27 28 /********************************************* 29 * 30 * 写数据子函数 31 * 32 **********************************************/ 33 void LCD_WR_Data(u16 val) 34 { 35 *(__IO u16 *)(Bank1_LCD_D)=val;//把我们定义的命令读写首地址转换为指针,指向LCD真实扩展地址 36 } 37 38 39 40 41 /********************************************* 42 * 43 * 同时写命令和数据 44 * 45 **********************************************/ 46 47 void LCD_WR_CMD(u16 index,u16 val) 48 { 49 *(__IO u16 *)(Bank1_LCD_C)=index; 50 *(__IO u16 *)(Bank1_LCD_D)=val; 51 } 52 53 54 55 56 57 58 59 60 61 /********************************************* 62 * 63 * LCD初始化函数,出厂配置好 64 * 65 **********************************************/ 66 void ILI9325_Init(void) 67 { 68 GPIO_ResetBits(GPIOE, GPIO_Pin_1); //Ӳݾشλ 69 delay(0xAFFf); 70 GPIO_SetBits(GPIOE, GPIO_Pin_1 ); 71 delay(0xAFFf); 72 73 LCD_WR_CMD(0x0001, 0x0100); // set SS and SM bit 74 LCD_WR_CMD(0x0002, 0x0700); // set 1 line inversion 75 LCD_WR_CMD(0x0003, 0x1030); // set GRAM write direction and BGR=1. 76 LCD_WR_CMD(0x0004, 0x0000); // Resize register 77 LCD_WR_CMD(0x0008, 0x0207); // set the back porch and front porch 78 LCD_WR_CMD(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0] 79 LCD_WR_CMD(0x000A, 0x0000); // FMARK function 80 LCD_WR_CMD(0x000C, 0x0000); // RGB interface setting 81 LCD_WR_CMD(0x000D, 0x0000); // Frame marker Position 82 LCD_WR_CMD(0x000F, 0x0000); // RGB interface polarity 83 84 LCD_WR_CMD(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB 85 LCD_WR_CMD(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0] 86 LCD_WR_CMD(0x0012, 0x0000); // VREG1OUT voltage 87 LCD_WR_CMD(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude 88 LCD_WR_CMD(0x0007, 0x0001); 89 delay(12000); // Dis-charge capacitor power voltage 90 LCD_WR_CMD(0x0010, 0x1490); // SAP, BT[3:0], AP, DSTB, SLP, STB 91 LCD_WR_CMD(0x0011, 0x0227); // DC1[2:0], DC0[2:0], VC[2:0] 92 delay(15500); // Delay 50ms 93 LCD_WR_CMD(0x0012, 0x001C); // Internal reference voltage= Vci; 94 delay(15000); // Delay 50ms 95 LCD_WR_CMD(0x0013, 0x1A00); // Set VDV[4:0] for VCOM amplitude 96 LCD_WR_CMD(0x0029, 0x0025); // Set VCM[5:0] for VCOMH 97 LCD_WR_CMD(0x002B, 0x000C); // Set Frame Rate 98 delay(15000); // Delay 50ms 99 LCD_WR_CMD(0x0020, 0x0000); // GRAM horizontal Address 100 LCD_WR_CMD(0x0021, 0x0000); // GRAM Vertical Address 101 // ----------- Adjust the Gamma Curve ----------// 102 LCD_WR_CMD(0x0030, 0x0000); 103 LCD_WR_CMD(0x0031, 0x0506); 104 LCD_WR_CMD(0x0032, 0x0104); 105 LCD_WR_CMD(0x0035, 0x0207); 106 LCD_WR_CMD(0x0036, 0x000F); 107 LCD_WR_CMD(0x0037, 0x0306); 108 LCD_WR_CMD(0x0038, 0x0102); 109 LCD_WR_CMD(0x0039, 0x0707); 110 LCD_WR_CMD(0x003C, 0x0702); 111 LCD_WR_CMD(0x003D, 0x1604); 112 //------------------ Set GRAM area ---------------// 113 LCD_WR_CMD(0x0050, 0x0000); // Horizontal GRAM Start Address 114 LCD_WR_CMD(0x0051, 0x00EF); // Horizontal GRAM End Address 115 LCD_WR_CMD(0x0052, 0x0000); // Vertical GRAM Start Address 116 LCD_WR_CMD(0x0053, 0x013F); // Vertical GRAM Start Address 117 LCD_WR_CMD(0x0060, 0xA700); // Gate Scan Line 118 LCD_WR_CMD(0x0061, 0x0001); // NDL,VLE, REV 119 120 LCD_WR_CMD(0x006A, 0x0000); // set scrolling line 121 //-------------- Partial Display Control ---------// 122 LCD_WR_CMD(0x0080, 0x0000); 123 LCD_WR_CMD(0x0081, 0x0000); 124 LCD_WR_CMD(0x0082, 0x0000); 125 LCD_WR_CMD(0x0083, 0x0000); 126 LCD_WR_CMD(0x0084, 0x0000); 127 LCD_WR_CMD(0x0085, 0x0000); 128 //-------------- Panel Control -------------------// 129 LCD_WR_CMD(0x0090, 0x0010); 130 LCD_WR_CMD(0x0092, 0x0600); 131 LCD_WR_CMD(0x0007, 0x0133); // 262K color and display ON 132 } 133 134 135 136 137 138 139 /********************************************* 140 * 141 * 204*320,向触摸屏送数据,一个点一个点都要扫描到 142 * 143 **********************************************/ 144 void ILI_9325_Draw_Point(u8 x,u16 y,u16 color) 145 { 146 LCD_WR_CMD(0x50,x); //x起始 147 LCD_WR_CMD(0x51,x); //x结束 148 LCD_WR_CMD(0x52,y); //y起始 149 LCD_WR_CMD(0x53,y); //y结束 150 151 LCD_WR_CMD(0x20,x); 152 LCD_WR_CMD(0x21,y); 153 LCD_WR_REG(0x22); 154 LCD_WR_Data(color); 155 } 156 157 158 159 160 161 /********************************************* 162 * 163 * 同时写命令和数据 164 * 165 **********************************************/ 166 167 void ILI_9325_CLEAR(u16 color) 168 { 169 u16 i=0,j=0; 170 171 for(i=0;i<240;i++) 172 { 173 for(j=0;j<320;j++) 174 { 175 ILI_9325_Draw_Point(i,j,color); 176 } 177 } 178 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:lcd_ILI9325.h 4 * 文件功能:LCD初始化头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #ifndef _LCD_ILI9325_H 10 #define _LCD_ILI9325_H 11 #include "pbdata.h" 12 13 14 //6000 0000 15 //A16 16位通讯 16 17 #define Bank1_LCD_D (u32)0x60020000 //命令位是1,所以在16位通讯命令格式时地址为2000 18 #define Bank1_LCD_C (u32)0x60000000 //数据位是0,所以在16位通讯数据格式时地址为0 19 20 #define RGB565(r, g, b) ((r >> 3) << 11 | (g >> 2) << 5 | (b >> 3)) //三色转换 21 22 #define RED RGB565(255,0,0) //红色 23 #define GREEN RGB565(0,255,0) //绿色 24 #define BLUE RGB565(0,0,255) //蓝色 25 #define BLACK RGB565(0,0,0) //黑色 26 #define WHITE RGB565(255,255,255) //白色 27 28 29 30 31 void ILI9325_Init(void);//LCD初始化函数 32 void LCD_WR_REG(u16 index);//写命令函数 33 void LCD_WR_Data(u16 val);//写数据函数 34 void LCD_WR_CMD(u16 index,u16 val);//同时写命令和数据函数 35 void ILI_9325_Draw_Point(u8 x,u16 y,u16 color);//一个点写入颜色 36 void ILI_9325_CLEAR(u16 color);//清屏 37 38 #endif
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:stm32_fsmc.c 4 * 文件功能:fsmc配置函数文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #include "pbdata.h" 10 11 void FSMC_Configuration(void) 12 { 13 14 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; 15 FSMC_NORSRAMTimingInitTypeDef p; 16 17 p.FSMC_AddressSetupTime = 0x02;//地址建立时间 18 p.FSMC_AddressHoldTime = 0x00;//地址保持时间 19 p.FSMC_DataSetupTime = 0x05;//数据建立时间 20 p.FSMC_BusTurnAroundDuration = 0x00;//总线恢复时间 21 p.FSMC_CLKDivision = 0x00;//时钟分频 22 p.FSMC_DataLatency = 0x00;//数据保持时间 23 p.FSMC_AccessMode = FSMC_AccessMode_B; 24 25 26 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;//NOR FLASH的 BANK1 27 28 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;//数据线与地址线不复用 29 30 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;//存储类型NOR FLASH 31 32 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//数据宽度为16位 33 34 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;//使用异步写模式,禁止突发模式 35 36 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;//本成员的配置只在突发模式下有效,等效型号极性为低 37 38 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;//禁止非对其突发模式 39 40 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;//本成员配置仅在突发模式下有效,NWAIT信号在什么时期 41 42 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;//禁止突发写操作 43 44 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;//禁止突发写操作 45 46 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;//写使能 47 48 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;//禁止扩展模式,扩展模式可以使用独立的读、写模式 49 50 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;//配置读写时序 51 52 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;//配置写时序 53 54 55 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); //初始化FSMC 56 57 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); //使能FSMC BANK1_SRAM 58 59 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:stm32_fsmc.h 4 * 文件功能:fsmc配置函数头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 #ifndef _STM32_FSMC_H 9 #define _STM32_FSMC_H 10 11 #include "pbdata.h" 12 void FSMC_Configuration(void);//FSMC配置函数 13 #endif
View Code
4、工程下载:
http://download.csdn.net/detail/a1181803348/8817017
四、LCD彩色液晶屏显示汉字、英文、数字
1、代码例程:
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 u8 i=0; 34 35 RCC_Configuration(); //系统时钟初始化 36 37 GPIO_Configuration();//端口初始化 38 39 USART_Configuration();//串口配置 40 41 NVIC_Configuration();//中断优先级配置 42 43 FSMC_Configuration();//FSMC配置 44 45 ILI9325_Init();//LCD配置 46 47 delay_ms(1000);//延时 48 49 ILI_9325_CLEAR(WHITE);//清屏,白色 50 51 52 53 54 for(i=0;i<5;i++) 55 { 56 show_Font(10+i*30,50,i,RED,WHITE); 57 } 58 59 for(i=0;i<13;i++) 60 { 61 show_Str(10+i*15,100,i,RED,WHITE); 62 } 63 64 65 66 67 /*主程序*/ 68 while(1) 69 { 70 delay_ms(200); 71 TFT_Draw_Rectangle(10,150,230,160,RED); 72 delay_ms(200); 73 TFT_Draw_Rectangle(10,150,230,160,BLUE); 74 delay_ms(200); 75 TFT_Draw_Rectangle(10,150,230,160,GREEN); 76 } 77 78 } 79 80 81 82 83 /******************************************************************************** 84 * 85 * 函数名称:RCC_Configuration(void) 86 * 函数功能:系统时钟高初始化函数 87 * 88 ********************************************************************************/ 89 void RCC_Configuration(void)//系统时钟高初始化函数 90 { 91 92 SystemInit();//系统初始化 93 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能 94 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能 95 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用 96 97 /*LCD使用的引脚配置时钟*/ 98 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 99 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); 100 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); 101 102 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE);//FSMC时钟配置 103 104 105 } 106 107 108 109 /******************************************************************************* 110 * 111 * 函数名称:GPIO_Configuration(void) 112 * 函数功能:GPIO初始化函数 113 * 114 ********************************************************************************/ 115 116 void GPIO_Configuration(void)//GPIO初始化函数 117 { 118 119 120 /*串口引脚配置*/ 121 GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 122 123 /*输出引脚配置*/ 124 /*结构体变量赋值*/ 125 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX 126 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率 127 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出 128 /*对应的GPIO初始化*/ 129 GPIO_Init(GPIOA,&GPIO_InitStructure); 130 131 132 /*输入引脚配置*/ 133 /*结构体变量赋值*/ 134 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX 135 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入 136 /*对应的GPIO初始化*/ 137 GPIO_Init(GPIOA,&GPIO_InitStructure); 138 139 140 141 /*FSMC管脚初始化*/ 142 /*结构体变量赋值*/ 143 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;//引脚配置 144 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//频率配置 145 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//输出 146 /*对应的GPIO初始化*/ 147 GPIO_Init(GPIOD,&GPIO_InitStructure);//完成初始化 148 149 GPIO_SetBits(GPIOD,GPIO_Pin_13);//打开背光 150 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//复位 151 GPIO_Init(GPIOE,&GPIO_InitStructure); 152 153 /*启用fsmc复用功能,复用上拉模式*/ 154 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_14 //D0 155 |GPIO_Pin_15 //D1 156 |GPIO_Pin_0 //D2 157 |GPIO_Pin_1 //D3 158 |GPIO_Pin_8 //D13 159 |GPIO_Pin_9 //D14 160 |GPIO_Pin_10;//D15 161 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; 162 GPIO_Init(GPIOD,&GPIO_InitStructure); 163 164 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7 //D4 165 |GPIO_Pin_8 //D5 166 |GPIO_Pin_9 //D6 167 |GPIO_Pin_10 //D7 168 |GPIO_Pin_11 //D8 169 |GPIO_Pin_12 //D9 170 |GPIO_Pin_13 //D10 171 |GPIO_Pin_14 //D11 172 |GPIO_Pin_15;//D12 173 GPIO_Init(GPIOE,&GPIO_InitStructure); 174 175 176 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11 //RS 177 |GPIO_Pin_4 //nOE 178 |GPIO_Pin_5; //nWE 179 GPIO_Init(GPIOD,&GPIO_InitStructure); 180 181 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7; //NE1 182 GPIO_Init(GPIOD,&GPIO_InitStructure); 183 184 185 186 187 } 188 189 190 191 192 193 /**************************************************************************** 194 * 195 * 函数名称:NVIC_Configuration(void) 196 * 函数功能:配置中断优先级 197 * 198 ****************************************************************************/ 199 200 void NVIC_Configuration(void) 201 { 202 NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量 203 204 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组 205 206 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 207 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 208 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级 209 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 210 211 NVIC_Init(&NVIC_InitStructure);//初始化 212 } 213 214 215 /********************************************************************************* 216 * 217 * 函数名称:USART_Configuration(void) 218 * 函数功能:串口配置函数 219 * 220 *********************************************************************************/ 221 void USART_Configuration(void) 222 { 223 /*定义串口配置结构体变量*/ 224 USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量 225 226 227 /*结构体变量赋值*/ 228 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 229 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位 230 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 231 USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验 232 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止 233 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能 234 235 236 /*发送串口配置初始化*/ 237 USART_Init(USART1, &USART_InitStructure); 238 239 240 /*打开串口接收中断*/ 241 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断 242 243 244 /*打开串口*/ 245 USART_Cmd(USART1,ENABLE);//串口使能,打开 246 247 /*清空中断标志位*/ 248 USART_ClearFlag(USART1,USART_FLAG_TC); 249 } 250 251 252 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
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 21 22 23 24 25 /****************************************************************************************** 26 * 27 * 自定义函数 28 * 29 ******************************************************************************************/ 30 31 /************************************* 32 * 33 * 时钟配置 34 * HSE作为PLL时钟 35 * PLL作为SYSCLK 36 * 37 **************************************/ 38 void RCC_HSE_Configuration(void) 39 { 40 RCC_DeInit(); //将外设RCC寄存器重设为缺省值 41 RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振HSE,HSE晶振打开ON 42 43 if(RCC_WaitForHSEStartUp() == SUCCESS)//等待HSE起震,SUCCESS:HSE晶振稳定且就绪 44 { 45 46 RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟 47 RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速AHB时钟 48 RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速AHB时钟 49 50 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);///设置PLL时钟源及倍频系数 51 RCC_PLLCmd(ENABLE); //使能PLL 52 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) ; ///检查指定的RCC标志位(PLL准备好标志)设置是否 53 54 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置系统时钟 55 while(RCC_GetSYSCLKSource() != 0x08); //0x08:PLL作为系统时钟 56 57 } 58 } 59 60 61 62 63 64 65 66 /************************************************** 67 * 68 * 普通延时函数 69 * 70 ***************************************************/ 71 void delay(u32 nCount) 72 { 73 for(;nCount!=0;nCount--); 74 } 75 76 77 78 79 /************************************************** 80 * 81 * 函数名称:delay_us(u32 nus) 82 * 函数功能:微秒延时函数 83 * 输入参数:输入值为延时us 84 * 85 ***************************************************/ 86 void delay_us(u32 nus) 87 { 88 u32 temp; 89 SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次 90 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 91 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 92 93 do 94 { 95 temp=SysTick->CTRL;//标志位,等到一直减到0 96 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 97 98 SysTick->CTRL=0x00; //关闭计数器 99 SysTick->VAL =0X00; //清空计数器 100 } 101 102 103 104 105 106 107 108 /*************************************************** 109 * 110 * 函数名称:delay_ms(u16 nms) 111 * 函数功能:毫秒级延时 112 * 输入参数:输入值位延时ms 113 * 114 ****************************************************/ 115 void delay_ms(u16 nms) 116 { 117 u32 temp; 118 SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次 119 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 120 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 121 122 do 123 { 124 temp=SysTick->CTRL;//标志位,等到一直减到0 125 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 126 127 SysTick->CTRL=0x00; //关闭计数器 128 SysTick->VAL =0X00; //清空计数器 129 } 130 131 132 133 /**************************************************** 134 * 135 * 重定义printf函数部分 136 * 137 ****************************************************/ 138 int fputc(int ch,FILE *F) 139 { 140 141 USART_SendData(USART1,(u8)ch); 142 143 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 144 145 return ch; 146 }
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 54 /******************************************************************** 55 * 56 * 自定义全局变量声明 57 * 58 ********************************************************************/ 59 extern u8 dt; 60 61 62 63 64 65 /******************************************************************** 66 * 67 * 自定义全函数声明 68 * 69 ********************************************************************/ 70 void RCC_HSE_Configuration(void); 71 void delay(u32 nCount); 72 void delay_us(u32 nus); 73 void delay_ms(u16 nms); 74 int fputc(int ch,FILE *F); 75 76 77 78 #endif
View Code
1 /** 2 ****************************************************************************** 3 * @file GPIO/IOToggle/stm32f10x_it.c 4 * @author MCD Application Team 5 * @version V3.0.0 6 * @date 04/06/2009 7 * @brief Main Interrupt Service Routines. 8 * This file provides template for all exceptions handler and 9 * peripherals interrupt service routine. 10 ****************************************************************************** 11 * @copy 12 * 13 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 14 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 15 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 16 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 17 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 18 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 19 * 20 * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2> 21 */ 22 23 /* Includes ------------------------------------------------------------------*/ 24 #include "stm32f10x_it.h" 25 #include "stm32f10x_exti.h" 26 #include "stm32f10x_rcc.h" 27 #include "stm32f10x_tim.h" 28 #include "misc.h" 29 #include "pbdata.h" 30 31 void NMI_Handler(void) 32 { 33 } 34 35 36 37 /** 38 * @brief This function handles Hard Fault exception. 39 * @param None 40 * @retval : None 41 */ 42 void HardFault_Handler(void) 43 { 44 /* Go to infinite loop when Hard Fault exception occurs */ 45 while (1) 46 { 47 } 48 } 49 50 /** 51 * @brief This function handles Memory Manage exception. 52 * @param None 53 * @retval : None 54 */ 55 void MemManage_Handler(void) 56 { 57 /* Go to infinite loop when Memory Manage exception occurs */ 58 while (1) 59 { 60 } 61 } 62 63 /** 64 * @brief This function handles Bus Fault exception. 65 * @param None 66 * @retval : None 67 */ 68 void BusFault_Handler(void) 69 { 70 /* Go to infinite loop when Bus Fault exception occurs */ 71 while (1) 72 { 73 } 74 } 75 76 /** 77 * @brief This function handles Usage Fault exception. 78 * @param None 79 * @retval : None 80 */ 81 void UsageFault_Handler(void) 82 { 83 /* Go to infinite loop when Usage Fault exception occurs */ 84 while (1) 85 { 86 } 87 } 88 89 /** 90 * @brief This function handles SVCall exception. 91 * @param None 92 * @retval : None 93 */ 94 void SVC_Handler(void) 95 { 96 } 97 98 /** 99 * @brief This function handles Debug Monitor exception. 100 * @param None 101 * @retval : None 102 */ 103 void DebugMon_Handler(void) 104 { 105 } 106 107 /** 108 * @brief This function handles PendSV_Handler exception. 109 * @param None 110 * @retval : None 111 */ 112 void PendSV_Handler(void) 113 { 114 } 115 116 /** 117 * @brief This function handles SysTick Handler. 118 * @param None 119 * @retval : None 120 */ 121 void SysTick_Handler(void) 122 { 123 } 124 125 126 127 /**************************************************************************** 128 * 129 * 函数名称:void TIM3_IRQHandler(void) 130 * 外部中断处理函数 131 * 132 ****************************************************************************/ 133 134 135 void TIM3_IRQHandler(void) 136 { 137 TIM_ClearITPendingBit(TIM3,TIM_IT_Update); 138 139 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) 140 { 141 if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET) 142 { 143 //LED 熄灭 144 GPIO_SetBits(GPIOB,GPIO_Pin_5); 145 } 146 else 147 { 148 //LED 发光 149 GPIO_ResetBits(GPIOB,GPIO_Pin_5); 150 } 151 } 152 153 } 154 155 156 157 158 /**************************************************************************** 159 * 160 * 函数名称:void EXTI9_5_IRQHandler(void) 161 * 外部中断处理函数 162 * 163 ****************************************************************************/ 164 void EXTI9_5_IRQHandler(void) 165 { 166 if(EXTI_GetITStatus(EXTI_Line5)==SET) //判断是否产生了中断 167 { 168 EXTI_ClearITPendingBit(EXTI_Line5);//中断清除 169 170 delay_ms(10);//延时消抖 171 172 if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET) //再次判断,消抖 173 { 174 if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)//如果LED是亮 175 { 176 GPIO_SetBits(GPIOB,GPIO_Pin_5); //则熄灭 177 } 178 else 179 { 180 GPIO_ResetBits(GPIOB,GPIO_Pin_5);//如果是熄灭的,就亮 181 } 182 } 183 } 184 } 185 186 187 188 189 190 191 192 193 194 /**************************************************************************** 195 * Ļ Ԇúvoid EXTI1_IRQHandler(void) 196 * ٦ ŜúEXTI2אԦmԌѲ 197 * ɫࠚӎ˽úϞ 198 * Զࠚӎ˽úϞ 199 * ˵ ķú 200 * ַԃרúϞ 201 ****************************************************************************/ 202 void EXTI1_IRQHandler(void) 203 { 204 205 } 206 207 /**************************************************************************** 208 * Ļ Ԇúvoid EXTI2_IRQHandler(void) 209 * ٦ ŜúEXTI2אԦmԌѲ 210 * ɫࠚӎ˽úϞ 211 * Զࠚӎ˽úϞ 212 * ˵ ķú 213 * ַԃרúϞ 214 ****************************************************************************/ 215 void EXTI2_IRQHandler(void) 216 { 217 218 } 219 220 /**************************************************************************** 221 * 222 * 函数名称:void EXTI3_IRQHandler(void) 223 * 函数功能:外部中断XTI3中断处理函数 224 * 225 ****************************************************************************/ 226 void EXTI3_IRQHandler(void) 227 { 228 229 230 } 231 232 233 234 235 /**************************************************************************** 236 * 237 * 函数名称:USART1_IRQHandler(void) 238 * 函数功能:串口中断1处理函数 239 * 240 ****************************************************************************/ 241 void USART1_IRQHandler(void) 242 { 243 244 if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断是否发生了串口中断 245 { 246 247 USART_SendData(USART1,USART_ReceiveData(USART1));//从串口中读取数据,然后发送出去 248 249 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 250 251 } 252 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:lcd_ILI9325.h 4 * 文件功能:LCD初始化头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #include "pbdata.h" 10 11 12 13 14 15 /*写入的字,刘(0) 博(1) ,(2) 你(3) 好(4)*/ 16 const u8 font[5][72]={ 17 {0x00,0x00,0x00,0x02,0x00,0x04,0x02,0x00,0x08,0x02,0x00,0x10,0x02,0x20,0x20,0x02,0x10,0x40,0x22,0x08,0x80,0x3E,0x07,0x00,0x1A,0x0E,0x00,0x02,0x7B,0x00,0x03,0xC1,0xC0,0x03,0x00,0xE0,0x06,0x00,0x20,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xC8,0x04,0x00,0x08,0x00,0x00,0x04,0x00,0x00,0x06,0x7F,0xFF,0xFC,0x3F,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00},//刘 18 {0x00,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x01,0x00,0x00,0x7F,0xFF,0xFE,0x21,0x00,0x00,0x01,0x00,0x80,0x09,0x00,0x80,0x09,0x00,0x80,0x0B,0xFE,0xC0,0x0A,0x48,0xA0,0x0A,0x48,0xB8,0x0A,0x48,0x80,0x0A,0x48,0x80,0x7F,0xFC,0x80,0x0A,0x48,0x84,0x0A,0x48,0x84,0x4A,0x48,0x86,0x2A,0x4B,0xFC,0x3B,0xFC,0x80,0x08,0x00,0x80,0x08,0x01,0x80,0x00,0x00,0x80,0x00,0x00,0x00},//博 19 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC4,0x00,0x01,0xF8,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//, 20 {0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x10,0x00,0x00,0x60,0x00,0x00,0xC0,0x00,0x03,0xFF,0xFE,0x1C,0x80,0x04,0x78,0x00,0x10,0x20,0x10,0x20,0x00,0x60,0xC0,0x01,0x83,0x00,0x07,0x1E,0x08,0x3E,0x08,0x04,0x32,0x00,0x04,0x02,0x00,0x06,0x02,0xFF,0xFC,0x02,0x00,0x00,0x02,0x00,0x00,0x02,0x08,0x00,0x02,0x86,0x00,0x03,0x03,0x80,0x03,0x01,0xE0,0x00,0x00,0x40,0x00,0x00,0x00},//你 21 {0x00,0x00,0x00,0x01,0x00,0x02,0x01,0x00,0x04,0x01,0x07,0x08,0x01,0xFC,0x88,0x3F,0xC0,0x90,0x39,0x00,0x60,0x01,0x01,0xC0,0x01,0x3E,0x60,0x01,0xE0,0x30,0x00,0x08,0x10,0x08,0x08,0x00,0x08,0x08,0x00,0x08,0x08,0x04,0x08,0x08,0x04,0x08,0x08,0x06,0x08,0xFF,0xFC,0x09,0x08,0x00,0x0A,0x08,0x00,0x0C,0x08,0x00,0x0C,0x08,0x00,0x00,0x18,0x00,0x00,0x08,0x00,0x00,0x00,0x00}//好 22 }; 23 24 25 26 27 /*写入的字符,www.liubo.com*/ 28 const u8 str[13][36]={ 29 {0x00,0x20,0x00,0x00,0x38,0x00,0x00,0x2F,0x80,0x00,0x01,0xF8,0x00,0x20,0xF0,0x00,0x37,0x00,0x00,0x3F,0x00,0x00,0x21,0xF0,0x00,0x00,0xF0,0x00,0x27,0x00,0x00,0x38,0x00,0x00,0x20,0x00},//w0 30 {0x00,0x20,0x00,0x00,0x38,0x00,0x00,0x2F,0x80,0x00,0x01,0xF8,0x00,0x20,0xF0,0x00,0x37,0x00,0x00,0x3F,0x00,0x00,0x21,0xF0,0x00,0x00,0xF0,0x00,0x27,0x00,0x00,0x38,0x00,0x00,0x20,0x00},//w1 31 {0x00,0x20,0x00,0x00,0x38,0x00,0x00,0x2F,0x80,0x00,0x01,0xF8,0x00,0x20,0xF0,0x00,0x37,0x00,0x00,0x3F,0x00,0x00,0x21,0xF0,0x00,0x00,0xF0,0x00,0x27,0x00,0x00,0x38,0x00,0x00,0x20,0x00},//w2 32 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x38,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//.3 33 {0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x08,0x04,0x00,0x08,0x04,0x00,0x08,0x07,0xFF,0xF8,0x0F,0xFF,0xF8,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00},//l4 34 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x08,0x00,0x20,0x08,0x00,0x20,0x08,0x06,0x3F,0xF8,0x06,0x7F,0xF8,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00},//i5 35 {0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x3F,0xF0,0x00,0x7F,0xF8,0x00,0x00,0x18,0x00,0x00,0x08,0x00,0x00,0x08,0x00,0x20,0x10,0x00,0x3F,0xF8,0x00,0x7F,0xF0,0x00,0x00,0x10,0x00,0x00,0x00},//u6 36 {0x00,0x00,0x00,0x04,0x00,0x00,0x07,0xFF,0xF8,0x0F,0xFF,0xF0,0x00,0x18,0x10,0x00,0x10,0x08,0x00,0x20,0x08,0x00,0x20,0x08,0x00,0x30,0x18,0x00,0x1F,0xF0,0x00,0x0F,0xC0,0x00,0x00,0x00},//b7 37 {0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0x0F,0xF0,0x00,0x18,0x30,0x00,0x30,0x08,0x00,0x20,0x08,0x00,0x20,0x08,0x00,0x30,0x08,0x00,0x18,0x30,0x00,0x0F,0xF0,0x00,0x07,0xC0,0x00,0x00,0x00},//o8 38 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x38,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//.9 39 {0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0x1F,0xF0,0x00,0x18,0x30,0x00,0x20,0x08,0x00,0x20,0x08,0x00,0x20,0x08,0x00,0x3C,0x08,0x00,0x1C,0x10,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00},//c10 40 {0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0x0F,0xF0,0x00,0x18,0x30,0x00,0x30,0x08,0x00,0x20,0x08,0x00,0x20,0x08,0x00,0x30,0x08,0x00,0x18,0x30,0x00,0x0F,0xF0,0x00,0x07,0xC0,0x00,0x00,0x00},//o11 41 {0x00,0x20,0x08,0x00,0x3F,0xF8,0x00,0x3F,0xF8,0x00,0x10,0x08,0x00,0x20,0x08,0x00,0x3F,0xF8,0x00,0x1F,0xF8,0x00,0x10,0x08,0x00,0x20,0x08,0x00,0x3F,0xF8,0x00,0x3F,0xF8,0x00,0x00,0x08}//m12 42 }; 43 44 45 46 47 48 49 /********************************************* 50 * 51 * 写命令子函数 52 * 53 **********************************************/ 54 void LCD_WR_REG(u16 index) 55 { 56 *(__IO u16 *)(Bank1_LCD_C)=index;//把我们定义的数据读写首地址转化为指针,指向LCD真实的扩展地址 57 } 58 59 60 61 62 /********************************************* 63 * 64 * 写数据子函数 65 * 66 **********************************************/ 67 void LCD_WR_Data(u16 val) 68 { 69 *(__IO u16 *)(Bank1_LCD_D)=val;//把我们定义的命令读写首地址转换为指针,指向LCD真实扩展地址 70 } 71 72 73 74 75 /********************************************* 76 * 77 * 同时写命令和数据 78 * 79 **********************************************/ 80 81 void LCD_WR_CMD(u16 index,u16 val) 82 { 83 *(__IO u16 *)(Bank1_LCD_C)=index; 84 *(__IO u16 *)(Bank1_LCD_D)=val; 85 } 86 87 88 89 90 91 92 93 94 95 /********************************************* 96 * 97 * LCD初始化函数,出厂配置好 98 * 99 **********************************************/ 100 void ILI9325_Init(void) 101 { 102 GPIO_ResetBits(GPIOE, GPIO_Pin_1); //Ӳݾشλ 103 delay(0xAFFf); 104 GPIO_SetBits(GPIOE, GPIO_Pin_1 ); 105 delay(0xAFFf); 106 107 LCD_WR_CMD(0x0001, 0x0100); // set SS and SM bit 108 LCD_WR_CMD(0x0002, 0x0700); // set 1 line inversion 109 LCD_WR_CMD(0x0003, 0x1030); // set GRAM write direction and BGR=1. 110 LCD_WR_CMD(0x0004, 0x0000); // Resize register 111 LCD_WR_CMD(0x0008, 0x0207); // set the back porch and front porch 112 LCD_WR_CMD(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0] 113 LCD_WR_CMD(0x000A, 0x0000); // FMARK function 114 LCD_WR_CMD(0x000C, 0x0000); // RGB interface setting 115 LCD_WR_CMD(0x000D, 0x0000); // Frame marker Position 116 LCD_WR_CMD(0x000F, 0x0000); // RGB interface polarity 117 118 LCD_WR_CMD(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB 119 LCD_WR_CMD(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0] 120 LCD_WR_CMD(0x0012, 0x0000); // VREG1OUT voltage 121 LCD_WR_CMD(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude 122 LCD_WR_CMD(0x0007, 0x0001); 123 delay(12000); // Dis-charge capacitor power voltage 124 LCD_WR_CMD(0x0010, 0x1490); // SAP, BT[3:0], AP, DSTB, SLP, STB 125 LCD_WR_CMD(0x0011, 0x0227); // DC1[2:0], DC0[2:0], VC[2:0] 126 delay(15500); // Delay 50ms 127 LCD_WR_CMD(0x0012, 0x001C); // Internal reference voltage= Vci; 128 delay(15000); // Delay 50ms 129 LCD_WR_CMD(0x0013, 0x1A00); // Set VDV[4:0] for VCOM amplitude 130 LCD_WR_CMD(0x0029, 0x0025); // Set VCM[5:0] for VCOMH 131 LCD_WR_CMD(0x002B, 0x000C); // Set Frame Rate 132 delay(15000); // Delay 50ms 133 LCD_WR_CMD(0x0020, 0x0000); // GRAM horizontal Address 134 LCD_WR_CMD(0x0021, 0x0000); // GRAM Vertical Address 135 // ----------- Adjust the Gamma Curve ----------// 136 LCD_WR_CMD(0x0030, 0x0000); 137 LCD_WR_CMD(0x0031, 0x0506); 138 LCD_WR_CMD(0x0032, 0x0104); 139 LCD_WR_CMD(0x0035, 0x0207); 140 LCD_WR_CMD(0x0036, 0x000F); 141 LCD_WR_CMD(0x0037, 0x0306); 142 LCD_WR_CMD(0x0038, 0x0102); 143 LCD_WR_CMD(0x0039, 0x0707); 144 LCD_WR_CMD(0x003C, 0x0702); 145 LCD_WR_CMD(0x003D, 0x1604); 146 //------------------ Set GRAM area ---------------// 147 LCD_WR_CMD(0x0050, 0x0000); // Horizontal GRAM Start Address 148 LCD_WR_CMD(0x0051, 0x00EF); // Horizontal GRAM End Address 149 LCD_WR_CMD(0x0052, 0x0000); // Vertical GRAM Start Address 150 LCD_WR_CMD(0x0053, 0x013F); // Vertical GRAM Start Address 151 LCD_WR_CMD(0x0060, 0xA700); // Gate Scan Line 152 LCD_WR_CMD(0x0061, 0x0001); // NDL,VLE, REV 153 154 LCD_WR_CMD(0x006A, 0x0000); // set scrolling line 155 //-------------- Partial Display Control ---------// 156 LCD_WR_CMD(0x0080, 0x0000); 157 LCD_WR_CMD(0x0081, 0x0000); 158 LCD_WR_CMD(0x0082, 0x0000); 159 LCD_WR_CMD(0x0083, 0x0000); 160 LCD_WR_CMD(0x0084, 0x0000); 161 LCD_WR_CMD(0x0085, 0x0000); 162 //-------------- Panel Control -------------------// 163 LCD_WR_CMD(0x0090, 0x0010); 164 LCD_WR_CMD(0x0092, 0x0600); 165 LCD_WR_CMD(0x0007, 0x0133); // 262K color and display ON 166 } 167 168 169 170 171 172 173 /********************************************* 174 * 175 * 204*320,向触摸屏送数据,一个点一个点都要扫描到 176 * 177 **********************************************/ 178 void ILI_9325_Draw_Point(u8 x,u16 y,u16 color) 179 { 180 LCD_WR_CMD(0x50,x); //x起始 181 LCD_WR_CMD(0x51,x); //x结束 182 LCD_WR_CMD(0x52,y); //y起始 183 LCD_WR_CMD(0x53,y); //y结束 184 185 LCD_WR_CMD(0x20,x); 186 LCD_WR_CMD(0x21,y); 187 LCD_WR_REG(0x22); 188 LCD_WR_Data(color); 189 } 190 191 192 193 194 195 /********************************************* 196 * 197 * 清屏 198 * 199 **********************************************/ 200 201 void ILI_9325_CLEAR(u16 color) 202 { 203 u16 i=0,j=0; 204 205 for(i=0;i<240;i++) 206 { 207 for(j=0;j<320;j++) 208 { 209 ILI_9325_Draw_Point(i,j,color); 210 } 211 } 212 } 213 214 215 216 217 218 219 220 /********************************************* 221 * 222 * 显示汉字 223 * 参数说明: x, 行 224 * y, 列 225 * id, 第几个字 226 * qj, 写字的颜色 227 * bj, 背景的颜色 228 * 229 * 230 **********************************************/ 231 232 233 void show_Font(u16 x,u16 y,u8 id,u16 qj,u16 bj) 234 { 235 u8 k=0,temp=0,t=0,y0=0; 236 237 y0=y;//初值 238 239 for(k=0;k<72;k++) 240 { 241 temp=font[id][k]; 242 for(t=0;t<8;t++) 243 { 244 if(temp&0x80) 245 ILI_9325_Draw_Point(x,y,qj); 246 else 247 ILI_9325_Draw_Point(x,y,bj); 248 249 temp<<=1; 250 y++; 251 if((y-y0)==24) 252 { 253 y=y0; 254 x++; 255 break; 256 } 257 } 258 } 259 } 260 261 262 263 264 265 266 267 268 269 /********************************************* 270 * 271 * 显示字符 272 * 参数说明: x, 行 273 * y, 列 274 * id, 第几个字 275 * qj, 写字的颜色 276 * bj, 背景的颜色 277 * 278 * 279 **********************************************/ 280 281 void show_Str(u16 x,u16 y,u8 id,u16 qj,u16 bj) 282 { 283 u8 k=0,temp=0,t=0,y0=0; 284 285 y0=y;//Եֵ 286 287 for(k=0;k<36;k++) 288 { 289 temp=str[id][k]; 290 for(t=0;t<8;t++) 291 { 292 if(temp&0x80) 293 ILI_9325_Draw_Point(x,y,qj); 294 else 295 ILI_9325_Draw_Point(x,y,bj); 296 297 temp<<=1; 298 y++; 299 if((y-y0)==24) 300 { 301 y=y0; 302 x++; 303 break; 304 } 305 } 306 } 307 } 308 309 310 311 312 313 314 /********************************************* 315 * 316 * 画方块 317 * 318 **********************************************/ 319 320 void TFT_Draw_Rectangle(u16 x1,u16 y1,u16 x2,u16 y2,u16 color) 321 { 322 u16 i=0,j=0; 323 324 for(i=x1;i<x2;i++) 325 { 326 for(j=y1;j<y2;j++) 327 { 328 ILI_9325_Draw_Point(i,j,color); 329 } 330 } 331 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:lcd_ILI9325.h 4 * 文件功能:LCD初始化头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #ifndef _LCD_ILI9325_H 10 #define _LCD_ILI9325_H 11 #include "pbdata.h" 12 13 14 //6000 0000 15 //A16 16位通讯 16 17 #define Bank1_LCD_D (u32)0x60020000 //命令位是1,所以在16位通讯命令格式时地址为2000 18 #define Bank1_LCD_C (u32)0x60000000 //数据位是0,所以在16位通讯数据格式时地址为0 19 20 #define RGB565(r, g, b) ((r >> 3) << 11 | (g >> 2) << 5 | (b >> 3)) //三色转换 21 22 #define RED RGB565(255,0,0) //红色 23 #define GREEN RGB565(0,255,0) //绿色 24 #define BLUE RGB565(0,0,255) //蓝色 25 #define BLACK RGB565(0,0,0) //黑色 26 #define WHITE RGB565(255,255,255) //白色 27 28 29 30 31 void ILI9325_Init(void);//LCD初始化函数 32 void LCD_WR_REG(u16 index);//写命令函数 33 void LCD_WR_Data(u16 val);//写数据函数 34 void LCD_WR_CMD(u16 index,u16 val);//同时写命令和数据函数 35 void ILI_9325_Draw_Point(u8 x,u16 y,u16 color);//一个点写入颜色 36 void ILI_9325_CLEAR(u16 color);//清屏 37 38 39 void show_Font(u16 x,u16 y,u8 id,u16 qj,u16 bj);//写字 40 void show_Str(u16 x,u16 y,u8 id,u16 qj,u16 bj);//显示字符 41 void TFT_Draw_Rectangle(u16 x1,u16 y1,u16 x2,u16 y2,u16 color);//画方块 42 43 44 #endif
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:stm32_fsmc.c 4 * 文件功能:fsmc配置函数文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #include "pbdata.h" 10 11 void FSMC_Configuration(void) 12 { 13 14 FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; 15 FSMC_NORSRAMTimingInitTypeDef p; 16 17 p.FSMC_AddressSetupTime = 0x02;//地址建立时间 18 p.FSMC_AddressHoldTime = 0x00;//地址保持时间 19 p.FSMC_DataSetupTime = 0x05;//数据建立时间 20 p.FSMC_BusTurnAroundDuration = 0x00;//总线恢复时间 21 p.FSMC_CLKDivision = 0x00;//时钟分频 22 p.FSMC_DataLatency = 0x00;//数据保持时间 23 p.FSMC_AccessMode = FSMC_AccessMode_B; 24 25 26 FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;//NOR FLASH的 BANK1 27 28 FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;//数据线与地址线不复用 29 30 FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;//存储类型NOR FLASH 31 32 FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;//数据宽度为16位 33 34 FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;//使用异步写模式,禁止突发模式 35 36 FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;//本成员的配置只在突发模式下有效,等效型号极性为低 37 38 FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;//禁止非对其突发模式 39 40 FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;//本成员配置仅在突发模式下有效,NWAIT信号在什么时期 41 42 FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;//禁止突发写操作 43 44 FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;//禁止突发写操作 45 46 FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;//写使能 47 48 FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;//禁止扩展模式,扩展模式可以使用独立的读、写模式 49 50 FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;//配置读写时序 51 52 FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;//配置写时序 53 54 55 FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); //初始化FSMC 56 57 FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); //使能FSMC BANK1_SRAM 58 59 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:stm32_fsmc.h 4 * 文件功能:fsmc配置函数头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 #ifndef _STM32_FSMC_H 9 #define _STM32_FSMC_H 10 11 #include "pbdata.h" 12 void FSMC_Configuration(void);//FSMC配置函数 13 #endif
View Code
2、工程下载:
http://download.csdn.net/detail/a1181803348/8824129
五、触摸屏工作原理
1. 触摸屏结构
2、功能说明
XPT2046 是一款 4 线制电阻式触摸屏控制器,内含 12 位分辨率 125KHz 转换速率逐步逼近型 A/D 转换器。XPT2046 支持从 1.5V 到 5.25V 的低电压 I/O 接口。XPT2046 能通过执行两次 A/D 转换查出被按的屏幕位置, 除此之外,还可以测量加在触摸屏上的压力。内部自带 2.5V 参考电压,可以作为辅助输入、温度测量和电池监测之用,电池监测的电压范围可以从 0V 到 6V。XPT2046 片内集成有一个温度传感器。 在 2.7V 的典型工作状态下,关闭参考电压,功耗可小于 0.75mW。XPT2046采用微小的封装形式:TSSOP-16,QFN-16和VFBGA-48。工作温度范围为-40℃~+85℃。与ADS7846、TSC2046、AK4182A 完全兼容
3、主要特性
工作电压范围为1.5V~5.25V
支持1.5V~5.25V 的数字I/O口
内建2.5V参考电压源
电源电压测量(0V~6V)
内建结温测量功能
触摸压力测量
采用3线制SPI通信接口
具有自动省电功能
4. 芯片封装与管脚定义
5. 数字接口
XPT2046数据接口是串行接口,其典型工作时序如图12所示,图中展示的信号来自带有基本串行接口的单片机或数据信号处理器。处理器和转换器之间的通信需要8个时钟周期,可采用SPI同步串行接口。一次完整的转换需要24个串行同步时钟(DCLK)来完成。 前8个时钟用来通过DIN引脚输入控制字节。当转换器获取有关下一次转换的足够信息后,将进入采样模式。3个多时钟周期后,控制字节设置完成,转换器进入转换状态。接着的12个时钟周期将完成真正的模数转换,第13个时钟将输出转换结果的最后一位。剩下的3个多时钟周期将用来完成被转换器忽略的最后字节(DOUT置低)。
控制字节由DIN输入的控制字如表5所示,它用来启动转换,寻址,设置ADC分辨率,配置和对XPT2046进行掉电控制。表6给出控制字的各控制位的详细说明。
起始位——第一位,即S位。控制字的首位必须是1,即S=1。在XPT2046的DIN引脚检测到起始位前,所有的输入将被忽略。
地址——接下来的3位(A2、A1和A0)选择多路选择器的现行通道,触摸屏驱动和参考源输入。
MODE——模式选择位,用于设置ADC的分辨率。MODE=0,下一次的转换将是12位模式;MODE=1,下一次的转换将是8位模式。
SER/DFR——位控制参考源模式,选择单端模式(SER/DFR=1),或者差分模式(SER/DFR=0)。在X坐标、Y坐标和触摸压力测量中,为达到最佳性能,首选差分工作模式。参考电压来自开关驱动器的电压。在单端模式下,转换器的参考电压固定为VREF相对于GND引脚的电压。
PD0和PD1——ADC的内部参考电压可以单独关闭或者打开,但是,在转换前,需要额外的时间让内部参考电压稳定到最终稳定值;如果内部参考源处于掉电状态,还要确保有足够的唤醒时间。ADC要求是即时使用,无唤醒时间的。另外还得注意,当BUSY是高电平的时候,内部参考源禁止进入掉电模式。XPT2046的通道改变后,如果要关闭参考源,则要重新对XPT2046写入命令。
6、触摸屏结构
7、 数字接口
XPT2046数据接口是串行接口,其典型工作时序如图12所示,图中展示的信号来自带有基本串行接口的单片机或数据信号处理器。处理器和转换器之间的通信需要8个时钟周期,可采用SPI同步串行接口。一次完整的转换需要24个串行同步时钟(DCLK)来完成。
前8个时钟用来通过DIN引脚输入控制字节。当转换器获取有关下一次转换的足够信息后,将进入采样模式。3个多时钟周期后,控制字节设置完成,转换器进入转换状态。接着的12个时钟周期将完成真正的模数转换,第13个时钟将输出转换结果的最后一位。剩下的3个多时钟周期将用来完成被转换器忽略的最后字节(DOUT置低)。
控制字节由DIN输入的控制字如表5所示,它用来启动转换,寻址,设置ADC分辨率,配置和对XPT2046进行掉电控制。表6给出控制字的各控制位的详细说明。
起始位——第一位,即S位。控制字的首位必须是1,即S=1。在XPT2046的DIN引脚检测到起始位前,所有的输入将被忽略。
地址——接下来的3位(A2、A1和A0)选择多路选择器的现行通道,触摸屏驱动和参考源输入。
MODE——模式选择位,用于设置ADC的分辨率。MODE=0,下一次的转换将是12位模式;MODE=1,下一次的转换将是8位模式。
SER/DFR——位控制参考源模式,选择单端模式(SER/DFR=1),或者差分模式(SER/DFR=0)。在X坐标、Y坐标和触摸压力测量中,为达到最佳性能,首选差分工作模式。参考电压来自开关驱动器的电压。在单端模式下,转换器的参考电压固定为VREF相对于GND引脚的电压。
PD0和PD1——ADC的内部参考电压可以单独关闭或者打开,但是,在转换前,需要额外的时间让内部参考电压稳定到最终稳定值;如果内部参考源处于掉电状态,还要确保有足够的唤醒时间。ADC要求是即时使用,无唤醒时间的。另外还得注意,当BUSY是高电平的时候,内部参考源禁止进入掉电模式。XPT2046的通道改变后,如果要关闭参考源,则要重新对XPT2046写入命令。
8、代码例程:
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 u16 count=0;//用于计数 34 u16 buffer[2][10]={{0},{0}};//用于保存每次的值,最后求平均值 35 u32 tp_x=0,tp_y=0; 36 37 RCC_Configuration(); //系统时钟初始化 38 39 GPIO_Configuration();//端口初始化 40 41 USART_Configuration();//串口配置 42 43 NVIC_Configuration();//中断优先级配置 44 45 FSMC_Configuration();//FSMC配置 46 47 ILI9325_Init();//LCD配置 48 49 SPI1_Configuration();//SPI1配置 50 51 Touch_GPIO();//触摸GPIO初始化 52 53 delay_ms(1000); 54 ILI_9325_CLEAR(BLUE);//蓝色清屏 55 56 57 58 /*主程序,用手在屏幕上画线*/ 59 while(1) 60 { 61 if(PEN==0)//如果有触摸 62 { 63 count=0; 64 65 do 66 { 67 buffer[0][count]=TPReadX();//保存X轴的值 68 buffer[1][count]=TPReadY();//保存Y轴的值 69 count++; 70 } while((PEN==0)&&count<10); 71 72 if(count==10) 73 { 74 tp_x=0; 75 tp_y=0; 76 77 for(count=0;count<10;count++) 78 { 79 tp_x=tp_x+buffer[0][count];//X轴值累加 80 tp_y=tp_y+buffer[1][count];//Y轴值累加 81 } 82 83 /*这两个是AD出来的值*/ 84 tp_x=tp_x/10; 85 tp_y=tp_y/10; 86 /*实际触摸屏上的坐标值*/ 87 tp_x=tp_x/17; 88 tp_y=320-tp_y/13; 89 90 ILI_9325_Draw_Point(tp_x,tp_y,RED);//屏幕上画出来 91 92 } 93 } 94 } 95 } 96 97 98 99 100 101 /******************************************************************************** 102 * 103 * 函数名称:RCC_Configuration(void) 104 * 函数功能:系统时钟高初始化函数 105 * 106 ********************************************************************************/ 107 void RCC_Configuration(void)//系统时钟高初始化函数 108 { 109 110 SystemInit();//系统初始化 111 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//串口对应GPIO时钟使能 112 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口时钟使能 113 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//引脚复用 114 115 /*LCD使用的引脚配置时钟*/ 116 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); 117 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); 118 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); 119 120 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); 121 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); 122 123 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);//SPI时钟 124 125 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC,ENABLE);//FSMC时钟配置 126 127 128 } 129 130 131 132 /******************************************************************************* 133 * 134 * 函数名称:GPIO_Configuration(void) 135 * 函数功能:GPIO初始化函数 136 * 137 ********************************************************************************/ 138 139 void GPIO_Configuration(void)//GPIO初始化函数 140 { 141 142 143 /*串口引脚配置*/ 144 GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 145 146 /*输出引脚配置*/ 147 /*结构体变量赋值*/ 148 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;////引脚配置TX 149 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率 150 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//发送要配置成复用推挽输出 151 /*对应的GPIO初始化*/ 152 GPIO_Init(GPIOA,&GPIO_InitStructure); 153 154 155 /*输入引脚配置*/ 156 /*结构体变量赋值*/ 157 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;////引脚配置RX 158 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//接收引脚配置成浮空输入 159 /*对应的GPIO初始化*/ 160 GPIO_Init(GPIOA,&GPIO_InitStructure); 161 162 163 164 /*FSMC管脚初始化*/ 165 /*结构体变量赋值*/ 166 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;//引脚配置 167 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//频率配置 168 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//输出 169 /*对应的GPIO初始化*/ 170 GPIO_Init(GPIOD,&GPIO_InitStructure);//完成初始化 171 172 GPIO_SetBits(GPIOD,GPIO_Pin_13);//打开背光 173 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//复位 174 GPIO_Init(GPIOE,&GPIO_InitStructure); 175 176 /*启用fsmc复用功能,复用上拉模式*/ 177 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_14 //D0 178 |GPIO_Pin_15 //D1 179 |GPIO_Pin_0 //D2 180 |GPIO_Pin_1 //D3 181 |GPIO_Pin_8 //D13 182 |GPIO_Pin_9 //D14 183 |GPIO_Pin_10;//D15 184 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; 185 GPIO_Init(GPIOD,&GPIO_InitStructure); 186 187 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7 //D4 188 |GPIO_Pin_8 //D5 189 |GPIO_Pin_9 //D6 190 |GPIO_Pin_10 //D7 191 |GPIO_Pin_11 //D8 192 |GPIO_Pin_12 //D9 193 |GPIO_Pin_13 //D10 194 |GPIO_Pin_14 //D11 195 |GPIO_Pin_15;//D12 196 GPIO_Init(GPIOE,&GPIO_InitStructure); 197 198 199 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11 //RS 200 |GPIO_Pin_4 //nOE 201 |GPIO_Pin_5; //nWE 202 GPIO_Init(GPIOD,&GPIO_InitStructure); 203 204 GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7; //NE1 205 GPIO_Init(GPIOD,&GPIO_InitStructure); 206 207 208 209 210 } 211 212 213 214 215 216 /**************************************************************************** 217 * 218 * 函数名称:NVIC_Configuration(void) 219 * 函数功能:配置中断优先级 220 * 221 ****************************************************************************/ 222 223 void NVIC_Configuration(void) 224 { 225 NVIC_InitTypeDef NVIC_InitStructure; //定义一个优先级配置结构体变量 226 227 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//分组 228 229 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 230 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//抢断优先级 231 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;//响应优先级 232 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能 233 234 NVIC_Init(&NVIC_InitStructure);//初始化 235 } 236 237 238 /********************************************************************************* 239 * 240 * 函数名称:USART_Configuration(void) 241 * 函数功能:串口配置函数 242 * 243 *********************************************************************************/ 244 void USART_Configuration(void) 245 { 246 /*定义串口配置结构体变量*/ 247 USART_InitTypeDef USART_InitStructure;//定义一个串口配置结构体变量 248 249 250 /*结构体变量赋值*/ 251 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 252 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//位宽,8位 253 USART_InitStructure.USART_StopBits = USART_StopBits_1;//停止位1 254 USART_InitStructure.USART_Parity = USART_Parity_No;//不奇偶校验 255 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//流控禁止 256 USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送使能 257 258 259 /*发送串口配置初始化*/ 260 USART_Init(USART1, &USART_InitStructure); 261 262 263 /*打开串口接收中断*/ 264 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//当接收到数据时,会产生中断 265 266 267 /*打开串口*/ 268 USART_Cmd(USART1,ENABLE);//串口使能,打开 269 270 /*清空中断标志位*/ 271 USART_ClearFlag(USART1,USART_FLAG_TC); 272 } 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
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 21 22 23 24 25 /****************************************************************************************** 26 * 27 * 自定义函数 28 * 29 ******************************************************************************************/ 30 31 /************************************* 32 * 33 * 时钟配置 34 * HSE作为PLL时钟 35 * PLL作为SYSCLK 36 * 37 **************************************/ 38 void RCC_HSE_Configuration(void) 39 { 40 RCC_DeInit(); //将外设RCC寄存器重设为缺省值 41 RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振HSE,HSE晶振打开ON 42 43 if(RCC_WaitForHSEStartUp() == SUCCESS)//等待HSE起震,SUCCESS:HSE晶振稳定且就绪 44 { 45 46 RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟 47 RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速AHB时钟 48 RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速AHB时钟 49 50 RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);///设置PLL时钟源及倍频系数 51 RCC_PLLCmd(ENABLE); //使能PLL 52 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) ; ///检查指定的RCC标志位(PLL准备好标志)设置是否 53 54 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置系统时钟 55 while(RCC_GetSYSCLKSource() != 0x08); //0x08:PLL作为系统时钟 56 57 } 58 } 59 60 61 62 63 64 65 66 /************************************************** 67 * 68 * 普通延时函数 69 * 70 ***************************************************/ 71 void delay(u32 nCount) 72 { 73 for(;nCount!=0;nCount--); 74 } 75 76 77 78 79 /************************************************** 80 * 81 * 函数名称:delay_us(u32 nus) 82 * 函数功能:微秒延时函数 83 * 输入参数:输入值为延时us 84 * 85 ***************************************************/ 86 void delay_us(u32 nus) 87 { 88 u32 temp; 89 SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次 90 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 91 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 92 93 do 94 { 95 temp=SysTick->CTRL;//标志位,等到一直减到0 96 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 97 98 SysTick->CTRL=0x00; //关闭计数器 99 SysTick->VAL =0X00; //清空计数器 100 } 101 102 103 104 105 106 107 108 /*************************************************** 109 * 110 * 函数名称:delay_ms(u16 nms) 111 * 函数功能:毫秒级延时 112 * 输入参数:输入值位延时ms 113 * 114 ****************************************************/ 115 void delay_ms(u16 nms) 116 { 117 u32 temp; 118 SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次 119 SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值 120 SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟 121 122 do 123 { 124 temp=SysTick->CTRL;//标志位,等到一直减到0 125 }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达 126 127 SysTick->CTRL=0x00; //关闭计数器 128 SysTick->VAL =0X00; //清空计数器 129 } 130 131 132 133 /**************************************************** 134 * 135 * 重定义printf函数部分 136 * 137 ****************************************************/ 138 int fputc(int ch,FILE *F) 139 { 140 141 USART_SendData(USART1,(u8)ch); 142 143 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待发送完成,判断标志位 144 145 return ch; 146 }
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 "stm32_spi.h"//SPI配置的函数库 52 #include "stm32_touch.h"//触摸屏配置函数库 53 54 #include "stdio.h" 55 56 57 /******************************************************************** 58 * 59 * 自定义全局变量声明 60 * 61 ********************************************************************/ 62 extern u8 dt; 63 64 65 66 67 68 /******************************************************************** 69 * 70 * 自定义全函数声明 71 * 72 ********************************************************************/ 73 void delay(u32 nCount); 74 void delay_us(u32 nus); 75 void delay_ms(u16 nms); 76 int fputc(int ch,FILE *F); 77 78 79 80 #endif
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:lcd_ILI9325.h 4 * 文件功能:LCD初始化头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #include "pbdata.h" 10 11 12 13 14 15 /********************************************* 16 * 17 * 写命令子函数 18 * 19 **********************************************/ 20 void LCD_WR_REG(u16 index) 21 { 22 *(__IO u16 *)(Bank1_LCD_C)=index;//把我们定义的数据读写首地址转化为指针,指向LCD真实的扩展地址 23 } 24 25 26 27 28 /********************************************* 29 * 30 * 写数据子函数 31 * 32 **********************************************/ 33 void LCD_WR_Data(u16 val) 34 { 35 *(__IO u16 *)(Bank1_LCD_D)=val;//把我们定义的命令读写首地址转换为指针,指向LCD真实扩展地址 36 } 37 38 39 40 41 /********************************************* 42 * 43 * 同时写命令和数据 44 * 45 **********************************************/ 46 47 void LCD_WR_CMD(u16 index,u16 val) 48 { 49 *(__IO u16 *)(Bank1_LCD_C)=index; 50 *(__IO u16 *)(Bank1_LCD_D)=val; 51 } 52 53 54 55 56 57 58 59 60 61 /********************************************* 62 * 63 * LCD初始化函数,出厂配置好 64 * 65 **********************************************/ 66 void ILI9325_Init(void) 67 { 68 GPIO_ResetBits(GPIOE, GPIO_Pin_1); //Ӳݾشλ 69 delay(0xAFFf); 70 GPIO_SetBits(GPIOE, GPIO_Pin_1 ); 71 delay(0xAFFf); 72 73 LCD_WR_CMD(0x0001, 0x0100); // set SS and SM bit 74 LCD_WR_CMD(0x0002, 0x0700); // set 1 line inversion 75 LCD_WR_CMD(0x0003, 0x1030); // set GRAM write direction and BGR=1. 76 LCD_WR_CMD(0x0004, 0x0000); // Resize register 77 LCD_WR_CMD(0x0008, 0x0207); // set the back porch and front porch 78 LCD_WR_CMD(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0] 79 LCD_WR_CMD(0x000A, 0x0000); // FMARK function 80 LCD_WR_CMD(0x000C, 0x0000); // RGB interface setting 81 LCD_WR_CMD(0x000D, 0x0000); // Frame marker Position 82 LCD_WR_CMD(0x000F, 0x0000); // RGB interface polarity 83 84 LCD_WR_CMD(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB 85 LCD_WR_CMD(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0] 86 LCD_WR_CMD(0x0012, 0x0000); // VREG1OUT voltage 87 LCD_WR_CMD(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude 88 LCD_WR_CMD(0x0007, 0x0001); 89 delay(12000); // Dis-charge capacitor power voltage 90 LCD_WR_CMD(0x0010, 0x1490); // SAP, BT[3:0], AP, DSTB, SLP, STB 91 LCD_WR_CMD(0x0011, 0x0227); // DC1[2:0], DC0[2:0], VC[2:0] 92 delay(15500); // Delay 50ms 93 LCD_WR_CMD(0x0012, 0x001C); // Internal reference voltage= Vci; 94 delay(15000); // Delay 50ms 95 LCD_WR_CMD(0x0013, 0x1A00); // Set VDV[4:0] for VCOM amplitude 96 LCD_WR_CMD(0x0029, 0x0025); // Set VCM[5:0] for VCOMH 97 LCD_WR_CMD(0x002B, 0x000C); // Set Frame Rate 98 delay(15000); // Delay 50ms 99 LCD_WR_CMD(0x0020, 0x0000); // GRAM horizontal Address 100 LCD_WR_CMD(0x0021, 0x0000); // GRAM Vertical Address 101 // ----------- Adjust the Gamma Curve ----------// 102 LCD_WR_CMD(0x0030, 0x0000); 103 LCD_WR_CMD(0x0031, 0x0506); 104 LCD_WR_CMD(0x0032, 0x0104); 105 LCD_WR_CMD(0x0035, 0x0207); 106 LCD_WR_CMD(0x0036, 0x000F); 107 LCD_WR_CMD(0x0037, 0x0306); 108 LCD_WR_CMD(0x0038, 0x0102); 109 LCD_WR_CMD(0x0039, 0x0707); 110 LCD_WR_CMD(0x003C, 0x0702); 111 LCD_WR_CMD(0x003D, 0x1604); 112 //------------------ Set GRAM area ---------------// 113 LCD_WR_CMD(0x0050, 0x0000); // Horizontal GRAM Start Address 114 LCD_WR_CMD(0x0051, 0x00EF); // Horizontal GRAM End Address 115 LCD_WR_CMD(0x0052, 0x0000); // Vertical GRAM Start Address 116 LCD_WR_CMD(0x0053, 0x013F); // Vertical GRAM Start Address 117 LCD_WR_CMD(0x0060, 0xA700); // Gate Scan Line 118 LCD_WR_CMD(0x0061, 0x0001); // NDL,VLE, REV 119 120 LCD_WR_CMD(0x006A, 0x0000); // set scrolling line 121 //-------------- Partial Display Control ---------// 122 LCD_WR_CMD(0x0080, 0x0000); 123 LCD_WR_CMD(0x0081, 0x0000); 124 LCD_WR_CMD(0x0082, 0x0000); 125 LCD_WR_CMD(0x0083, 0x0000); 126 LCD_WR_CMD(0x0084, 0x0000); 127 LCD_WR_CMD(0x0085, 0x0000); 128 //-------------- Panel Control -------------------// 129 LCD_WR_CMD(0x0090, 0x0010); 130 LCD_WR_CMD(0x0092, 0x0600); 131 LCD_WR_CMD(0x0007, 0x0133); // 262K color and display ON 132 } 133 134 135 136 137 138 139 /********************************************* 140 * 141 * 204*320,向触摸屏送数据,一个点一个点都要扫描到 142 * 143 **********************************************/ 144 void ILI_9325_Draw_Point(u8 x,u16 y,u16 color) 145 { 146 LCD_WR_CMD(0x50,x); //x起始 147 LCD_WR_CMD(0x51,x); //x结束 148 LCD_WR_CMD(0x52,y); //y起始 149 LCD_WR_CMD(0x53,y); //y结束 150 151 LCD_WR_CMD(0x20,x); 152 LCD_WR_CMD(0x21,y); 153 LCD_WR_REG(0x22); 154 LCD_WR_Data(color); 155 } 156 157 158 159 160 161 /********************************************* 162 * 163 * 同时写命令和数据 164 * 165 **********************************************/ 166 167 void ILI_9325_CLEAR(u16 color) 168 { 169 u16 i=0,j=0; 170 171 for(i=0;i<240;i++) 172 { 173 for(j=0;j<320;j++) 174 { 175 ILI_9325_Draw_Point(i,j,color); 176 } 177 } 178 }
View Code
1 /************************************************************************************************************************************* 2 * 3 * 文件名称:lcd_ILI9325.h 4 * 文件功能:LCD初始化头文件 5 * 6 ***************************************************************************************************************************************/ 7 8 9 #ifndef _LCD_ILI9325_H 10 #define _LCD_ILI9325_H 11 #include "pbdata.h" 12 13 14 //6000 0000 15 //A16 16位通讯 16 17 #define Bank1_LCD_D (u32)0x60020000 //命令位是1,所以在16位通讯命令格式时地址为2000 18 #define Bank1_LCD_C (u32)0x60000000 //数据位是0,所以在16位通讯数据格式时地址为0 19 20 #define RGB565(r, g, b) ((r >> 3) << 11 | (g >> 2) << 5 | (b >> 3)) //三色转换 21 22 #define RED RGB565(255,0,0) //红色 23 #define GREEN RGB565(0,255,0) //绿色 24 #define BLUE RGB565(0,0,255) //蓝色 25 #define BLACK RGB565(0,0,0) //黑色 26 #define WHITE RGB565(255,255,255) //白色 27 28 29 30 31 void ILI9325_Init(void);//LCD初始化函数 32 void LCD_WR_REG(u16 index);//写命令函数 33 void LCD_WR_Data(u16 val);//写数据函数 34 void LCD_WR_CMD(u16 index,u16 val);//同时写命令和数据函数 35 void ILI_9325_Draw_Point(u8 x,u16 y,u16 color);//一个点写入颜色 36 void ILI_9325_CLEAR(u16 color);//清屏 37 38 #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
9、工程下载:
http://yunpan.cn/cQ9wMzEa9ZqTp (提取码:660e)