Timer
What is a Timer?
A timer is a peripheral circuit that measures time and generates notifications when a preset time has elapsed. Its basic function is similar to the timers found in household appliances.
A timer consists of three key components: a counter, data register, and comparator. The counter operates by receiving a clock signal and counting each cycle.
For example, with a clock frequency of 1Hz, the counter increments once per second. By reading the counter value, the elapsed time can be accurately determined.
Next, to track elapsed time, a data register and comparator work together within the timer.
The data register stores a predefined counter value corresponding to the desired notification time. As the counter increments, the comparator continuously compares the counter value with the value in the data register. When both values match, the comparator generates a notification, typically in the form of a timer interrupt, indicating that the set time has elapsed.
For example, if the counter value starts at 0 and the clock signal has a cycle of 1Hz, setting the data register value to 3 in the program and starting the counter will initiate the timing process. The comparator detects when the counter reaches 3, meaning 3 seconds have elapsed, then triggers a timer interrupt to notify that the time has passed.
In the previous example, time was measured in seconds, but for MCU-controlled devices, it is necessary to manage elapsed time in milliseconds or microseconds.
This can be achieved by increasing the clock signal frequency supplied to the counter.