Abstract

Almost every microcontroller comes with several built-in timer/counters, and these are extremely useful to the embedded programmer - perhaps second in usefulness only to digital I/O or GPIO. The terminology timer/counter reflects the fact that the underlying counter hardware can usually be configured to count either regular clock pulses giving us a timer or irregular event pulses making it a counter.

These timer/counters are often referred to as “hardware timers” to distinguish them from software timers which are programs written to carry out some timing function by purely software means.

Hardware Integration:

Typically we talk about the timer “register” which is the place we load data into for specific counting on the timer. This counts as a peripheral register In addition to this, there is also the Control and Status registers that can act upon our Timer The bit size of the rest of the hardware has no impact nor correlation to the Timer’s bit size. Though you can in theory have a lower bit size timer than the rest of the system this is very rare and the majority are at least the same bit size if not more (Think 8 bit systems with 16bit timers)

The “Reload” Register affects what value that register reloads to when it reloads otherwise to 0\x0000

Interrupts:

Most timers provide functionality to provide an interrupt when it “overflows” and goes back to 0. This can be utilised by loading a specific value instead of the timer starting at 0, which then lets you have a very specific amount of time before your time hits the overflow, allowing for specific times to be counted.

Hardware Timers:

Typically these tend to be N-bit UP counters. However they can also be DOWN counters.

Example Timer Exam Question:

Question 1:

If an initial count of 1194 is loaded to the register, what is the period of the square wave generated at the output of the counter? Assume the clock frequency is 1.193 MHz and the counter countdowns by 2 for each clock cycle.

Number of clock cycles for state change of wave (i.e T/2), is which means that the time for the full wave is so with that we cam calculate

$T_{clock} = 1/f = \frac{1}{1.193*10^6}=0.84\micro S$
$\therefore T=0.84*1194=1*10^{-3}=1ms$ 

Question 2:

Generate a wave of 91hz from a timer with frequency of 12MHz count