Electronic – How to calculate the time of one single tick on microcontroller

clockclock-speedmicrocontrollertimer

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.

Best Answer

To answer your first question.

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.