/******************************************************************************* * File Name: Key.c * Function Describe:device for the Keys * Relate Module: * Explain: Hardware version is HS110C * Writer: ShiLiangWen * Date: 2015.1.26 *******************************************************************************/ #define THIS_FILE_ID 14 //-------------------------------------------------------------------------------- #include "stm32f10x.h" #include "Key.h" #include //key value variable unsigned long g_ulKeyValue; unsigned char g_ucDKC=0; unsigned char g_ucUKC=0; unsigned char g_ucKeyFree=0; unsigned char g_ucKeyMode=0; unsigned char g_ucKeyPPTPress=0; unsigned char g_ucEncode[20]; ////////////////////////// function declare ////////////////////////////// unsigned long KeyScanPort(void); ////////////////////////// end declare ////////////////////////////// /******************************************************************************** KeyInit *******************************************************************************/ void CheckPwrKeyOn(void) {//长按电源键直到电筒灯亮后释放 unsigned int tick=0; if(KeyPwrStatus !=0) { POWER_ON_FAILED: SlwTrace(INF, "\r\nPwrOnFailed",1); printf("KeyPwrStatus===%016x\r\n",KeyPwrStatus); DelayMs(10); PWR_EN_LOW; while(1); } while(KeyPwrStatus==0) { if(++tick >= 2000000)//0 { MODEM_LED2_HIGH; SlwTrace(INF, "\r\nPwrOnOk",1); PWR_EN_HIGH;// return; } } SlwTrace(INF,"\r\nPwr erro",1); goto POWER_ON_FAILED; } //检测关机 void SoftPwrOffCheck(void) { static unsigned int seconds=0; static unsigned char KeyPowerCnt=0; if(KeyPwrStatus==0) { KeyPowerCnt++; printf("KeyPowerCnt++===%d\r\n",KeyPowerCnt); if(KeyPowerCnt>2) { // if(g_uGtTaskEn||BOOTDebug){//写号模式直接关机 // os_dly_wait(20); // PWR_EN_LOW; // }else { // UISetNextStatus(UIS_POWEROFF); // } SlwTrace(INF, "PwrOffOk",1); ShowMessageBox("正在关机!"); MODEN_POWER_EN_HIGH; IWDG_ReloadCounter(); os_dly_wait(100); MODEN_POWER_EN_LOW; // IWDG_ReloadCounter(); // os_dly_wait(220); PWR_EN_LOW; while(1); // GuiShowBmp(0,0,"BYEBYE.bmp"); // PWR_EN_LOW; // while(1); } }else KeyPowerCnt=0; } void KeyInit(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); PWR_BackupAccessCmd( ENABLE ); RCC_LSEConfig( RCC_LSE_OFF ); BKP_TamperPinCmd(DISABLE); /*GPIO先全部设置为上拉输入*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //PPT GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN; GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure); //PANEL_UP GPIO_InitStructure.GPIO_Pin=KEY_PANEL_UP_PIN; GPIO_Init(KEY_PANEL_UP_PORT, &GPIO_InitStructure); //PANEL_DOWN GPIO_InitStructure.GPIO_Pin=KEY_PANEL_DOWN_PIN; GPIO_Init(KEY_PANEL_DOWN_PORT, &GPIO_InitStructure); //PANEL_OK GPIO_InitStructure.GPIO_Pin=KEY_SOS_OK_PIN; GPIO_Init(KEY_SOS_OK_PORT, &GPIO_InitStructure); //PANEL_EXIT GPIO_InitStructure.GPIO_Pin=KEY_PANEL_EXIT_PIN; GPIO_Init(KEY_PANEL_EXIT_PORT, &GPIO_InitStructure); //PANEL_MENU GPIO_InitStructure.GPIO_Pin=KEY_PANEL_MENU_PIN; GPIO_Init(KEY_PANEL_MENU_PORT, &GPIO_InitStructure); //KEY_POWER GPIO_InitStructure.GPIO_Pin=KEY_FUNC_P1_PIN; GPIO_Init(KEY_FUNC_P1_PORT, &GPIO_InitStructure); //INFO GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN; GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure); //FUNC GPIO_InitStructure.GPIO_Pin=KEY_FUNC_P2_PIN; GPIO_Init(KEY_FUNC_P2_PORT, &GPIO_InitStructure); // //PPT_MIC // GPIO_InitStructure.GPIO_Pin=KEY_PPT_MIC_PIN; // GPIO_Init(KEY_PPT_MIC_PORT, &GPIO_InitStructure); //LED_BACK_LIGHT GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin=KEY_LED_PIN; GPIO_Init(KEY_LED_PORT, &GPIO_InitStructure); //chargechek GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin=CHARGEChek_PIN; GPIO_Init(CHARGEChek_PORT,&GPIO_InitStructure); //ear check GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin=EAR_CHEK_PIN; GPIO_Init(EAR_CHEK_PORT,&GPIO_InitStructure); //CHANGELED GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin=CHARGELED_PIN; GPIO_Init(CHARGELED_PORT, &GPIO_InitStructure); CheckPwrKeyOn(); g_ucKeyMode=0;//连续触发模式 RunMake(THIS_FILE_ID); } unsigned long g_ulEncode=0; /******************************************************************************** * Function:KeyScanPort *******************************************************************************/ unsigned long KeyScanPort(void) { unsigned long k1,k2; k1=GPIOB->IDR; k1&=KEY_ALL_PORTB; k1<<=16; k2=GPIOC->IDR; k2&=KEY_ALL_PORTC; return (k1|k2); } /************************************************************ *GetKey 读键盘数据 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果 参数: g_ucKeyMode=1时,单次模式 g_ucKeyMode=0时,持续模式 返回值: 0--按键无变化 1 --按键有按下: 单次模式下,当按键第一次按下才返回一次该值; 持续模式下,按着按键不放将间隔返回该值 g_ulKeyValue保存当前按键值 -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次 释放后,g_ulKeyValue保存上次按下的按键值 *************************************************************/ int GetKey(void) { static unsigned char sucCt=0;//连续模式下响应频率控制; static unsigned char sucFlag=0; static unsigned char sucPress=0; static unsigned char sucLastPress=0; static unsigned long Key=0; static unsigned long Key1=0; static unsigned long Key2=0; static unsigned long Key3=0; static unsigned long Key4=0; static unsigned int KeyOstime=0; if(os_time_get() < KeyOstime) return 0; KeyOstime = os_time_get()+1;//1 //去抖动 0 1 if(++sucFlag>4){ //1 sucFlag=0; } if(sucFlag==0){ Key1=KeyScanPort(); }else if(sucFlag==1){ Key2=KeyScanPort(); }else if(sucFlag==2){ Key3=KeyScanPort(); }else { Key4=KeyScanPort(); } if(Key1!=Key2||Key2!=Key3||Key3!=Key4)return 0; //按键状态发生变更 if(Key!=Key1){ Key=Key1; if(Key!=((KEY_ALL_PORTB<<16)| KEY_ALL_PORTC)) { sucPress=1; //有按下 g_ucUKC=0; printf("KEY=%lx\r\n",Key); } else { sucPress=0; //无按下 g_ucDKC=0; } } if(sucPress){//有按键按下 if(g_ucKeyMode){ //单次模式 if(sucLastPress!=sucPress){ sucLastPress=sucPress; g_ulKeyValue=Key; return 1; }else{ return 0; } }else{ //持续模式 sucLastPress=sucPress; if(0==sucCt){ g_ulKeyValue=Key1; sucCt=80;//100;//10ms*100=1000ms return 1; }else{ sucCt--; return 0; } } }else{ //无按键按下 sucCt=0; if(sucLastPress){ sucLastPress=0; return -1; } } return 0; } void KeyCount(void) { if(g_ucDKC<255)g_ucDKC++; if(g_ucUKC<255)g_ucUKC++; } void OnOff_Init(void) { EXTI_InitTypeDef EXTI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN; GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9); EXTI_InitStructure.EXTI_Line = EXTI_Line9; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); } void OnOff_Init2(void) { EXTI_InitTypeDef EXTI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN; GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9); EXTI_InitStructure.EXTI_Line = EXTI_Line9; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = DISABLE; EXTI_Init(&EXTI_InitStructure); } void EXTI9_5_IRQHandler(void) { if(SET==EXTI_GetFlagStatus(EXTI_Line9)) {//外部电源关了 // if(modemInitReady==1) // { PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源 modemPwrOffNow=1; // } EXTI_ClearFlag(EXTI_Line9); } } void Charge_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN; GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource9); } /******************************************************************************** * End of Module *******************************************************************************/