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.

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 obtained.

  • \$\begingroup\$ Why the hate against embedded LaTeX? \$\endgroup\$ – Mister Mystère Apr 20 '15 at 20:47
  • \$\begingroup\$ I did not have time to learn use it so far, or the fact that it exists on this site. \$\endgroup\$ – Bence Kaulics Apr 20 '15 at 21:18
  • 1
    \$\begingroup\$ +1 for the effort to improve! (good answer too) \$\endgroup\$ – Mister Mystère Apr 20 '15 at 23:12
  • 1
    \$\begingroup\$ @Frankenstein If it is really a 32 bit timer with 80 MHz then it is correct. It counts over 4 billion which takes time. \$\endgroup\$ – Bence Kaulics Dec 27 '16 at 9:24
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – The Beast Jan 7 '17 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

  • \$\begingroup\$ This is quite confusing because you have used quotes - could you edit and remove these? \$\endgroup\$ – David Apr 20 '15 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

I think this will answer for your 1st and 2nd Question

  • \$\begingroup\$ Does using different timers will affect measuring the elapsed time or it will be the same using any timer. Also we can improve precision by choosing higher n-bit timer and higher clock frequency in source ?. Thanks \$\endgroup\$ – The Beast Dec 27 '16 at 1:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.