OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6595|回复: 3

STM32 定时器扫描矩阵按键程序求教!(已解决)

[复制链接]

1

主题

2

帖子

0

精华

新手上路

积分
37
金钱
37
注册时间
2014-4-29
在线时间
2 小时
发表于 2014-5-8 12:41:53 | 显示全部楼层 |阅读模式
5金钱
补充:程序没什么问题,应该是10ms定时时间有点长,有时间把完整的程序以及12864液晶驱动贴上来。

这是矩阵程序(参考吴坚鸿的按键程序)是从51上面移植过来的,在51上面运行正常,但在STM32上没有反应,主要表现在按键扫描函数Key_Scan里面 switch的case1 执行完之后,直接跳转到case9,请大虾帮忙看看,谢了~
采用上拉输入、推挽输出、定时器扫描
/*key.h文件*/
[mw_shl_code=c,true]#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[/mw_shl_code]



/*key.c文件*/
[mw_shl_code=c,true]#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; } }[/mw_shl_code]



正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-5-8 13:52:23 | 显示全部楼层
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

25

主题

683

帖子

0

精华

论坛大神

Rank: 7Rank: 7Rank: 7

积分
1351
金钱
1351
注册时间
2012-4-25
在线时间
195 小时
发表于 2014-5-8 13:56:17 | 显示全部楼层
写得这么繁琐复杂,看你的行和列GPIO都是连续的,完合可以用两个for 循环 移位判定按键状态。

 只要简单的 不超过 30行代码吧
1-1
回复

使用道具 举报

86

主题

984

帖子

0

精华

论坛大神

Rank: 7Rank: 7Rank: 7

积分
1850
金钱
1850
注册时间
2013-4-15
在线时间
163 小时
发表于 2014-6-24 10:03:09 | 显示全部楼层
回复【2楼】正点原子:
---------------------------------
貌似写的也很繁杂
合肥-文盲
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-7 00:53

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表