找回密码
 立即注册

QQ登录

只需一步,快速开始

帖子
查看: 4146|回复: 6
收起左侧

51单片机C程序,高手帮忙检查一下那里出了问题,调试很长时间都没检查出来

[复制链接]
ID:51565 发表于 2013-7-15 18:33 | 显示全部楼层 |阅读模式
大家帮忙检查一下那里出了问题,调试很长时间都没检查出来:
以下是源码:
/*---------------------------------------------*/
/*   简单计算器数码管显示,用于两个整数的加减乘除,其中两个数及其结果都不能超过8位数,否则显示“-ERROR-”*/
/*芯片:STC89C52RC  */
/*---------------------------------------------*/
#include<reg52.h>//头包含
#include<intrins.h>//头文件包含,_crol_()函数移位函数,类似C语言中"<<"操作符
#define unit unsigned int//宏定义
#define uchar unsigned char
//硬件:
sbit wela=P2^7;//LED位选
sbit dula=P2^6;//LED段选
uchar stack[8];uchar top;//用于存放待显示数字的栈
long int num1,num2,reasult;
uchar key=255,i,opsignal,error;
uchar code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//数码管数字显示十六进制编码
uchar code errorCode[]={0xbf,0x86,0x88,0x88,0xc0,0x88,0xbf};//"ERROR"英文字母编码
uchar lenth(long int n);//计算数字的位数用于将待显示数存进stack[]中
long int power(uchar n);//幂函数10的n次方
void delayms(unit ms);延时函数
void display(uchar mode);显示函数
uchar keyscan();键盘扫描函数返回按键键值0~15,矩阵按键
//主函数
void main()
{
        while(1)
        {
                key=keyscan();
                if(key<10)
                {
                        stack[top]=key;                       
                        top++;               
                }
                else if(key>11)
            {
                        for(i=0;i<top;i++)num1=num1+stack[i]*power(top-i-1);
                        if(key==12)opsignal=1;
                         else if(key==13)opsignal=2;
                         else if(key==14)opsignal=3;
                         else if(key==15)opsignal=4;
                        top=0;
                        error=0;
                }
                else if(key==10){top=0;error=0;}
                else if(key==11)
                {

                        for(i=0;i<top;i++)num2=num2+stack[i]*power(top-i-1);
                        switch(opsignal)
                                {
                                        case 1:reasult=num1+num2;break;
                                        case 2:reasult=num1-num2;break;
                                        case 3:reasult=num1*num2;break;
                                        case 4:reasult=num1/num2;break;
                                }
                }
                for(i=0;i<top;i++)reasult=reasult+stack[i]*power(top-i-1);
                if(top>8)error=1;
                display(reasult);
               
        }
}
//
uchar lenth(long int n)
{
        uchar i,l;
        for(i=0;i<8;i++)if(n/power(i)==0){l=i;break;}
        return l;
}
//
long int power(uchar n)
{
        uchar i;
        long int p=1;
        for(i=0;i<n;i++)p=p*10;
        return p;

}
//延时函数(毫秒)
void delayms(unit ms)
{
        uchar i,j;
        for(i=0;i<ms;i++)
                for(j=0;j<110;j++);
}
//数码管动态扫描
void display(long int number)//
{

        uchar i,l,aa=0x01;
        l=lenth(number);
        for(i=0;i<l;i++)
        {
                stack[i]=number/power(l-i-1);
                number=number%power(l-i-1);
        }
        if(error==0)
        {
                for(i=0;i<l;i++)       
                {
                        wela=1;
                        P0=aa;
                        wela=0;
                        aa=_crol_(aa,1);
                        dula=1;
                        P0=table[stack[i]];
                        dula=0;
                        P0=0xff;
                        delayms(10);
                }       
        }
        else if(error==1)
        {
                aa=0x01;
                for(i=0;i<7;i++)
                {
                        wela=1;
                        P0=aa;
                        wela=0;
                        aa=_crol_(aa,1);
                        dula=1;
                        P0=errorCode[i];
                        dula=0;
                        P0=0xff;
                        delayms(10);
                }       
        }
}
//扫描函数(I/O)并返回按键
uchar keyscan()
{
        uchar key=255,temp;
        P3=0xfe;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0x7e:key=15;break;//"/"除
                                case 0xbe:key=14;break;//"*"乘
                                case 0xde:key=13;break;//"-"减
                                case 0xee:key=12;break;//"+"加
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xfd;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xed:key=11;break;//"="
                                case 0xdd:key=10;break;//"CLR"
                                case 0xbd:key=9;break;
                                case 0x7d:key=8;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xfb;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xeb:key=7;break;
                                case 0xdb:key=6;break;
                                case 0xbb:key=5;break;
                                case 0x7b:key=4;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xf7;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xe7:key=3;break;
                                case 0xd7:key=2;break;
                                case 0xb7:key=1;break;
                                case 0x77:key=0;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
return key;
}



