完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本帖最后由 木丁西 于 2014-6-2 23:27 编辑
#include void KeyDriver(); ***it KEY_IN_4 = P2^7; ***it KEY_IN_3 = P2^6; ***it KEY_IN_2 = P2^5; ***it KEY_IN_1 = P2^4; ***it KEY_OUT_1 = P2^3; ***it KEY_OUT_2 = P2^2; ***it KEY_OUT_3 = P2^1; ***it KEY_OUT_4 = P2^0; ***it ADDR0 = P1^0; ***it ADDR1 = P1^1; ***it ADDR2 = P1^2; ***it ADDR3 = P1^3; ***it ENLED = P1^4; unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表 { 0x31, 0x32, 0x33, 0x26 }, //数字键1、数字键2、数字键3、向上键 { 0x34, 0x35, 0x36, 0x25 }, //数字键4、数字键5、数字键6、向左键 { 0x37, 0x38, 0x39, 0x28 }, //数字键7、数字键8、数字键9、向下键 { 0x30, 0x1B, 0x0D, 0x27 }} ; //数字键0、ESC键、 回车键、向右键 unsigned char Ledbuffer[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char code LedChar[]={ 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E }; unsigned char keystate[4][4] ={{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}; void main() { ADDR3 = 1; ENLED = 0; EA = 1; TMOD = 0X01; TH0 = 0XFC; TL0 = 0X67; ET0 = 1; TR0 = 1; Ledbuffer[0] = LedChar[0]; while(1) { KeyDriver(); } } void ShowNumber(unsigned long num) { signed char buffer[6]; unsigned char i; for(i=0; i<=5; i++) { buffer = num%10; num = num/10; } for(i=5; i>=1; i--) { if(buffer == 0) { Ledbuffer = 0xff; } else break; } for(; i>=0; i--) { Ledbuffer = LedChar[buffer]; } } void KeyAction(unsigned char keycode) { static unsigned long result = 0; static unsigned long addend = 0; if((keycode >= 0x30) && (keycode <= 0x39)) { addend = (addend*10) + (keycode - 0x30); ShowNumber(addend); } else if(keycode == 0x26) { result += addend; addend = 0; ShowNumber(result); } else if(keycode == 0x0D) { result += addend; addend = 0; ShowNumber(result); } else if(keycode == 0x1B) { addend = 0; result = 0; ShowNumber(addend); } } void KeyDriver() { unsigned char i, j; static unsigned char backup[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}}; for(i=0; i<4; i++) { for(j=0; j<4; j++) { if(backup[j] != keystate[j]) { if(backup[j] == 0) { KeyAction(KeyCodeMap[j]); } backup[j] = keystate[j]; } } } } void LedScan() { static unsigned char i = 0; P0 = 0XFF; switch(i) { case 0: ADDR2 = 0; ADDR1 = 0; ADDR0 = 0; i++; P0 = Ledbuffer[0];break; case 1: ADDR2 = 0; ADDR1 = 0; ADDR0 = 1; i++; P0 = Ledbuffer[1];break; case 2: ADDR2 = 0; ADDR1 = 1; ADDR0 = 0; i++; P0 = Ledbuffer[2];break; case 3: ADDR2 = 0; ADDR1 = 1; ADDR0 = 1; i++; P0 = Ledbuffer[3];break; case 4: ADDR2 = 1; ADDR1 = 0; ADDR0 = 0; i++; P0 = Ledbuffer[4];break; case 5: ADDR2 = 1; ADDR1 = 0; ADDR0 = 1; i=0; P0 = Ledbuffer[5];break; default: break; } } void KeyScan() { unsigned char i; static unsigned char keybuffer[4][4] = { {0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF}}; static unsigned char keyout = 0; keybuffer[keyout][0] = (keybuffer[keyout][0] << 1) | KEY_IN_1; keybuffer[keyout][1] = (keybuffer[keyout][1] << 1) | KEY_IN_2; keybuffer[keyout][2] = (keybuffer[keyout][2] << 1) | KEY_IN_3; keybuffer[keyout][3] = (keybuffer[keyout][3] << 1) | KEY_IN_4; for(i=0; i<4; i++) { if((keybuffer[keyout] & 0x0f) == 0x0f) { keystate[keyout] = 1; } else if((keybuffer[keyout] & 0x0f) == 0x00 ) { keystate[keyout] = 0; } } keyout++; keyout = keyout & 0x03; switch(keyout) { case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break; case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break; case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break; case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break; default: break; } } void InterruptTimer0() interrupt 1 { TH0 = 0XFC; TL0 = 0X67; LedScan(); KeyScan(); } 求助:为何按键按下去了 。。数码管就不亮了 |
|
相关推荐
8个回答
|
|
主函数里面都没有调用显示函数。
下面的是不是有问题:i都没用到。 for(i=0; i<4; i++) { if((keybuffer[keyout] & 0x0f) == 0x0f) { keystate[keyout] = 1; } else if((keybuffer[keyout] & 0x0f) == 0x00 ) { keystate[keyout] = 0; } } |
|
|
|
本帖最后由 木丁西 于 2014-6-3 13:38 编辑
不知道为什么复制后 就有部分无法显示出来 不过程序只是一点点小问题 我找出来了 //2014年6月2日23:40:15 //2014年6月3日10:29:25成功 #include void KeyDriver(); ***it KEY_IN_4 = P2^7; ***it KEY_IN_3 = P2^6; ***it KEY_IN_2 = P2^5; ***it KEY_IN_1 = P2^4; ***it KEY_OUT_1 = P2^3; ***it KEY_OUT_2 = P2^2; ***it KEY_OUT_3 = P2^1; ***it KEY_OUT_4 = P2^0; ***it ADDR0 = P1^0; ***it ADDR1 = P1^1; ***it ADDR2 = P1^2; ***it ADDR3 = P1^3; ***it ENLED = P1^4; unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表 { 0x31, 0x32, 0x33, 0x26 }, //数字键1、数字键2、数字键3、向上键 { 0x34, 0x35, 0x36, 0x25 }, //数字键4、数字键5、数字键6、向左键 { 0x37, 0x38, 0x39, 0x28 }, //数字键7、数字键8、数字键9、向下键 { 0x30, 0x1B, 0x0D, 0x27 }} ; //数字键0、ESC键、 回车键、向右键 unsigned char Ledbuffer[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; unsigned char code LedChar[]={ 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E }; unsigned char keystate[4][4] ={{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}; void main() { ADDR3 = 1; ENLED = 0; EA = 1; TMOD = 0X01; TH0 = 0XFC; TL0 = 0X67; ET0 = 1; TR0 = 1; Ledbuffer[0] = LedChar[0]; while(1) { KeyDriver(); } } void ShowNumber(unsigned long num) { unsigned char buffer[6]; signed char i; //问题出在这里,若为unsigned型,就会导致无法显示溢出,因为 //后面i--会成为负数成为死循环 for(i=0; i<=5; i++) { buffer = num%10; num = num/10; } for(i=5; i>=1; i--) { if(buffer == 0) { Ledbuffer = 0xff; } else break; } for(; i>=0; i--) //注意这里,i<0后,会成为死循环,所以i应该为signed类型 { Ledbuffer = LedChar[buffer]; } } void KeyAction(unsigned char keycode) { static unsigned long result = 0; static unsigned long addend = 0; if((keycode >= 0x30) && (keycode <= 0x39)) { addend = (addend*10) + (keycode - 0x30); ShowNumber(addend); } else if(keycode == 0x26) { result += addend; addend = 0; // ShowNumber(result); } else if(keycode == 0x0D) { result += addend; addend = 0; ShowNumber(result); } else if(keycode == 0x1B) { addend = 0; result = 0; ShowNumber(addend); } } void KeyDriver() { unsigned char i, j; static unsigned char backup[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}}; for(i=0; i<4; i++) { for(j=0; j<4; j++) { if(backup[j] != keystate[j]) { if(backup[j] == 0) { KeyAction(KeyCodeMap[j]); } backup[j] = keystate[j]; } } } } void LedScan() { static unsigned char i = 0; P0 = 0XFF; switch(i) { case 0: ADDR2 = 0; ADDR1 = 0; ADDR0 = 0; i++; P0 = Ledbuffer[0];break; case 1: ADDR2 = 0; ADDR1 = 0; ADDR0 = 1; i++; P0 = Ledbuffer[1];break; case 2: ADDR2 = 0; ADDR1 = 1; ADDR0 = 0; i++; P0 = Ledbuffer[2];break; case 3: ADDR2 = 0; ADDR1 = 1; ADDR0 = 1; i++; P0 = Ledbuffer[3];break; case 4: ADDR2 = 1; ADDR1 = 0; ADDR0 = 0; i++; P0 = Ledbuffer[4];break; case 5: ADDR2 = 1; ADDR1 = 0; ADDR0 = 1; i=0; P0 = Ledbuffer[5];break; default: break; } } void KeyScan() { unsigned char i; static unsigned char keybuffer[4][4] = { {0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},{0xFF, 0xFF, 0xFF, 0xFF}}; static unsigned char keyout = 0; keybuffer[keyout][0] = (keybuffer[keyout][0] << 1) | KEY_IN_1; keybuffer[keyout][1] = (keybuffer[keyout][1] << 1) | KEY_IN_2; keybuffer[keyout][2] = (keybuffer[keyout][2] << 1) | KEY_IN_3; keybuffer[keyout][3] = (keybuffer[keyout][3] << 1) | KEY_IN_4; for(i=0; i<4; i++) { if((keybuffer[keyout] & 0x0f) == 0x0f) { keystate[keyout] = 1; } else if((keybuffer[keyout] & 0x0f) == 0x00 ) { keystate[keyout] = 0; } } keyout++; keyout = keyout & 0x03; switch(keyout) { case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break; case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break; case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break; case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break; default: break; } } void InterruptTimer0() interrupt 1 { TH0 = 0XFC; TL0 = 0X67; LedScan(); KeyScan(); } |
|
|
|
这个还是有问题 衰 该如何解决复制问题呢
|
|
|
|
看了你的代码,按键扫描这块确定没有问题???
else if((keybuffer[keyout][i] & 0x0f) == 0x00 ) //这个为什么会等于0x00 等于0x00相当于四个按键都为0,不解??? { keystate[keyout] = 0; } switch(keyout) //这个有什么作用不明白??? { case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break; case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break; case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break; case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break; default: break; } |
|
|
|
没有问题 我已经在板子上试了一试
else if((keybuffer[keyout][i] & 0x0f) == 0x00 ) 这里是利用中断 每次移位一位 如果检测到 按键状态满足了4个0 就说明按下了 然后我就给按键状态keystate[keyout] = 0 。。switch(keyout) 这个是选择矩阵键盘的行 中断一次换一行 keyout是指行 如果有兴趣的话可以加我qq探讨一下1012421396 非常谢谢你如此用心 (我上传到 程序从Keil里复制粘贴后有部分看不到了 ) |
|
|
|
你这个是手把手教你学习单片机与C语言上面的一个例程,我刚刚也在学习,只是开发板与你用的不一样,问题很多!现在一个个的排查!
|
|
|
|
下载来学习,谢谢。
|
|
|
|
哇 真棒!看着很不错诶 谢谢分享
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
使用Keil建立完整的工程,并使用外部中断0触发数码管显示903
418 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-使用AHT20进行环境监测之AHT20传感器介绍
1083 浏览 0 评论
846 浏览 0 评论
886 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
882 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11834 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-1 04:57 , Processed in 0.703028 second(s), Total 88, Slave 71 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号