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);
}