Input capture mode in Embedded C - Time & Space Complexity
We want to understand how the time taken by input capture mode changes as the amount of input grows.
How does the program's work increase when it waits for and processes more input data?
Analyze the time complexity of the following code snippet.
void capture_input(char *buffer, int max_length) {
int i = 0;
char c;
while(i < max_length - 1) {
c = getchar();
if(c == '\n' || c == EOF) {
break;
}
buffer[i++] = c;
}
buffer[i] = '\0';
}
This code reads characters one by one from input until it reaches the maximum length or a newline/end signal.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop reading each character from input.
- How many times: Up to max_length - 1 times, depending on input.
As the allowed input size grows, the program reads more characters one by one.
| Input Size (max_length) | Approx. Operations (characters read) |
|---|---|
| 10 | Up to 9 characters |
| 100 | Up to 99 characters |
| 1000 | Up to 999 characters |
Pattern observation: The number of operations grows roughly in direct proportion to the input size.
Time Complexity: O(n)
This means the time to capture input grows linearly with the number of characters allowed.
[X] Wrong: "The input capture always takes the same time no matter how much input is allowed."
[OK] Correct: The loop reads each character until it reaches the limit or newline, so more allowed input means more reads and more time.
Understanding how input reading scales helps you explain how programs handle user data efficiently and respond quickly.
"What if we changed the input capture to stop only on a special character instead of max length? How would the time complexity change?"
Practice
Input Capture Mode in embedded C timer modules?Solution
Step 1: Understand Input Capture Mode function
Input Capture Mode records the timer count when an input event (like a signal edge) happens.Step 2: Compare options with this function
Only To record the timer value when a specific input event occurs describes recording the timer value on an input event, which matches the purpose.Final Answer:
To record the timer value when a specific input event occurs -> Option AQuick Check:
Input Capture = Record timer on event [OK]
- Confusing Input Capture with PWM generation
- Thinking it resets the timer automatically
- Assuming it outputs signals instead of capturing
Solution
Step 1: Identify capture edge select bit meaning
Usually, setting the capture edge select bit to 1 configures capture on rising edge; 0 is for falling edge.Step 2: Match options with rising edge setting
Set the capture edge select bit to 1 sets the bit to 1, which is correct for rising edge trigger.Final Answer:
Set the capture edge select bit to 1 -> Option CQuick Check:
Edge select = 1 for rising edge [OK]
- Confusing edge select bit values
- Trying to clear timer instead of setting edge
- Enabling unrelated PWM mode
volatile uint16_t captured_value = 0;
void ISR_InputCapture() {
captured_value = TIMER_CAPTURE_REG;
CLEAR_CAPTURE_INTERRUPT_FLAG();
}
// Assume timer increments every 1us
// Input event occurs at 1500us
What will be the value of captured_value after the input event?Solution
Step 1: Understand timer and capture behavior
The timer increments every 1 microsecond, so at 1500us the timer count is 1500.Step 2: ISR captures timer value on input event
The ISR stores the current timer count (1500) intocaptured_value.Final Answer:
1500 -> Option AQuick Check:
Timer count at event = captured value = 1500 [OK]
- Assuming captured value resets to zero
- Confusing overflow with current count
- Ignoring ISR capture assignment
void ISR_InputCapture() {
uint16_t value = TIMER_CAPTURE_REG;
process(value);
CLEAR_CAPTURE_INTERRUPT_FLAG();
}
What is the main problem?Solution
Step 1: Analyze variable scope in ISR
The variablevalueis local to ISR and lost after ISR finishes.Step 2: Consider data usage outside ISR
If the captured value is needed elsewhere, it should be stored in a global or volatile variable.Final Answer:
The captured value is stored in a local variable and lost after ISR ends -> Option DQuick Check:
Local variable in ISR = lost data [OK]
- Clearing interrupt flag too early
- Not enabling timer before capture
- Avoiding function calls in ISR (not always wrong)
Solution
Step 1: Understand pulse width measurement
Pulse width is the time between rising and falling edges of a signal.Step 2: Use input capture on both edges
Capture timer value at rising edge (start) and falling edge (end), then subtract to get pulse width.Final Answer:
Capture timer value on rising edge, then capture again on falling edge; subtract first from second -> Option BQuick Check:
Pulse width = falling edge time - rising edge time [OK]
- Capturing only one edge and guessing width
- Resetting timer incorrectly
- Ignoring one edge of the pulse
Ready to prove it?
Pass the test and this pattern moves unknown
