cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3xx device driver / lib lack

o gaste
Associate III
Posted on May 23, 2017 at 18:02

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

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee
Posted on May 24, 2017 at 19:24

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

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

View solution in original post

2 REPLIES 2
Imen.D
ST Employee
Posted on May 24, 2017 at 19:24

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

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on May 24, 2017 at 21:19

tank's for this explanations I will do that

I tested my method and it works well and it change DutyCycle on the fly