cancel
Showing results for 
Search instead for 
Did you mean: 

Choosing a timer and what mode for only counting time periods.

Posted on April 19, 2017 at 12:06

I'm using HAL_TIM library for my timer proposal. I would like to count 100 us. My idea is starting to run a MCU timer in free run mode and having and interruption every this timer period. I have read there are several timers in my st used (stm32L476), and every timer has 3 or 4 channels: pwm, output mode, input capture, compare, etc.. But I don't know which will be the best for my proposal. I guess I can discard PWM modes or input capture mode, cause I only want to be internally counting time being passed, I don't want to be outputing any signal or reading an input pin. 

Which is the channel or mode I must choose in order to have an interruption every 100us, 10us, or Xus? (of course I know I must handle the interruptions by sw but first I need to count period in the correct channel/mode)

  • input capture
  • output compare
  • pwm generation
  • one pulse mode output.

Any help will be appreciate. Thanks in advance.

#timer-channels #hal #stm32l4-timer #hal_tim
35 REPLIES 35
Posted on April 19, 2017 at 15:07

You don't need any channels to get a periodic interrupt.

Set the Prescaler to clock the TIM at 1 MHz, set the Period to 100-1, enable the Update interrupt, and you'll get an interrupt every 100us

Depending on the APB clock, but Prescaler = (SystemCoreClock / 1000000) - 1   ie figure source divided by 1 MHz to get divisor for 1 MHz

If you run the TIM with maximal period, you can use and advance the channel triggers by 100 at each interrupt for similar effect, but it is more labour intensive.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Posted on April 19, 2017 at 15:23

Use a basic timer to do this, with interrupt on overflow. Use the smallest timebase for tuning the overflow. Then if you want to create a countdown, in the interrupt, decrease a 32 bit global variable unless it is 0xffffffff

From the main loop you will have a simple timeout counter that will self stop.

Posted on April 19, 2017 at 18:07

Hi Eugenia,

The STM32 microcontrollers family have different timers with different features, but all of those with the basic feature of free counting. If you're using the STCubeMX software, in the pinout tab you can see the features of each timer, and for example you can see that TIM1 to TIM5 have the special features as PWM, output compare and input capture that includes the use of channels for signal input / output, if you don't need a signal then there is not necessary to set any channel. There are other timers that have only  the counting mode and the one pulse mode. If you want to make a time count I suggest to use the basic timers, you just check the option ''activated'' and set the parameters (prescaler and counter value that depens of the clock frequency) in the Configuration tab and the interruption. If you want to use a timer with the special features just for free counting in the pinout tab select the timer (for example the TIM1) and then just select the clock source, the TIM1 is now activated and you can set the p

arameters in the Configuration tab and the interruption (prescaler and counter value that depens of the clock frequency).

0690X00000606VgQAI.png0690X00000606h2QAA.png0690X00000606ozQAA.png0690X00000606j2QAA.png
Posted on April 19, 2017 at 16:27

but which channel must I choose? I would like to configure the timer handler struct or doing it through Cube app, but I must choose one of these 4 operation mode channels. I have never seen this before. Since I was working with 8 bit mcu timers where there was only one mode: free run, and you only (brievly said) must to save the value of the period you want to count.

However, thanks for your suggestions. I'm sure they will be usefull immediatly.

Posted on April 19, 2017 at 16:30

Is it possible to work with no channel? I thought I must select one of them.

Thanks.

Posted on April 19, 2017 at 17:29

You don t need a channel to create a delay or a timeout. As clive said, just set the timer to overflow which can trigger an interrupt.

Free running mode, autoincremented. A channel is used to generate a signal onto a pin, for example, or generate interrupts faster than the timer period.

T J
Lead
Posted on April 20, 2017 at 01:09

yes, in summary, you must use a single timer for your 100uS timerOverflow interrupt.

there is no point to use channel compare, because they go active at the count and deactivate at a count of zero, therefore the overflow rate is determining the frequency, not the channel 'compare value'

Posted on April 20, 2017 at 00:16

A periodic interrupt just needs the timebase set up, and the Update interrupt enabled.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 20, 2017 at 09:31

For sure, that was the all I need!  thanks a lot!

all the answers has been too usefull.

Thanks to all of youuu.