如题,现晶振为12MHZ,部分程序如下:
sbit pwm_out0= P1^0;
sbit pwm_out1 = P1^1;
sbit pwm_out2 = P1^2;
sbit pwm_out3 = P1^3;
sbit pwm_out4 = P1^4;
sbit pwm_out5 = P1^5;
sbit pwm_out6 = P2^0;
sbit pwm_out7 = P2^1;
sbit pwm_out8 = P2^2 ;
sbit pwm_out9 = P2^3 ;
sbit pwm_out10 = P2^4;
sbit pwm_out11 = P2^5 ;
void PWM()
{
unsigned char i;
if(pwm_base<33) //基值在32次范围内比较循环12次,节约时间,第32次必为低,锁存
{
for(i=0;i<12;i++) //循环12次
{
if(pwm_base>pwm_num[i]) //当基值小于阈值时,pwm输出高电平,
pwm_out[i]=0;
else
pwm_out[i]=0; //否则,当基值大于或等于阈值时,输出pwm的低电平
}
pwm_out0 = pwm_out[0];
pwm_out1 = pwm_out[1];
pwm_out2 = pwm_out[2];
pwm_out3 = pwm_out[3];
pwm_out4 = pwm_out[4];
pwm_out5 = pwm_out[5];
pwm_out6 = pwm_out[6];
pwm_out7 = pwm_out[7];
pwm_out8 = pwm_out[8];
pwm_out9 = pwm_out[9];
pwm_out10 = pwm_out[10];
pwm_out11 = pwm_out[11];
}
if(pwm_base>254) //大于255即256次后,计数基值置0
pwm_base=0;
}
void LED_t1() interrupt 3
{
TH1 = (65536-70)/256; //设置初值
TL1 = (65536-70)%256;
ET1 = 0;
pwm_base++;
ET1 = 1;
}
定时器定时为70us,需要在70us内完成一次PWM函数的计算,但是在12MHZ晶振时执行一次PWM();最少需要300us的时间。所以,必然会使输出的PWM不准确。
求改进方法……换晶振?改程序? |