Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Papers

Comments on PWM Triangle Wave from two clocks

Post

PWM Triangle Wave from two clocks

+5
−0

What follows is a proposed concept of a simple (in principle!) way to generate a fixed-amplitude triangle wave, using two clocks, an XOR gate, and not using any processor cycles.

It is practical in the low-mid-100-Hz ballpark, with typical mcu clocks. Motivating application is dither waveform for solenoid valve control signals.


SUMMARY

Two timers output two clocks A, B, with fixed 50% duty cycle. XOR(A,B) produces a symmetrical PWM triangle wave, with modulation frequency at ($f_{\rm A} + f_{\rm B})$ , and triangle frequency at $(f_{\rm A} - f_{\rm B})$. No other additional hardware. Illustrated below, showing how the XOR output is equivalent to a PWM. (click to zoom)

xor-pwm

Given a master clock $f_{\rm CPU}$, the available frequencies are:

$$f_{\rm TRIANGLE} = \frac{2 f_{\rm CPU}}{(n^2 - 1)}$$

"$n$" should be an odd integer. The two clocks are set to $f_{\rm CPU}/(n+1)$ and $f_{\rm CPU}/(n-1)$, to make the dividers even numbers, so that A and B can have 50% duty cycles.

The low-pass filter design is also parametrized by $n$. Let's define the passband at the triangle’s 5th harmonic, and the stopband at $(f_{\rm A} + f_{\rm B})$. The dimensionless value $f_{\rm STOP} / f_{\rm PASS}$ will represent the filter's transition band, and this value comes out to $n/5$.

The dimensionless transition band corresponds to "filter complexity". A narrower transition band places more demands on the filter design. Given a lower limit of this value, a practical upper limit for the triangle frequency can then be derived as

$$ \frac{f_{\rm TRIANGLE(max)}}{f_{\rm CPU}} = \frac{2}{25} \left(\frac{f_{\rm PASS}}{f_{\rm STOP}}\right)^2$$

The table below uses this formula to show typical limits of the application:

min acceptable $f_{\rm STOP}/f_{\rm PASS}$ max $f_{\rm TRIANGLE}/f_{\rm CPU}$
20 1 / 5000
40 1 / 20000
60 (shown in example) 1 / 45000
80 1 / 80000
100 1 / 125000

Example Circuit producing 199Hz from 10MHz f_cpu example-circuit

Simulation of above circuit. 0-1V inputs produce 12mV - 988 mV peaks simulation-full-scale

Detail showing less than 1% ripple. simulation-ripple-detail


Additional details of the analysis, and example showing the design calculation, are here (pdf file, 5 pages).

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (5 comments)
General comments
coquelicot‭ wrote over 3 years ago · edited over 3 years ago

That seems to be an interesting trick. But I don't understand how a XOR can produce a triangle wave. According to my very limited knowledge of digital electronics, there is a low and high state. How does that produce the intermediate states of the triangle wave? or are you using the RC network to this end? Perhaps could you add a short explanation of this process for the non specialists? NB: if you wish, I can edit your math formulas in Latex to make them more readable.

Pete W‭ wrote over 3 years ago · edited over 3 years ago

No problem! Added the green clock waveforms to show this visually. It can also be done with an AND gate, which is more intuitive, but the XOR gate produces a 0-100% PWM, while the AND gate only 0-50%. Regarding the LaTex -- that would be great! (if it isn't much trouble.) I never learned how to do that.

coquelicot‭ wrote over 3 years ago

Nice! a digital heterodyne! I much like this idea (+1). I will suggest you an edit for the Latex formula. Note: it's very easy to learn basic Tex/Latex typesetting (1h at most), and that's very useful in many circumstances and sites.

Pete W‭ wrote over 3 years ago · edited over 3 years ago

Thanks! That makes it look a lot better.

a concerned citizen‭ wrote almost 3 years ago

Late comment, but that's a nice trick. A 2nd order Bessel might do just a tad better, though at the cost of an opamp.