51单片机定时器中断产生pwm的占空比调节问题。能帮忙看看嘛?谢谢
void timer0() interrupt 1
{
if(t0_flag == 0)
{
t1_flag = 1;
PWM = 1;
TH0 = (65536 - tpwm )/256;
TL0 = (65536 - tpwm)%256;
}
else
{
t0_flag = 0;
PWM = 0;
TH1 = (65536- 10000 + tpwm )/256;
TL1 = (65536 - 10000 + tpwm )%256;
}
}
tpwm/10000 是否就是代表占空比?如果不行的话要如何实现?
void ConfigPWM(unsigned int fr, unsigned char dc)
{
unsigned long tmp;
unsigned int high, low;
tmp = 11059200/12/fr;
high = (tmp*dc)/100;
low = tmp - high;
high = 65536 - high + 12;
low = 65536 - low + 12;
HighRH = (unsigned char)(high >>8);
HighRL = (unsigned char)high;
LowRH = (unsigned char)(low >> 8);
LowRL = (unsigned char)low;
TMOD &= 0xF0;
TMOD |= 0x01;
TH0 = HighRH;
TL0 = HighRL;
ET0 = 1;
TR0 = 1;
PWMOUT = 1;
}
void ClosePWM()
{
TR0 = 0;
ET0 = 0;
PWMOUT = 1;
}
while (1)
{
ConfigPWM(100, 10); //频率100Hz,占空比10%
for (i=0; i<40000; i++);
ClosePWM();
ConfigPWM(100, 40); //频率100Hz,占空比40%
for (i=0; i<40000; i++);
ClosePWM();
ConfigPWM(100, 90); //频率100Hz,占空比90%
for (i=0; i<40000; i++);
ClosePWM(); //关闭PWM,相当于占空比100%
for (i=0; i<40000; i++);
}
}
这个比较方便易懂,你可以参考一下