资源简介

c语言编写的基于51单片机的计算器程序(4x4按键);0~9十个数字键,加减乘除四个键,等号按键和清零按键。实现长度最长为6位数的加、减、乘、除,可根据自己的硬件,进行改写,增加计算数字的长度。

资源截图

代码片段和文件信息

#include
#define uchar unsigned char
#define uint unsigned int
uchar code table1[]={
0x3f0x060x5b0x4f
0x660x6d0x7d0x07
0x7f0x6f};
uchar code table2[]={
0x7f0xbf0xdf0xef0xf70xfb0xfd0xfe};
sbit dula=P1^7;
sbit wela=P1^6;
void keyscan();
uchar keykey1temptemp0temp1temp2abcdweigeshibaiqianwanshiwan;
unsigned long int numnum1num11num12num2num3num4num5num6;
void delay(uint z)
{
uint xy;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void deal();
void display();
void init();
void main()
{
init();
while(1)
{
keyscan();
deal();
display();
}
}
void keyscan()
{
P3=0xf0;
temp=P3;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp1=P3;
}  
}
P3=0x0f;
temp=P3;
if(temp!=0x0f)
{
delay(5);
if(temp!=0x0f)
{
temp2=P3;
}
while(temp!=0x0f)
{
temp=P3;
display();
}
temp0=temp1|temp2;
switch(temp0)
{
case 0xee:key=7;b=b+1;break;
case 0xde:key=8;b=b+1;break;
case 0xbe:key=9;b=b+1;break;
case 0x7e:key=‘+‘;break;
case 0xed:key=4;b=b+1;break;
case 0xdd:key=5;b=b+1;break;
case 0xbd:key=6;b=b+1;break;
case 0x7d:key=‘-‘;break;
case 0xeb:key=1;b=b+1;break;
case 0xdb:key=2;b=b+1;break;
case 0xbb:key=3;b=b+1;break;
case 0x7b:key=‘*‘;break;
case 0xe7:key=0;b=b+1;break;
case 0xd7:key=‘=‘;c=c+1;break;
case 0xb7:key=‘.‘;break;
case 0x77:key=‘/‘;break;
}
a=key;
}
}
void display()
{
dula=1;
P0=table1[ge];
dula=0;
P0=0xff;
wela=1;
P0=table2[wei];
wela=0;
P0=0xff;
delay(1);

if(shi!=0||bai!=0||qian!=0||wan!=0||shiwan!=0)
{
dula=1;
P0=table1[shi];
dula=0;
P0=0xff;
wela=1;
P0=table2[wei+1];
wela=0;
P0=0xff;
delay(1);
}

if(bai!=0||qian!=0||wan!=0||shiwan!=0)
{
dula=1;
P0=table1[bai];
dula=0;
P0=0xff;
wela=1;
P0=table2[wei+2];
wela=0;
P0=0xff;
delay(1);
}
if(qian!=0||wan!=0||shiwan!=0)
{
dula=1;
P0=table1[qian];
dula=0;
P0=0xff;
wela=1;
P0=table2[wei+3];
wela=0;
P0=0xff;
delay(1);
}
if(wan!=0||shiwan!=0)
{
dula=1;
P0=table1[wan];
dula=0;
P0=0xff;
wela=1;
P0=table2[wei+4];
wela=0;
P0=0xff;
delay(1);
}
if(shiwan!=0)
{
dula=1;
P0=table1[shiwan];
dula=0;
P0=0xff;
wela=1;
P0=table2[wei+5];
wela=0;
P0=0xff;
delay(1);
}
}

void deal()
{
if(a!=‘+‘  &&  a!=‘-‘  && a!=‘*‘&&a!=‘/‘&&a!=‘=‘&&a!=‘.‘)
{
if(b==1)
{
num1=a;
ge=num1;
}
if(b==2)
{
num2=a;
ge=num2;
shi=num1;
if(c!=0)
{
shi=0;
bai=0;
qian=0;
wan=0;
shiwan=0;
b=1;
c=0;
}
}
if(b==3)
{
num3=a;
ge=num3;
shi=num2;
bai=num

评论

共有 条评论