STM32————复用功能按键输入(一键多用)

#define LED_ON GPIO_ResetBits(GPIOC,GPIO_Pin_8)//亮
#define LED_OFF GPIO_SetBits(GPIOC,GPIO_Pin_8)//不亮
#define Key_DownStatu GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0//按下
#define Key_UpStatu GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==1//松开
#define LEDPORT GPIO_Pin_8
#define KEYPORT GPIO_Pin_15
#define LEDOutputMode GPIO_Mode_Out_PP
#define Speed GPIO_Speed_50MHZ
#define KeyOutputMode GPIO_Mode_IPU
/*****************************************/
void Init_LED_Ked_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructrue;
RCC_APB2PeriphClockCmd(Rcc_APB2Periph_GPIOC|Rcc_APB2Periph_GPIOB,ENABLE);
//使能GPIOC,GPIOB端口时钟

GPIO_InitStructrue.GPIO_Pin = LEDPORT;
GPIO_InitStructrue.GPIO_Mode = LEDOutputMode;
GPIO_InitStructrue.GPIO_Speed = Speed;
GPIO_Init (GPIOC,&GPIO_InitStructrue);
GPIO_SetBits(GPIOC,LEDPORT);

GPIO_InitStructrue.GPIO_Pin = KEYPORT;
GPIO_InitStructrue.GPIO_Mode = KeyOutputMode;
GPIO_InitStructrue.GPIO_Speed = Speed;
GPIO_Init (GPIOB,&GPIO_InitStructrue);
GPIO_SetBit(GPIOB,KEYPORT);
}
//1ms延迟函数
void Delay_ms(u16 time)
{
u16 i=0;
while(time–)
{
i = 12000;
while(i–);

}

}
u8 KeyPressed(u16 time)
{
u16 count = 0;

if (Key_DownStatu)
{
Delay_ms(10);
if (Key_DownStatu)
{
while(!Key_DownStatu)
{
Delay_ms(10);
count++;

	}
  
}

}
//返回0 无动作
//返回1 短按,灯亮与灭切换
//返回2 系统复位
if (count == 0)
return 0;
else if (count < time/10)
return 1;
else
return 2;

}
void SYSReset(void)
{
_set_FAULTMASK(1);//关闭中断
NVIC_SystemReset();//系统复位
}
main(void)
{
u16 count = 0;
Init_LED_Ked_GPIO_Config();
while(1)
{
switch(KeyPressed(3000))
{
case 0: break;
case 1: count++;
if (count % 2 == 0)
LED_ON;
else
LED_OFF;
break;
case 2:
Delay_ms(10);
SYSReset();
break;
}
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/News53231323/article/details/113245220