Embedded systems

Timer / counter peripheral

A timer is a small hardware counter inside the MCU that ticks up automatically on every pulse of a clock, freeing the CPU from having to count time itself. Think of it as a metronome with a built-in tally counter: it clicks steadily on its own, and you can ask it to ring a bell (raise an interrupt) every time the count reaches a number you set. Because it runs in silicon parallel to your program, you can keep precise time, measure how long an event lasts, or generate a steady waveform while the processor gets on with other work or even goes to sleep.

The same block is called a 'counter' when you feed it external pulses instead of the system clock — count gear teeth flying past a sensor, revolutions of a wheel, or coins dropping. A prescaler divides the clock first (e.g. 16 MHz ÷ 64) so one timer can span microseconds to seconds. From this one peripheral you get the heartbeat tick of an RTOS, the timebase for PWM (compare the count to a threshold to set duty cycle), input capture (latch the exact count when a pin changes, to measure pulse width or frequency), and watchdog timers that reset a hung system. It is arguably the most-used peripheral in all of embedded design.

Never use a software delay loop (delay(500)) for anything that must be accurate or that has to run while other things happen — the loop hogs the CPU and drifts with clock speed. Hardware timers keep time independently and let the processor do real work meanwhile.

Also called
hardware timertimer/counter硬體計時器TIM