Question:
How to determine duty cycle of high frequency square wave?
BignBlue
2013-04-24 23:18:33 UTC
I am trying to program a microcontroller to calculate the duty cycle of a square wave. I am using interrupts to determine the time at which rising and falling edges of the waveform occur, so i can calculate the active time and the period of the waveform. However, it only seems to work at low frequencies (1-10Hz). Anything above this, the duty cycle value fluctuates wildly and is not correct. Any ideas on how I can get this working? Any help would be appreciated
Three answers:
?
2013-04-25 12:30:51 UTC
I don't know how you are operating your counter/timer, but you should run it at a "fast enough" counter rate. If possible, use it so that a copy (snapshot) is taken by the peripheral hardware when the pin changes state as well as producing an interrupt event. This way, your value is very accurate. Set things up so that this takes place on either transistion of the pin, too. And then just let the counter freerun, recording the snapshot values as they come in.



It's the responsibility of your software to grab a copy before it gets overwritten by the next event and to check for overwriting, if it happens (which it may, if the duty cycle is close to 0% or 100%.) This way you can at least recover in your state machine algorithm and provide reasonable output values. Also, you must deal with rollover of the counter. That's also your job.



Clearly, you compute the total period from the difference of values that are two pin-change events apart. It's best that you set up an IIR filter on that value so that you can squeeze out noise from it, too. And you compute the duty cycle by comparing the difference between adjacent values against this filtered period.



With an MSP430, where the A2, A3 or B7 timer counter system can easily handle these tasks, you should be able to handle signal frequencies very much higher than 10Hz. With low resolution into the MHz, perhaps.



I'm just guessing, but I suspect you are borrowing someone else's software. Otherwise, you'd know exactly why you aren't getting better results. So it's hard to provide suggestions when you aren't directly engaged in the process.
?
2013-04-25 01:16:08 UTC
Most likely, your micro has reached its "speed limit"!

You get the times from interrupts: that's the correct way.

During the interrupt, you perform a few operations: that takes time! Say, 10 instructions of 20us = 200us before it can serve another interrupt. And 200us / half wave => 400us full wave (50% duty-cycle) => 2500Hz.



Calculate the duration of your interrupt routine: 1 / T will give you an idea of your limits . This depends on the micro clock frequency, of course.



Also, I assume that you use a timer, running on the background. At interrupts, you read the value of the timer and RESET it. Maybe, also, your timer is set to run too slow...
lashley
2016-08-06 02:46:27 UTC
Essentially it is the sum of a bunch of calculations. A square wave is correctly a combo of a number of sine waves of exclusive frequency. The pleasant analysis would handiest be an approximation of the square wave in a real world atmosphere. However there's a sequence that should work. Identical for a triangular wave. It is a sum of a bunch of sine waves at one-of-a-kind freq's and amplitudes. So the pc answer would be to take the quality match of sets of waves and sum the reactance in the proper means of the answer to every of the man or woman calculations.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...