123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- /*******************************************************************************
- * 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 <includes.h>
- //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;
- int g_iEncode=0;
- unsigned char g_ucEncode[20];
- ////////////////////////// function declare //////////////////////////////
- unsigned long KeyScanPort(void);
- ////////////////////////// end declare //////////////////////////////
- /********************************************************************************
- KeyInit
- *******************************************************************************/
- 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);
- /*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_PANEL_OK_PIN;
- GPIO_Init(KEY_PANEL_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_POWER_PIN;
- GPIO_Init(KEY_POWER_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_PIN;
- GPIO_Init(KEY_FUNC_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);
-
- 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);
- }
- /***************************************
- *flag=1 按键按下 flag=-1按键释放
- ****************************************/
- void KeyToModem(int flag)
- {
- //char buf[400];
- // RunMake(THIS_FILE_ID);
- if(g_ulKeyValue==KEY_PPT){
- if(flag==1){
- MODEM_DTR_LOW;
- }else{
- MODEM_DTR_HIGH;
- }
- }
- //buf[0]=1;
- //if(buf[0])return;
- // if(g_ulKeyValue==KEY_UP){
- // if(flag==1){
- // MODEM_DSR_LOW;
- // SlwTrace(INF,"[UP+]\r\n");
- // }else{
- // MODEM_DSR_HIGH;
- // SlwTrace(INF,"[UP-]\r\n");
- // }
- // }
- //
- // if(g_ulKeyValue==KEY_DOWN){
- // if(flag==1){
- // MODEM_RING_LOW;
- // SlwTrace(DEBUG,"[DOWN+]\r\n");
- // }else{
- // MODEM_RING_HIGH;
- // SlwTrace(DEBUG,"[DOWN-]\r\n");
- // }
- // }
-
- }
- /************************************************************
- *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;
- //GetEncodeKey();
- //去抖动 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)| KEY_ALL_PORTC))
- {
- sucPress=1; //有按下
- g_ucUKC=0;
- //printf("KEY=%lx\r\n",Key);
- }
- else
- {
- printf("KEY=%lx\r\n",Key);
- 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=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_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 EXTI9_5_IRQHandler(void)
- {
- if(SET==EXTI_GetFlagStatus(EXTI_Line9))
- {//外部电源关了
- if(modemInitReady==1)
- {
- PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
- modemPwrOffNow=1;
- }
- EXTI_ClearFlag(EXTI_Line9);
- }
- }
- /********************************************************************************
- * Function:EncodeKeyScanPort
-
- *******************************************************************************/
- unsigned char EncodeKeyScanPort(void)
- {
- unsigned char v;
- unsigned long k1,k2;
- k2=GPIOB->IDR;
- k2&=KEY_PANEL_UP_PIN;
- k1=GPIOA->IDR;
- k1&=KEY_PANEL_DOWN_PIN;
-
- k1=k1|k2;
- v=(unsigned char)k1&0xff;
- return v;
- }
- /********************************************************************************
- GetEncodeKey
- 返回0 --不变
- 1 --加1
- -1 --减1
- 当
- *******************************************************************************/
- int GetEncodeKey(void)
- {
- static unsigned char sucV[3]={0,0,0};
- static unsigned char si=0;
- static unsigned char lastEncode;
- unsigned char Encode;
- unsigned char flag;
- sucV[si]=EncodeKeyScanPort();
- Encode=sucV[0];
- if(0!=(Encode&KEY_PANEL_DOWN_PIN) && 0==(lastEncode&KEY_PANEL_DOWN_PIN)){ //上升沿
- if(Encode & KEY_PANEL_UP_PIN){
- g_iEncode=1;
- }else{
- g_iEncode=-1;
- }
- }else if(0==(Encode&KEY_PANEL_DOWN_PIN) && 0!=(lastEncode&KEY_PANEL_DOWN_PIN)){//下降沿
- if(Encode & KEY_PANEL_UP_PIN){
- g_iEncode=-1;
- }else{
- g_iEncode=1;
- }
- }
- lastEncode= Encode;
- return flag;
- }
- /********************************************************************************
- * End of Module
- *******************************************************************************/
|