Subsection 8.1.1 The Cost of Waiting
Waiting has been built into every program in this course so far. In your first blinky the delay was a busy-wait loop, a
for loop that burned processor cycles doing nothing, and you then replaced it with the course’s delay_ms(), which fixed the busy-wait’s imprecision (Subsection 1.4.2). What delay_ms() did not fix is that the CPU can do nothing else while it runs: the processor is committed to the delay for its whole duration. A program that calls delay_ms(1000) is unresponsive for one full second.
You have felt this cost. In the Lab 2 button-race game, the countdown flashes were produced by delays, and while the code sat inside a delay, it could not watch the buttons, so a press during the countdown could slip past unregistered. The polling loops of Days 3 and 4 have the mirror-image problem: the CPU is fully awake, but it spends its time asking “anything yet?” over and over, and every additional job added to the loop makes every other job’s response slower.
What we actually want is to separate keeping time from doing work. Let a dedicated piece of hardware count the milliseconds; let the CPU compute, print, and respond; and give the hardware a way to tell the CPU when the time has come. The first half of that bargain is the hardware timer.
