完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
#include
typedef unsigned char uchar; typedef unsigned int uint; #define dula_h PORTD|=0x01 #define dula_l PORTD&=0xfe #define wela_h PORTD|=0x02 #define wela_l PORTD&=0xfd #define keyport PORTB #define keypin (PINB) #define keydre DDRB #define keystate0 0 //按键确认状态 #define keystate1 1 //按键无动作状态 char keystate=1; //按键状态初始化为按键无动作状态 uchar keynum=0; void delayms(uint z) //1ms { uint x,y; for(x=z;x>0;x--) for(y=157;y>0;y--); } uchar table[]={ //共阳数码管 0xc0,0xf9,0xa4,0xb0, 0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e}; void smg(void) { dula_l; wela_l; PORTC=0x01; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x02; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x04; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x08; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x10; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x20; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x40; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); PORTC=0x80; wela_h; wela_l; PORTC=table[keynum]; dula_h; dula_l; delayms(1); } void keyscan(void) { uchar temp1,temp2,column,row; switch(keystate) { case keystate0: //keystate0,按键按下确认状态,下一状态为keystate1(按键无动作状态) keydre=0xff; keyport=0x0f; keydre=0xf0; temp1=keypin; keydre=0xff; keyport=0xf0; keydre=0x0f; temp2=keypin; if((temp1|temp2)!=0xff) { temp1|=temp2; if(!(temp1&0x80)) row=3; else if(!(temp1&0x40)) row=2; else if(!(temp1&0x20)) row=1; else if(!(temp1&0x10)) row=0; if(!(temp1&0x08)) column=3; else if(!(temp1&0x04)) column=2; else if(!(temp1&0x02)) column=1; else if(!(temp1&0x01)) column=0; keynum=column*4+row; while(!(keyport==0x0f|keyport==0xf0)); keystate=keystate1; break; } else keystate=keystate1; break; case keystate1: //keystate1,按键无动作状态,下一状态为keystate0(按键无动作状态) keydre=0xff; keyport=0x0f; //输出0x0f keydre=0xf0; temp1=keypin; keydre=0xff; keyport=0xf0; //输出0xf0 keydre=0x0f; temp2=keypin; if((temp1|temp2)!=0xff) { keystate=keystate0; } else keystate=keystate1; break; } } void main(void) { DDRC=0xff; DDRD|=0x03; while(1) { keyscan(); smg(); } } |
|
|
|
只有小组成员才能发言,加入小组>>
2497 浏览 0 评论
1062浏览 2评论
681浏览 1评论
443浏览 0评论
177浏览 0评论
295浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-11 00:07 , Processed in 1.187955 second(s), Total 80, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号