cancel
Showing results for 
Search instead for 
Did you mean: 

Timer values determination question...

kamputty
Associate II
Posted on March 30, 2010 at 18:28

Timer values determination question...

6 REPLIES 6
Posted on May 17, 2011 at 13:45

This should work for a 72 MHz system clock.

-Clive

 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

 u16 x; // Period, interrupt periodicity in timer ticks

 u16 y; // Prescale another clock divider (1-65536)

 int TIM3_Frequency;

 RCC_ClocksTypeDef RCC_ClockFreq;

 // APB1 has a max speed of 36 MHz, with 72 MHz system clock a /2 divisor is used.

// x = 4500; y = 8;

// x = 7200; y = 5;

 x = 1000; y = 36; // Clock at 1MHz, 1000 ticks, 1 KHz

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = (x - 1);

  TIM_TimeBaseStructure.TIM_Prescaler = (y - 1); // 1-65536

  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 1,2 or 4 for tDTS

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* TIM IT enable */

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

  /* TIM2 enable counter */

  TIM_Cmd(TIM3, ENABLE);

  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = TIM3;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  RCC_GetClocksFreq(&RCC_ClockFreq);

  TIM3_Frequency = (RCC_ClockFreq.PCLK1_Frequency / y) / x; // APB1 (TIM3)

  printf(''TIM3 Interrupt rate %d Hz\n'',TIM3_Frequency);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
daviddavid94
Associate II
Posted on May 17, 2011 at 13:45

>>APB1 has a max speed of 36 MHz ...

This is true, but remember that the clock signal which is presented to the timers TIM2-TIM7 might be twice the APB1 clock (72 MHz) - if the APB1 prescaler is anything but 1. See the ''clock tree'' in RM0008.

Posted on May 17, 2011 at 13:45

Indeed, my bad.

-Clive

 x = 1000; y = 72; // Clock at 1MHz, 1000 ticks, 1 KHz

..

  RCC_GetClocksFreq(&RCC_ClockFreq);

    if (RCC_ClockFreq.HCLK_Frequency == RCC_ClockFreq.PCLK1_Frequency)

        TIM3_Frequency = RCC_ClockFreq.PCLK1_Frequency;

    else

        TIM3_Frequency = RCC_ClockFreq.PCLK1_Frequency * 2;

  TIM3_Frequency = (TIM3_Frequency / y) / x; // APB1 (TIM3)

  printf(''TIM3 Interrupt rate %d Hz\n'',TIM3_Frequency);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kamputty
Associate II
Posted on May 17, 2011 at 13:45

Hi all,

thanks for the reply. So the values you have for ''X'' and ''Y'' are for 1KHz or can I plug in any values?

I ran the program with your settings, and I'm seeing approx 9ms transition time...

So what would be the values for 1hz? I ended up with 132 for period, and (72000000/1200)-1 for prescaler (59999)

How were those numbers derived? I got them from a sample...

Again, I really appreciate the help and am trying to understand!

kamputty
Associate II
Posted on May 17, 2011 at 13:45

Hi all,

Okay, does this formula work?

== setup ==

sysclock = 72,000,000

tim_counter_clock = 24,000,000

freq = 36,000

duty = 2 (1=100%, 2=50%, 3=33%, 4=25%, etc)

TIM_Period (TIMx_ARR) = (tim_counter_clock / freq) -1

Prescaler (TIMx_PSC) = (sysclock / tim_counter_clock) -1

TIM_Pulse (TIMx_CCRx) = (Prescaler / duty)

TIM_Period (TIMx_ARR) = (24,000,000 / 36,000) -1 = 2

Prescaler (TIMx_PSC) = (72,000,000 / 24,000,000) -1 = 666

TIM_Pulse (TIMx_CCRx) = (666 / 2) = 333 (50%)

now the key is keeping the values at 16bit, so I would need to adjust my ''tim_counter_clock''

Does this sound reasonable?

Posted on May 17, 2011 at 13:45

>>thanks for the reply. So the values you have for ''X'' and ''Y'' are for 1KHz or can I plug in any values?

Generally speaking I would keep the Y (prescaler) value as small as possible to keep the granularity of the 16-bit timer ticks as small as possible, while keeping X under 65536. I'd pick a value that makes the math easy, so dividing the clock by 10, 36 or 72.

>>I ran the program with your settings, and I'm seeing approx 9ms transition time...

I'm not using it to generate pulses, but rather interrupts. The interrupt rate matches the rated printed (by the 2nd corrected fragment), and was compared against a 1000 Hz (1ms) SYSTICK timer.

>>So what would be the values for 1hz? I ended up with 132 for period, and (72000000/1200)-1 for prescaler (59999)

With a TIMCLOCK of 24 MHz, x = 50000, y = 480, I have a 1 second interrupt.

>How were those numbers derived?

Simple division. Base on the source clocks.

((240000000 / 480) / 50000 = 1

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