一个简单的一秒加一显示在数码管上的程序 我有定时器写的但时是显示始终是0000;
时间:10-02
整理:3721RD
点击:
变量好像么有用处。请问问题出现哪里
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void display(uint);
void delay(uint);
void num();
uint count;
void main()
{
TMOD = 0x01;
TH0 = (65535-46080)/256;//50ms
TL0 = (65535-46080)%256;
TR0 = 1;
while(1)
{
num();
display(count);
}
}
void display(uint count)
{
P1 = 0xfe;
P0 = table[count/1000];
delay(5);
P1 = 0xff;
delay(5);
P1 = 0xfd;
P0 = table[count/100%10];
delay(5);
P1 = 0xff;
delay(5);
P1 = 0xfb;
P0 = table[count/10%10];
delay(5);
P0 = 0xff;
delay(5);
P1 = 0xf7;
P0 = table[count%10];
delay(5);
P1 = 0xff;
delay(5);
}
void num()
{
uint i;
if(TF0 == 1)
{
TF0 = 0;
TH0 = (65535-46080)/256;//50ms
TL0 = (65535-46080)%256;
i++;
if(i == 20)
{
i = 0;
count++;
}
}
}
void delay(uint z)
{
uint x,y;
for(x = z;x>0;x--)
for(y = 110;y>0;y--);
}
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void display(uint);
void delay(uint);
void num();
uint count;
void main()
{
TMOD = 0x01;
TH0 = (65535-46080)/256;//50ms
TL0 = (65535-46080)%256;
TR0 = 1;
while(1)
{
num();
display(count);
}
}
void display(uint count)
{
P1 = 0xfe;
P0 = table[count/1000];
delay(5);
P1 = 0xff;
delay(5);
P1 = 0xfd;
P0 = table[count/100%10];
delay(5);
P1 = 0xff;
delay(5);
P1 = 0xfb;
P0 = table[count/10%10];
delay(5);
P0 = 0xff;
delay(5);
P1 = 0xf7;
P0 = table[count%10];
delay(5);
P1 = 0xff;
delay(5);
}
void num()
{
uint i;
if(TF0 == 1)
{
TF0 = 0;
TH0 = (65535-46080)/256;//50ms
TL0 = (65535-46080)%256;
i++;
if(i == 20)
{
i = 0;
count++;
}
}
}
void delay(uint z)
{
uint x,y;
for(x = z;x>0;x--)
for(y = 110;y>0;y--);
}
去我那里看看,我发过帖子,有源代码
定时器驱动代码不完整,少了一些配置
你那个count 值有没有返回回去?
void display(uint count) count你换一个名字 比如 Num ,Num = Count ,试试
void num()
{
uint i;
if(TF0 == 1)
i 前面加 static 或者 改成全局 变量 这样就不会 每次进入 都初始化 为0 后面的根本就加不到20 count也不会加
void main()内TR0=1上面位置写入ET0=1;EA=1;这样才能开启定时器。你没有认真学习定时器的使用方法。
非常感谢您的指导,按照您的方式,程序正常运行。
您好,谢谢你的建议,不过,我没有用中断。