include "key.h"
define KEY1 (GPIOA->IDR & (0X1<<0))
define KEY2 (GPIOC->IDR & (0X1<<4))
define KEY3 (GPIOC->IDR & (0X1<<5))
define KEY4 (GPIOC->IDR & (0X1<<6))
void KEY_Config(void)//key1 按键
{
//1.开启时钟
RCC->APB2ENR &= ~(1<<2);//清零
RCC->APB2ENR |= (1<<2);//致1
RCC->APB2ENR &= ~(1<<4);//清零 PC4
RCC->APB2ENR |= (1<<4);//致1
//2.配置io 0100
GPIOA->CRL &= ~(0xf<<0);//将0-3置零
GPIOA->CRL |=(0x4<<0);
GPIOC->CRL &= ~(0xf<<16);//将0-3置零
GPIOC->CRL |=(0x4<<16);
GPIOC->CRL &= ~(0xf<<20);//将0-3置零
GPIOC->CRL |=(0x4<<20);
GPIOC->CRL &= ~(0xf<<24);//将0-3置零
GPIOC->CRL |=(0x4<<24);
}
uint8_t Get_KeyVal(void) //uint8_t 无符号整型数据
{
uint8_t val=0;
if(KEY1)//按下得到高电平
{
Delay_ms(20);//按键消抖
if(KEY1)
{
while(KEY1){}//等待按键抬起
val=1;
}
}
if(!KEY2)
{
Delay_ms(20);//按键消抖
if(!KEY2)
{
while(!KEY2){}//等待按键抬起
val=2;
}
}
if(!KEY3)
{
Delay_ms(20);//按键消抖
if(!KEY3)
{
while(!KEY3){}//等待按键抬起
val=3;
}
return val;
}
if(!KEY4)
{
Delay_ms(20);//按键消抖
if(!KEY4)
{
while(!KEY4){}//等待按键抬起
val=4;
}
}
return val;
}
标签:Delay,20,val,KEY3,IDR,笔记,STM32,按键 From: https://www.cnblogs.com/lmkjava/p/17549725.html