回复

举报

ID:7485 发表于 2013-7-16 07:40 | 显示全部楼层
什么问题?
针对性的找问题会快捷些。如果盲目的找,整个程序分析一遍,睡会有这功夫和耐心?
回复

举报

ID:51565 发表于 2013-7-16 11:26 | 显示全部楼层
ahshmj 发表于 2013-7-16 07:40
什么问题?
针对性的找问题会快捷些。如果盲目的找,整个程序分析一遍,睡会有这功夫和耐心?

我检查了很多遍,程序的过程貌似没问题,函数都没问题,是不是出现了数组溢出,或是软件驱动硬件时方式不当。麻烦帮忙仔细查看主函数。谢谢!
回复

举报

ID:7485 发表于 2013-7-16 19:13 | 显示全部楼层
你到底感觉哪儿不对?直观的叙述。
回复

举报

ID:7485 发表于 2013-7-16 19:35 | 显示全部楼层
大家帮忙检查一下那里出了问题,调试很长时间都没检查出来:
以下是源码:
/*---------------------------------------------*/
/*   简单计算器数码管显示,用于两个整数的加减乘除,其中两个数及其结果都不能超过8位数,否则显示“-ERROR-”*/
/*芯片:STC89C52RC  */
/*---------------------------------------------*/
#include<reg52.h>//头包含
#include<intrins.h>//头文件包含,_crol_()函数移位函数,类似C语言中"<<"操作符
#define unit unsigned int//宏定义
#define uchar unsigned char
//硬件:
sbit wela=P2^7;//LED位选
sbit dula=P2^6;//LED段选
uchar stack[8];uchar top//用于存放待显示数字的栈    红色的分号改成半角字符(西文标点)
long int num1,num2,reasult;
uchar key=255,i,opsignal,error;
uchar code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//数码管数字显示十六进制编码
uchar code errorCode[]={0xbf,0x86,0x88,0x88,0xc0,0x88,0xbf};//"ERROR"英文字母编码
uchar lenth(long int n);//计算数字的位数用于将待显示数存进stack[]中
long int power(uchar n);//幂函数10的n次方
void delayms(unit ms);延时函数                                  //加上注释符号,下面2行同
void display(uchar mode);显示函数
uchar keyscan();键盘扫描函数返回按键键值0~15,矩阵按键
//主函数
void main()
{
        while(1)
        {
                key=keyscan();
                if(key<10)
                {
                        stack[top]=key;                       
                        top++;               
                }
                else if(key>11)
            {
                        for(i=0;i<top;i++)num1=num1+stack*power(top-i-1);
                        if(key==12)opsignal=1;
                         else if(key==13)opsignal=2;
                         else if(key==14)opsignal=3;
                         else if(key==15)opsignal=4;
                        top=0;
                        error=0;
                }
                else if(key==10){top=0;error=0;}
                else if(key==11)
                {

                        for(i=0;i<top;i++)num2=num2+stack*power(top-i-1);
                        switch(opsignal)
                                {
                                        case 1:reasult=num1+num2;break;
                                        case 2:reasult=num1-num2;break;
                                        case 3:reasult=num1*num2;break;
                                        case 4:reasult=num1/num2;break;
                                }
                }
                for(i=0;i<top;i++)reasult=reasult+stack*power(top-i-1);
                if(top>8)error=1;
                display(reasult);
               
        }
}
//
uchar lenth(long int n)
{
        uchar i,l;
        for(i=0;i<8;i++)if(n/power(i)==0){l=i;break;}
        return l;
}
//
long int power(uchar n)
{
        uchar i;
        long int p=1;
        for(i=0;i<n;i++)p=p*10;
        return p;

}
//延时函数(毫秒)
void delayms(unit ms)
{
        uchar i,j;
        for(i=0;i<ms;i++)
                for(j=0;j<110;j++);
}
//数码管动态扫描
void display(long int number)//  这里的参数类型和前面声明中的不一致
{

        uchar i,l,aa=0x01;
        l=lenth(number);
        for(i=0;i<l;i++)
        {
                stack=number/power(l-i-1);
                number=number%power(l-i-1);
        }
        if(error==0)
        {
                for(i=0;i<l;i++)       
                {
                        wela=1;
                        P0=aa;
                        wela=0;
                        aa=_crol_(aa,1);
                        dula=1;
                        P0=table[stack];
                        dula=0;
                        P0=0xff;
                        delayms(10);
                }       
        }
        else if(error==1)
        {
                aa=0x01;
                for(i=0;i<7;i++)
                {
                        wela=1;
                        P0=aa;
                        wela=0;
                        aa=_crol_(aa,1);
                        dula=1;
                        P0=errorCode;
                        dula=0;
                        P0=0xff;
                        delayms(10);
                }       
        }
}
//扫描函数(I/O)并返回按键
uchar keyscan()
{
        uchar key=255,temp;
        P3=0xfe;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0x7e:key=15;break;//"/"除
                                case 0xbe:key=14;break;//"*"乘
                                case 0xde:key=13;break;//"-"减
                                case 0xee:key=12;break;//"+"加
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xfd;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xed:key=11;break;//"="
                                case 0xdd:key=10;break;//"CLR"
                                case 0xbd:key=9;break;
                                case 0x7d:key=8;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xfb;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xeb:key=7;break;
                                case 0xdb:key=6;break;
                                case 0xbb:key=5;break;
                                case 0x7b:key=4;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xf7;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xe7:key=3;break;
                                case 0xd7:key=2;break;
                                case 0xb7:key=1;break;
                                case 0x77:key=0;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
return key;
}


