Build note
Timers: accurate time without delay loops
_delay_ms() burns CPU cycles. A timer counts on its own, in hardware, while the
CPU does something else. It can interrupt on a schedule or reset the chip when the
program stops responding.
A timer is a clock hand that ticks by itself. You choose how fast it moves, and you decide what happens when it reaches a particular mark.
Three timers, one idea
The ATmega328P has three independent timer/counter units:
| Timer | Width | Counts to | Typical use |
|---|---|---|---|
| Timer0 | 8-bit | 255 | Millisecond timekeeping, simple PWM |
| Timer1 | 16-bit | 65535 | Servo pulses, precise PWM |
| Timer2 | 8-bit | 255 | PWM, can use a watch crystal |
Each one has a control register for the mode and output behaviour, a second one for the clock source, the live count, a compare value, and an interrupt enable. The names change per timer; the jobs do not.
The prescaler decides the tick
CS02:0 selects how much the system clock is divided before it reaches the
counter.
CS |
Division | Tick at 16 MHz |
|---|---|---|
001 |
1 | 62.5 ns |
010 |
8 | 0.5 us |
011 |
64 | 4 microseconds |
100 |
256 | 16 microseconds |
101 |
1024 | 64 microseconds |
The arithmetic that follows is one line:
tick period = prescaler / F_CPU
At 16 MHz with a prescaler of 64, one tick is 4 microseconds. That is the setting the examples use.
CTC versus overflow
There are two ways to get a periodic interrupt, and the difference matters.
Normal mode lets the counter run to 255 and overflow. The period is fixed by the width and the prescaler. With Timer0, 256 ticks at 4 microseconds is 1.024 ms, not 1 ms. The example’s comment says exactly that, and the code counts 64 overflows to get roughly 65.5 ms.
CTC mode resets the counter when it matches OCR0A. The period can be tuned
in single ticks:
TCCR0A |= (1 << WGM01); // CTC
TCCR0B |= (1 << CS01) | (1 << CS00); // /64, 4 us per tick
OCR0A = 249; // (249 + 1) ticks = 1 ms
TIMSK0 |= (1 << OCIE0A);
sei();
In CTC the interrupt period is (OCR0A + 1) ticks, which is why the value is 249
rather than 250. That off-by-one is easy to miss and shows up as a period that is
always slightly wrong.
The ISR then does the work:
volatile uint64_t ticks = 0;
ISR(TIMER0_COMPA_vect)
{
if (++ticks >= 500) {
ticks = 0;
PORTB ^= (1 << PB1);
}
}
volatile is not optional. Without it the compiler is free to keep the counter in
a register and the main loop may never see the change.
One thing to watch on an 8-bit chip: a 64-bit counter costs several instructions per increment. A 16-bit or 32-bit counter is usually enough unless you are tracking hours.
The watchdog is a different animal
The watchdog timer is not one of the three. It runs from its own oscillator and its only job is to reset the chip if software stops petting it.
wdt_enable(WDTO_1S);
while (1) {
do_work();
wdt_reset(); // I am alive
}
If the loop hangs, the watchdog resets the chip after a second. The example simulates that: it blinks ten times while resetting the watchdog, then stops resetting and lets the reset happen. On the next boot, three quick flashes show that the chip restarted.
Two details make watchdog code reliable:
- Disable it at startup. Some bootloaders leave the watchdog enabled, which
can put the chip in a reset loop before your code ever runs.
MCUSR = 0; wdt_disable();at the top ofmainclears that. - Reset it on every path. A long operation with no
wdt_reset()is a false hang.
When to use which
- A periodic task: CTC timer interrupt.
- A one-off delay that does not block: start a timer and check its flag.
- A stuck program: watchdog.
- Precise waveforms: Timer1 in a PWM mode, which is the next part.
How this was made
- Written by: Rupesh, from his own
c-05-timernotes and code, with AI drafting and editing. - Numbers: from the ATmega328P datasheet and the tick arithmetic.
- AI-assisted sections: structure and copy editing.