diff --git a/libraries/HAL_Drivers/drv_pwm.c b/libraries/HAL_Drivers/drv_pwm.c index 6543ced..a0e7b63 100644 --- a/libraries/HAL_Drivers/drv_pwm.c +++ b/libraries/HAL_Drivers/drv_pwm.c @@ -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;