Skip to content

HardwareTimer - is there an Interrupt if it overflows? #37

@SunboX

Description

@SunboX

Hi,

I have an external event (clock) source. I want to count the pulses (with a duty cycle of 1.25us). Is there an interrupt that fires if the counter overflowes?
This is, what I tried so far:

#include <Arduino.h>

#define pin PA8

uint32_t channel;
static HardwareTimer *MyTim;
static volatile uint32_t timestamp;

void InputCapture_IT_callback(void)
{
  timestamp = micros();
}

void Rollover_IT_callback(void)
{
  digitalWriteFast(PB_4, HIGH);
  Serial.println("rollover");
  MyTim->setCount(0);
  MyTim->pause();
}

void setup()
{
  Serial.begin(115200);

  pinMode(PB4, OUTPUT);

  pinMode(pin, INPUT_PULLUP);
  digitalWrite(pin, HIGH);

  // Automatically retrieve TIM instance and channel associated to pin
  // This is used to be compatible with all STM32 series automatically.
  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->pause();
  MyTim->setMode(channel, TIMER_INPUT_CAPTURE_FALLING, pin);
  MyTim->setPrescaleFactor(1);

  MyTim->setOverflow(288, TICK_FORMAT); // count up to 288 bits

  MyTim->attachInterrupt(channel, InputCapture_IT_callback);
  MyTim->attachInterrupt(Rollover_IT_callback);
  MyTim->setCount(0);
  MyTim->refresh();
  MyTim->resume();
}

void loop()
{
  uint32_t timeDiff = micros() - timestamp;

  if (timeDiff > 300) // 300us -> reset
  {
    MyTim->setCount(0);
    MyTim->refresh();
    MyTim->resume();
    
    digitalWriteFast(PB_4, LOW);
  }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions