Pulse Counter
Pulse Counter
Tell me how to count pulses in hardware so that when the value X is reached, an event/interrupt is executed
Re: Pulse Counter
There's an example in STM examples how to use timers in input capture mode.
File->Examples->STM32duino Examples->Peripherals->HardwareTimer
File->Examples->STM32duino Examples->Peripherals->HardwareTimer
Re: Pulse Counter
I don't see that the HardwareTimer input capture is a great help by itself. You could use that to set up a callback, but you would have to process one callback/interrupt for each cycle of the input. That could be OK for frequencies into kHz range, but just using standard Arduino attachInterrupt() could do the same while being more portable.
Perhaps you could get the HarwareTimer library to do the basic timer setup, attach a callback for the update event interrupt, and then override the mode etc for external clock on ETR or TI1/TI2 inputs. I would probably just change the timer registers directly for that, I don't see huge advantage in adding extra layers of abstraction.
Perhaps you could get the HarwareTimer library to do the basic timer setup, attach a callback for the update event interrupt, and then override the mode etc for external clock on ETR or TI1/TI2 inputs. I would probably just change the timer registers directly for that, I don't see huge advantage in adding extra layers of abstraction.
Re: Pulse Counter
The task is, pulses come from the sensor, pulses can come with different frequencies. The fastest are 1413 Hz, the slowest are 0.1 Hz. Although this is not the limit.
It is necessary to know the presence of a pulse, as well as the pulse time. In extreme cases, the total time in which X pulses arrived. (Or vice versa).
In Cube MX, select Clock Source - Internal Clock or ETR2. ETR2 is an external signal, which is what I need
It is necessary to know the presence of a pulse, as well as the pulse time. In extreme cases, the total time in which X pulses arrived. (Or vice versa).
In Cube MX, select Clock Source - Internal Clock or ETR2. ETR2 is an external signal, which is what I need
Re: Pulse Counter
Originally you said you wanted to get notified only after some number 'X' of pulses had been counted, but now you call that an "extreme case".
If "X" is much greater than 1, it might make sense to set up a timer for external clock, and have it generate an interrupt when X pulses have been counted.
On the other hand, if you want to measure individual pulse durations, perhaps look for an example showing how to use input capture, as suggested by GonzoG.
If "X" is much greater than 1, it might make sense to set up a timer for external clock, and have it generate an interrupt when X pulses have been counted.
On the other hand, if you want to measure individual pulse durations, perhaps look for an example showing how to use input capture, as suggested by GonzoG.
Re: Pulse Counter
You didn't understand me.
We need to count the pulses:
1. Either interrupt every 10th pulse and get the time during which the pulses arrived. But if the pulse didn't arrive, then we won't know the number
2. Or interrupt every 1 second and get the number of pulses. But then we don't know whether the pulse just arrived and the time expired or arrived earlier.
We need to count the pulses:
1. Either interrupt every 10th pulse and get the time during which the pulses arrived. But if the pulse didn't arrive, then we won't know the number
2. Or interrupt every 1 second and get the number of pulses. But then we don't know whether the pulse just arrived and the time expired or arrived earlier.
Re: Pulse Counter
You seem to be proposing different ways of measuring frequency, of either:
1) Measuring how much time it takes to count a certain number of pulses, say 10 of them. Or...
2) Counting how many pulses occur in a fixed time, like one second.
Either way can work, but you seem to be worried about what to do if the pulses stop or are erratic.
Maybe you would be better off just generating an interrupt for every pulse, or even for both the rising and falling edges if need be. For the frequency you mentioned, this should be possible, but don't go overboard with processing in the interrupt routine. Timer input capture could be part of this, but might not be required.
If the pulses just stop, then yes, the interrupt routine will not get called again. However, if you record the time when the interrupt routine gets called, your mainline code could detect if the pulses have stopped, and take whatever action is necessary.
1) Measuring how much time it takes to count a certain number of pulses, say 10 of them. Or...
2) Counting how many pulses occur in a fixed time, like one second.
Either way can work, but you seem to be worried about what to do if the pulses stop or are erratic.
Maybe you would be better off just generating an interrupt for every pulse, or even for both the rising and falling edges if need be. For the frequency you mentioned, this should be possible, but don't go overboard with processing in the interrupt routine. Timer input capture could be part of this, but might not be required.
If the pulses just stop, then yes, the interrupt routine will not get called again. However, if you record the time when the interrupt routine gets called, your mainline code could detect if the pulses have stopped, and take whatever action is necessary.
Re: Pulse Counter
So I want to do this on a hardware timer, but there is little information on Arduino
Re: Pulse Counter
You have already been pointed to the HardwareTimer examples. To be more specific, take a look at this one: https://github.com/stm32duino/STM32Exam ... apture.ino
That example uses "method 1" to measure frequency, but with 'X' (number of pulses to count) equal to 1.
For method 1 you were concerned about what will happen "if the pulse didn't arrive". The example code will detect that situation, because the 16-bit timer counter register will eventually overflow and wrap back to zero. If that happens twice with no pulse being detected, it sets the measured frequency to zero. You have not mentioned the processor you are using, and the systemClock frequency, but read the comments in the example to work out what the minimum frequency that can be measured is. Hint: for the range of frequency you mentioned, you probably have to change the PrescalerFactor (which in effect would be changing the 'X' pulse count value). Some STM32 processors have 32-bit timers, that could measure lower frequencies in the same way without losing precision, but as far as I know stm32duino only supports those timers as if they were 16-bit, so to make use of that you would have to venture out of the stm32duino comfort zone. There are lots of other ways that timers can be used that are also not supported by stm32duino itself.
Also be aware of exactly what happens with that example if you say have say a 1kHz input (still within the range you mentioned). The loop processing only prints the frequency once every second, so the vast majority of measured frequency values are not used by the example - it just reports the last one. You could change that in any way you like, for example, you could calculate an average, and/or check for minimum and maximum values.
That example uses "method 1" to measure frequency, but with 'X' (number of pulses to count) equal to 1.
For method 1 you were concerned about what will happen "if the pulse didn't arrive". The example code will detect that situation, because the 16-bit timer counter register will eventually overflow and wrap back to zero. If that happens twice with no pulse being detected, it sets the measured frequency to zero. You have not mentioned the processor you are using, and the systemClock frequency, but read the comments in the example to work out what the minimum frequency that can be measured is. Hint: for the range of frequency you mentioned, you probably have to change the PrescalerFactor (which in effect would be changing the 'X' pulse count value). Some STM32 processors have 32-bit timers, that could measure lower frequencies in the same way without losing precision, but as far as I know stm32duino only supports those timers as if they were 16-bit, so to make use of that you would have to venture out of the stm32duino comfort zone. There are lots of other ways that timers can be used that are also not supported by stm32duino itself.
Also be aware of exactly what happens with that example if you say have say a 1kHz input (still within the range you mentioned). The loop processing only prints the frequency once every second, so the vast majority of measured frequency values are not used by the example - it just reports the last one. You could change that in any way you like, for example, you could calculate an average, and/or check for minimum and maximum values.