AT89C52单片机的两个定时器如何同时工作?
时间:10-02
整理:3721RD
点击:
以下是我的程序,定时器1可以输出500us的PWM信号,但定时器2却没工作,无法让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;
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;
}
}
}
#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;
}
}
}
我是来看大神的