学前班
最后登录1970-1-1
在线时间 小时
注册时间2014-5-3
|
这是矩阵程序(参考吴坚鸿的按键程序)是从51上面移植过来的,在51上面运行正常,但在STM32上没有反应,主要表现在按键扫描函数Key_Scan里面 switch的case1 执行完之后,直接跳转到case9,请大虾帮忙看看,谢了~
/*key.h文件*/
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#include "delay.h"
#include "12864.h"
#include "usart.h"
#include "led.h"
/*
#define key_sr1 PDin(4)
#define key_sr2 PDin(5)
#define key_sr3 PDin(8)
#define key_sr4 PDin(9)
*/
#define key_sr1 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_4)//读取按键
#define key_sr2 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_5)//
#define key_sr3 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_8)//
#define key_sr4 GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_9)//
#define key_dr1 PEout(10)
#define key_dr2 PEout(11) //
#define key_dr3 PEout(12) //
#define key_dr4 PEout(13) //
#define key_dr5 PEout(14) //
#define key_dr6 PEout(15)
#define const_voice_short 100 //
#define const_key_time 20 //
void KEY_Init(void);
void KEY_Scan(void);
void key_service(void);
extern u16 uiVoiceCnt;
extern u8 ucKeySec;
#endif
/*key.c文件*/
#include "key.h"
//IO初始化
void KEY_Init(void) //IO³õʼ»¯
{
GPIO_InitTypeDef GPIO_InitStructure;
//KEY0-->GPIOA.13,KEY1-->GPIOA.15 上拉输入
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9;//PE2~4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIOD4,5,8,9
//GPIO_SetBits(GPIOD,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);//
GPIO_SetBits(GPIOE,GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
}
u16 uiVoiceCnt=0; //
u8 ucKeySec = 0; //
u8 ucKeyStep = 1; //
u8 ucKeyLock = 0; //
u16 uiKeyTimeCnt = 0;
void KEY_Scan()
{
//LED0 = 0;
switch(ucKeyStep)
{
case 1: //
key_dr1=0;
key_dr2=1;
key_dr3=1;
key_dr4=1;
key_dr5=1;
key_dr6=1;
uiKeyTimeCnt=0; //
ucKeyStep++; //
break;
case 2: //
uiKeyTimeCnt++;
if(uiKeyTimeCnt>2)
{
uiKeyTimeCnt=0;
ucKeyStep++; //
}
break;
case 3:
if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
ucKeyStep++; //
ucKeyLock=0; //
uiKeyTimeCnt=0; //
}
else if(ucKeyLock==0)
{
if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1; //
ucKeySec=1; //
}
}
else if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//
ucKeySec=2; //
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
{
uiKeyTimeCnt++; //
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1; //
ucKeySec=3; //
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=4; //??4??
}
}
}
break;
case 4: //????????????
key_dr1=1;
key_dr2=0;
key_dr3=1;
key_dr4=1;
key_dr5=1;
key_dr6=1;
uiKeyTimeCnt=0; //???????
ucKeyStep++; //??????????
break;
case 5: //???????????????????,????????????????
uiKeyTimeCnt++;
if(uiKeyTimeCnt>1)
{
uiKeyTimeCnt=0;
ucKeyStep++; //??????????
}
break;
case 6:
if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
ucKeyStep++; //????????,??????????
ucKeyLock=0; //????????
uiKeyTimeCnt=0; //????????????,??????
}
else if(ucKeyLock==0) //?????,???????
{
if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=5; //??5??
}
}
else if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=6; //??6??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=7; //??7??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=8; //??8??
}
}
}
break;
case 7: //????????????
key_dr1=1;
key_dr2=1;
key_dr3=0;
key_dr4=1;
key_dr5=1;
key_dr6=1;
uiKeyTimeCnt=0; //???????
ucKeyStep++; //??????????
break;
case 8: //???????????????????,????????????????
uiKeyTimeCnt++;
if(uiKeyTimeCnt>1)
{
uiKeyTimeCnt=0;
ucKeyStep++; //??????????
}
break;
case 9:
if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
ucKeyStep++; //????????,??????????
ucKeyLock=0; //????????
uiKeyTimeCnt=0; //????????????,??????
}
else if(ucKeyLock==0) //?????,???????
{
if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=9; //??9??
}
}
else if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=10; //??10??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=11; //??11??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
{
uiKeyTimeCnt++; //
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1; //
ucKeySec=12; //
}
}
}
break;
case 10: //
key_dr1=1;
key_dr2=1;
key_dr3=1;
key_dr4=0;
key_dr5=1;
key_dr6=1;
uiKeyTimeCnt=0; //
ucKeyStep++; //
break;
case 11: //
uiKeyTimeCnt++;
if(uiKeyTimeCnt>1)
{
uiKeyTimeCnt=0;
ucKeyStep++; //??????????
}
break;
case 12:
if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
ucKeyStep++; //
ucKeyLock=0; //
uiKeyTimeCnt=0; //
}
else if(ucKeyLock==0) //
{
if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//
ucKeySec=13; //
}
}
else if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=14; //??14??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=15; //??15??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=16; //??16??
}
}
}
break;
case 13: //????????????
key_dr1=1;
key_dr2=1;
key_dr3=1;
key_dr4=1;
key_dr5=0;
key_dr6=1;
uiKeyTimeCnt=0; //???????
ucKeyStep++; //??????????
break;
case 14: //???????????????????,????????????????
uiKeyTimeCnt++;
if(uiKeyTimeCnt>1)
{
uiKeyTimeCnt=0;
ucKeyStep++; //??????????
}
break;
case 15:
if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
ucKeyStep++; //????????,??????,??????
ucKeyLock=0; //????????
uiKeyTimeCnt=0; //????????????,??????
}
else if(ucKeyLock==0) //?????,???????
{
if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=17; //??17??
}
}
else if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=18; //??18??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=19; //??19??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=20; //??20??
}
}
}
break;
case 16: //????????????
key_dr1=1;
key_dr2=1;
key_dr3=1;
key_dr4=1;
key_dr5=1;
key_dr6=0;
uiKeyTimeCnt=0; //???????
ucKeyStep++; //??????????
break;
case 17: //???????????????????,????????????????
uiKeyTimeCnt++;
if(uiKeyTimeCnt>1)
{
uiKeyTimeCnt=0;
ucKeyStep++; //??????????
}
break;
case 18:
if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
ucKeyStep=1; //????????,??????,??????
ucKeyLock=0; //????????
uiKeyTimeCnt=0; //????????????,??????
}
else if(ucKeyLock==0) //?????,???????
{
if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=21; //??21??
}
}
else if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=22; //??22??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=23; //??23??
}
}
else if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
{
uiKeyTimeCnt++; //????????
if(uiKeyTimeCnt>const_key_time)
{
uiKeyTimeCnt=0;
ucKeyLock=1;//??????,??????,??????,?????????
ucKeySec=24; //??24??
}
}
}
break;
}
//return ucKeySec;
}
void key_service() //
{
u8 ucKeyThird = 0;
u8 temp = 0;
LED0 = !LED0;
switch(ucKeySec) //????????
{
case 1:// 1??
printf("Ò»ºÅ¼ü");
// SoftReset();
//uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 2:// 2??
AssitButton();//????????
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
ucKeyThird = 1;
while(ucKeyThird)
{
switch(ucKeySec)
{
case 1:
//SoftReset();
ucKeySec=0;
ucKeyThird = 0;
break;
case 2:
AssitButton();
ucKeySec=0;
ucKeyThird = 0;
break;
case 3:
prepare();
ucKeySec=0; //???????????,??????,??????
ucKeyThird = 0;
break;
case 4:
CediButton();
ucKeySec=0; //???????????,??????,??????
ucKeyThird = 0;
break;
case 10:
printf("The No.10 Button");
PointButton();
ucKeySec=0;
ucKeyThird = 0;
break;
case 11:
PointButton();
ucKeySec=0;
ucKeyThird = 0;
break;
case 15:
printf("The No.15 Button");
StatusButton();
ucKeySec=0;
ucKeyThird = 0;
break;
case 24:
ucKeySec=0; //???????????,??????,??????
ucKeyThird = 1;
break;
}
}
break;
case 3:// 3??
++temp;
printf("The No.3 Button");
prepare();
//Init1();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 4:// 4??
--temp;
printf("Ëĺżü");
CediButton();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 5:// 5??
printf("ÎåºÅ¼ü");
temp++;
if(1==temp)
{
Down();
}
if(2==temp)
{
Up();
temp = 0;
}
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 6:// 6??
printf("ÁùºÅ¼ü");
FrontButton();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 7:// 7??
printf("Æߺżü");
EditorButton();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 8:// 8??
printf("°ËºÅ¼ü");
SendButton();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 9:// 9??
printf("¾ÅºÅ¼ü");
ClockButton();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 10:// 10??
printf("Ê®ºÅ¼ü");
ConfigeButton();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 11:// 11??
printf("ʮһºÅ¼ü");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 12:// 12??
printf("Ê®¶þºÅ¼ü\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 13:// 13??
printf("Ê®ÈýºÅ¼ü\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 14:// 14??
printf("Ê®Ëĺżü\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 15:// 15??
InitInterface();
printf("Ê®ÎåºÅ¼ü\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 16:// 16??
printf("Ê®ÁùºÅ¼ü\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 17:// 17??
printf("The No.17 Button\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 18:// 18??
printf("The No.18 Button\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 19:// 19??
printf("The No.19 Button\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 20:// 20??
InitInterface();
printf("The No.20 Button\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 21:// 21??
printf("The No.21 Button\n");
//LCD();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 22:// 22??
printf("The No.22 Button\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 23:// 23??
printf("The No.23 Button\n");
//LCD1();
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
case 24:// 24??
//LCD2();
printf("The No.24 Button\n");
uiVoiceCnt=const_voice_short; //??????,??????
ucKeySec=0; //???????????,??????,??????
break;
}
}
|
|