Author Topic: Pulse Counter in STM32  (Read 5183 times)

0 Members and 1 Guest are viewing this topic.

Offline Md_AslamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Pulse Counter in STM32
« on: September 01, 2022, 08:38:50 am »
I am using the STM32 Nucleo-073RZ board to count the number of pulses provided on one of the GPIO pins of the STM32. I searched on the internet and found that using the Timer/Counter of STM32 will do this work but there is one problem, that is, I need to count pulses continuously say for 10 days and as my signal is of 10KHz so in 1 second STM32 will have 10000 values but timer available in the mentioned board is only 16-bit(65535). Hence I need two help:
1) How to start working with Timer/Counter for external pulses in STM32
2) How to count millions of pulses with 16-bit available Timer/Counter in STM32

Quick help would be highly appreciated
 

Offline janoc

  • Super Contributor
  • ***
  • Posts: 3958
  • Country: de
Re: Pulse Counter in STM32
« Reply #1 on: September 01, 2022, 10:54:09 am »
I am using the STM32 Nucleo-073RZ board to count the number of pulses provided on one of the GPIO pins of the STM32. I searched on the internet and found that using the Timer/Counter of STM32 will do this work but there is one problem, that is, I need to count pulses continuously say for 10 days and as my signal is of 10KHz so in 1 second STM32 will have 10000 values but timer available in the mentioned board is only 16-bit(65535). Hence I need two help:
1) How to start working with Timer/Counter for external pulses in STM32

It is quite well explained in the datasheet/reference guide for the chip.

2) How to count millions of pulses with 16-bit available Timer/Counter in STM32

Quick help would be highly appreciated

You can daisy-chain multiple counters on STM32 (depends on which counter and which chip - check the doc!). You can also always have an interrupt triggered on overflow where you advance your own in-memory counter, which can be as wide as you want. A 32bit variable + 16 bit hw counter will give you a 48bit counter - at 10kHz good for almost 900 years before it overflows.

16bit hardware counter and 10kHz signal that gives you one interrupt per 6.5 seconds or so, any microcontroller should be able to handle that with no issues.

This is completely bog standard approach on every MCU.
 

Offline Md_AslamTopic starter

  • Contributor
  • Posts: 24
  • Country: in
Re: Pulse Counter in STM32
« Reply #2 on: September 05, 2022, 07:45:38 am »
I am using the STM32 Nucleo-073RZ board to count the number of pulses provided on one of the GPIO pins of the STM32. I searched on the internet and found that using the Timer/Counter of STM32 will do this work but there is one problem, that is, I need to count pulses continuously say for 10 days and as my signal is of 10KHz so in 1 second STM32 will have 10000 values but timer available in the mentioned board is only 16-bit(65535). Hence I need two help:
1) How to start working with Timer/Counter for external pulses in STM32

It is quite well explained in the datasheet/reference guide for the chip.

2) How to count millions of pulses with 16-bit available Timer/Counter in STM32

Quick help would be highly appreciated

You can daisy-chain multiple counters on STM32 (depends on which counter and which chip - check the doc!). You can also always have an interrupt triggered on overflow where you advance your own in-memory counter, which can be as wide as you want. A 32bit variable + 16 bit hw counter will give you a 48bit counter - at 10kHz good for almost 900 years before it overflows.

16bit hardware counter and 10kHz signal that gives you one interrupt per 6.5 seconds or so, any microcontroller should be able to handle that with no issues.

This is completely bog standard approach on every MCU.

I got the first answer.
But I have little doubt in the second answer
As you said with a 32-bit variable and 16-bit counter I can achieve the 48-bit variable/counter, but there is no datatype in STM32 that can hold a variable value more than 32-bit so how I will be able to store it in some variable and say transfer with UART protocol to the PC.

PS: As far as I know max limit of datatype in stm32 is 32-bit, correct me if I am wrong.
 

Offline janoc

  • Super Contributor
  • ***
  • Posts: 3958
  • Country: de
Re: Pulse Counter in STM32
« Reply #3 on: September 05, 2022, 08:53:32 am »
I got the first answer.
But I have little doubt in the second answer
As you said with a 32-bit variable and 16-bit counter I can achieve the 48-bit variable/counter, but there is no datatype in STM32 that can hold a variable value more than 32-bit so how I will be able to store it in some variable and say transfer with UART protocol to the PC.

PS: As far as I know max limit of datatype in stm32 is 32-bit, correct me if I am wrong.

That is both wrong - and completely irrelevant.

You have the (u)int64_t type. That the CPU is 32 bit only means that the compiler will emit some library functions if you try to do some arithmetic with wider numbers (don't remember whether Cortex M has native 64bit instructions or not).

And it is irrelevant because you can always keep those 16 bits of the count stored inside of the timer/counter register. In the worst case you can always implement the necessary support yourself - arbitrary precision arithmetic is a thing.

This is pretty basic computer science stuff, IMO.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf