Skip to main content
o gaste
Associate II
May 23, 2017
Solved

STM32F3xx device driver / lib lack

  • May 23, 2017
  • 2 replies
  • 711 views
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

    This topic has been closed for replies.
    Best answer by Imen.D
    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

    2 replies

    Imen.DBest answer
    ST Technical Moderator
    May 24, 2017
    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

    In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
    o gaste
    o gasteAuthor
    Associate II
    May 24, 2017
    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