×
单片机 > 单片机程序设计 > 详情

Stm32按键输入控制LED灯

发布时间:2020-08-26 发布时间:
|

说明:GPIOA,GPIO_Pin_0对应key1;GPIOA,GPIO_Pin_1对应key2;GPIOA,GPIO_Pin_2对应LED1;GPIOA,GPIO_Pin_3对应LED2

  1. #include "stm32f10x.h"  

  2. #include "stm32f10x_rcc.h"  

  3. #include "stm32f10x_gpio.h"  

  4. #include "system_stm32f10x.h"  

  5.   

  6. /* 控制小灯: 0 灭 1 亮 */  

  7. #define ON 1  

  8. #define OFF 0  

  9.   

  10. #define KEY_ON  0  

  11. #define KEY_OFF 1  

  12.   

  13. void RCC_Configuration(void);  

  14. void GPIO_Configuration(void);   

  15. void SetLed(uint8_t set);  

  16. void delay_ms(u16 time);  

  17.   

  18. uint8_t KeyScan(GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin_x);  

  19.   

  20. int main()  

  21. {  

  22.     SystemInit();  

  23.       

  24.     RCC_Configuration();  

  25.     GPIO_Configuration();   

  26.     SetLed(ON);   

  27.       

  28.     while (1)  

  29.     {  

  30.         if (KeyScan(GPIOA,GPIO_Pin_0) == KEY_ON)  

  31.         {  

  32.             /* LED1反转 读取GPIOA 0端口位的值并用1减去之后再写入此位即LED1的控制位 */  

  33.             GPIO_WriteBit(GPIOA,GPIO_Pin_2,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)));          

  34.         }  

  35.           

  36.         if (KeyScan(GPIOA,GPIO_Pin_1) == KEY_ON)  

  37.         {  

  38.             /* LED2反转 读取GPIOA 0端口位的值并用1减去之后再写入此位即LED2的控制位 */  

  39.             GPIO_WriteBit(GPIOA,  GPIO_Pin_3,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3)));         

  40.         }  

  41.     }  

  42. }  

  43.   

  44. void RCC_Configuration(void)    

  45. {     

  46.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);     

  47. }   

  48.   

  49. void GPIO_Configuration(void)    

  50. {    

  51.   GPIO_InitTypeDef GPIO_InitStructure;    

  52.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;   

  53.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     

  54.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     

  55.   GPIO_Init(GPIOA, &GPIO_InitStructure);    

  56.     

  57.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;   

  58.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; /* 配置按键的引脚为上拉 */    

  59.   //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   /*输入模式不需要配置端口的输出速率GPIO_Speed*/  

  60.   GPIO_Init(GPIOA, &GPIO_InitStructure);  

  61. }   

  62.   

  63. void SetLed(uint8_t set)  

  64. {  

  65.   if(set==ON){  

  66.     GPIO_SetBits(GPIOA,GPIO_Pin_2);//LED1    

  67.     GPIO_SetBits(GPIOA,GPIO_Pin_3);//LED2  

  68.   }  

  69.     

  70.   if(set==OFF){  

  71.     GPIO_ResetBits(GPIOA,GPIO_Pin_2);  

  72.     GPIO_ResetBits(GPIOA,GPIO_Pin_3);  

  73.   }  

  74. }  

  75.   

  76. /** 

  77.   * @brief  : 按键按下检测 

  78.   * @param  : 端口 : GPIOx  端口位 : GPIO_Pin_x 

  79.   * @retval : 按键的状态 : 按下 弹起 

  80.   */  

  81. uint8_t KeyScan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin_x )  

  82. {  

  83.     /* 检测是否有按键按下 */  

  84.     if ( GPIO_ReadInputDataBit( GPIOx, GPIO_Pin_x ) == KEY_ON )  

  85.     {  

  86.         /* 延时消抖 延时大约5ms */  

  87.         delay_ms(5);  

  88.         if ( GPIO_ReadInputDataBit( GPIOx, GPIO_Pin_x ) == KEY_ON )  

  89.         {  

  90.             while ( GPIO_ReadInputDataBit( GPIOx, GPIO_Pin_x ) == KEY_ON ); /* 等待按键释放 */  

  91.             return KEY_ON;  

  92.         }  

  93.         else  

  94.         {  

  95.             return KEY_OFF;  

  96.         }  

  97.     }  

  98.     return KEY_OFF;  

  99. }  

  100.   

  101. void delay_ms(u16 time)     

  102. {    

  103.   u16 i=0;    

  104.   while(time--)     

  105.   {    

  106.     i=12000;    

  107.     while(i--);    

  108.   }    

  109. }   


关键字:Stm32  按键输入  控制LED灯 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机CO2检测显示程序解析