找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1325|回复: 0
打印 上一主题 下一主题
收起左侧

基于51的简易计算器

[复制链接]
跳转到指定楼层
楼主
ID:355780 发表于 2018-6-21 10:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<reg52.h>
#define  uchar unsigned char
#define  ulong  unsigned long

//uchar code num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40};                        //共阴极
               //   0    1           2    3         4    5    6    7    8     9   熄灭  -
//uchar code loc[]={0xff,0xfe,0xfd,0xfb,0xf7};        
//uchar code ero[]={0x79,0x50,0x5c};

uchar code num[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x40};                        //共阳极
uchar code loc[]={~0x00,~0x10,~0x20,~0x40,~0x80,~0x08,~0x04,~0x02,~0x01};        
uchar code ero[]={~0x79,~0x77,~0x3f};

ulong  n=0,n1=0,n2=0;           //赋初值                                                                                                                        

uchar flag=0;               //计算类型选择关键字

void delay(int t);
void display(int n);
void error();

main()
{

        while(1)
        {        
                uchar temp;
            //第一行检测
            P1=0xfe;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(10);
                        temp=P1;
                        temp=temp&0xfe;
                        if(temp!=0xfe)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xee:n1=0;n2=0;n=0;flag=0;break;                                //清零
                                        case 0xde:n1=10*n1+0;n=n1;break;                                                             //0
                                        case 0xbe: if(flag==1)n=n2+n1;                                        //=
                                                          if(flag==2)n=n2-n1;
                                                          if(flag==3)n=n2*n1;
                                                          if(flag==4)n=n2/n1;
                                                            n1=0;                        
                                                      break;
                                        case 0x7e:                                                            // +
                                             n2=n1;n1=0;
                                                     flag=1;break;
                                }
                                while(temp!=0xf0)
                                {
                                                temp=P1;
                                                temp=temp&0xf0;

                            }        

                        }


                }
        //扫描第二行
                P1=0xfd;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(10);
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xed:n1=10*n1+1;n=n1;break;                                //4
                                        case 0xdd:n1=10*n1+2;n=n1;break;                        //5
                                        case 0xbd:n1=10*n1+3;n=n1;break;                                //6
                                        case 0x7d:                                      // -        
                                                          n2=n1;n1=0;
                                                          flag=2;break;

                                }
                                while(temp!=0xf0)
                                {
                                temp=P1;
                                temp=temp&0xf0;
                                }               

                        }


                }
        //扫描第三行
          P1=0xfb;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(10);
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xeb:n1=10*n1+4;n=n1;break;
                                        case 0xdb:n1=10*n1+5;n=n1;break;
                                        case 0xbb:n1=10*n1+6;n=n1;break;
                                        case 0x7b:                                                     // *                                                                                
                                                      n2=n1;n1=0;
                                                  flag=3;break;
                                }               
                                while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }        
                        }
                }
        //扫描第四行
                P1=0xf7;
                temp=P1;
                temp=temp&0xf0;
                if(temp!=0xf0)
                {
                        delay(10);
                        temp=P1;
                        temp=temp&0xf0;
                        if(temp!=0xf0)
                        {
                                temp=P1;
                                switch(temp)
                                {
                                        case 0xe7:n1=10*n1+7;n=n1;break;                           //7
                                        case 0xd7:n1=10*n1+8;n=n1;break;                           //8
                                        case 0xb7:n1=10*n1+9;n=n1;break;                           //9

                                        case 0x77:                                    // /        
                                                      n2=n1;n1=0;
                                                  flag=4;break;                                                        

                                }
                                while(temp!=0xf0)
                                {
                                        temp=P1;
                                        temp=temp&0xf0;
                                }                        
                    }
            }
                display(n);
        }
}


//延时函数
void delay(int t)
{
        int x,y;
        for(x=0;x<t;x++)
                for(y=0;y<t;y++);        
}



//数码管显示
void display(int n)
{
                //溢出处理
                uchar g,s,b,q,a,c;
                int abs;
                if((n>999999)||(n<-99999))
                error();

                //正数
                if((n>=0)&&(n<=999999))               
                {
                                g=n%10;
                                s=n/10%10;
                                b=n/100%10;
                                q=n/1000%10;
                                a=n/10000%10;
                                c=n/100000%10;
                                P0=num[g];
                                delay(5);            
                                P2=loc[4];     
                                delay(2);      
                                P2=loc[0];
                                delay(3);
                                if(n>=10)        
                                {
                                        P0=num[s];
                                        P2=loc[3];
                                        delay(2);
                                        P2=loc[0];
                                        delay(3);
                                }                                            

                                if(n>=100)        
                                {
                                        P0=num[b];
                                        P2=loc[2];
                                        delay(2);
                                        P2=loc[0];
                                        delay(3);
                                }

                                if(n>=1000)        
                                {        
                                        P0=num[q];
                                        P2=loc[1];
                                        delay(2);
                                        P2=loc[0];
                                        delay(3);
                                }
                                        if(n>=10000)        
                                {
                                        P0=num[a];
                                        P2=loc[5];
                                        delay(2);
                                        P2=loc[0];
                                        delay(3);
                                }
                                if(n>=100000)        
                                {
                                        P0=num[c];
                                        P2=loc[6];
                                        delay(2);
                                        P2=loc[0];
                                        delay(3);
                                }
                }

                //负数
                if((n<0)&&(n>=-999))
                {
                        abs=-n;
                        g=abs%10;
                        s=abs/10%10;
                        b=abs/100%10;
                        q=abs/1000%10;
                        P0=num[g];        
                        P2=loc[4];
                        delay(2);
                        P2=loc[0];
                        delay(2);
                        if((abs/10%10>0)||(abs/100%10>0))        
                                {
                                        P0=num[s];
                                        P2=loc[3];;
                                        delay(2);
                                        P2=loc[0];
                                        delay(2);
                                if((abs/100%10>0))        
                                        {
                                                P0=num[b];
                                                P2=loc[2];
                                                delay(2);
                                                P2=loc[0];
                                                delay(2);
                                                if((abs/1000%10>0))        
                                                        {        
                                                                P0=num[q];
                                                                P2=loc[1];
                                                                delay(2);
                                                                P2=loc[0];
                                                                delay(2);

                                                        }
                                                else
                                                        {
                                                                P0=num[11];
                                                                P2=loc[1];
                                                                delay(2);
                                                                P2=loc[0];
                                                                delay(2);
                                                        }
                                        }
                                else
                                        {
                                                P0=num[11];
                                                P2=loc[2];
                                                delay(2);
                                                P2=loc[0];
                                                delay(2);
                                        }

                                }
                        else
                                {
                                        P0=num[11];
                                        P2=loc[3];
                                        delay(2);
                                        P2=loc[0];
                                        delay(2);
                                }

                }

}

//溢出显示
void error()
{
                P2=loc[5];
                P0=ero[0];
                delay(2);
                P2=loc[0];
                delay(3);

                P2=loc[1];
                P0=ero[1];
                delay(2);
                P2=loc[0];
                delay(3);

                P2=loc[2];
                P0=ero[1];
                delay(2);
                P2=loc[0];
                delay(3);

                P2=loc[3];
                P0=ero[2];
                delay(2);
                P2=loc[0];
                delay(3);

                P2=loc[4];
                P0=ero[1];
                delay(2);
                P2=loc[0];
                delay(3);

}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表