Page 1 of 1

How to measure the duty-cycle of an signal?

Posted: Sun Apr 11, 2021 11:33 am
by stombo
I'd like to measure the duty-cycle of an signal with an unknown frequency, somewhere from 200 Hz up to 1 kHz. The real frequency should not matter. Another challenge is, that this signal is only send from time to time, or better to say, as long as the signal is sent, i'd like to measure and act on it, and if sending stops the system should go into deep-sleep to save as much power as possible.

I'm pretty shure there a many read-to-use examples out there, but i also like to know how this is to be done :-)

The incoming signal is already set to the logiclevel of the STM32 (3,3V), so it could be directly feed into one of the digital inputs of the Blue Pill an looks like this:

Code: Select all

      _____   _   _    _     __     _
_____|     |_| |_| |__| |___|  |___| |_____
 (1)   (2)   (3)         (4)           (5)
(1) The normal status i LOW (0V)
(2) If the sender prepares to send, he raises the status to HIGH (3,3V)
(3) Then it sends with some duty-cycle (here 50%)
(4) Duty-cycle can change
(5) If sending is done, signal falls back to LOW

I guess if should fed the signal to a interrupt pin which detects a raising edge and triggers a ISR which then put's some timecode into a variable like "raised_at" to know when the signal has raised. Then reprogram the port to act on a falling edge. If that falling edge occures, the ISR will be triggered again and put a timecode into "fall_at" and again reprogram to act on rising edge.
As soon as the next one appears, the ISR see the timecodes in both variables and could calc a duty-cycle by:
high_time = fall_at - raised_at
low_time = now - fall_at
ratio = high_time / low_time

Next, it should carry out, let's say 20 measures into an integral value to get a good mean-value. This should then be send to an upstream server. For that i need a counter and a variable.

Re: How to measure the duty-cycle of an signal?

Posted: Sun Apr 11, 2021 12:30 pm
by GonzoG

Re: How to measure the duty-cycle of an signal?

Posted: Tue Apr 13, 2021 11:43 am
by stevestrong
Which resolution you want to achieve?
If not critical then you can make it without any specific timer, just user micros() for time measurement.
In this case, taking into account other system interrupts like systick and USB (if you use it) you better use median instead of mean of the values.

Re: How to measure the duty-cycle of an signal?

Posted: Mon Jul 12, 2021 1:53 pm
by jcbuntu
Hello, Stombo.
I begin to code on STM32F1xx and F4xx without libraries ( barre metal is a funny name !).
For STM32F1xx, i've saw, in doc "cd00246267-stm32f100xx-advanced-armbased-32bit-mcus-stmicroelectronics" RM0041 §12.3.7 p233, one answer low level to your question.
For STM32F4xx, it' doc RM0090 §17.3.7 p 539.

It's one of my future test project next months !

Re: How to measure the duty-cycle of an signal?

Posted: Mon Jul 12, 2021 1:56 pm
by jcbuntu
I've forgot : in Arduino IDE, pulseIn() work fine ...