4、按键控制LED

1、GPIO初始化中增加按键时钟初始化使能

 

2、GPIO初始化中增加按键引脚配置

 

3、查看引脚状态函数:GPIO_ReadInputDataBit   

 

4、例程代码

 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 
19 
20 
21 
22 /********************************************************************************
23 *
24 *  函数名称:main(void)
25 *  函数功能:主函数
26 *  函数说明:不能用void定义主函数
27 *
28 ********************************************************************************/
29 int main(void)//void不能void定义主函数
30 {
31     RCC_Configuration();    //系统时钟初始化
32     
33     GPIO_Configuration();//端口初始化
34     
35 
36 /*进入GPIO处理*/
37   while(1)
38   {
39         if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==0)
40             GPIO_ResetBits(GPIOB,GPIO_Pin_5);//指定端口设置低电平,led熄灭
41         else
42             GPIO_SetBits(GPIOB,GPIO_Pin_5);//指定端口设置为高电平,led发光
43   }
44     
45 }
46     
47     
48 
49 
50 /********************************************************************************
51 *
52 *  函数名称:RCC_Configuration(void)
53 *  函数功能:系统时钟高初始化函数
54 *
55 ********************************************************************************/
56     void RCC_Configuration(void)//系统时钟高初始化函数
57   {
58         
59     SystemInit();//系统初始化
60     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//对应GPIO时钟使能
61         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//对应GPIO时钟使能
62     }
63     
64     
65 
66 /*******************************************************************************
67 *
68 * 函数名称:GPIO_Configuration(void)
69 * 函数功能:GPIO初始化函数
70 *
71 ********************************************************************************/    
72     
73     void GPIO_Configuration(void)//GPIO初始化函数
74   {
75     GPIO_InitTypeDef GPIO_InitStructure;//定义一个GPIO设置的结构体变量 
76 
77 /*对LED引脚初始化*/        
78         /*结构体变量赋值*/
79       GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;////引脚配置
80       GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//配置频率
81       GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
82          /*对应的GPIO初始化*/
83       GPIO_Init(GPIOB,&GPIO_InitStructure);
84         
85 /*对key的引脚初始化*/
86         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;////引脚配置
87       GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
88          /*对应的GPIO初始化*/
89       GPIO_Init(GPIOC,&GPIO_InitStructure);
90         
91   }
92     
93 
94     
95 
96     
97     
98     
View Code
 1 /****************************************************************************************************************
 2 *
 3 * 文件名称:pbdata.c
 4 * 文件功能:自定义函数或者全局变量的初始化
 5 *
 6 ****************************************************************************************************************/
 7 
 8 /*头文件声明*/
 9 #include"pbdata.h"
10 
11 
12 /********************************************************************************************
13 *
14 * 自定义全局变量
15 *
16 ********************************************************************************************/
17 u8 dt=0;
18 
19 
20 
21 
22 
23 /******************************************************************************************
24 *
25 * 自定义函数
26 *
27 ******************************************************************************************/
28 
29 
30 
31 /**************************************************
32 *
33 *  函数名称:delay_us(u32 nus)
34 *  函数功能:微秒延时函数
35 *  输入参数:输入值为延时us
36 *
37 ***************************************************/
38 void delay_us(u32 nus)
39 {
40     u32 temp;
41     SysTick->LOAD = 9*nus;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9次
42     SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值
43     SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟
44     
45     do
46     {
47           temp=SysTick->CTRL;//标志位,等到一直减到0
48          }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达
49     
50     SysTick->CTRL=0x00; //关闭计数器
51     SysTick->VAL =0X00; //清空计数器
52 }
53 
54 
55 
56 
57 
58 
59 
60 /***************************************************
61 *
62 * 函数名称:delay_ms(u16 nms)
63 * 函数功能:毫秒级延时
64 * 输入参数:输入值位延时ms
65 *
66 ****************************************************/
67 void delay_ms(u16 nms)
68 {
69     u32 temp;
70     SysTick->LOAD = 9000*nms;//载入初值,72M/8=9M,也就是1/9us,9*1/9us,所以是执行9000次
71     SysTick->VAL=0X00;//清空计数器,清空后,就自动设置自己设定的计数器的值
72     SysTick->CTRL=0X01;//使能,减到零动作(不发生中断),采用外部时钟
73     
74     do
75     {
76           temp=SysTick->CTRL;//标志位,等到一直减到0
77          }while((temp&0x01)&&(!(temp&(1<<16))));//等待时间到达
78     
79     SysTick->CTRL=0x00; //关闭计数器
80     SysTick->VAL =0X00; //清空计数器
81 }
View Code
 1 /*pbdata.h*/
 2 /***************************************************************************************************
 3 *
 4 * 文件名称:pbdata.h
 5 * 文件功能:自定义的函数和全局变量的声明头文件
 6 *
 7 ***************************************************************************************************/
 8 
 9 #ifndef _pbdata_H
10 #define _pbdata_H
11 
12 #include"stm32f10x.h"
13 
14 
15 
16 /********************************************************************
17 *
18 *  自定义全局变量声明
19 *
20 ********************************************************************/
21 extern u8 dt;
22 
23 
24 
25 
26 
27 /********************************************************************
28 *
29 *  自定义全函数声明
30 *
31 ********************************************************************/
32 void delay(u32 nCount);
33 void delay_us(u32 nus);
34 void delay_ms(u16 nms);
35 
36 
37 
38 
39 #endif
View Code

 

5、工程下载:

http://download.csdn.net/detail/a1181803348/8737843

 

posted @ 2015-05-25 19:57  如风轻逸  阅读(414)  评论(0编辑  收藏  举报