Skip to main content
Visitor II
September 14, 2023
Solved

Using TIM1 To Count External Pulses On STM32H563

  • September 14, 2023
  • 4 replies
  • 1819 views

I am playing with the TIM_InputCapture example (calculates frequency of external pulse) for the NUCLEO-H563ZI dev board. The example works OK except:

  • Frequency is not updated for no edges (signal generator off)
  • Minimum frequency is about 3KHz. I need to measure 0 - 2KHz

There should be a way to trigger the latching of the count reg in SW rather than the timer. The pulse frequency is low enough that I can look at the pulse count in non-ISR code. 

How do I (a) trigger the latching of the count reg, and (b) disable the timer event?

    This topic has been closed for replies.
    Best answer by waclawek.jan

     

     

    > There should be a way to trigger the latching of the count reg in SW rather than the timer. 

    Why? Just read out TIMx_CNT.

    JW

    4 replies

    Super User
    September 14, 2023

     

     

    > There should be a way to trigger the latching of the count reg in SW rather than the timer. 

    Why? Just read out TIMx_CNT.

    JW

    mccabehmAuthor
    Visitor II
    September 14, 2023

    The CNT reg would continue to increment while I read the time. I would like to (a) disable interrupts, (b) latch the CNT reg, (c) read the time, (d) re-enable interrupts, (e) read the latched count.

    Also, my attempts at reading the CNT reg yield unexpected results. The differences in the count compared to the input frequency and the time interval -- don't correlate at all.

    Super User
    September 14, 2023

    The input capture function does this. It stores the value of CNT at the time of the trigger. You can't get more accurate than that. Your method would be subject to IRQ and software delays.

    Super User
    September 15, 2023

    Any software method is subject to IRQ and software delays, regardless of whether you disable/enable interrupts around it.

    Reading out TIMx_CNT is a simple and short operation, you can't force capture in software any more effectively.

    While you did not tell us any relevant details - which STM32, which timer, what clocks - your problems probably stem from timer having a limited overall period, probably you are using a 16-bit timer. Calculate how fast will it oferflow; try 32-bit timer, try using appropriate prescalers. Alternatively, you could count timer overflows in interrupt and factor them into your frequency calculation, but this is tricky and I recommend to avoid it.

    You should also have some sort of timeout. That establishes a minimum input signal frequency. There is no such thing as measuring 0Hz input; to measure it precisely you would have to wait infinite time.

    JW