理强 2015-12-18 09:35 采纳率: 40%
浏览 1598
已结题

单片机简单的计算器功能

 #include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp,temp2,aa,bai,shi,ge,num,num1,last,last2,fuhao,ff;
sbit dula=P2^6;
sbit wela=P2^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z);
void display(uchar bai, uchar shi, uchar ge);
void init();
uchar keyscan();


void main()
{
num=0;
while(num==0)
{
num=keyscan();
bai=num;
display(bai,0,0);
}
num=0;
while(num==0)
{keyscan();
shi=keyscan();
display(bai,shi,0);
num=0;}
while(num==0)
{ge=keyscan();
display(bai,shi,ge);
num=0;}
last=100*bai+10*shi+ge;

while(num==0){
fuhao=keyscan();
switch(num)  
            {  
            case 10:fuhao=10  ;  break;  
            case 11:fuhao=11 ;  break;  
            case 12:fuhao=12  ;  break;  
            case 13:fuhao=13  ;  break;  
            } 
num=0;
            } 

while(num==0)
{keyscan();
bai=keyscan();
display(bai,0,0);
num=0;}
while(num==0)
{keyscan();
shi=keyscan();
display(bai,shi,0);
num=0;}
while(num==0)
{ge=keyscan();
display(bai,shi,ge);
num=0;}
last2=100*bai+10*shi+ge;

delay(10000);

while(num==0)
{
ff=keyscan();
switch(fuhao)  
            {  
            case 10:ff=last+last2  ;  break;  
            case 11:ff=last-last2 ;  break;  
            case 12:ff=last*last2 ;  break;  
            case 13:ff=last/last2  ;  break;  
            }  
}


bai=ff/100;
shi=ff%100/10;
ge=ff%10;
while(1)
    {
    display(bai,shi,ge);
    }   

}



uchar keyscan()
{
            P3=0xfe;
            temp2=P3;
            temp2=temp2&0xf0;
            while(temp2!=0xf0)
                {
                    delay(5);
                    temp2=P3;
                    temp2=temp2&0xf0;
                    while(temp2!=0xf0)
                    {
                        temp2=P3;
                    switch(temp2)
                        {
                            case 0xee:num=1;
                                break;
                            case 0xde:num=2;
                                break;
                            case 0xbe:num=3;
                                break;
                            case 0x7e:num=4;
                                break;
                        }
                    while(temp2!=0xf0)
                        {
                            temp2=P3;
                            temp2=temp2&0xf0;
                        }
                    }
                }

            P3=0xfd;
            temp2=P3;
            temp2=temp2&0xf0;
            while(temp2!=0xf0)
                {
                    delay(5);
                    temp2=P3;
                    temp2=temp2&0xf0;
                    while(temp2!=0xf0)
                    {
                        temp2=P3;
                    switch(temp2)
                        {
                            case 0xed:num=5;
                                break;
                            case 0xdd:num=6;
                                break;
                            case 0xbd:num=7;
                                break;
                            case 0x7d:num=8;
                                break;
                        }
                    while(temp2!=0xf0)
                        {
                            temp2=P3;
                            temp2=temp2&0xf0;
                        }
                    }
                }


            P3=0xfb;
            temp2=P3;
            temp2=temp2&0xf0;
            while(temp2!=0xf0)
                {
                    delay(5);
                    temp2=P3;
                    temp2=temp2&0xf0;
                    while(temp2!=0xf0)
                    {
                        temp2=P3;
                    switch(temp2)
                        {
                            case 0xeb:num=9;
                                break;
                            case 0xdb:num=10;
                                break;
                            case 0xbb:num=11;
                                break;
                            case 0x7b:num=12;
                                break;
                        }
                    while(temp2!=0xf0)
                        {
                            temp2=P3;
                            temp2=temp2&0xf0;
                        }
                    }
                }


            P3=0xf7;
            temp2=P3;
            temp2=temp2&0xf0;
            while(temp2!=0xf0)
                {
                    delay(5);
                    temp2=P3;
                    temp2=temp2&0xf0;
                    while(temp2!=0xf0)
                    {
                        temp2=P3;
                    switch(temp2)
                        {
                            case 0xe7:num=13;
                                break;
                            case 0xd7:num=14;
                                break;
                            case 0xb7:num=15;
                                break;
                            case 0x77:num=16;
                                break;
                        }
                    while(temp2!=0xf0)
                        {
                            temp2=P3;
                            temp2=temp2&0xf0;
                        }
                    }
                }
return num;

}







void display(uchar bai,uchar shi,uchar ge)
{
    dula=1;
    P0=table[bai];
    dula=0;
    P0=0xff; //消影

    wela=1;
    P0=0xfe;
    wela=0;
    delay(5);

    dula=1;
    P0=table[shi];
    dula=0;
    P0=0xff;

    wela=1;
    P0=0xfd;
    wela=0;
    delay(5);

    dula=1;
    P0=table[ge];
    dula=0;
    P0=0xff;

    wela=1;
    P0=0xfb;
    wela=0;
    delay(5);



}





void delay(uint z)
{
    uint x,y;
    for(x=z;x>0;x--)
        for(y=110;y>0;y--);
}

void init()
{
    wela=0;
    dula=0;
    temp=0;
    TMOD=0x01;
    TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
    EA=1;
    ET0=1;
    TR0=1;

}

void timer0() interrupt 1
{

    TMOD=0x01;
    TH0=(65536-50000)/256;
    TL0=(65536-50000)%256;
    aa++;
}
  • 写回答

3条回答 默认 最新

  • 纹枰老妖 2015-12-18 09:49
    关注

    题主,恕我愚昧。我耐心的看完了您的代码,还是不明白您要问什么。。。

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line