Consider the following embedded C code that captures input from a timer input capture register. What will be the printed output?
#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; }
Look at how the ISR updates the capture values and when the flag is set.
The ISR simulates two captures: first at 1000, second at 3000. The pulse width is the difference: 3000 - 1000 = 2000.
Choose the correct statement about input capture mode in embedded systems.
Think about what input capture mode is designed to measure.
Input capture mode records the timer value when an external event occurs, allowing measurement of time intervals.
Analyze the following code snippet. What error will occur when it runs?
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; }
Check the if condition inside the ISR carefully.
The condition uses assignment '=' instead of comparison '==', so it always sets count to 0 and the first block always runs, preventing proper capture.
Identify the correct fix for the syntax error in this input capture code snippet.
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; } }
Look for missing punctuation in variable declarations.
The declaration of 'count' is missing a semicolon, causing a syntax error.
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?
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; } }
Consider how the count variable and CaptureDone flag control measurement cycles.
The ISR captures two values per cycle and sets CaptureDone once. After that, it resets count, so only one pulse width is measured per CaptureDone flag set.