|
多年前读过技术文档,写过一个函数(但我也不知道是否正确)贴出来给你参考
void SetPwmFre(int TIMER_Num,int channal_X,int Pin_Num,int Set_Fre) //SetPwmFre(2,3,9,20000); //
{
unsigned long int prescale=0;
prescale = (((1 / Set_Fre)*1000000000)/256) / 13.889;
prescale = ceil(prescale);
pinMode(Pin_Num, PWM);
HardwareTimer timer_Pwm(TIMER_Num); //用于PWM频率调整,TIMER-X对应的引脚有4个,位于4个通道,具体请查DATASHEET
timer_Pwm.pause();
timer_Pwm.setPrescaleFactor(prescale);
timer_Pwm.setOverflow(255);
timer_Pwm.setCompare(channal_X,127);
timer_Pwm.setMode(channal_X,TIMER_PWM);
timer_Pwm.refresh();
timer_Pwm.resume();
/*
分频系数(prescale) 分 辨 率 / 频 率 最大计时周期(分辨率×65 535)
1 13.889 ns/(约)72 MHz 910 μs
9 125 ns /8 MHz 8.191875 ms
18 250 ns /4 MHz 16.38375 ms
36 0.5 μs /2 MHz 32.7675 ms
72 1 μs /1 MHz 65.535 ms
720 10 μs /100 KHz 655.35 ms
1440 20 μs /50 KHz 1.3107 s
7200 100 μs /10 KHz 6.5535 s
65536 910.22 μs /1098.6 Hz (约)59.65 s
pwm_Freq = (72*10^6)/(prescale*(N+1)
= 7200*10^4 / (1800*2)=20000!
N=溢出值=1 上边的代码中有设定为1了.
*/
}
|
|