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

stm32的按键控制

发布时间:2025-02-13 发布时间:
|

 1 /*

 2 ::按键控制

 3 PA8接LED,PE2接按键

 4 */

 5 #include"stm32f10x.h"

 6 void RCC_Configuration(void);

 7 void GPIO_Config(void);

 8 void Delay(__IO uint32_t nCount);

 9 

10 int main()

11 {

12   RCC_Configuration();                   //系统时钟配置|使能GPIO口

13   GPIO_Config();                            //LED控制配置

14   while (1)

15   {

16       if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))

17     {

18          Delay(0x000FF);//延时防抖

19         if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))

20         {

21             GPIO_WriteBit(GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)));

22         }

23     }

24   }

25 }

26 /****************************************************************************

27 * 名    称:void GPIO_Config(void)

28 * 功    能:GPIO初始化函数

29 * 入口参数:无

30 * 出口参数:无

31 * 说    明:

32 * 调用方法:无 

33 ****************************************************************************/ 

34 void GPIO_Config(void)

35 {

36   GPIO_InitTypeDef GPIO_InitStructure;

37   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;    //配置LEDA8                  

38   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出

39   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        

40   GPIO_Init(GPIOA, &GPIO_InitStructure);

41   

42   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    //配置按键PE2          

43   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入

44   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;        

45   GPIO_Init(GPIOE, &GPIO_InitStructure);

46 }

47 /****************************************************************************

48 * 名    称:void RCC_Configuration(void)

49 * 功    能:系统时钟配置为72MHZ|使能GPIO口

50 * 入口参数:无

51 * 出口参数:无

52 * 说    明:

53 * 调用方法:无 

54 ****************************************************************************/ 

55 void RCC_Configuration(void)

56 {   

57   SystemInit();

58   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOE, ENABLE);//使能GPIO口

59 }

60 /****************************************************************************

61 * 名    称:void Delay(__IO uint32_t nCount)

62 * 功    能:延时函数

63 * 入口参数:无

64 * 出口参数:无

65 * 说    明:

66 * 调用方法:无 

67 ****************************************************************************/ 

68 void Delay(__IO uint32_t nCount)

69 {

70    for(; nCount != 0; nCount--);

71 }


关键字:stm32  按键控制 

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

热门文章 更多
利用STM32的Systick中断实现按键消抖