Introduction
Input capture mode helps you measure the exact time when an event happens on a pin, like when a button is pressed.
void InputCapture_Init(void) {
// Configure timer for input capture
// Set capture edge (rising/falling)
// Enable input capture interrupt
}
void InputCapture_ISR(void) {
// Read captured timer value
// Process the captured time
// Clear interrupt flag
}void InputCapture_Init(void) {
// Set timer prescaler
// Configure input capture on channel 1
// Capture on rising edge
// Enable interrupt
}void InputCapture_ISR(void) {
uint16_t capturedValue = TIMER_CAPTURE_REGISTER;
// Use capturedValue to calculate pulse width
CLEAR_INTERRUPT_FLAG();
}#include <stdint.h> #include <stdio.h> volatile uint16_t captured_time = 0; // Simulated timer capture register uint16_t TIMER_CAPTURE_REGISTER = 1234; // Simulated interrupt flag int INTERRUPT_FLAG = 0; void InputCapture_Init(void) { // Normally hardware setup here printf("Input capture initialized.\n"); } void InputCapture_ISR(void) { captured_time = TIMER_CAPTURE_REGISTER; INTERRUPT_FLAG = 0; // Clear interrupt printf("Captured time: %u\n", captured_time); } int main(void) { InputCapture_Init(); // Simulate an input capture event INTERRUPT_FLAG = 1; if (INTERRUPT_FLAG) { InputCapture_ISR(); } return 0; }
Input Capture Mode in embedded C timer modules?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?captured_value.void ISR_InputCapture() {
uint16_t value = TIMER_CAPTURE_REG;
process(value);
CLEAR_CAPTURE_INTERRUPT_FLAG();
}
What is the main problem?value is local to ISR and lost after ISR finishes.Pass the test and this pattern moves unknown