Skip to main content
mbmail4
Associate III
January 13, 2016
Question

TIMER Question

  • January 13, 2016
  • 7 replies
  • 1124 views
Posted on January 13, 2016 at 04:53

Hi Everyone.

I've looked through some of the older posts re: timer delay function, and I have used one of those posts as a base to what I have implemented. The problem is, when I run the delay function in a loop to test, it runs for about one second then it stops (crashes?).

From what I've read so far, my set up should be a free running 16bit counter.

Can anyone see what the problem is.

      TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

      TIM_TimeBaseStructure.TIM_Prescaler = 0x0;

      TIM_TimeBaseStructure.TIM_Period = 0xFFFF;

      TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

      TIM_Cmd(TIM2, ENABLE);

   while(1)

   {

   GPIOC->BRR  = GPIO_Pin_11;

   delay(100);

   GPIOC->BSRR = GPIO_Pin_11;

   delay(100);

   }

     

void delay(uint16_t ticks)

{

  uint16_t start = TIM2->CNT;

  do { } while((TIM2->CNT - start) < ticks);

}
    This topic has been closed for replies.

    7 replies

    Radosław
    Associate II
    January 13, 2016
    Posted on January 13, 2016 at 10:25

      do { } while((uint16_t)(TIM2->CNT - start) < ticks);

    Tesla DeLorean
    Guru
    January 13, 2016
    Posted on January 13, 2016 at 12:43

    Stops where? Use a debugger.

    Is this an F1 part? What specific STM32

    If an F2/F4 TIM2 is 32-bit, suggest you configure it as such, and use uint32_t, it will be more efficient that 16-bit, and won't potentially need casting

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    pkumar1883
    Associate III
    January 13, 2016
    Posted on January 13, 2016 at 14:48

    Hi b.m.002,

    If you only want to make a timer based delay function then I would recommend to use

    systick.

    It uses very less configurations. Easy to use.

    If need help for TIMER problem then mention your STM32 part number. So that specific help can be provided.

    Walid FTITI_O
    Visitor II
    January 13, 2016
    Posted on January 13, 2016 at 16:37

    Hi b.m.002,

    You have to enable the Timer counter the moment of start ( begin of delay function), and not before.

    -Hannibal-

    mbmail4
    mbmail4Author
    Associate III
    January 13, 2016
    Posted on January 14, 2016 at 00:34

    Thanks everyone for your input. I have a STM32VLDISCOVERY fitted with the STM32F100RBT6B microcontroller.

    I've made some changes, and what I want to do is have a delay routine available that will give me 1uS granularity. Generally most applications need delays in the low mS, but for this exercise I want to figure out why this setup is not working.

    When I changed the location the timer was enabled, this worked for the previous setup, but it counted some other delay value which for testing at the time was not important. The code below is hopefully set up to produce 1uS events, and this does not seem to work at the moment, the timer does not seem to want to start at all, maybe its something I don't understand about the way timers work in the stm32? If I got it correct, then TIMER2 roll over should occur at 1/24Mhz * 65535 = 2.7mS, so in the main loop delaying for 100 should work ie: 100uS.

    void TIMER2_Configuration_new(void)

    {

      TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

      TIM_TimeBaseStructure.TIM_Prescaler = 23;

      TIM_TimeBaseStructure.TIM_Period = 0;

      TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    }

    void delay(uint16_t ticks)

    {

      TIM_Cmd(TIM2, ENABLE);

      uint16_t start = TIM2->CNT;

      do { } while((TIM2->CNT - start) < ticks);

      TIM_Cmd(TIM2,DISABLE);

    }

    Thanks

    Martin.
    Tesla DeLorean
    Guru
    January 14, 2016
    Posted on January 14, 2016 at 03:45

    TIM_TimeBaseStructure.TIM_Period = 0; // No going to work

    TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // Maximal 0..65535

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    mbmail4
    mbmail4Author
    Associate III
    January 14, 2016
    Posted on January 14, 2016 at 04:36

    Hi clive, I change the period setting to 0xFFFF, but its still wont work? so far I haven't figured out what else to try. It works briefly and then stops.

    My scope trace seems to show the period to be correct for the setup, that is, its a 100uS dely.