pwm 驱动优化
实测发现, 改为低电平作为有效电平后,输出脉宽更准确一些, 故作此修改。另外移除了一段无效代码逻辑,降低理解负担。 Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
@@ -264,14 +264,6 @@ static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
|
||||
rt_uint64_t tim_clock;
|
||||
|
||||
tim_clock = tim_clock_get(htim);
|
||||
if (__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV2)
|
||||
{
|
||||
tim_clock = tim_clock / 2;
|
||||
}
|
||||
else if (__HAL_TIM_GET_CLOCKDIVISION(htim) == TIM_CLOCKDIVISION_DIV4)
|
||||
{
|
||||
tim_clock = tim_clock / 4;
|
||||
}
|
||||
|
||||
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
|
||||
tim_clock /= 1000000UL;
|
||||
@@ -357,6 +349,7 @@ static rt_err_t drv_pwm_set_pulse(TIM_HandleTypeDef *htim, struct rt_pwm_configu
|
||||
tim_clock /= 1000000UL;
|
||||
|
||||
period = (__HAL_TIM_GET_AUTORELOAD(htim) + 1) * (htim->Instance->PSC + 1) * 1000UL / tim_clock;
|
||||
configuration->pulse = period - configuration->pulse; // 改为低电平有效,故需要计算一下低电平的脉宽
|
||||
pulse = (rt_uint64_t)configuration->pulse * (__HAL_TIM_GET_AUTORELOAD(htim) + 1) / period;
|
||||
|
||||
if (pulse < MIN_PULSE)
|
||||
@@ -452,7 +445,7 @@ static rt_err_t stm32_hw_pwm_init(struct stm32_pwm *device)
|
||||
|
||||
oc_config.OCMode = TIM_OCMODE_PWM1;
|
||||
oc_config.Pulse = 0;
|
||||
oc_config.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
oc_config.OCPolarity = TIM_OCPOLARITY_LOW;
|
||||
oc_config.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
oc_config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
|
||||
oc_config.OCIdleState = TIM_OCIDLESTATE_RESET;
|
||||
|
Reference in New Issue
Block a user