51单片机输出PWM波形频率为什么不能超过350Hz?确切的说,给定时器赋值1000以下,显示就出现很大的误差?

#include <reg51.h>
sbit output=P1^1;
sbit s1=P2^0;sbit s2=P2^1;sbit s3=P2^2;sbit s4=P2^3;sbit s5=P2^4;sbit s6=P2^5;sbit s7=P2^6;
sbit s8=P2^7;
unsigned int count=0;
#define TH_M1 (65536-count)/256;
#define TL_M1 (65536-count)%256;
main()
{ IE=0x82;
TMOD=0x01;
output=1;
P2=0xff;
while(1)
{ if(s1==0) count=5;
else if(s2==0) count=10; //频率为:50K Hz
else if(s3==0) count=50; //频率为:10K
else if(s4==0) count=100; //频率为:5K
else if(s5==0) count=500; //频率为:1K
else if(s6==0) count=1000; //频率为:500
else if(s7==0) count=5000; //频率为:100
else if(s8==0) count=10000; //频率为:50
TH0=TH_M1;TL0=TL_M1;
TR0=1;
while(TF0==0);
TF0=0;
output=~output;
}
}

第1个回答  2011-10-30
你的程序通过循环得以执行,循环本身就需要时间。准确的PWM最好通过计数器自动装载。 要不,你就通过单步调试测出每一条语句的执行时间,加(或减)到定时器里去。汇编语言较易实现,C必须耐心测试,还不能有中断。
第2个回答  2011-10-31
使用T2,16位自动装载模式,或T0,T1的8位自动装载模式