输出PWM周期及占空比的运算
1.周期:
[ (arr+1) * (psc+1) ] / 72000KHz = T(ms)
2.占空比:
须根据PWM的相关配置计算:
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_SetCompare1(TIM3,parameter);
请注意这里的“_PWM2”、“_Low”,这时占空比的计算如下:
占空比 = parameter /(arr+1)
下面改变配置:
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_SetCompare1(TIM3,parameter);
//占空比 = 1 - parameter /(arr+1)
当然可改项还有TIM_CounterMode、TIM_OCMode,不妨自己实践验证一下。
附上一组数据:
TIM3_OCInit(9999,7199);//arr=9999, psc=7199
TIM_SetCompare1(TIM3,4000);
(拍了示波器的图片的,奈何图片太大,上传不了。。。有条件的同学可以自己验证一下。)