总结:
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IPD;//高电平有效触发
if(KEY_A0 == 1){
delay_ms(10);
while(KEY_A0==1);
do sth
}
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IPU;//低电平有效触发
if(KEY_A1 == 1){
delay_ms(10);
while(KEY_A1==1);
do sth
}
;
1按键上拉下拉
http://www.elecfans.com/emb/danpianji/20181204826673.html
下图是上拉电路,按键闭合后电路输出为低电平
2按键配置
void KEY_GPIO_init(void){
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能B
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IPD;//高电平有效触发
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_0;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode =GPIO_Mode_IPU;//低电平有效触发
GPIO_InitStruct.GPIO_Pin =GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStruct);
printf("KEY_GPIO-A0-1 _OK!!\r\n");
}
3 按键任务
while(1)
{
if(KEY_A0 == 1){
delay_ms(10);
while(KEY_A0==1);
printf("led0 open\r\n");
LED_B0=1;
OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_HMSM_STRICT,&err); //延时500ms
LED_B0=0;
}
OSTimeDlyHMSM(0,0,0,20,OS_OPT_TIME_HMSM_STRICT,&err); //循环里必须有延时,不然没有时间响应其他程序
}
while(1)
{
if(KEY_A1==0){
delay_ms(10);
while(KEY_A1==0);
printf("KEY_A1被按下\r\n");
}
OSTimeDlyHMSM(0,0,0,20,OS_OPT_TIME_HMSM_STRICT,&err); //延时500ms
}