Timer peripheral in microcontroller

Thread Starter

King2

Joined Jul 17, 2022
163
I'm sorry for not asking specific microcontroller related questions but I think the principle of timer is almost the same in all microcontrollers. I want to understand this basic principle of timer/counter in microcontroller.

Assume there is an 8 bit timer that starts at one and overflows to 255. It set the flag when it overflow.

My question is whether the microcontroller continuously increments the value of the timer or just checks that the timer flag is set and performs action if its set
 

ericgibbs

Joined Jan 29, 2010
18,766
Hi K2,
Typically, the Timers count Down from 255 to 0, the Flag sets at 0 and then Counts down 255 ... 0 repeating
The Timer can be preloaded with a Count which it counts down to zero.

The Timers have an Enable count control input pin.
E
 

bidrohini

Joined Jul 29, 2022
190
Timer register’s value increases/decreases automatically at a predefined rate (supplied by the user). That means, like MCU, the timer requires a clock in order to function. As each clock pulse increments the timer's counter by one.
 

DickCappels

Joined Aug 21, 2008
10,153
Timers for the last 30+ years have become very versatile with many modes of operation. Your choice will be limited by your choice of microcontroller.
 

MrChips

Joined Oct 2, 2009
30,708
How counter/timer module behaves varies from one MCU to another.
Some count down only, some count up, some count up and down. There is no standard mode of operation.

Some can count to a preset value and restart. Some can count up and down between two preset values.

Some count system clock pulses. Some can count external events.

Two very popular features are input capture and output compare. The first can be used to time external events. The latter is commonly used for frequency generation and PWM output.

Versatility is the common attribute.
 

boostbuck

Joined Oct 5, 2017
501
The timer operation occurs in the hardware, in the sense that it is 'below' the level of any user code. It is normally driven from the common clock with combinational logic (or microcode possibly) to control if the clock is active (counting is on or off), direction of counting, and the flag to indicate state such as zero crossing.
 

BobTPH

Joined Jun 5, 2013
8,809
In addition to all the other answers, the timer can generally interrupt at the overflow when enabled. This allows one to extend the time range by keeping a second counter and incrementing it on each interrupt.
 

Thread Starter

King2

Joined Jul 17, 2022
163
I will try to describe the basic of any Timer Module.

A basic 8-bit timer will count from 0 to 255

To get an interrupt when count is "5", pre-load the timer with a count of 255-5 (e.g., 250)

Preset the timer to 250

trigger an interrupt When the timer overflows from 255 to 0

Counter

250- ------------Pre-load
251
252
253
254
255
0 - ------------------interrupt

repeat process without stopping
 

MrChips

Joined Oct 2, 2009
30,708
I will try to describe the basic of any Timer Module.

A basic 8-bit timer will count from 0 to 255

To get an interrupt when count is "5", pre-load the timer with a count of 255-5 (e.g., 250)

Preset the timer to 250

trigger an interrupt When the timer overflows from 255 to 0

Counter

250- ------------Pre-load
251
252
253
254
255
0 - ------------------interrupt

repeat process without stopping
No. You cannot make a generalized assumption.
You need to define the make and model number of the MCU.
Every MCU is different. Even in one MCU there are different timer modules and modes of operation.
 
Top