The simplest way to use input ports is polling,
which means that a program every so often
checks all ports of interest, like asking
"Do you have anything of interest for me?"
But actively polling ports means wasting time,
slowing down the program, as each check
consumes some lines of code.
Therefore, CPUs can set interrupt registers
which trigger some interesting actions when
their related input port sends data.
They unhinge and stop the program pointer
and save its current value in a special memory,
the interrupt stack, on the next free unused "slot".
Then they set the program pointer to their
programmed interrupt-handler address and
let the program pointer run again.
The interrupt (event) handler code works with
the input port data, and when it is done
(tech lingo: it served the interrupt),
it must set the return command.
Upon the return command, the program pointer
value last saved to the interrupt stack is loaded
as the current program pointer again,
the stack size is reduced by 1 and
the original program continues.
Now the program does not need to waste code
to poll all input lines of interest. It just tells them
once where their interrupt handler code is
(the program memory address), and that's it.
A special I/O group in this context implements
timers that trigger an event interrupt every
soandso many desired (programmed) clock cycles.
Like all interrupt registers, timers can also be
cleared by the program, for instance as the
first thing in a one-shot timer handler.
09. Interrupts and Timers