按键驱动总结方法一

#define KEY_NONE 0//0xDFFE
#define KEY1 0x0001
#define KEY2 0x2000
#define KEY1_LONG 0x0011
#define KEY2_LONG 0x2100


typedef struct
{
uint8_t slient;
uint16_t delay;
uint8_t done;
uint16_t value;
}KEY_Structure;

void KEY_GPIO_Config(void);
uint16_t ReadButton(void);
void KeyStInit(void);
void KeyProcess(void);

 

KEY_Structure KeySt;

void KEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;

GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;

GPIO_Init(GPIOC,&GPIO_InitStructure);
}

uint16_t ReadButton(void)
{
return ((GPIOA->IDR & 0x0001) | (GPIOC->IDR & 0x2000));
}

void KeyStInit(void)
{
KeySt.delay = 0;
KeySt.done = 0;
KeySt.slient = 0;
KeySt.value = 0;
}

void KeyProcess(void)
{
if(KeySt.slient>0)
{
KeySt.value = 0;
KeySt.delay = 0;
}

if(ReadButton() != KEY_NONE)
{
if(KeySt.delay == 0)
{
KeySt.delay = 1;
KeySt.value = ReadButton();
}
else if(KeySt.delay > 100)
{
if(KeySt.value == KEY2)
{
if(KeySt.delay > 200)
{
KeySt.value |= 0x100;
KeySt.done = 1;
}

}

if((KeySt.delay % 50) == 0)
{
KeySt.done = 1;
}

}
else
{

}
}
else
{
if(KeySt.delay < 2)
{
KeySt.delay = 0;
KeySt.done = 0;
}
else if(KeySt.delay < 100)
{
KeySt.delay = 0;
KeySt.done = 1;
KeySt.slient = 30;
}
else
{
KeySt.delay = 0;
}
}
}

 

extern KEY_Structure KeySt;
void macTIM_INT_FUN (void)
{
if ( TIM_GetITStatus( macTIMx, TIM_IT_Update) != RESET )
{
if(KeySt.delay)
{
KeySt.delay++;
}

if(KeySt.slient)
{
KeySt.slient--;
}

TIM_ClearITPendingBit(macTIMx , TIM_FLAG_Update);
}
}

 

posted @ 2018-12-25 21:13  请叫我晓风哥哥  阅读(264)  评论(0编辑  收藏  举报