This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

PWM analyzer / Pulse counter

Is it possible to count pulses with the 51822 from an external device in the range of 250-15.000 pulses pr. second, while the CPU is powered down (softdevice sleep)?

I've had a look at the pwm_analyzer sample, which might do the trick. But I'm not familiar with all the GPIOTE events and tasks. and I'm a little unsure that the resolution is high enough for this purpose. As far as I can tell the example measures both the low and high duty cycles, and firing an interrupt after right after it has measured the high cycle.

I'm not particular interested in generating an interrupt for this reading, as I would rather read the results while the BLE stack is executing to avoid extra wake-up cycles. But according to the example, it seems there is no specific code that restarts the events/task. So I guess it means that they will continuously provide the result to the registers.

Am I totally off here or is there an easier way to simply count the number of pulses from an external device?

Looking at the Timer description in the reference manual, it can also operate in a counter mode triggered by a COUNT task. Is it possible to setup a GPIO pin to trigger that task by a lo-hi transition? This way I can simply read the register whenever the SD is awake and do the frequency calculation based on the time elapsed since last read.

  • Hi

    Initial thoughts on this is that yes, you can use TIMER peripheral for this purpose and have it in COUNTER mode. Then the hardware will count the pulses and, as you say, you can make calculations to calculate the frequency based on the counter value. However, this will not be specifically low power, as TIMER peripheral constantly requires 16HMz clock to be enabled, so the operation will require ~1mA of current.

    However, if you intend only periodically to measure the frequency of the PWM signal, then it should be possible to monitor GPIO pin for signal transition, and let that signal transition start a TIMER. When the timer has been started, you configure the pin signal transition to stop the timer and trigger an interrupt so you can check the value of the timer and calculate the frequency. If you do not need to check the frequency very often, this method could be relatively low power, since you only need to enable the peripherals while you perform the measurement. Let me know if you would consider this method instead of the other one.

    What do you need to measure exactly, the duty cycle of the PWM or also the frequency?

  • Thanks for your thorough answer, I wasn't aware that the HFClock was needed. What about the LFClock, I have it on board - will this make any difference?

    I only need to measure the frequency (count pulses), and the duty cycle was just an alternative to that, in order to do the measuring in a small window. I could live with a measurement every second, but 5 measures would be optimal.

    Anyhow, I've made a mistake. The pulse input of 250-15000 is actually the number of pulses per minute. So the real figure is 4-250Hz, which is just making the pulse periods longer, and more costly to measure.

    I like your idea in the seconds paragraph, but it would still mean that if I was to measure a 4Hz signal, the TIMER would need to be on for at least 250ms right (@ ~1mA)?

    This would then consume ~0,25 mA at maximum, but only for 1 reading per second. At 4 readings we are back at ~1 mA, so it would probably be easier to just use the Timer as a counter as you mention.

    Well, right now I'll probably start with the counter. Aside from the pwm_analyzer, is there any examples on using a Timer in counter mode?

  • Ok, since the frequency is that low, then perhaps, as you have pointed out, there is not so much power to save by periodically enable the peripherals. However, if your average pulse frequency is e.g. 50 Hz, and you need to measure on average perhaps 3 times a second, then the method would pay off since you would only have the peripherals enabled 6% of the time. On the other hand, if your pulse freqency at any instance is down to 4 Hz and you are measuring 5 times a socond, it is not going to work.

    However, if the above method would work, you could use RTC timer instead of TIMER to measure the pulse, which is very low power. This should be adequate since your max frequency is only 250Hz. Current consumption is <1uA for RTC+LFCLK if you use 32kHz external crystal as the low frequency clock source. If you use the internal 32kHz RC clock source, the current consumption is ~10uA with calibration to obtain 250ppm accuracy, see this thread.

    Example code for TIMER in COUNTER mode is found on Nordic's Github

  • Hi Stefan!

    Could you relink the timer exampe? I have similar task and I would like to download it, but unavailable.

    Thank you!

Related