|
|
/******************** (C) COPYRIGHT 2013 CEPARK锟洁功锟杰匡拷锟斤拷学习锟斤拷******************** |
|
|
* 锟侥硷拷锟斤拷 锟斤拷key4x4.c |
|
|
* 锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷应锟矫猴拷锟斤拷锟斤拷 |
|
|
* 实锟斤拷平台锟斤拷CEPARK锟洁功锟杰匡拷锟斤拷学习锟斤拷 |
|
|
* 硬锟斤拷锟斤拷锟接o拷------------------------- |
|
|
* | PB8 - H4 | |
|
|
* | PB9 - H3 | |
|
|
* | PB10 - H2 | |
|
|
* | PB11 - H1 | |
|
|
* | PB12 - L4 | |
|
|
* | PB13 - L3 | |
|
|
* | PB14 - L2 | |
|
|
* | PB15 - L1 | |
|
|
* ------------------------- |
|
|
* 锟斤拷姹<EFBFBD> 锟斤拷ST3.5.0 |
|
|
**********************************************************************************/ |
|
|
|
|
|
#include "key.h" |
|
|
#include "delay.h" |
|
|
|
|
|
/* |
|
|
* 锟斤拷锟斤拷锟斤拷锟斤拷Key_GPIO_Config |
|
|
* 锟斤拷锟斤拷 锟斤拷锟斤拷锟矫帮拷锟斤拷锟矫碉拷锟斤拷I/O锟斤拷 |
|
|
* 锟斤拷锟斤拷 锟斤拷锟斤拷 |
|
|
* 锟斤拷锟<EFBFBD> 锟斤拷锟斤拷 |
|
|
*/ |
|
|
void Key4x4_GPIO_Config(void) |
|
|
{ |
|
|
GPIO_InitTypeDef GPIO_InitStructure; |
|
|
|
|
|
/*锟斤拷锟斤拷锟斤拷锟斤拷锟剿口碉拷时锟斤拷*/ |
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA,ENABLE); |
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 ; |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; |
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure); |
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10 | GPIO_Pin_11; |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure); |
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;//KEY0-KEY1 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入 |
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOE4,3 |
|
|
} |
|
|
|
|
|
|
|
|
u8 Key_Scan(void) |
|
|
{ |
|
|
u16 temp; |
|
|
u8 ucBackValue=16; |
|
|
//====扫锟斤拷锟揭伙拷锟<EFBFBD>========== |
|
|
|
|
|
PBout(1) = 0; |
|
|
PBout(0) = 1; |
|
|
PBout(10) = 1; |
|
|
PBout(11) = 1; |
|
|
|
|
|
temp=GPIO_ReadInputData(GPIOA); //锟斤拷锟斤拷锟斤拷锟斤拷锟节得碉拷锟斤拷值 |
|
|
|
|
|
temp=temp&0x003c; //锟斤拷锟轿碉拷4位 |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟<EFBFBD>4位锟斤拷全锟斤拷1 |
|
|
{ |
|
|
delay_ms(20); // 锟斤拷时锟斤拷锟斤拷锟劫讹拷 |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷俅胃锟<EFBFBD>4位确锟斤拷锟斤拷锟斤拷全1 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA)&0x003c; //锟斤拷锟斤拷锟剿次帮拷锟斤拷锟斤拷值 |
|
|
switch(temp) |
|
|
{ |
|
|
case 0x0038: |
|
|
ucBackValue = 13;break; |
|
|
case 0x0034: |
|
|
ucBackValue = 6;break; |
|
|
case 0x002c: |
|
|
ucBackValue = 5;break; |
|
|
case 0x001c: |
|
|
ucBackValue = 4;break; |
|
|
default:break; |
|
|
} |
|
|
while(temp!=0x003c) //锟饺达拷锟斤拷锟斤拷锟脚匡拷锟斤拷锟斤拷始锟斤拷然锟斤拷执锟斤拷 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
//====锟节讹拷锟斤拷锟斤拷0========== |
|
|
PBout(1) = 1; |
|
|
PBout(0) = 0; |
|
|
PBout(10) = 1; |
|
|
PBout(11) = 1; |
|
|
|
|
|
temp=GPIO_ReadInputData(GPIOA); //锟斤拷锟斤拷锟斤拷锟斤拷锟节得碉拷锟斤拷值 |
|
|
temp=temp&0x003c; //锟斤拷锟轿碉拷4位 |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟<EFBFBD>4位锟斤拷全锟斤拷1 |
|
|
{ |
|
|
delay_ms(20); // 锟斤拷时锟斤拷锟斤拷锟劫讹拷 |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷俅胃锟<EFBFBD>4位确锟斤拷锟斤拷锟斤拷全1 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA)&0x003c; //锟斤拷锟斤拷锟剿次帮拷锟斤拷锟斤拷值 |
|
|
switch(temp) |
|
|
{ |
|
|
case 0x0038: |
|
|
ucBackValue = 12; break; |
|
|
case 0x0034: |
|
|
ucBackValue = 3; break; |
|
|
case 0x002c: |
|
|
ucBackValue = 2; break; |
|
|
case 0x001c: |
|
|
ucBackValue = 1; break; |
|
|
default:break; |
|
|
} |
|
|
while(temp!=0x003c) //锟饺达拷锟斤拷锟斤拷锟脚匡拷 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
} |
|
|
} |
|
|
} |
|
|
//====锟斤拷3锟斤拷锟斤拷0========== |
|
|
|
|
|
PBout(1) = 1; |
|
|
PBout(0) = 1; |
|
|
PBout(10) = 0; |
|
|
PBout(11) = 1; |
|
|
|
|
|
temp=GPIO_ReadInputData(GPIOA); //锟斤拷锟斤拷锟斤拷锟斤拷锟节得碉拷锟斤拷值 |
|
|
temp=temp&0x003c; //锟斤拷锟轿碉拷4位 |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟<EFBFBD>4位锟斤拷全锟斤拷1 |
|
|
{ |
|
|
delay_ms(20); // 锟斤拷时锟斤拷锟斤拷锟劫讹拷 |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷俅胃锟<EFBFBD>4位确锟斤拷锟斤拷锟斤拷全1 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA)&0x003c; //锟斤拷锟斤拷锟剿次帮拷锟斤拷锟斤拷值 |
|
|
switch(temp) |
|
|
{ |
|
|
case 0x0038: |
|
|
ucBackValue = 14; break; |
|
|
case 0x0034: |
|
|
ucBackValue = 9; break; |
|
|
case 0x002c: |
|
|
ucBackValue = 8; break; |
|
|
case 0x001c: |
|
|
ucBackValue = 7; break; |
|
|
default:break; |
|
|
} |
|
|
while(temp!=0x003c) //锟饺达拷锟斤拷锟斤拷锟脚匡拷 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
} |
|
|
} |
|
|
} |
|
|
//====锟斤拷4锟斤拷锟斤拷0========== |
|
|
PBout(1) = 1; |
|
|
PBout(0) = 1; |
|
|
PBout(10) = 1; |
|
|
PBout(11) = 0; |
|
|
|
|
|
temp=GPIO_ReadInputData(GPIOA); //锟斤拷锟斤拷锟斤拷锟斤拷锟节得碉拷锟斤拷值 |
|
|
temp=temp&0x003c; //锟斤拷锟轿碉拷4位 |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟<EFBFBD>4位锟斤拷全锟斤拷1 |
|
|
{ |
|
|
delay_ms(20); // 锟斤拷时锟斤拷锟斤拷锟劫讹拷 |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
if (temp!=0x003c) //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷俅胃锟<EFBFBD>4位确锟斤拷锟斤拷锟斤拷全1 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA)&0x003c; |
|
|
switch(temp) |
|
|
{ |
|
|
case 0x0038: |
|
|
ucBackValue = 15; break; |
|
|
case 0x0034: |
|
|
ucBackValue = 11; break; |
|
|
case 0x002c: |
|
|
ucBackValue = 0; break; |
|
|
case 0x001c: |
|
|
ucBackValue = 10; break; |
|
|
default:break; |
|
|
} |
|
|
while(temp!=0x003c) //锟饺达拷锟斤拷锟斤拷锟脚匡拷 |
|
|
{ |
|
|
temp=GPIO_ReadInputData(GPIOA); |
|
|
temp=temp&0x003c; |
|
|
} |
|
|
} |
|
|
} |
|
|
return ucBackValue; |
|
|
} |
|
|
|
|
|
//按键处理函数 |
|
|
//返回按键值 |
|
|
//mode:0,不支持连续按;1,支持连续按; |
|
|
//0,没有任何按键按下 |
|
|
//1,KEY0按下 |
|
|
//2,KEY1按下 |
|
|
//3,KEY3按下 WK_UP |
|
|
//注意此函数有响应优先级,KEY0>KEY1>KEY_UP!! |
|
|
u8 KEY_Scan(u8 mode) |
|
|
{ |
|
|
static u8 key_up=1;//按键按松开标志 |
|
|
if(mode)key_up=1; //支持连按 |
|
|
if((IN==0||OUT==0)) |
|
|
{ |
|
|
delay_ms(10);//去抖动 |
|
|
if(IN==0)return IN_PRES; |
|
|
else if(OUT==0)return OUT_PRES; |
|
|
}else if(OUT==1&&IN==1)key_up=1; |
|
|
return 0;// 无按键按下 |
|
|
} |
|
|
|
|
|
|