【文件属性】:
文件名称:基于51的按键LED显示
文件大小:102KB
文件格式:DSN
更新时间:2016-08-24 03:18:50
51单片机
#include
#include
#define unchar unsigned char
#define key_press 2
#define key_ok 1
#define key_nopress 0
unchar res_col;
unchar res_row;
unchar input;
unchar keystate;
unchar keynum;
unchar scanrdy, clr,m, l;
unchar datas[4];
sbit test=P2^7 ; //
sbit tf=P3^3; //
sbit sav_clk=P3^2;
unchar led_cathode;
void scan_keyboard(){ //10ms执行一次扫描
input= 0xff;//输入键值初始值为ff;
P1=0x70;//p1.0-p1.6键盘线 ,设置行输出0,
res_col=P1&0x70; //比较结果为0则为无按键按下
if(res_col!=0x70){ //如果有列线电平被拉低
P1=0x0f;//设置列输出0
res_row=P1&0x0f;
if(res_row!=0x0f) //如果有行线电平被拉低
input= res_row|res_col; //有效输入为行列相或
}
switch (keystate){ //进入状态机!!!!!!!!!!!!
case key_press :
if(input==0xff)keystate=key_nopress;
else {
switch (input){ //按键成功后的输出处理过程!
case 0x6e: keynum=0x06;break; //(0,0)键值为1
case 0x5e: keynum=0x5b;break; //(0,1)键值为2
case 0x3e: keynum=0x4f;break; //(0,2)键值为3
case 0x6d: keynum=0x66;break; //(1,0)键值为4
case 0x5d: keynum=0x6d;break; //(1,1)键值为5
case 0x3d: keynum=0x7d;break; //(1,2)键值为6
case 0x6b: keynum=0x07;break; //(2,0)键值为7
case 0x5b: keynum=0x7f;break; //(2,1)键值为8
case 0x3b: keynum=0x6f;break;//(2,2)键值为9
case 0x67: keynum=0x3f;break; //(3,0)键值为0
case 0x57: clr=1; break; //(3,1)键为清0复位
/*
switch(input){
case 0x6e: keynum=0x3f;break; //(0,0)键值为a
case 0x12: keynum=0x4f;break; //(0,1)键值为b
case 0x13: keynum=0x5f;break;
case 0x14: keynum=3;break;
case 0xe1: keynum=4;break;
case 0xe2: keynum=5;break;
case 0xe3: keynum=6;break;
case 0xe4: keynum=7;break;
case 0xe5: keynum=8;break;
case 0xe6: keynum=9;break;
case 0xe7: keynum=0;break;
}
//default :keynum=0x00;
*/
}
if(l!=4){
datas[l]=keynum;
l++;
if(l-1==1){
m=datas[1];
datas[1]=datas[0];
datas[0]=m;
}
if(l-1==2){
m=datas[2];
datas[2]=datas[1];
datas[1]=m;
m=datas[0];
datas[0]=datas[1];
datas[1]=m;
}
if(l-1==3){
m=datas[2];
datas[2]=datas[3];
datas[3]=m;
m=datas[2];
datas[2]=datas[1];
datas[1]=m;
m=datas[0];
datas[0]=datas[1];
datas[1]=m;
}
}
keystate=key_ok;
}
break;
case key_ok :
if(input==0xff)keystate=key_nopress;
break;
case key_nopress :
if(input!=0xff)keystate=key_press;
break;
}
scanrdy=0;
}
void delay(){
unchar i=254;
for(;i>0;i--);
}
void display(){ //显示子程序!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
unchar t,n;
led_cathode=0xfe;
if(clr==1) { //清0键处理
clr=0;
l=0;
for(n=0;n<4;n++){
datas[n]=0x00;
}
datas[0]=0x3f;
}
for(t=0;t<=l;t++){
SBUF=datas[t] ;
while(TI==1);
P0=led_cathode;
led_cathode=led_cathode<<1|0x01;
delay();
}
}
void init_function(){
EA=1;
ET0 =1;
TMOD=0x01;
TH0=0xd8;
TL0=0xf0;//赋予定时器初值th0,tl0,10ms延时
IT0 =1; //边沿触发模式
TR0 =1;
SCON=0x00;//设置串行通信方式0;且发送完毕中断ti也置为0;
ES=1;
keystate=0;
clr=0;
l=0;
datas[0]=0x3f;
}
main(){
init_function();
while(1){
while(scanrdy==0) ;
scan_keyboard();
display();
}
}
void scan_begin(void) interrupt 1 {
scanrdy=1;
TH0=0xd8;
TL0=0xf0;
}
void sendover(void) interrupt 4 {
TI=0;
sav_clk=0;
_nop_();
sav_clk=1;
}