完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
`#include unsigned char code Ledchar[4][4] = { {0x3f,0x06,0x5b,0x4f}, {0x66,0x6d,0x7d,0x07}, {0x7f,0x6f,0x77,0x7c}, {0x39,0x5e,0x79,0x71}, }; unsigned char keysta[4][4] = { {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; void main() { unsigned char keyup[4][4] = { {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; static unsigned char i,j; EA = 1; ET0 = 1; TMOD = 0X01; TH0 = 0XFC; TL0 = 0X18; TR0 = 1; P0 = Ledchar[0][0]; while(1) { for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(keysta[j] != keyup[j]) { if(keyup[j] == 0) { P0 = Ledchar[j]; } } keyup[j] = keysta[j]; } } } } void keyscan() { unsigned char keybuf[4][4] = { {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, }; static unsigned char th,tl; static unsigned char i,j; P3 = 0xf0; th = P3; switch(th) { case 0xe0: i = 0; break; case 0xd0: i = 1; break; case 0xb0: i = 2; break; case 0x70: i = 3; break; default:break; } P3 = 0x0f; tl = P3; switch(tl) { case 0x0e: j = 0; break; case 0x0d: j = 1; break; case 0x0b: j = 2; break; case 0x07: j = 3; break; default:break; } if((keybuf[j] & 0x0f) == 0x00) { keysta[j] = 0; } if((keybuf[j] & 0x0f) == 0xff) { keysta[j] = 1; } } void TimeInt() interrupt 1 { TH0 = 0XFC; TL0 = 0X18; keyscan(); } 为什么总是卡在主函数P0 = Ledchar[0][0];这里 大神求教 ` |
|
相关推荐
4个回答
|
|
消抖应该在按键按下后进行消抖,你写的程序只是定时去执行keyscan();这个函数,而起不到消抖的作用
|
|
|
|
牛逼 |
|
|
|
我是在按键按下之后,通过看他连续4毫秒的扫描。判定其按下,还是恢复,直接返回按键消抖后的值 |
|
|
|
通过方法的比较,这种矩阵按键扫描方法不行,换了一种扫描方法,就准确的解决了 #include ***it LSA=P2^2; ***it LSB=P2^3; ***it LSC=P2^4; ***it key3 = P1^0; ***it key2 = P1^1; ***it key1 = P1^2; ***it key0 = P1^3; ***it key33= P1^4; ***it key22 = P1^5; ***it key11 = P1^6; ***it key00 = P1^7; unsigned char code LedChar[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 }; unsigned char KeySta[4][4] = { {1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1} }; unsigned char LedBuff[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,0xFF,0xFF }; 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键、 回车键、 向右键 }; void KeyDriver(); void main() { EA = 1; //使能总中断 TMOD = 0x01; //设置T0为模式1 TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms TL0 = 0x67; ET0 = 1; //使能T0中断 TR0 = 1; //启动T0 LedBuff[0] = LedChar[0]; while (1) { KeyDriver(); //调用按键驱动函数 } } void ShowNumber(unsigned long num) { signed char i; unsigned char buf[8]; for(i=0; i<8; i++) { buf = num % 10; num = num / 10; } for(i=7; i>=1; i--) { if(buf == 0) { LedBuff = 0x00; } else break; } for(; i>=0; i--) { LedBuff = LedChar[buf]; } } 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] != KeySta[j]) { if(backup[j] == 0) { KeyAction(KeyCodeMap[j]); } backup[j] = KeySta[j]; } } } } /* 按键扫描函数,需在定时中断中调用,推荐调用间隔1ms */ void KeyScan() { static unsigned char KeyValue=0; unsigned char i; static unsigned char keybuf[4][4] = { {0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}, }; keybuf[KeyValue][0] = (keybuf[KeyValue][0] <<1) | key0; keybuf[KeyValue][1] = (keybuf[KeyValue][1] <<1) | key1; keybuf[KeyValue][2] = (keybuf[KeyValue][2] <<1) | key2; keybuf[KeyValue][3] = (keybuf[KeyValue][3] <<1) | key3; for(i=0;i<4;i++) { if((keybuf[KeyValue] & 0x0F) == 0x00) { KeySta[KeyValue] = 0; } else if((keybuf[KeyValue] & 0x0F) == 0x0F) { KeySta[KeyValue] = 1; } } KeyValue++; KeyValue = KeyValue & 0x03; switch(KeyValue) { case 0:key00=0;key33=1;break; case 1:key00=1;key11=0;break; case 2:key11=1;key22=0;break; case 3:key22=1;key33=0;break; default:break; } } /* 数码管动态扫描刷新函数,需在定时中断中调用 */ void LedScan() { static unsigned char i = 0; //动态扫描的索引 P0 = 0x00; //显示消隐 switch (i) { case 0: LSA=0;LSB=0;LSC=0; i++; P0=LedBuff[0]; break; case 1: LSA=1;LSB=0;LSC=0; i++; P0=LedBuff[1]; break; case 2: LSA=0;LSB=1;LSC=0; i++; P0=LedBuff[2]; break; case 3: LSA=1;LSB=1;LSC=0; i++; P0=LedBuff[3]; break; case 4: LSA=0;LSB=0;LSC=1; i++; P0=LedBuff[4]; break; case 5: LSA=1;LSB=0;LSC=1; i++; P0=LedBuff[5]; break; case 6: LSA=0;LSB=1;LSC=1; i++; P0=LedBuff[6]; break; case 7: LSA=1;LSB=1;LSC=1; i=0; P0=LedBuff[7]; break; default: break; } } /* T0中断服务函数,用于数码管显示扫描与按键扫描 */ void InterruptTimer0() interrupt 1 { TH0 = 0xFC; TL0 = 0x67; LedScan(); KeyScan(); } |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
飞凌嵌入式ElfBoard EL 1板卡-i2c与从设备通讯编程示例之i2c-tools工具使用
1226 浏览 0 评论
stc15f2k60s2利用串口传输字模存储到eeprom并进行点阵显示
1453 浏览 1 评论
1457 浏览 0 评论
615 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-串口通讯编程示例之串口编写程序
1283 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11602 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-4 12:12 , Processed in 0.610118 second(s), Total 79, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号