0
0
Embedded Cprogramming~20 mins

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

Choose your learning style9 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.