Skip to main content
asecen89
Associate III
May 21, 2014
Question

TIM3 timer period

  • May 21, 2014
  • 8 replies
  • 1835 views
Posted on May 21, 2014 at 15:46

Hi,

I use STM32F427 chip. In my application, I am generating a 25.6 kHz signal using an external signal with 9.984 MHz. In order to do so, the calculation for timer period should be as follows; (9.984MHz / 25.6kHz) - 1 = 389. However, in the application the microcontroller generates the 25.6 kHz when I set the timer period to 390. I am confused by that, shouldn't I subtract 1 by 390 to obtain the timer period? 

Thanks
    This topic has been closed for replies.

    8 replies

    Tesla DeLorean
    Guru
    May 21, 2014
    Posted on May 21, 2014 at 16:11

    Yes, 390-1 should be the number, you'd want to consider how you are measuring the respective signals, and if the resynchronization of the input put might impact it.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    asecen89
    asecen89Author
    Associate III
    May 21, 2014
    Posted on May 21, 2014 at 16:32

    I have a counter in my code to count the number of pulses that the timer generates. Also a PPS signal is connected to one of microcontroller pins which triggers an interrupt at each pulse. It turned out that when I check the number of pulses that counted between two PPS signals, it is exactly 25600 when period is 390, and it is 25666 when period is 389.

    asecen89
    asecen89Author
    Associate III
    May 21, 2014
    Posted on May 21, 2014 at 16:33

    I have a counter in my code to count the number of pulses that the timer generates. Also a PPS signal is connected to one of microcontroller pins which triggers an interrupt at each pulse. It turned out that when I check the number of pulses that counted between two PPS signals, it is exactly 25600 when period is 390, and it is 25666 when period is 389.

    waclawek.jan
    Super User
    May 21, 2014
    Posted on May 21, 2014 at 16:58

    Do you set directly the timer ARR register, or do you use some sort of ''library call'', which performs the decrement?

    JW

    asecen89
    asecen89Author
    Associate III
    May 22, 2014
    Posted on May 22, 2014 at 12:45

    The piece of code that sets the timer period is below;

      TIM_TimeBaseStructure.TIM_Prescaler = 0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseStructure.TIM_Period = 390;

      TIM_TimeBaseStructure.TIM_ClockDivision = 0;

      TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    asecen89
    asecen89Author
    Associate III
    May 22, 2014
    Posted on May 22, 2014 at 16:23

    In reference manual it was written that the counter starts from 0 and counts to ARR value. I use PWM1 mode to generate the pulse, can this cause to counter to count from 1 to ARR value? It doesn't really make sense, but could this happen?

    waclawek.jan
    Super User
    May 22, 2014
    Posted on May 22, 2014 at 16:49

    We would need to see all the relevant portions of your code to judge.

    Can you measure the input and output frequency by some independent method, e.g. using a frequency counter? Or, alternatively, you can try two smaller ARR values to clearly see the difference - say, 10 and 11 - and have a look at the resulting waveform with an oscilloscope?

    JW

    asecen89
    asecen89Author
    Associate III
    May 23, 2014
    Posted on May 23, 2014 at 09:25

    I am posting the whole timer configuration code below;

      TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

      TIM_OCInitTypeDef  TIM_OCInitStructure;

      GPIO_InitTypeDef GPIO_InitStructure;

      TIM_ICInitTypeDef  TIM_ICInitStructure;

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;

      GPIO_Init(GPIOC, &GPIO_InitStructure);

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

      GPIO_Init(GPIOB, &GPIO_InitStructure);

      GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3);

      GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3); 

      TIM_TimeBaseStructure.TIM_Prescaler = 0;

      TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

      TIM_TimeBaseStructure.TIM_Period = 390;

      TIM_TimeBaseStructure.TIM_ClockDivision = 0;

      TIM_ETRClockMode1Config(TIM3, TIM_ExtTRGPSC_OFF,       TIM_ExtTRGPolarity_NonInverted, 0);

     

      TIM_TIxExternalClockConfig(TIM3, TIM_TIxExternalCLK1Source_TI1,          TIM_ICPolarity_Rising, 0);

      TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_External1);

      TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

      TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;

      TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

      TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

      TIM_ICInitStructure.TIM_ICFilter = 0;

      TIM_SelectHallSensor(TIM3, ENABLE);  

      TIM_ICInit(TIM3, &TIM_ICInitStructure);

      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

      TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;

      TIM_OCInitStructure.TIM_Pulse = 15;

      TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

      TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;

      TIM_OC2Init(TIM3, &TIM_OCInitStructure);