数码管显示异常
时间:10-02
整理:3721RD
点击:
请分析下,该程序为何显示异常?
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //common conthod
void DelayMS(uint ms)
{
uchar t;
while(ms--)
for(t=0;t<120;t++);
}
display(unsigned int num)
{
if(num<10)
{
P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xdf;
DelayMS(5);
P2=0XFF;
}
else if(10<num<100)
{
P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100/10];
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xdf;
DelayMS(5);
P2=0XFF;
}
else if (100<num<1000)
{P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100/10];
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%1000/100];
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xdf;
DelayMS(5);
P2=0XFF;}
else
{P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100/10];
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%1000/100];
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%10000/1000];
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100000/10000];
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num/100000];
P2=0Xdf;
DelayMS(5);
P2=0XFF;
}
}
void main()
{ unsigned char num=0;
while(1)
{
if(P3^4==0)
{
display(num);
if(P3^4==0)
{
num=num+2;
display(num);
}
}
display(num);
}
}
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //common conthod
void DelayMS(uint ms)
{
uchar t;
while(ms--)
for(t=0;t<120;t++);
}
display(unsigned int num)
{
if(num<10)
{
P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xdf;
DelayMS(5);
P2=0XFF;
}
else if(10<num<100)
{
P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100/10];
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xdf;
DelayMS(5);
P2=0XFF;
}
else if (100<num<1000)
{P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100/10];
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%1000/100];
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=0x00;
P2=0Xdf;
DelayMS(5);
P2=0XFF;}
else
{P2=0XFF;
P0=DSY_CODE[num%10];
P2=0XFE;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100/10];
P2=0XFd;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%1000/100];
P2=0XFb;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%10000/1000];
P2=0XF7;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num%100000/10000];
P2=0Xef;
DelayMS(5);
P2=0XFF;
P0=DSY_CODE[num/100000];
P2=0Xdf;
DelayMS(5);
P2=0XFF;
}
}
void main()
{ unsigned char num=0;
while(1)
{
if(P3^4==0)
{
display(num);
if(P3^4==0)
{
num=num+2;
display(num);
}
}
display(num);
}
}
查看一下硬件 看看是否有问题
如何异常啊?是一样都不显示,还是乱码,还是全亮啊
不错的文件,值得收藏