📖 Scenario: You are working with a microcontroller that needs to measure the time between two events using input capture mode. This is common in real-world applications like measuring the speed of a rotating wheel or the duration of a pulse signal.
🎯 Goal: Build a simple embedded C program that sets up input capture mode on a timer, captures the input event time, and stores the captured value.
📋 What You'll Learn
Create a variable to store the captured timer value
Configure the timer input capture mode
Write an interrupt service routine to read the captured value
Print the captured value to the console (simulate with a print statement)
💡 Why This Matters
🌍 Real World
Input capture mode is used in embedded systems to measure time intervals precisely, such as speed sensors, pulse width measurement, and frequency counting.
💼 Career
Understanding input capture mode is important for embedded software engineers working with microcontrollers in automotive, robotics, and industrial automation.
Progress0 / 4 steps
1
Create a variable to store the captured timer value
Create an unsigned int variable called captured_value and initialize it to 0.
Embedded C
Hint
Use the syntax unsigned int captured_value = 0; to declare and initialize.
2
Configure the timer input capture mode
Write a function called setup_input_capture() that configures the timer for input capture mode. Inside the function, set a hypothetical register TIMER_ICR to 1 to enable input capture.
Embedded C
Hint
Define a function with void setup_input_capture() and inside set TIMER_ICR = 1;.
3
Write an interrupt service routine to read the captured value
Write an interrupt service routine function called input_capture_ISR(). Inside it, assign the value of a hypothetical register TIMER_ICV to the variable captured_value.
Embedded C
Hint
Define void input_capture_ISR() and inside assign captured_value = TIMER_ICV;.
4
Print the captured value
Write a main() function that calls setup_input_capture(), simulates an input capture event by setting TIMER_ICV = 1234;, calls input_capture_ISR(), and then prints the captured_value using printf.
Embedded C
Hint
Remember to include stdio.h and use printf to display the captured value.
Practice
(1/5)
1. What is the main purpose of Input Capture Mode in embedded C timer modules?
easy
A. To record the timer value when a specific input event occurs
B. To generate PWM signals automatically
C. To reset the timer periodically
D. To output a fixed frequency signal
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 A
Quick Check:
Input Capture = Record timer on event [OK]
Hint: Input Capture records timer on input events [OK]
Common Mistakes:
Confusing Input Capture with PWM generation
Thinking it resets the timer automatically
Assuming it outputs signals instead of capturing
2. Which of the following is the correct way to configure an input capture to trigger on a rising edge in embedded C?
easy
A. Clear the timer counter register
B. Set the capture edge select bit to 0
C. Set the capture edge select bit to 1
D. Enable the PWM output mode
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 C
Quick Check:
Edge select = 1 for rising edge [OK]
Hint: Rising edge capture usually means edge bit = 1 [OK]
What will be the value of captured_value after the input event?
medium
A. 1500
B. 0
C. Timer overflow value
D. Undefined
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) into captured_value.
Final Answer:
1500 -> Option A
Quick Check:
Timer count at event = captured value = 1500 [OK]
Hint: Captured value equals timer count at event time [OK]
Common Mistakes:
Assuming captured value resets to zero
Confusing overflow with current count
Ignoring ISR capture assignment
4. Identify the error in this input capture interrupt service routine:
void ISR_InputCapture() {
uint16_t value = TIMER_CAPTURE_REG;
process(value);
CLEAR_CAPTURE_INTERRUPT_FLAG();
}
What is the main problem?
medium
A. The process function should not be called inside ISR
B. The interrupt flag is cleared before reading the capture register
C. The ISR does not enable the timer
D. The captured value is stored in a local variable and lost after ISR ends
Solution
Step 1: Analyze variable scope in ISR
The variable value is 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 D
Quick Check:
Local variable in ISR = lost data [OK]
Hint: Store captured data in global variable, not local [OK]
Common Mistakes:
Clearing interrupt flag too early
Not enabling timer before capture
Avoiding function calls in ISR (not always wrong)
5. You want to measure the pulse width of a signal using input capture mode on a timer. Which approach correctly uses input capture to find the pulse width?
hard
A. Reset timer on every input event and read timer after fixed delay
B. Capture timer value on rising edge, then capture again on falling edge; subtract first from second
C. Capture timer value only on rising edges and multiply by 2
D. Use input capture only on falling edges and ignore rising edges
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 B
Quick Check:
Pulse width = falling edge time - rising edge time [OK]
Hint: Measure pulse by capturing rising and falling edges [OK]
Common Mistakes:
Capturing only one edge and guessing width
Resetting timer incorrectly
Ignoring one edge of the pulse
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.