0
0
Embedded Cprogramming~10 mins

Input capture mode in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Input capture mode
Timer running
Input signal edge detected
Capture timer value
Store captured value
Process or read captured value
Wait for next edge or event
The timer runs continuously until an input signal edge is detected, then the timer value is captured and stored for processing.
Execution Sample
Embedded C
volatile int captured_value = 0;
volatile int flag = 0;

void input_capture_ISR() {
  captured_value = TIMER_REG;
  flag = 1;
}

int main() {
  while(1) {
    if(flag) { process(captured_value); flag=0; }
  }
}
This code captures the timer value on an input event interrupt and processes it in the main loop.
Execution Table
StepEventTimer ValueCaptured ValueFlagAction
1Timer running1000-0Waiting for input edge
2Input edge detected1025-0ISR triggered
3ISR execution102510251Captured timer value, set flag
4Main loop check102510251Flag is set, process value
5Processing done102510250Flag cleared, wait next edge
6Timer running105010250Waiting for next input edge
💡 Loop runs indefinitely, capturing timer values on input edges
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6
Timer Value100010251025102510251050
Captured Value--1025102510251025
Flag001100
Key Moments - 2 Insights
Why does the captured value not change until the input edge occurs?
Because the timer runs continuously but the capture only happens inside the ISR when the input edge triggers it, as shown in steps 2 and 3 of the execution table.
Why do we use a flag variable after capturing the timer value?
The flag signals the main loop that a new captured value is ready to process, preventing missed or repeated processing. This is shown in steps 3 to 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the captured value immediately after the ISR executes?
A1000
B1025
C-
D1050
💡 Hint
Check the 'Captured Value' column at step 3 in the execution_table.
At which step does the flag get cleared?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Flag' column in the execution_table and see when it changes from 1 to 0.
If the input edge never occurs, what happens to the captured value?
AIt stays unchanged
BIt keeps updating with timer value
CIt resets to zero
DIt causes an error
💡 Hint
Refer to the variable_tracker for 'Captured Value' before any input edge event.
Concept Snapshot
Input capture mode uses a timer running continuously.
When an input signal edge occurs, the timer value is captured and stored.
An interrupt service routine (ISR) handles the capture.
A flag signals the main program to process the captured value.
This allows precise timing measurement of input events.
Full Transcript
Input capture mode works by running a timer continuously. When an input signal edge is detected, an interrupt triggers and captures the current timer value. This value is stored in a variable. A flag is set to tell the main program that a new captured value is ready. The main program then processes this value and clears the flag. The timer keeps running, waiting for the next input edge. This method helps measure the exact time when an input event happens.