Bird
Raised Fist0
Embedded Cprogramming~20 mins

Input capture mode in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Input Capture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this input capture code snippet?

Consider the following embedded C code that captures input from a timer input capture register. What will be the printed output?

Embedded C
#include <stdio.h>

volatile unsigned int IC_Value1 = 0;
volatile unsigned int IC_Value2 = 0;
volatile unsigned char CaptureDone = 0;

void InputCapture_ISR(void) {
    static unsigned char count = 0;
    if(count == 0) {
        IC_Value1 = 1000; // Simulated capture value
        count++;
    } else if(count == 1) {
        IC_Value2 = 3000; // Simulated capture value
        CaptureDone = 1;
        count = 0;
    }
}

int main() {
    InputCapture_ISR();
    InputCapture_ISR();
    if(CaptureDone) {
        printf("Pulse width: %u\n", IC_Value2 - IC_Value1);
    } else {
        printf("Capture not done\n");
    }
    return 0;
}
APulse width: 2000
BPulse width: 4000
CCapture not done
DPulse width: 3000
Attempts:
2 left
💡 Hint

Look at how the ISR updates the capture values and when the flag is set.

🧠 Conceptual
intermediate
1:30remaining
Which statement about input capture mode is correct?

Choose the correct statement about input capture mode in embedded systems.

AInput capture mode is used to output analog voltages from a microcontroller.
BInput capture mode generates PWM signals automatically without software intervention.
CInput capture mode measures the time between two external events using a timer.
DInput capture mode disables all interrupts during operation.
Attempts:
2 left
💡 Hint

Think about what input capture mode is designed to measure.

🔧 Debug
advanced
2:00remaining
What error does this input capture code cause?

Analyze the following code snippet. What error will occur when it runs?

Embedded C
volatile unsigned int IC_Value1 = 0;
volatile unsigned int IC_Value2 = 0;
volatile unsigned char CaptureDone = 0;

void InputCapture_ISR(void) {
    static unsigned char count = 0;
    if(count == 0) {
        IC_Value1 = 1000;
        count++;
    } else if(count == 1) {
        IC_Value2 = 3000;
        CaptureDone = 1;
        count = 0;
    }
}

int main() {
    InputCapture_ISR();
    InputCapture_ISR();
    if(CaptureDone) {
        printf("Pulse width: %u\n", IC_Value2 - IC_Value1);
    } else {
        printf("Capture not done\n");
    }
    return 0;
}
AThe code causes an infinite loop due to incorrect count increment.
BThe condition 'if(count = 0)' assigns zero instead of comparing, so IC_Value1 is never set properly.
CThe code runs correctly and prints 'Pulse width: 2000'.
DThe code causes a compilation error due to missing semicolons.
Attempts:
2 left
💡 Hint

Check the if condition inside the ISR carefully.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this input capture code?

Identify the correct fix for the syntax error in this input capture code snippet.

Embedded C
volatile unsigned int IC_Value1 = 0;
volatile unsigned int IC_Value2 = 0;
volatile unsigned char CaptureDone = 0;

void InputCapture_ISR(void) {
    static unsigned char count = 0;
    if(count == 0) {
        IC_Value1 = 1000;
        count++;
    } else if(count == 1) {
        IC_Value2 = 3000;
        CaptureDone = 1;
        count = 0;
    }
}
AChange 'void InputCapture_ISR(void)' to 'void InputCapture_ISR()'
BReplace 'volatile' with 'static' for IC_Value1 and IC_Value2
CRemove the else if block entirely
DAdd a semicolon after 'static unsigned char count = 0;'
Attempts:
2 left
💡 Hint

Look for missing punctuation in variable declarations.

🚀 Application
expert
2:30remaining
How many valid pulse widths can this input capture code measure?

Given the following input capture ISR that stores two capture values before setting a done flag, how many distinct pulse widths can be measured before the flag resets?

Embedded C
volatile unsigned int IC_Value1 = 0;
volatile unsigned int IC_Value2 = 0;
volatile unsigned char CaptureDone = 0;

void InputCapture_ISR(void) {
    static unsigned char count = 0;
    if(count == 0) {
        IC_Value1 = TIMER_REG;
        count++;
    } else if(count == 1) {
        IC_Value2 = TIMER_REG;
        CaptureDone = 1;
        count = 0;
    }
}
AOnly one pulse width can be measured before CaptureDone resets.
BTwo pulse widths can be measured before CaptureDone resets.
CAn unlimited number of pulse widths can be measured without resetting CaptureDone.
DNo pulse widths can be measured because count never increments.
Attempts:
2 left
💡 Hint

Consider how the count variable and CaptureDone flag control measurement cycles.

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

  1. Step 1: Understand Input Capture Mode function

    Input Capture Mode records the timer count when an input event (like a signal edge) happens.
  2. 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.
  3. Final Answer:

    To record the timer value when a specific input event occurs -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Set the capture edge select bit to 1 -> Option C
  4. Quick Check:

    Edge select = 1 for rising edge [OK]
Hint: Rising edge capture usually means edge bit = 1 [OK]
Common Mistakes:
  • Confusing edge select bit values
  • Trying to clear timer instead of setting edge
  • Enabling unrelated PWM mode
3. Given this code snippet for input capture:
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?
medium
A. 1500
B. 0
C. Timer overflow value
D. Undefined

Solution

  1. Step 1: Understand timer and capture behavior

    The timer increments every 1 microsecond, so at 1500us the timer count is 1500.
  2. Step 2: ISR captures timer value on input event

    The ISR stores the current timer count (1500) into captured_value.
  3. Final Answer:

    1500 -> Option A
  4. 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

  1. Step 1: Analyze variable scope in ISR

    The variable value is local to ISR and lost after ISR finishes.
  2. Step 2: Consider data usage outside ISR

    If the captured value is needed elsewhere, it should be stored in a global or volatile variable.
  3. Final Answer:

    The captured value is stored in a local variable and lost after ISR ends -> Option D
  4. 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

  1. Step 1: Understand pulse width measurement

    Pulse width is the time between rising and falling edges of a signal.
  2. 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.
  3. Final Answer:

    Capture timer value on rising edge, then capture again on falling edge; subtract first from second -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard