Skip to main content
saiteja
Associate III
October 26, 2018
Question

Regarding timer delay & Interrupt

  • October 26, 2018
  • 12 replies
  • 2108 views

void MX_TIM5_Init(void)

{

 TIM_ClockConfigTypeDef sClockSourceConfig;

 TIM_MasterConfigTypeDef sMasterConfig;

 htim5.Instance = TIM5;

 htim5.Init.Prescaler = 84;

 htim5.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim5.Init.Period = 99; //interrupt in 100 micro sec

// htim5.Init.Period = 50; //interrupt in 50 micro sec

 htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 if (HAL_TIM_Base_Init(&htim5) != HAL_OK)

 {

  Error_Handler();

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

 if (HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK)

 {

  Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)

 {

  Error_Handler();

 }

}

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)

{

 if(tim_baseHandle->Instance==TIM5)

 {

 /* USER CODE BEGIN TIM5_MspInit 0 */

 /* USER CODE END TIM5_MspInit 0 */

  /* Peripheral clock enable */

  __HAL_RCC_TIM5_CLK_ENABLE();

  /* Peripheral interrupt init */

  HAL_NVIC_SetPriority(TIM5_IRQn, 0, 0);

  HAL_NVIC_EnableIRQ(TIM5_IRQn);

 /* USER CODE BEGIN TIM5_MspInit 1 */

 /* USER CODE END TIM5_MspInit 1 */

 }

}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)

{

 if(tim_baseHandle->Instance==TIM5)

 {

 /* USER CODE BEGIN TIM5_MspDeInit 0 */

 /* USER CODE END TIM5_MspDeInit 0 */

  /* Peripheral clock disable */

  __HAL_RCC_TIM5_CLK_DISABLE();

  /* Peripheral interrupt Deinit*/

  HAL_NVIC_DisableIRQ(TIM5_IRQn);

 }

 /* USER CODE BEGIN TIM5_MspDeInit 1 */

 /* USER CODE END TIM5_MspDeInit 1 */

} 

This is how i have initialised the timer now i am looking to put delay between

SetPWM(x,dutycycle);

SetPWM(y,dutycycle);

SetPWM(z,dutycycle);

delay();

SetPWM(x,0);

SetPWM(y,0);

SetPWM(z,0);

when iam using timer delay or normal delay the system is hanging as i am changing the PWM during delay is there any solution for this.

    This topic has been closed for replies.

    12 replies

    T J
    Senior III
    October 26, 2018

    please explain a little better .. you are using blocking code.

    You have 3 PWM channels

    You want to wait some time and update the channels...

    how much time ? call it, reload time

    You could initialise another timer to interrupt you at the reload rate..

    under that timer interrupt, you can reset the PWM and exit.

    next interrupt update again and exit.

    its non-blocking code.

    99.999% cpu time free..

    saiteja
    saitejaAuthor
    Associate III
    October 26, 2018

    i Have 3 PWM channels i want to take the present dutycycle and after some delay i want to make it zero nothing but i want to blink. dutycycle may be changed continuously but not constant change in interval time that depends on user. delay is also different depends on user selection it should be between 25 to 250 ms. during blinking if change in PWM it has to take that PWM and continue blinking.

    saiteja
    saitejaAuthor
    Associate III
    October 26, 2018

    PWM pins iam using are PB0,PC6,PC7 in STM32F407VG Controller

    saiteja
    saitejaAuthor
    Associate III
    October 26, 2018

    SetPWM(x,dutycycle);

    SetPWM(y,dutycycle);

    SetPWM(z,dutycycle);

    delay();

    SetPWM(x,0);

    SetPWM(y,0);

    SetPWM(z,0);

    delay();

    initialisation everthing is OK but i stuck at this point

    saiteja
    saitejaAuthor
    Associate III
    October 26, 2018

    if iam giving contant PWM and if i write the above code it is working fine if there is a change in PWM then it is hanging

    T J
    Senior III
    October 26, 2018

    can you show this routine ?

    SetPWM

    I would be adjusting the PWM by registers directly, not updating with HAL.

    are you running multi-tasking like RTOS ?

    Can you show where you are calling this routine ? SetPWM

    saiteja
    saitejaAuthor
    Associate III
    October 27, 2018

    void SetPWM(uint8_t pwmID, uint16_t dutyCycle)

    {

    uint32_t for_var, for_var_2;

    /* Helper variable to compare with last set pulse value */

    uint32_t previous_pulse_value_temp = 0;

    /* For local calculations */

    TIM_OC_InitTypeDef sConfigOC_temporary;

    sConfigOC_temporary.OCMode = TIM_OCMODE_PWM1;

     sConfigOC_temporary.Pulse = 0;

     sConfigOC_temporary.OCPolarity = TIM_OCPOLARITY_HIGH;

     sConfigOC_temporary.OCFastMode = TIM_OCFAST_DISABLE;

    //Setting the duty cycle here for pwm channel_1

    switch(pwmID)

    {

    case 1:

    sConfigOC.Pulse = (uint32_t)round((PERIOD_VALUE * (float)dutyCycle / 255));

    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

    {

    Error_Handler();

    }

    if(HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1) != HAL_OK)

    {

    Error_Handler();

    }

       break;

    case 2:

    sConfigOC.Pulse = (uint32_t)round((PERIOD_VALUE * (float)dutyCycle / 255));

    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

    {

    Error_Handler();

    }

    if(HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2) != HAL_OK)

    {

    Error_Handler();

    }

    /* Also effect the higher frequency test pin for green color */

    sConfigOC.Pulse = (uint32_t)round((PERIOD_VALUE_NEWCONFIG * (float)dutyCycle / 255));

    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

    {

    Error_Handler();

    }

    if(HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2) != HAL_OK)

    {

    Error_Handler();

    }

       break;

    case 3:

    // sConfigOC.Pulse = (uint32_t)(PERIOD_VALUE * dutyCycle / 255);

    sConfigOC.Pulse = (uint32_t)round((PERIOD_VALUE * (float)dutyCycle / 255));

    if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

    {

    Error_Handler();

    }

    if(HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3) != HAL_OK)

    {

    Error_Handler();

    }

    /* Also effect the higher frequency test pin for blue color */

    sConfigOC.Pulse = (uint32_t)round((PERIOD_VALUE_NEWCONFIG * (float)dutyCycle / 255));

    if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

    {

    Error_Handler();

    }

    if(HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3) != HAL_OK)

    {

    Error_Handler();

    }

    break;

      default :

    break;

    }

    }

    saiteja
    saitejaAuthor
    Associate III
    October 27, 2018

    no iam not running the RTOS

    dbgarasiya
    Senior
    December 28, 2019

    If i want to configure timer of 5 sec how can i do it

    what are configuration to do ?

    T J
    Senior III
    December 28, 2019

    do you use the cube ?

    do you have a scope ?

    google this "stm32 timer cookbook

    5 PDFs are presented.

    saiteja
    saitejaAuthor
    Associate III
    December 28, 2019

    No, I am using Keil. Got the solution for this, thank You