| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- #include "includes.h"
- unsigned long g_ulKeyValue;
- unsigned char g_ucKeyFree=0;
- unsigned char g_ucKeyMode=0;
- unsigned char g_ucKeyPPTPress=0;
- unsigned char g_ucUKC=0;
- //void CheckPwrKeyOn(void)
- //{//长按电源键直到电筒灯亮后释放
- // unsigned int tick=0;
- // if(KeyPwrStatus !=0)
- // {
- // POWER_ON_FAILED:
- // SlwTrace(INF, "\r\nPwrOnFailed",1);
- // DelayMs(10);
- // PWR_EN_LOW;
- // while(1);
- // }
- //
- // while(KeyPwrStatus==0)
- // {
- // if(++tick >= 200000)
- // {
- // MODEM_LED2_HIGH;
- // SlwTrace(INF, "\r\nPwrOnOk",1);
- //// while(KeyPwrStatus==0){
- ////// DelayMs(30);
- ////// MODEM_LED2_LOW;
- ////// DelayMs(30);
- ////// MODEM_LED2_HIGH;
- //// IWDG_ReloadCounter();
- //// }
- // //MODEM_LED2_LOW;
- //
- // PWR_EN_HIGH;//
- // return;
- // }
- // }
- // SlwTrace(INF,"\r\nPwr erro",1);
- // goto POWER_ON_FAILED;
- //}
- //检测关机
- void SoftPwrOffCheck(void)
- {
- // static unsigned int seconds;
- //
- // if(!seconds) seconds=uSysTickGet()+500;//首次进入
- //
- // if(KeyPwrStatus==0)
- // {
- //
- // if(uSysTickGet() > seconds)
- // {
- // SlwTrace(INF, "PwrOffOk",1);
- //// MODEM_LED1_HIGH;
- // if(g_ucTMMode==1)while(1);
- //
- // UISetNextStatus(UIS_POWEROFF_WAIT);
- //// GuiClearAll();
- //// GuiShowBmp(0,0,"BYEBYE.bmp");
- //// MODEM_PWREN_LOW;
- //// PWR_EN_LOW;
- //// MODEM_LED1_LOW;
- //// while(1);
- // }
- // }else seconds=uSysTickGet()+100;//200
- //
- }
- void KeyInit(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
-
- /*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);
-
- //MIC PTT
- GPIO_InitStructure.GPIO_Pin=KEY_MICPTT_PIN;
- GPIO_Init(KEY_MICPTT_PORT, &GPIO_InitStructure);
-
- // INFO VOL+
- GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
- GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
- // FUNC VOL-
- GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
- GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
-
- //HEADSET_PIN 耳机检测
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin=HEADSET_PIN;
- GPIO_Init(HEADSET_PORT, &GPIO_InitStructure);
-
-
-
- //CheckPwrKeyOn();
-
- g_ucKeyMode=0;//连续触发模式
- }
- static unsigned long KeyScanPort(void)
- {
- unsigned long k1,k2;
-
- k1=GPIOB->IDR;
- k1&=KEY_ALL_PORTB;
- k1<<=16;
- return (k1);
- }
- /************************************************************
- *GetKey
- 读键盘数据
- 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
- 参数:
- g_ucKeyMode=1时,单次模式
- g_ucKeyMode=0时,持续模式
- 返回值:
- 0--按键无变化
- 1 --按键有按下:
- 单次模式下,当按键第一次按下才返回一次该值;
- 持续模式下,按着按键不放将间隔返回该值
- g_ulKeyValue保存当前按键值
- -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
- 释放后,g_ulKeyValue保存上次按下的按键值
- *************************************************************/
- unsigned char PttKeyFlag;
- 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;
-
- //去抖动 0 1
- if(++sucFlag>2){ //1
- sucFlag=0;
- }
-
- if(sucFlag==0){
- Key1=KeyScanPort();
- }else{
- Key2=KeyScanPort();
- }
- if(Key1!=Key2)return 0;
- //按键状态发生变更
- if(Key!=Key1){
- Key=Key1;
- if(Key!=(KEY_ALL_PORTB<<16))
- {
- sucPress=1; //有按下
- printf("KEY=%lx\r\n",Key);
-
- }
- else
- {
- sucPress=0; //无按下
- }
- }
- if(sucPress){//有按键按下
- if(g_ucKeyMode){ //单次模式
- if(sucLastPress!=sucPress){
- sucLastPress=sucPress;
- g_ulKeyValue=Key;
- if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
- // printf("##############\r\n");
- return 1;
- }else{
- return 0;
- }
- }else{ //持续模式
- sucLastPress=sucPress;
- if(0==sucCt){
- g_ulKeyValue=Key1;
- if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
- sucCt=100;//10ms*100=1000ms
- // printf("************\r\n");
- 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++;
- }
- int GetKeyVBAT(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;
- //去抖动 0 1
- if(++sucFlag>2){ //1
- sucFlag=0;
- }
- if(sucFlag==0){
- Key1=GetKeyVbat();
- }else{
- Key2=GetKeyVbat();
- }
- if(Key1!=Key2)return 0;
- //按键状态发生变更
- if(Key!=Key1){
- Key=Key1;
- if(Key<290)
- {
- sucPress=1; //有按下
- printf("KEY=%d.+++++++++\r\n",Key);
-
- }
- else
- {
- sucPress=0; //无按下
- }
- }
- if(sucPress){//有按键按下
- if(g_ucKeyMode){ //单次模式
- if(sucLastPress!=sucPress){
- sucLastPress=sucPress;
- if(Key==0){
- g_ulKeyValue=KEY_PANEL_MENU;
- }
- else if(Key<40){
- g_ulKeyValue=KEY_UP_DOWN_IP;
- }
- else if(Key<60){
- g_ulKeyValue=KEY_PANEL_UP;
- }else if(Key<120){
- g_ulKeyValue=KEY_PANEL_DOWN;
- }
- else if(Key<160){
- g_ulKeyValue=KEY_PANEL_EXIT;
- }else if(Key<210){
- g_ulKeyValue=KEY_PANEL_P1;
- }else
- g_ulKeyValue=KEY_PANEL_P2;
- return 1;
- }else{
- return 0;
- }
- }else{ //持续模式
- sucLastPress=sucPress;
- // printf("KEYVbat=%d............\r\n",Key);
- if(0==sucCt){
-
- if(Key==0){
- g_ulKeyValue=KEY_PANEL_MENU;
- }
- else if(Key<40){
- g_ulKeyValue=KEY_UP_DOWN_IP;
- }
- else if(Key<60){
- g_ulKeyValue=KEY_PANEL_UP;
- }else if(Key<120){
- g_ulKeyValue=KEY_PANEL_DOWN;
- }
- else if(Key<160){
- g_ulKeyValue=KEY_PANEL_EXIT;
- }else if(Key<210){
- g_ulKeyValue=KEY_PANEL_P1;
- }else
- g_ulKeyValue=KEY_PANEL_P2;
-
- sucCt=100;//10ms*100=1000ms
- return 1;
- }else{
- sucCt--;
- return 0;
- }
- }
- }else{ //无按键按下
- sucCt=0;
- if(sucLastPress){
- sucLastPress=0;
- return -1;
- }
- }
- return 0;
- }
- void OnOff_Init(void)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO , ENABLE);
-
- 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_PortSourceGPIOB, GPIO_PinSource11);
-
- EXTI_InitStructure.EXTI_Line = EXTI_Line11;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- }
- void EXTI15_10_IRQHandler(void)
- {
- if(SET==EXTI_GetFlagStatus(EXTI_Line11))
- {//外部电源关了
-
- printf("Power down----\r\n");
-
- if(modemInitReady==1)
- {
- PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
- modemPwrOffNow=1;
- }
- EXTI_ClearFlag(EXTI_Line11);
- }
- }
|