# How to calculate the time of one single tick on microcontroller

I have been in Embedded system for 8 months. I have worked application part more compare with low level. I have basic questions on systems ticks,

1. How to calculate the system tick of Timer, if I am starting the Hardware Timer of a controller for generating any delay. If I am having the 16-bit timer for example and I configured the system clock for 8MHz. As per my knowledge the single tick will be T=1/f. Then one single tick will take 0.125 microsecond. Is this correct? or any other dependency to calculate it. Whether this calculation will vary with controller by controller?
2. Is there any way to configure the clock(eg 1MHz) for only Timer or any other peripheral with out changing the System Clock(eg 8Mhz)
3. If controller goes to low power mode(typically a changing of power mode). The timer resolution will also change? whether I need to change the setting of Timer for each power Mode.

Basically it is all the same with all microcontrollers and your calculation was correct. In your example, with a 16 bit Timer and, $$f_{\text{SystemClock}} = f_{\text{timer}} = 8MHz$$

As you said we have a tick in every, $$T_{\text{timer}} = \frac{1}{f_{\text{SystemClock}}} = \frac{1}{f_{\text{timer}}} = \frac{1}{8MHz} = 0.125μs$$

With a 16 bit Timer it means,

$$ticks_{\text{max}} = (2^{16} - 1) = 65535$$

ticks. So the timer will overflow in every,

$$t_{\text{overflow}} = ticks_{\text{max}} \times T_{\text{timer}} = 65535 \times 0.125μs = 8.191875ms$$

You can count overflows to get a specific delay. Now if you want to change toverflow's value

• You can divide the fSystemClock as the other answers mentioned it, and run your timer with a frequency different from the fSystemClock. This way you will have more time between two timer ticks and toverflow will be higher.
• Or, you can set a maximum value for the timer so it will overflow earlier and toverflow will be less.

This way a lot of delay value can be achieved.

• @BenceKaulics Hay, so i'm using 32 bit timer with PLL Clock = 80 Mhz as source => I will have a tick every 0.0125 us. And the overflow will be 4294967295 * 0.0125μs = 53.6 sec wich is very big and seems illogic to me. What do you think ? thanks Dec 27, 2016 at 1:49
• @Frankenstein If it is really a 32 bit timer with 80 MHz then it is correct. It counts over 4 billion which takes time. Dec 27, 2016 at 9:24
• @BenceKaulics Thanks. However, while measuring time I get 280 ticks wich will give us 3.5 uSeconds. However using a GHS Utility : 'target timer' it gives 20 uSeconds (This mean every tick is 0.0714 uS => 1/x = 14 Mhz : weird : ). So my calculus are not correct bu i'm trying to find why ??!! I'm also using 1 as prescaler so it's 80Mhz not less. Even if I'm using 2,4,8 prescaler I would have 40Mhz, 20Mhz ... So what do you think ? many thanks to you Jan 7, 2017 at 12:23
• @Frankenstein This problem worth posting a new question and include a link to this question. It is starting to get too complicated to discuss in comments and also some other user can contribute in case a new question. Jan 7, 2017 at 12:25
• @BenceKaulics I'm afraid I can't. I'm using an automotive MCU where the datasheet / code samples are not available in public. However I will include some part of the code. Jan 7, 2017 at 16:05

## 1. instruction clock / system clock frequency

the instruction clock frequency is different on different micro controller families. a PIC micro controller has a instruction clock of 1/4 the clock frequency. and every (mostly) instruction (except goto) takes 1 instruction clock cycle, goto takes 2 cycles.

on an atmel µC the instruction clock is the same as the clock frequency.

## 2. other clock for only one timer

on various µC you can set a bunch of dividers and overflow settings please check the data sheet of your specific µC on a nearly every PIC you can set dividers like 1/8, so every 8 instruction clock cycle the timer increases with one. so the timer runs at 1MHz if your instruction Clock is 8MHz (oscillator running at 32MHz)

or a divider of 1/2 so your timer runs @ 1MHz if, system clock @ 2MHz and oscillator @8MHz

## 3. low power mode

some µC have multiple internal oscillators e.g. : a 8MHz a 4MHz and a 32.786 kHz and if you switch between those frequency's you can lower the power consumption. but you change the instruction clock and thus also the timer

• This is quite confusing because you have used quotes - could you edit and remove these? Apr 20, 2015 at 9:43

That might be depends on prescaler Register. The purpose of the prescaler is to allow the timer to be clocked at the rate you desire. For shorter (8 and 16-bit) timers, there will often be a tradeoff between resolution (high resolution requires a high clock rate) and range (high clock rates cause the timer to overflow more quickly).

For example, you cannot (without some tricks) get 1us resolution and a 1sec maximum period using a 16-bit timer. If you want 1us resolution you are limited to about 65ms maximum period. If you want 1sec maximum period, you are limited to about 16us resolution. The prescaler allows you to juggle resolution and maximum period to fit your needs.

Follow the Datasheets of controller which you are using. Go through the Timer Register. Some Controller will have fixed Value(Not modifiable by Programmer) prescaler and in some controller it can be modifiable