如何使AT89C52单片机的两个定时器同时工作?
#include<AT89X52.h>
#define uchar unsigned char
#define uint unsigned int
sbit pwm = P2^7;
sbit LED=P1^1;
uint t,i,j,t1,t2,num;
uchar flag1,flag2;
void delay(uint z)
{
for(i=z;i>0;i--)
for(j=112;j>0;j--);
}
void main()
{
TMOD = 0x11;
TH0 = (65536-1024)/256;
TL0 = (65536-1024)%256;
TH1 = (65536-512)/256;
TL1 = (65536-512)%256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
pwm = 1;
LED=1;
while(1)
{
flag1=1;
delay(1000);
flag1=0;
delay(1000);
flag2=1;
delay(1000);
flag2=0;
delay(1000);
}
}
void timer0() interrupt 1 using 0
{
TH0=(65536-1024)/256;
TL0=(65536-1024)%256;
LED=~LED;
}
void timer1() interrupt 3 using 1
{
TH1 = (65536-512)/256;
TL1 = (65536-512)%256;
if(flag1==1)
{
t++;
if(t == 2)
{
pwm = 0;
}
if(t == 200 )
{
pwm = 1;
t = 0;
}
}
if(flag2==1)
{
t1++;
if(t1 == 3)
{
pwm = 0;
}
if(t1 == 200 )
{
pwm = 1;
t1 = 0;
}
}
}
你那个软件延时太长了吧,因为软件延时过长,执行完一次那个语句,中断计时已经完了。
上面那个我说错了,我调试了一下你的T0计时器是工作的,只不过你的延时太短了,眼睛无法分辨出来。因为你是延时1024us那么LED闪烁的时候我们的眼睛分辨不出来。
#include<AT89X52.h>
#define uchar unsigned char
#define uint unsigned int
sbit pwm = P2^7;
sbit LED=P1^1;
uint t,i,j,t1,t2,num,aa;
uchar flag1,flag2;
void delay(uint z)
{
for(i=z;i>0;i--)
for(j=112;j>0;j--);
}
void main()
{
TMOD = 0x11;
TH0 = (65536-1024)/256;
TL0 = (65536-1024)%256;
TH1 = (65536-512)/256;
TL1 = (65536-512)%256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
pwm = 1;
LED=1;
while(1)
{
flag1=1;
delay(1000);
flag1=0;
delay(1000);
flag2=1;
delay(1000);
flag2=0;
delay(1000);
}
}
void timer0() interrupt 1 using 0
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
LED=~LED;
}
}
void timer1() interrupt 3 using 1
{
TH1 = (65536-512)/256;
TL1 = (65536-512)%256;
if(flag1==1)
{
t++;
if(t == 2)
{
pwm = 0;
}
if(t == 200 )
{
pwm = 1;
t = 0;
}
}
if(flag2==1)
{
t1++;
if(t1 == 3)
{
pwm = 0;
}
if(t1 == 200 )
{
pwm = 1;
t1 = 0;
}
}
}
明白了,谢谢
你这个代码是为了实现什么功能?这样子写会出问题的
不错