#include<reg51.h>
#define uchar unsigned char;
uchar LED1,LED2,LED3,LED4;
uchar keyval,calflag,s_dat,d_dat;
uchar distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
unsigned int number,r_dat;
void delay(unsigned int a)
{
unsigned int i,j;
for(i=0;i<a;i++)
for(j=0;j<1000;j++);
}
void dealdat()
{
switch(calflag)
{
case 0:
r_dat=s_dat+d_dat;
break;
case 1:
r_dat=s_dat-d_dat;
break;
case 2:
r_dat=s_dat*d_dat;
break;
case 3:
r_dat=s_dat/d_dat;
break;
default:break;
}
LED1=(r_dat/1000)%10;
LED2=(r_dat/100)%10;
LED3=(r_dat/10)%10;
LED4=r_dat%10;
}
uchar kbscan(void)
{
unsigned char sccode,recode;
P3=0x0f; //å0æ«æ,å线è¾å ¥
if ((P3 & 0x0f) != 0x0f) //æé®æä¸
{
delay(20); //延æ¶å»æå¨
if ((P3&0x0f)!= 0x0f)
{
sccode = 0xef; //éè¡æ«æåå¼
while((sccode&0x01)!=0)
{
P3=sccode;
if((P3&0x0f)!=0x0f)
{
recode=(P3&0x0f)|0xf0;
return((~sccode)+(~recode));
}
else
sccode=(sccode<<1)|0x01;
}
}
}
return 0; //æ é®æä¸ï¼è¿å0
}
void getkey(void)
{
unsigned char key;
key=kbscan();
if(key==0){keyval=0xff;return;}
switch(key)
{
case 0x11:keyval=7;break;
case 0x12:keyval=4;break;
case 0x14:keyval=1;break;
case 0x18:keyval=10;break;
case 0x21:keyval=8;break;
case 0x22:keyval=5;break;
case 0x24:keyval=2;break;
case 0x28:keyval=0;break;
case 0x41:keyval=9;break;
case 0x42:keyval=6;break;
case 0x44:keyval=3;break;
case 0x48:keyval=11;break;
case 0x81:keyval=12;break;
case 0x82:keyval=13;break;
case 0x84:keyval=14;break;
case 0x88:keyval=15;break;
default:keyval=0xff;break;
}
}
void t0isr() interrupt 1
{
TH0=0xf4;
TL0=0x48;
switch(number)
{
case 0:P2=0x04;P0=distab[LED1];break;
case 1:P2=0x08;P0=distab[LED2];break;
case 2:P2=0x01;P0=distab[LED3];break;
case 3:P2=0x02;P0=distab[LED4];break;
default:break;
}
number++;
if(number>3)number=0;
}
main()
{
TMOD = 0x01;
number = 0;
TH0=0xf4;
TL0=0x48;
TR0=1;
ET0=1;
EA=1;
LED1=0;
LED2=0;
LED3=0;
LED4=0;
while(1)
{
getkey();
switch(keyval)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
LED3=LED4;
LED4=keyval;
LED1=0;
LED2=0;
break;
case 10:break; //"ON"
case 11: //"="
d_dat=LED3*10+LED4;
dealdat();
break;
case 12: //"/"
calflag=3;
s_dat=LED3*10+LED4;
LED3=0;
LED4=0;
break;
case 13: //"*"
calflag=2;
s_dat=LED3*10+LED4;
LED3=0;
LED4=0;
break;
case 14: //"-"
calflag=1;
s_dat=LED3*10+LED4;
LED3=0;
LED4=0;
break;
case 15: //"+"
calflag=0;
s_dat=LED3*10+LED4;
LED3=0;
LED4=0;
break;
default:break;
}
}
}
追é®
为ä»ä¹æå°±æ¯ç¨è¿æ®µç¨åºè·ä¸èµ·æ¥å¢ ä½ æ¯ç¨çproteus仿ççåï¼ç»æ个é®ç®± æåç»ä½ é£ä¸ªä»¿çå¾ ä½ å¸®æååºæ¥ç©éµé®ç计ç®å¨ï¼è¦æ±å åä¹é¤ç计ç®å¨è¡åï¼ï¼
追çzhxiufan@163.com
追é®åç»ä½ äºå¦ æè°¢äºå
为什么我就是用上面这段程序跑不起来呢 你是用的proteus仿真的吗?给我个邮箱 我发给你那个仿真图 你帮我写出来矩阵键盘计算器(要求加减乘除的计算器行吗?)