以下是我的pwm设置小程序 我的pwm的频率为啥最大才300HZ左右 再大大不了了 而且在两端频率 即在过小或300以上的时候 程序就出问题了 示波器测出乱七八糟的波形了
#include "reg52.h"
sbit OUT=P2^0; //pwm输出
sbit add=P2^7; //占空比增加
sbit dec=P2^6; //占空比减少
sbit addhz=P2^5; //改变频率 实际上是增加周期 减少频率
sbit dechz=P2^4; //
unsigned int tt=10; //
unsigned int th0=0xff;
unsigned int tl0=0xfe;
unsigned int th;
unsigned char hpwm=50; //高电平占空比
unsigned char lpwm=50; //低电平
unsigned char count =0;
void time0() interrupt 1
{
//TH0=(65536-2)/256;
//TL0=(65536-2)%256;
TH0=th0;;
TL0=tl0;
count++;
if (count >= 101)
count = 0;
if (count < (100 - hpwm))
OUT = 1;
else
OUT = 0;
TR0=1;
}
void main()
{
TMOD=0x01;
//TH0=(65536-2)/256;
//TL0=(65536-2)%256;
TH0=th0;
TL0=tl0;
EA = 1;
TR0=1;
//IE=0x82;
ET0=1;
OUT=1;
while(1)
{
if(!add)
{ while(add==0);
hpwm=hpwm+10;
if(hpwm>=100)
hpwm=100;
if(hpwm<=0)
hpwm=0;
}
if(!dec)
{
while(dec==0);
hpwm=hpwm-10;
if(hpwm>=100)
hpwm=100;
if(hpwm<=0)
hpwm=0;
}
if(!addhz) //改变频率 实际上是增加周期 减少频率
{
while(addhz==0);
tt=tt+50;
}
if(!dechz)
{
while(dechz==0);
tt=tt-10;
}
if(tt>=50000)
tt=50000;
if(tt<=0)
tt=10;
th=65536-tt;
th0=th/256;
tl0=th%256;
}
}
|