Key模板
#ifndef KEY_H #define KEY_H #include "main.h" #ifdef KEY_C #include "stm32f10x_gpio.h" #include "sys.h" #define DoubDelay 500 #define LongDelay 3000 #define CounterMax 20000 void KEY_Shift(void); #endif #define NumOfKey 2 #define KEY_LevelLoose 0x00 #define KEY_LevelPress 0x01 #define KEY_OperateNone 0x00 #define KEY_OperateFail 0x01 #define KEY_OperateRise 0x02 #define KEY_OperateLong 0x03 #define KEY_OperateDoub 0x04 #define KEY_RunA 0x01 #define KEY_RunB 0x02 typedef struct{ uchar level; uchar operate; } KEY_TypeDef; extern KEY_TypeDef KEY_Status[NumOfKey]; void KEY_Init (void); void KEY_Action(void); #endif
#define KEY_C #include "key.h" ushort KEY_Counter[NumOfKey]; uchar KEY_Level[NumOfKey]; KEY_TypeDef KEY_Status[NumOfKey]; void KEY_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //----------GPIO Init---------- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_Init(GPIOC, &GPIO_InitStructure); //----------ENABLE---------- KEY_Shift(); } void KEY_Shift(void) { for(uchar i = 0; i < NumOfKey; i ++) { KEY_Counter[i] += 10; KEY_Counter[i] = Min_ushort(KEY_Counter[i], CounterMax); KEY_Status[i].operate = KEY_OperateNone; if(KEY_Status[i].level != KEY_Level[i]) { KEY_Status[i].level = KEY_Level[i]; if(KEY_Level[i] == KEY_LevelPress) { if(KEY_Counter[i] < DoubDelay) { KEY_Status[i].operate = KEY_OperateDoub; } else { KEY_Status[i].operate = KEY_OperateRise; KEY_Counter[i] = 0; } } else { KEY_Status[i].operate = KEY_OperateFail; } } else { if(KEY_Level[i] == KEY_LevelPress) { if(KEY_Counter[i] == LongDelay) { KEY_Status[i].operate = KEY_OperateLong; } } } } } void KEY_Action(void) { KEY_Level[0] = PCin(6); KEY_Level[1] = PCin(7); KEY_Shift(); }