cancel
Showing results for 
Search instead for 
Did you mean: 

Counter input capture

AKrup
Associate II

I'm trying to set up a counter to capture the period of an input signal. I'm getting lost in the different CCMR bits. If it possible to do the following:

  1. wait for a raising edge on an input.
  2. on the raising edge, start counter
  3. on the next raising edge
    1. record CNT (I can do that, in CCR)
    2. reset CNT to 0
    3. fire an interrupt
  4. for bonus points, if the leading edge fails to arrive by a certain count, reset CNT to that overshoot and fire an interrupt

(e.g., I'm expecting a raising edge every 10,000 ticks of the timer. If the timer goes to 12,000, I'm going to decide that the signal failed to arrive. I want to reset the counter to 2,000 and keep counting from there, as if input capture did happen. I can use a counter compare interrupt and set CNT manually, but I don't know how to account for the ticks that happen while I'm in the ISR)

2 REPLIES 2

Which STM32?

In older, like 'F1 or 'F4, can't do 2 directly and you'd need to employ DMA; in newer, like 'L4 or 'F7, you can do all. This requires not only to read the CCMR bits description, but also the SMCR bits description (and generally the whole timer chapter in RM); you are looking for Combined reset + trigger mode of the slave-mode controller (mind the discontinuous SMS bits).

For the bonus points you'd simply use a compare in a different channel and reset and stop the timer in its ISR.

JW

Thank you. So far, I've been developing this on an F030, but I'm OK moving up to a bigger part if I need to.