In this short lecture, we will talk about one of the functionalities of a timer:
input capture.
As the name implies, input capture is to capture the timestamp of the occurrence of an event
in a digital signal.
The occurrence of an event is represented by a signal edge, including a rising edge,
a falling edge, or either a rising or falling edge.
By obtaining the timestamps of two events, software can calcuate the time duration.
Let's look at a simple example.
Suppose the auto-reload register (ARR) is set to 99, the timer counts up, and it only
captures rising edges of the external signal.
When the timer hardware detects a rising edge of the external signal, two important operations
are performed by the hardware automatically: first, the timer hardware copies the value
of the timer counter to the capture and compare register CCR.
Accordingly, hardware sets CCR to 30.
Second, the timer hardware generates an interrupt request, and the processor starts to execute
the corresponding interrupt handler ISR().
Typically, the interrupt handler reads the CCR register and saves the CCR value for future
reference.
When the next rising edge of the external signal arrives, the timer makes another capture,
and the hardware automatically performs these two operations again.
The timer hardware latches the timer counter into CCR, and generates an interrupt request.
By taking the difference of these two CCR values, software can calculate the time span
between these two rising edges.
If software configures the timer to capture both rising and falling edges, then the timer
can measure the duration when the external signal is high or low.
Similarly, if the timer is programmed to capture on only rising edges, or only falling edges,
then the timer can measure the period of the external signal.
The timer relies on a special hardware component, called edge detector, to trigger the capture.
Depending on the setting of the edge sensitivity, the capture can be triggered on a rising edge,
a falling edge, or either a rising or falling edge.
When a capture takes place, hardware automatically copies the value of the timer counter into
the CCR register.
After obtaining two CCR values, we can use this equation to calculate the time span.
Now, the question is: what is the time unit in that equation?
The answer is simple.
The time unit is the period of the timer's counter clock.
Let's do a quick review of the timer counter.
The timer counter is driven by the clock CNT.
Software can select a source clock, and slow down the source clock by a factor of PSC +1
For each cycle of the CNT clock, the counter is incremented or decremented by one, depending
on the counting mode.
For example, this diagram shows the counter clock and the counter values.
If the counter clock is 1KHz, then the time resolution of counting is 1 millisecond.
In summary, software can use these two equations to calculate the time duration.
The time duration equals to the difference of two captured CCR values, multiplied by
the counter clock period.
The timer hardware may offer some advanced functionalities.
For example, for debouncing and noise removal, the timer may include a programmable digital filter
This figure shows an example how a signal is filtered when the filter acceptance is 2.
A falling transition is confirmed only if two consecutive readings of the external signal
are low.
To reduce the processor's workload, a timer may also have a programmable input prescaler.
We can set the timer to make one capture every two events, every four events, and so on.
When the external signal has a high frequency, the input prescaler can reduce the number
of interrupts generated, and thus significantly decrease the workload on the processor.
Here is the diagram for input capture.
Software needs to program several timer registers to configure the filter duration or called
filter acceptance, the edge sensitivity, and the input prescaler.
The other settings are very similar to timer output compare.
Take a look at the previous lecture on timer output compare.
Here is a quiz: How does the software know a capture event has taken place?
The answer is via interrupts.
Therefore, we need to enable timer interrupt.
Software must perform two steps to enable an interrupt: first, program the peripheral
register to allow this peripheral to generate interrupt requests, and second, program the
NVIC controller to allow it to accept interrupt requests.
Here is the example code to enable and disable the timer interrupts.
If the external signal is connected to channel #1 of timer 4, software needs to set the channel
1 interrupt enable (CC1IE) bit of the DIER register to 1.
In addition, software also needs to program the NVIC controller to enable timer 4 interrupts.
Let's take another quiz.
For a timer, multiple timer events can generate an interrupt, such as the counter overflow
or underflow, the event on channel 1, the event on channel 2, and so on.
However, usually all interrupt events of a timer share the same interrupt handler.
Then, the question is: how does the software know which timer event has triggered the timer
interrupt?
The answer lies in the timer's status register.
This short code gives you a simple example of finding out what timer events have occurred.
In the timer interrupt handler, we test each flag bit of the timer's status register to
check whether the corresponding event has taken place.
One common mistake we often make in the interrupt handler is that software does not clear the
interrupt flag bits in the timer's status register.
When a specific event triggers the timer to make an interrupt request, hardware sets the
corresponding flag bit to 1 in the status register.
However, in the timer interrupt handler, software must to clear the flag bit to zero to avoid
repeatedly calling the interrupt handler.
Thank you for watching.
Please visit the book website for more examples.
See you next time.