#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 PowerOffCheck(void) { //开机后,长按电源键关机 static unsigned int secondTick=0; if((GPIOB->IDR & GPIO_Pin_3) == 0) {//按下了 if(++secondTick >= 900000) {//关机 IapTrace("Power on"); GPIOB->BSRR=GPIO_Pin_8; // while(1) // { // // } } }else{ secondTick=0; } } void CheckPwrKeyOn(void) {//长按电源键直到电筒灯亮后释放 unsigned int tick=0; if(KeyPwrStatus !=0) { POWER_ON_FAILED: IapTrace("\r\nPwrOnFailed"); DelayMs(10); PWR_EN_LOW; while(1); } while(KeyPwrStatus==0) { if(++tick >= 200000) { IapTrace("\r\nPwrOnOk"); PWR_EN_HIGH;// return; } } IapTrace("\r\nPwr erro"); goto POWER_ON_FAILED; } void KeyInit(void) { GPIO_InitTypeDef GPIO_InitStructure; //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 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); // 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); //KEY_POWER 电源检测几秒 /*GPIO设置为浮空*/ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin=KEY_POWER_PIN; GPIO_Init(KEY_POWER_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; u32 Flashtick=0; void KeyCount(void) { //if(g_ucDKC<255)g_ucDKC++; if(g_ucUKC<255)g_ucUKC++; }