Capturing a PWM pulse using timers?
Posted: Mon Sep 04, 2023 7:28 pm
I am trying to capture the input of an RC receiver using an STM32F411CE board. I have checked that the input is correct and that it is being read at the pin I'm attaching, however the interrupt routine is never getting called, even though the pulse is being generated at the correct pin, the code I'm using is as follows, based off the example in the stm32duino github.
I've been trying this for a while and the output is always 0 (captures only occur once and never update). What am I doing wrong here? (P.S am noob, please help)
Code: Select all
#define pin PA15
bool flag = true;
uint32_t channel;
volatile uint32_t t, LastCapture = 0, CurrentCapture;
HardwareTimer *MyTim;
void InputCapture_IT_callback(void)
{
pinMode(PA15,OUTPUT);
CurrentCapture = MyTim->getCaptureCompare(channel);
if (flag==true) {
LastCapture = CurrentCapture;
MyTim->setMode(channel,TIMER_INPUT_CAPTURE_FALLING,pin);
flag=false;
}
else{
if (CurrentCapture > LastCapture) {
t = (CurrentCapture - LastCapture);
}
else{
t = (0xFFFF + CurrentCapture - LastCapture);
}
MyTim->setMode(channel,TIMER_INPUT_CAPTURE_RISING,pin);
flag = true;
}
}
void setup()
{
Serial.begin(115200);
TIM_TypeDef *Instance = (TIM_TypeDef *)pinmap_peripheral(digitalPinToPinName(pin), PinMap_PWM);
channel = STM_PIN_CHANNEL(pinmap_function(digitalPinToPinName(pin), PinMap_PWM));
MyTim = new HardwareTimer(Instance);
MyTim->setMode(channel, TIMER_INPUT_CAPTURE_RISING, pin);
uint32_t PrescalerFactor = 1;
MyTim->setPrescaleFactor(PrescalerFactor);
MyTim->attachInterrupt(channel, InputCapture_IT_callback);
Serial.println(channel);
}
void loop()
{
Serial.println((String)"Interval = " + t);
delay(1000);
}