2017-05-23 09:02 AM
I dont know I must be wrong but I didn't find function call to change the dutycycle of a pwm
so I wrote this and I added it to stm32F3xx_hal_tim.c
/**
* @brief Set the PWM DutyCycle.
* @param htim: TIM handle
* @param Channel: TIM Channels to setted
* This parameter can be one of the following values:
* @arg TIM_CHANNEL_1: TIM Channel 1 selected
* @arg TIM_CHANNEL_2: TIM Channel 2 selected
* @arg TIM_CHANNEL_3: TIM Channel 3 selected
* @arg TIM_CHANNEL_4: TIM Channel 4 selected
* @param v: value of dutycycle
* @retval HAL status
*/
HAL_StatusTypeDef HAL_TIM_PWM_SetDutyCyle(TIM_HandleTypeDef *htim, uint32_t Channel, float v)
{
__HAL_LOCK(htim);
/* Check the parameters */
assert_param(IS_TIM_CHANNELS(Channel));
if(v<0 || v>1)
return HAL_ERROR;
htim->State = HAL_TIM_STATE_BUSY;
switch (Channel)
{
case TIM_CHANNEL_1:
{
assert_param(IS_TIM_CC1_INSTANCE(htim->Instance));
/* Set the CCR */
htim->Instance->CCR1 = htim->Instance->ARR*v;
}
break;
case TIM_CHANNEL_2:
{
assert_param(IS_TIM_CC2_INSTANCE(htim->Instance));
/* Set the CCR */
htim->Instance->CCR2 = htim->Instance->ARR*v;
}
break;
case TIM_CHANNEL_3:
{
assert_param(IS_TIM_CC3_INSTANCE(htim->Instance));
/* Set the CCR */
htim->Instance->CCR3 = htim->Instance->ARR*v;
}
break;
case TIM_CHANNEL_4:
{
assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
/* Set the CCR */
htim->Instance->CCR4 = htim->Instance->ARR*v;
}
break;
default:
break;
}
htim->State = HAL_TIM_STATE_READY;
__HAL_UNLOCK(htim);
return HAL_OK;
}
and the function prototype in the include stm32f3xx_hal_tim.h in the TIM_Exported_Functions_Group3 group
/
** @addtogroup TIM_Exported_Functions_Group3
* @{
*/
/* Timer PWM functions *********************************************************/
HAL_StatusTypeDef HAL_TIM_PWM_Init(TIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_TIM_PWM_DeInit(TIM_HandleTypeDef *htim);
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim);
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *htim);
/* Blocking mode: Polling */
HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_TIM_PWM_Stop(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_TIM_PWM_SetDutyCyle(TIM_HandleTypeDef *htim, uint32_t Channel, float v);/* Non-Blocking mode: Interrupt */
HAL_StatusTypeDef HAL_TIM_PWM_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel);
HAL_StatusTypeDef HAL_TIM_PWM_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel);
/* Non-Blocking mode: DMA */
HAL_StatusTypeDef HAL_TIM_PWM_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length);
HAL_StatusTypeDef HAL_TIM_PWM_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel);
/**
* @}
*/
I dont know if I'm wrong to post that here or even if it will interest someone but if it helps I will be happy
good works everyone
Solved! Go to Solution.
2017-05-24 10:24 AM
Hi
olivier.gaste
,Thank you forsharing your sugesstion code.
In fact, you can
update the PWM duty cycle through
HAL_TIM_PWM_ConfigChannel function.Thanks
Imen
2017-05-24 10:24 AM
Hi
olivier.gaste
,Thank you forsharing your sugesstion code.
In fact, you can
update the PWM duty cycle through
HAL_TIM_PWM_ConfigChannel function.Thanks
Imen
2017-05-24 02:19 PM
tank's for this explanations I will do that
I tested my method and it works well and it change DutyCycle on the fly