我按照郭天祥老师教的编写了数码管倒数的方法,在论坛下载了郭天祥老师TX-1c的proteus原理图进行模拟运行,结果数码管上显示的数字错乱了。
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp,t0,t1,swan,wan,qian,bai,shi,ge,flag,flag1;
sbit dula=P2^6;
sbit wela=P2^7;
uint shu;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x79,0x38,0};
void display(uchar swan,uchar wan,uchar qian,uchar bai,uchar shi,uchar ge);
void delay(uint z);
void init();
void main()
{
init();
while(1)
{
if(flag1 !=1){
display(7,6,5,bai,shi,ge);
}else{
display(16,17,18,18,0,20);
}
}
}
void init()
{
flag=0;
shu=432;
temp=0xfe;
P0=temp;
TMOD=0x11;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
TH1=(65536-50000)/256
TL1=(65536-50000)%256
EA=1
ET1=1
TR1=1
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t0++;
if(flag == 0){
if(t0==10)
{
t0=1;
temp=_crol_(temp,1);
P1=temp;
}
}else{
if(t0%4==0){
P1=~P1;
}
if(t0 ==60){
TR0=0;
P1=0xff;
flag1=1;
}
}
}
void timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
t1++;
if(t1==2)
{
t1=0;
shu--;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
if(shu == 398)
{
TR0=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
t0=0;
flag=1;
P1=0xff;
TR1=0;
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar swan,uchar wan,uchar qian,uchar bai,uchar shi,uchar ge)
{
P0=0xff;//消影
dula=1;
P0=table[swan];
dula=0;
delay(5);
wela=1;
P0=0xfe;
wela=0;
//P0=0x0;
P0=0xff;//消影
dula=1;
P0=table[wan];
dula=0;
delay(5);
wela=1;
P0=0xfd;
wela=0;
//P0=0x0;
P0=0xff;
dula=1;
P0=table[qian];
dula=0;
delay(5);
wela=1;
P0=0xfb;
wela=0;
//P0=0x0;
P0=0xff;
dula=1;
P0=table[bai];
dula=0;
delay(5);
wela=1;
P0=0xf7;
wela=0;
//P0=0x0;
P0=0xff;//消影
dula=1;
P0=table[shi];
dula=0;
delay(5);
wela=1;
P0=0xef;
wela=0;
//P0=0x0;
P0=0xff;
dula=1;
P0=table[ge];
dula=0;
delay(5);
wela=1;
P0=0xdf;
wela=0;
//P0=0x0;
P0=0xff;
}
这是代码
这是运行后截图。