123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include "key_drivers.h"
- #include "adc.h"
- Key_TypeDef Key_On_Off;
- Key_TypeDef Key_Walk;
- Key_TypeDef Key_Light;
- Key_TypeDef Key_Set;
- Key_TypeDef Key_Down;
- Key_TypeDef Key_Up;
- static void _GPIO_Read(Key_TypeDef* Key)
- {
- static uint8_t ReadData;
-
- if(*(Key->ADC_result)>3000)
- {
- ReadData = 0x00;
- }
- else if(*(Key->ADC_result)<1000)
- {
- ReadData = 0x01;
- }
- Key->Trg = ReadData & (ReadData ^ (Key->Cont));
- Key->Cont = ReadData;
- }
- void KeyParamInit(Key_TypeDef * Key)
- {
- Key->Cont = 0;
- Key->Trg = 0;
- Key->PressFlasg = 0;
- Key->TimeCnt = 0;
- Key->KeyStatus = Key_Status_NoPress;
- }
- void KeyParamInit_no_cont(Key_TypeDef * Key)
- {
- Key->Trg = 0;
- Key->PressFlasg = 0;
- Key->TimeCnt = 0;
- Key->KeyStatus = Key_Status_NoPress;
- }
- //按键GPIO初始化
- void KeyInitial(void)
- {
- //初始化Key_On_Off
- Key_On_Off.ADC_result =&ADC_Result[1] ;
- KeyParamInit(&Key_On_Off);
-
- //初始化Key_Walk
- Key_Walk.ADC_result =&ADC_Result[2] ;
- KeyParamInit(&Key_Walk);
-
- //初始化Key_Light
- Key_Light.ADC_result =&ADC_Result[3] ;
- KeyParamInit(&Key_Light);
-
- //初始化Key_Down
- Key_Down.ADC_result =&ADC_Result[4] ;
- KeyParamInit(&Key_Down);
- //初始化Key_Up
- Key_Up.ADC_result =&ADC_Result[5] ;
- KeyParamInit(&Key_Up);
-
- }
- //按键检测函数
- void Key_Check(Key_TypeDef* Key, uint8_t DelayTime)
- {
- _GPIO_Read(Key);
- if(Key->Trg & 0x01)//表示按键有触发(下降沿)
- {
- Key->PressFlasg = 1;
- }
- if(Key->PressFlasg == 1)//按键被触发一次
- {
- if(Key->Cont & 0x01)//表示按键未放开
- {
- Key->TimeCnt++;
- if(Key->TimeCnt >= DelayTime)//长按时间 DelayTime * 10ms
- {
- Key->TimeCnt = 0;
- Key->PressFlasg = 0;
- Key->KeyStatus = Key_Status_LongPress;
- return;
- }
- }
- else if(Key->Cont == 0x00)//按键弹起
- {
- Key->TimeCnt = 0;
- Key->PressFlasg = 0;
- Key->KeyStatus = Key_Status_ShortPress;
- return;
- }
- }
- return;
- }
- KeyScanCode_Struct_t KeyScan(void)
- {
- KeyScanCode_Struct_t Result;
-
- Result.Code = 0x00;
-
- if(*(Key_On_Off.ADC_result)>3000)
- Result.Key_Bit.Power = 0;
- else if(*(Key_On_Off.ADC_result)<1000)
- Result.Key_Bit.Power = 1;
-
- if(*(Key_Walk.ADC_result)>3000)
- Result.Key_Bit.Walk = 0;
- else if(*(Key_Walk.ADC_result)<1000)
- Result.Key_Bit.Walk = 1;
-
- if(*(Key_Up.ADC_result)>3000)
- Result.Key_Bit.Add = 0;
- else if(*(Key_Up.ADC_result)<1000)
- Result.Key_Bit.Add = 1;
-
- if(*(Key_Down.ADC_result)>3000)
- Result.Key_Bit.Dec = 0;
- else if(*(Key_Down.ADC_result)<1000)
- Result.Key_Bit.Dec = 1;
-
- // if(*(Key_Set.ADC_result)>4000)
- // Result.Key_Bit.Set = 0;
- // else if(*(Key_Set.ADC_result)<500)
- // Result.Key_Bit.Set = 1;
- if(*(Key_Light.ADC_result)>3000)
- Result.Key_Bit.Light = 0;
- else if(*(Key_Light.ADC_result)<1000)
- Result.Key_Bit.Light = 1;
-
- return Result;
- }
|