Skip to main content
lizerd
Associate III
November 29, 2010
Question

Timer interrupt Question

  • November 29, 2010
  • 4 replies
  • 813 views
Posted on November 29, 2010 at 09:31

Timer interrupt Question

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:16

    Third question:

     

    Is this the best way to go for a simple time based interrupt routine ??

     

    Well define ''best''. What period do you want, is it continuous or variable, etc.

    SysTick is pretty simple. The RTC can go sub-second with the appropriate scale. The TIMx timers can be a lot simpler than you have presented if you just want a constant periodic interrupt ticker.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    lowpowermcu
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:16

    Hi,

    You need to configure the time base, enable update interrupt and configure the NVIC.

    Add 

       /* TIM update IT enable */

      TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

    Remove from configuration:

    /********    satrt                 *******/

      /* Output Compare Timing Mode configuration: Channel1 */

      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

      TIM_OCInitStructure.TIM_Pulse = 3000;

      TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

      TIM_OC1Init(TIM2, &TIM_OCInitStructure);

      TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

      /* TIM IT enable */

      TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);

    /********   end                 *******/

    /* !!!!!!!!!!!!!!!!!!!!! Don't remove this!!!!!!!!!!!!!!!!!!!!!!!!!!! */

      /* TIM2 enable counter */

      TIM_Cmd(TIM2, ENABLE);

    In the timer interrupt, as you said you need to clear the interrupt

    change

    void TIM2_IRQHandler(void)

    {

      if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)

      {

        TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);

        capture = TIM_GetCapture1(TIM2);

        TIM_SetCompare1(TIM2, capture + 3000);

         // Main Clock Timer

        Timer_mSec++;

        if(Timer_mSec == 1000)

        {

            Timer_mSec = 0;

            Timer_Sec++;

            if(Timer_Sec == 60)

            {

                Timer_Sec = 0;

                Timer_Min++;

            }

        }

      }

      TestTime++;

    }

    by

    void TIM2_IRQHandler(void)

    {

      if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

      {

         // Main Clock Timer

        Timer_mSec++;

        if(Timer_mSec == 1000)

        {

            Timer_mSec = 0;

            Timer_Sec++;

            if(Timer_Sec == 60)

            {

                Timer_Sec = 0;

                Timer_Min++;

            }

        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

      }

    }

    Remark:   TIM_OC1Init() configures Output Compare channel: pwm1, pwm2

    lizerd
    lizerdAuthor
    Associate III
    May 17, 2011
    Posted on May 17, 2011 at 14:16

    i am using the SysTick already, so i have learned how to use the SysTick.

    But now i trying to master the ''Normal'' Timer interrupt.

    So right now i trying to find a simple interrupt based on the TIM timers.

    I have checked and tested some of the timer example. but i did not find any one that is a simple interrupt based timer.

    Lets say. a timer that go´s into interrupt every 1ms

    I haven´t tested

    0690X0000060MlRQAU.gif code yet,

    Tesla DeLorean
    Guru
    May 17, 2011
    Posted on May 17, 2011 at 14:16

    For the VL Discovery a simple implementation with blinking LEDs

    void TIM2_IRQHandler(void)

    {

      if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

      {

        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

        /* Whatever */

        STM32vldiscovery_LEDToggle(LED3);

      }

    }

    int main(void)

    {

      TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

      NVIC_InitTypeDef NVIC_InitStructure;

      /* Initialise LEDs LD3&LD4, both off */

      STM32vldiscovery_LEDInit(LED3);

      STM32vldiscovery_LEDInit(LED4);

      STM32vldiscovery_LEDOff(LED3);

      STM32vldiscovery_LEDOff(LED4);

      /* Enable the TIM2 gloabal Interrupt */

      NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

      NVIC_Init(&NVIC_InitStructure);

      /* TIM2 clock enable */

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

      /* VL clocks at 24 MHz, change to 72 - 1 for a 72 MHz system

      /* Time base configuration */

      TIM_TimeBaseStructure.TIM_Period = 1000 - 1;  // 1 MHz down to 1 KHz (1 ms)

      TIM_TimeBaseStructure.TIM_Prescaler = 24 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)

      TIM_TimeBaseStructure.TIM_ClockDivision = 0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

      /* TIM IT enable */

      TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

      /* TIM2 enable counter */

      TIM_Cmd(TIM2, ENABLE);

      while (1);

    }

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..