Skip to main content
mayakk12
Associate II
February 25, 2012
Question

Timer value calculation

Posted on February 25, 2012 at 10:34

hi,

I am using STM32VL board.. In my application i am reading voltage for every 6.25micro sec. so i'm using timer TIM2 in interupt mode for that. can any one tell me what value i need to set fro prescaler and period.

Sys clock is 24Mhz and ADC clock is 12 MZ.

thanks a lot in advance

#timer-value
This topic has been closed for replies.

18 replies

Tesla DeLorean
Guru
February 25, 2012
Posted on February 25, 2012 at 14:50

I am using STM32VL board.. In my application i am reading voltage for every 6.25micro sec. so i'm using timer TIM2 in interupt mode for that. can any one tell me what value i need to set fro prescaler and period. Sys clock is 24Mhz and ADC clock is 12 MZ.

Some basic math involved, 6.25 us is 160 KHz, TIMCLK is 24 MHz

24,000,000 / 160,000 = 150;

Prescale = 0; // ie 1 - 1, no division

Period = 150 - 1;

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
mayakk12
mayakk12Author
Associate II
February 27, 2012
Posted on February 27, 2012 at 11:31

hi,

Thanks a lot for ur help.

can you tell me why subtraction is required there..??

mayakk12
mayakk12Author
Associate II
February 27, 2012
Posted on February 27, 2012 at 11:34

hi ..

thanks a lot for the help..

can u tell me why subtration is required there?

Tesla DeLorean
Guru
February 27, 2012
Posted on February 27, 2012 at 14:02

can u tell me why subtration is required there?

 

The value is the final count, zero based, thus a divide by N is achieved by programming a value of N-1 into the period. Please refer to part documentation for more details.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
sanjib
Associate III
December 26, 2013
Posted on December 26, 2013 at 07:49

Can anyone tell me how to generate an interrupt of 1.5625 microsecond. I am using Timer 2 . Timer frequency is 84000000. what is the value I should set in ARR and prescalar. Please help me

sanjib
Associate III
December 26, 2013
Posted on December 26, 2013 at 08:25

Tesla DeLorean
Guru
December 26, 2013
Posted on December 26, 2013 at 13:12

1.5625 microsecond = 640 KHz, seems a bit high for an interrupt rate. If you are looking to generate a pin output, connect the timer directly, and don't use interrupts, or consider more carefully what it is you're trying to do.

84,000,000 / 640,000 = 132.25

That's non-integer, so either you'll have to tolerate the frequency being off slightly, or run the CPU at a speed that is cleanly divisible (ie 128 MHz, or whatever)

Prescaler = 0;

Period = 132 - 1;
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
sanjib
Associate III
December 27, 2013
Posted on December 27, 2013 at 07:41

Thanks for the reply. My task is to read the adc channels after 1.5625 microsecond this is the requirement. Input frequency is 160 khz. if we set the vale to 131 we get 1.571428571 us. which is not my requirement. therfore can you explain me briefly so that I can considered my requirement as I am new to stm32 controller.

Thanks in Advance
sanjib
Associate III
December 27, 2013
Posted on December 27, 2013 at 07:49

how to set the sysclk to 128000000 as 128000000/200 = 640 khz. but I have a doubt that timer frequency is 84 mhz or the maximum is 168 Mhz . So can we set it to 128000000. if yes please say me how to do this. I will be greatful to you as i have no idea and I am new to stm32f4

Tesla DeLorean
Guru
December 27, 2013
Posted on December 27, 2013 at 15:05

It depends on the TIM unit, which APB bus it's on, and what APB dividers are being used.

For the typical 168 MHz system APB2 is at DIV2, and it's TIMCLKs at DIV1 (168 MHz), and APB1 is at DIV4 and it's TIMCLKs at DIV2 (84 MHz)

These setting usually appear in system_stm32f4xx.c, along with the PLL settings. To run the processor at 128 MHz instead of 168 MHz you'd want to change PLL_N from 336 to 256, and adjust other related constants to match the new speed, ie SystemCoreClock

At the sample rate in question I'd probably not use an interrupt, but rather use the TIM to drive the ADC, and in turn the DMA

An F1 example about 3/5 down, not directly applicable to the F4

https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Reading%20voltage%20-ADC&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=2527

A dual F4 example about 1/4 down,

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/ADC%20in%20Dual%20Mode%20Simultaneously&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=395

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