One of the easiest uses of an output-compare function is to produce a pulse of a specific
duration. First, a value corresponding to the leading edge of the pulse is written
to the output-compare register. The output compare is configured to automatically set
the corresponding output either high or low, depending on the polarity of the pulse being
produced. After this compare occurs, the output compare is reprogrammed to automatically
change the output pin back to its inactive level at the next compare. A value
corresponding to the width of the pulse is added to the original output-compare register
value, and this result is written to the output-compare register. Since the pin-state
changes occur automatically at specific values of the free-running counter, the pulse
width can be controlled accurately to the resolution of the free-running counter independent
of software latencies. By repeating the actions for generating pulses, an output
signal of a specific frequency and duty cycle can be generated.