改过后应如下:     (void display(uchar number 参数类型的问题自己斟酌这样是否合适)
/*   简单计算器数码管显示,用于两个整数的加减乘除,其中两个数及其结果都不能超过8位数,否则显示"-ERROR-"*/
/*芯片:STC89C52RC  */
/*---------------------------------------------*/
#include<reg52.h>//头包含
#include<intrins.h>//头文件包含,_crol_()函数移位函数,类似C语言中"<<"操作符
#define unit unsigned int//宏定义
#define uchar unsigned char
//硬件:
sbit wela=P2^7;//LED位选
sbit dula=P2^6;//LED段选
uchar stack[8];
uchar top;//用于存放待显示数字的栈
long int num1,num2,reasult;
uchar key=255,i,opsignal,error;
uchar code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//数码管数字显示十六进制编码
uchar code errorCode[]={0xbf,0x86,0x88,0x88,0xc0,0x88,0xbf};//"ERROR"英文字母编码
uchar lenth(long int n);//计算数字的位数用于将待显示数存进stack[]中
long int power(uchar n);//幂函数10的n次方
void delayms(unit ms);//延时函数
void display(uchar number);//显示函数
uchar keyscan();//键盘扫描函数返回按键键值0~15,矩阵按键
//主函数
void main()
{
        while(1)
        {
                key=keyscan();
                if(key<10)
                {
                        stack[top]=key;                        
                        top++;               
                }
                else if(key>11)
            {
                        for(i=0;i<top;i++)num1=num1+stack*power(top-i-1);
                        if(key==12)opsignal=1;
                         else if(key==13)opsignal=2;
                         else if(key==14)opsignal=3;
                         else if(key==15)opsignal=4;
                        top=0;
                        error=0;
                }
                else if(key==10){top=0;error=0;}
                else if(key==11)
                {

                        for(i=0;i<top;i++)num2=num2+stack*power(top-i-1);
                        switch(opsignal)
                                {
                                        case 1:reasult=num1+num2;break;
                                        case 2:reasult=num1-num2;break;
                                        case 3:reasult=num1*num2;break;
                                        case 4:reasult=num1/num2;break;
                                }
                }
                for(i=0;i<top;i++)reasult=reasult+stack*power(top-i-1);
                if(top>8)error=1;
                display(reasult);
               
        }
}
//
uchar lenth(long int n)
{
        uchar i,l;
        for(i=0;i<8;i++)if(n/power(i)==0){l=i;break;}
        return l;
}
//
long int power(uchar n)
{
        uchar i;
        long int p=1;
        for(i=0;i<n;i++)p=p*10;
        return p;

}
//延时函数(毫秒)
void delayms(unit ms)
{
        uchar i,j;
        for(i=0;i<ms;i++)
                for(j=0;j<110;j++);
}
//数码管动态扫描
//void display(long int number)//
void display(uchar number)
{

        uchar i,l,aa=0x01;
        l=lenth(number);
        for(i=0;i<l;i++)
        {
                stack=number/power(l-i-1);
                number=number%power(l-i-1);
        }
        if(error==0)
        {
                for(i=0;i<l;i++)        
                {
                        wela=1;
                        P0=aa;
                        wela=0;
                        aa=_crol_(aa,1);
                        dula=1;
                        P0=table[stack];
                        dula=0;
                        P0=0xff;
                        delayms(10);
                }        
        }
        else if(error==1)
        {
                aa=0x01;
                for(i=0;i<7;i++)
                {
                        wela=1;
                        P0=aa;
                        wela=0;
                        aa=_crol_(aa,1);
                        dula=1;
                        P0=errorCode;
                        dula=0;
                        P0=0xff;
                        delayms(10);
                }        
        }
}
//扫描函数(I/O)并返回按键
uchar keyscan()
{
        uchar key=255,temp;
        P3=0xfe;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0x7e:key=15;break;//"/"除
                                case 0xbe:key=14;break;//"*"乘
                                case 0xde:key=13;break;//"-"减
                                case 0xee:key=12;break;//"+"加
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xfd;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xed:key=11;break;//"="
                                case 0xdd:key=10;break;//"CLR"
                                case 0xbd:key=9;break;
                                case 0x7d:key=8;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xfb;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xeb:key=7;break;
                                case 0xdb:key=6;break;
                                case 0xbb:key=5;break;
                                case 0x7b:key=4;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
        P3=0xf7;
        temp=P3;
        temp=temp&0xf0;
        if(temp!=0xf0)
        {
                delayms(10);
                temp=P3;
                temp=temp&0xf0;               
                if(temp!=0xf0)
                {
                        temp=P3;
                        switch(temp)
                        {
                                case 0xe7:key=3;break;
                                case 0xd7:key=2;break;
                                case 0xb7:key=1;break;
                                case 0x77:key=0;break;
                        }               
                        while(temp!=0xf0)
                        {
                                temp=P3;
                                temp=temp&0xf0;
                        }
                }
        }
return key;
}
回复

举报

ID:51565 发表于 2013-7-21 12:07 来自触屏版 | 显示全部楼层
首先谢谢你有耐心地把这么乱的代码看完,谢谢。但是问题仍然没有解决,可能是我没有把问题表述清楚。问题是这样的:程序能通过编译并运行,所以你所指出来的错误应该都是发表贴子的时候不小心的错误。问题是在keil中完成编译并生成hex文件下载到单片机后,我按下按键输入第一个数,没问题,可按下加号的时候数码管却现显示上个数字的最后一个数字,而且再接着按其他按键的时候仍然显示这个数字,当我按下等号的时候竟然有时显示一串奇怪的数字有时直接显示error。郁闷啊,检查好长时间了都没有头绪。我是一名高中毕业生,暑假在家自学单片机,还刚刚起步,想好好的学好在大学里能够学得更深,同时学习单片机也是我个人的爱好。本想着要自己开发出一个简单的计算器,没想到却碰到这个麻烦。唉,杯具啊,想也是基础不扎实造成的。想请高手帮忙解决,拜托了啊!
回复

举报

ID:51894 发表于 2013-7-21 15:02 | 显示全部楼层
根据楼主你的问题描述应该是位选和段选的关系没有处理好 你再看看动态数码管的一些资料应该对你有帮助
回复

举报

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

本版积分规则

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

Powered by 单片机教程网

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