Measure PWM duty cycle (%)

10 views
Skip to first unread message

David VB

unread,
Jul 13, 2024, 11:52:55 AM7/13/24
to jallist
Hi,

I need a procedure/function that would return the duty cyle ratio of a 5 Vp-p signal applied on one pin (not critical) of the PIC.   PIC is not yet chosen, and precision of the measure is not critical (up to 5 % is ok)

Context:
I made a temperature controlled fan PWM driver based on the TC651.    This chip outputs a 5V PWM signal whose duty cycle is proportional to the temperature.  This outputs is boosted by a BJT to drive a bunch of fans.
I'd like to measure that duty cysle and display it on a 7-segment display (LED or LCD, still not sure)
The display part is not a problem, but I lack the procedure/function that would return me the duty cycle as a byte between 0 and 100

I think I know the basics of doing it (using a timer to measure tops from Fosc, gated by the signal to measure, and dividing the tops by the total number of tops during one cycle), but I am uncomfortable to code it....

Does any of you already have such a procedure/function in his toolbox ?

Thanks for your help

David VB

unread,
Jul 14, 2024, 1:24:25 PM7/14/24
to jallist
You may ask why I do not feel confortable to program it myself ...

It is because of the special cases: when PWM is = 0 % or 100 % and also when some of the intervals are small (PWM >= 95 % or <= 5 %)

In the first two cases (0 % and 100 %), I can manage it by using the signals of the chip: it has an output that goes low when PWM = 100 %, and I can take the PWM signal, send it through a diode to a simple integrator to confirm that PWM is indeed > 0 %

But how to handle the other cases remain obscure to me....

BTW, a useful information: the frequency of the PWM signal is very low.   It's about 30 Hz

Finally, I was wrong about the chip designation.   It's in fact the TC648B

Rob CJ

unread,
Jul 14, 2024, 1:36:23 PM7/14/24
to jal...@googlegroups.com
Hi David,

What you need - I think - is a timer that also runs at a frequency where the complete range of the timer equals 30 Hz which is started the moment the PWM signal goes high and stops when it goes low.

If the value of the timer is then halfway the range of the timer you have a 50% duty cycle.

So for 30 Hz or 33 ms using a 16 bit timer with a range of 65636 the timer should run at 65536 / 0.033 = 1.966.080 Hz, so a value of 2 MHz might be close enough.

Would this work for you?

Kind regards,

Rob






Van: jal...@googlegroups.com <jal...@googlegroups.com> namens David VB <pinhe...@gmail.com>
Verzonden: zondag 14 juli 2024 18:52
Aan: jallist <jal...@googlegroups.com>
Onderwerp: [jallist] Re: Measure PWM duty cycle (%)
 
--
You received this message because you are subscribed to the Google Groups "jallist" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallist+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallist/610afca5-b3ff-4bb7-bc5e-662f9e10dfa4n%40googlegroups.com.

David VB

unread,
Jul 15, 2024, 12:27:24 PM7/15/24
to jallist
Hi Rob,

Thank you for your answer.
As I understood, this is the basic theory, yes.

In the meantime, I found something on the Microchip Forum, especially the part " One approach is to dedicate TWO timers or capture channels to the task: One grabs the ON time (or OFF time); the other grabs the period of the entire waveform "

If I understand correctly, I need a PIC with two timers capable of CCP (used in capture mode), == OR == two timers that can be controlled by a gate (which will be the PWM signal), if I can configure the edge of the control gate, which does not seem be the case on common PIC's....
Then:
- Start to count a Fosc (maybe 2 MHz as you suggested) when rising edge (or falling, not important)  at both timers
- When falling edge, stop the count on the first timer
- When rising edge, stop the count on the second timer
- Divide first number by second number, multiply by 100, and display
- repeat

Since the PWM signal has a very low frequency (30 Hz), I will have plenty of time to process the computation in between two periods of the PWM signal.
The display will be based on 74hc595 driven 7-segments.   This does not take much time either to update, and I have them laying around.

What do you think of that approach ?   Does it sound right to you ?

Thanks again !

Rob CJ

unread,
Jul 15, 2024, 12:43:10 PM7/15/24
to jal...@googlegroups.com
Hi David,

Maybe it can be done simpler because when you use the 2 MHz clock you are almost close to the period of the PWM signal and I think you can use one timer.

So you could use Timer 1 with gate control, see below.
You can also set an interrupt on the gate when the measurement is done, see:
Maybe this works too using only 1 timer.

Kind regards,

Rob



Verzonden: maandag 15 juli 2024 18:07
Aan: jallist <jal...@googlegroups.com>
Onderwerp: Re: [jallist] Re: Measure PWM duty cycle (%)
 

David VB

unread,
Jul 15, 2024, 1:48:44 PM7/15/24
to jallist
That's exactly what I need ! 

And I found a low-pin count PIC that is able to do it: PIC12F1822, since I only need 3 inputs and 3 outputs :-)

Thank you for your help !
Reply all
Reply to author
Forward
0 new messages