单片机c语言编程问题,功能是用定时器T0使发光管间隔200ms闪烁,定时器T1使数码管循环显示0-59.
#include<reg52.h>#defineuintunsignedint#defineucharunsignedcharsbitdula=P2^6;sbitwela...
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit led1=P1^0;
uchar num,num1,num2,shi,ge;
uchar code tabledula[]=
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delayxms(uint xms);
void desplay(uchar shi,uchar ge);
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
while(1)
{
desplay(shi,ge);
}
}
void delayxms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void desplay(uchar shi,uchar ge)
{
dula=1;
P0=tabledula[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayxms(5);
dula=1;
P0=tabledula[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayxms(5);
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num1++;
if(num1==4)
{
num1=0;
led1=~led1;
}
}
void T1_time() interrupt 3
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num2++;
if(num2==20)
{
num2=0;
num++;
if(num==60)
num=0;
shi=num/10;
ge=num%10;
}
}
如上程序所示,在最后的中断函数void T1_time() interrupt 3
中的"num2=0;"这句如果少了的话,竟然也会实现数码管0-59循环显示,只不过时间不是一秒间隔,而是好几秒,谁能告诉我这是为什么?因为少了这句的话从程序上看应该只会显示0然后1,然后就不变了。 展开
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit led1=P1^0;
uchar num,num1,num2,shi,ge;
uchar code tabledula[]=
{
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
void delayxms(uint xms);
void desplay(uchar shi,uchar ge);
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
EA=1;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
while(1)
{
desplay(shi,ge);
}
}
void delayxms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void desplay(uchar shi,uchar ge)
{
dula=1;
P0=tabledula[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayxms(5);
dula=1;
P0=tabledula[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayxms(5);
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num1++;
if(num1==4)
{
num1=0;
led1=~led1;
}
}
void T1_time() interrupt 3
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num2++;
if(num2==20)
{
num2=0;
num++;
if(num==60)
num=0;
shi=num/10;
ge=num%10;
}
}
如上程序所示,在最后的中断函数void T1_time() interrupt 3
中的"num2=0;"这句如果少了的话,竟然也会实现数码管0-59循环显示,只不过时间不是一秒间隔,而是好几秒,谁能告诉我这是为什么?因为少了这句的话从程序上看应该只会显示0然后1,然后就不变了。 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询