求大神帮写一液晶+STC89c52单片机+4*4矩阵键盘实现简易计算器的C语言程序,实现加减乘除以及扩展功能

2024-11-19 22:38:10
推荐回答(2个)
回答(1):

没有液晶的,以前做了一个有一个数码管的,发给你供参考。

#include
#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 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;
}
}
}

回答(2):

用 PROTEUS 软件仿真的: