Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Generating sine waves with PWM

Generating sine waves with PWM

2005-12-05 by James Washer

I'm just starting to play around with sine waves using PWM and a lookup table. 

I'm concerned as to the maximum frequency of a sine that can be generated. Walking through an example, based on Fast PWM.

Xtal=9830400Hz
8bit Fast PWM
This gives PWM frequency of (9830400/256) 19200Hz.

So, if I wanted to generate a sine wave at 2200Hz, I'd only be changing the pulse width ~9 times per cycle. This seems completely inadequate.

As this is my first attempt at sine wave generation, I have a couple of questions

1) Does the math above seem correct, or am I mistaken in my limited understanding of using PWM

2) Assuming my math is correct, then there must be one rule-of-thumb for how how a frequency one can generate using fast-pwm for a given Xtal frequency. What is the rule?

3) Anything else you'd care to add/offer?

thanks

 - jim

Re: [AVR-Chat] Generating sine waves with PWM

2005-12-05 by Jim Wagner

I'm working on a project with similar constraints (even
uses 2200Hz as one of the tones). There are not very many
ways to improve things. 

I am using an 6-bt R-2R DAC driven by six port pins. Since
the DAC output only has small steps in it, the harmonic
content is much lower and the rate and filtering are much
less demanding.

You can add a smoothing filter with more sections (poles).
I am actually using a 6-pole switched-capacitor filter that
is "tuned" according to an external clock (also provided by
the micro). A good filter like this can do wonders. With
such a small number of samples (points) per cycle, a simple
2-pole active filter just won't hack it.

You can decrease the TOP value (that is, reduce the number
of bits) so that the PWM period is shorter and you can get
more of them in per tone cycle. With such few samples, it
makes little sense to try for a full 8-bits. Every halving
of TOP (from the standard value of 255) drops one
resolution bit and doubles the repetion frequency of the
PWM, doubling the number of samples per tone cycle.

Use the fastest possible main clock (that is, highest
frequency crystal). This will give you the fastest possible
PWM clock and that will give the highest possible PWM
repetition rate. 

Not many other choices than these! That is why I chose not
to use PWM for this kind of application.

Jim Wagner

---------------------------------------------------------------
The Think Different Store
http://www.thinkdifferentstore.com/
For All Your Mac Gear
---------------------------------------------------------------

RE: [AVR-Chat] Generating sine waves with PWM

2005-12-05 by MuRaT KaRaDeNiZ

Hello,

There are other choices:
Check the avr tiny series. There are devices with special high speed pwm
peripherals. These micro(s) (I dont remember exact part number) have a
pll clock multiplier built in, just to generate high clock frequency for
this demanding 
peripherals, you get 100khz and more easily.

And there are the at90pwm2, at90pwm3 devices, they have >1mhz DAC
built-in. (beside complex high speed pwm peripherals). I will use these
devices in a few weeks in my thesis project.



Regards.

Murat Karadeniz
http://www.onlinefavbar.com/mukas
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
Behalf Of Jim Wagner
Sent: Monday, December 05, 2005 10:09 PM
To: AVR-Chat@yahoogroups.com
Subject: Re: [AVR-Chat] Generating sine waves with PWM


I'm working on a project with similar constraints (even
uses 2200Hz as one of the tones). There are not very many
ways to improve things. 

I am using an 6-bt R-2R DAC driven by six port pins. Since
the DAC output only has small steps in it, the harmonic
content is much lower and the rate and filtering are much
less demanding.

You can add a smoothing filter with more sections (poles).
I am actually using a 6-pole switched-capacitor filter that
is "tuned" according to an external clock (also provided by
the micro). A good filter like this can do wonders. With
such a small number of samples (points) per cycle, a simple 2-pole
active filter just won't hack it.

You can decrease the TOP value (that is, reduce the number
of bits) so that the PWM period is shorter and you can get
more of them in per tone cycle. With such few samples, it
makes little sense to try for a full 8-bits. Every halving
of TOP (from the standard value of 255) drops one
resolution bit and doubles the repetion frequency of the
PWM, doubling the number of samples per tone cycle.

Use the fastest possible main clock (that is, highest
frequency crystal). This will give you the fastest possible
PWM clock and that will give the highest possible PWM repetition rate. 

Not many other choices than these! That is why I chose not
to use PWM for this kind of application.

Jim Wagner

---------------------------------------------------------------
The Think Different Store
http://www.thinkdifferentstore.com/
For All Your Mac Gear
---------------------------------------------------------------



 
Yahoo! Groups Links

Re: [AVR-Chat] Generating sine waves with PWM

2005-12-05 by James Washer

thanks... I wasn't aware of these.

 - jim

On Mon, 5 Dec 2005 23:38:01 +0200
"MuRaT KaRaDeNiZ" <karademu.yahoogroups@onlinefavbar.com> wrote:
Show quoted textHide quoted text
> Hello,
> 
> There are other choices:
> Check the avr tiny series. There are devices with special high speed pwm
> peripherals. These micro(s) (I dont remember exact part number) have a
> pll clock multiplier built in, just to generate high clock frequency for
> this demanding 
> peripherals, you get 100khz and more easily.
> 
> And there are the at90pwm2, at90pwm3 devices, they have >1mhz DAC
> built-in. (beside complex high speed pwm peripherals). I will use these
> devices in a few weeks in my thesis project.
> 
> 
> 
> Regards.
> 
> Murat Karadeniz
> http://www.onlinefavbar.com/mukas
> 
> 
> 
> 
> -----Original Message-----
> From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On
> Behalf Of Jim Wagner
> Sent: Monday, December 05, 2005 10:09 PM
> To: AVR-Chat@yahoogroups.com
> Subject: Re: [AVR-Chat] Generating sine waves with PWM
> 
> 
> I'm working on a project with similar constraints (even
> uses 2200Hz as one of the tones). There are not very many
> ways to improve things. 
> 
> I am using an 6-bt R-2R DAC driven by six port pins. Since
> the DAC output only has small steps in it, the harmonic
> content is much lower and the rate and filtering are much
> less demanding.
> 
> You can add a smoothing filter with more sections (poles).
> I am actually using a 6-pole switched-capacitor filter that
> is "tuned" according to an external clock (also provided by
> the micro). A good filter like this can do wonders. With
> such a small number of samples (points) per cycle, a simple 2-pole
> active filter just won't hack it.
> 
> You can decrease the TOP value (that is, reduce the number
> of bits) so that the PWM period is shorter and you can get
> more of them in per tone cycle. With such few samples, it
> makes little sense to try for a full 8-bits. Every halving
> of TOP (from the standard value of 255) drops one
> resolution bit and doubles the repetion frequency of the
> PWM, doubling the number of samples per tone cycle.
> 
> Use the fastest possible main clock (that is, highest
> frequency crystal). This will give you the fastest possible
> PWM clock and that will give the highest possible PWM repetition rate. 
> 
> Not many other choices than these! That is why I chose not
> to use PWM for this kind of application.
> 
> Jim Wagner
> 
> ---------------------------------------------------------------
> The Think Different Store
> http://www.thinkdifferentstore.com/
> For All Your Mac Gear
> ---------------------------------------------------------------
> 
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
>

Re: [AVR-Chat] Generating sine waves with PWM

2005-12-06 by Ralph Hilton

On Mon, 5 Dec 2005 10:55:06 -0800 you wrote:

>I'm just starting to play around with sine waves using PWM and a lookup table. 
>
>I'm concerned as to the maximum frequency of a sine that can be generated. Walking through an example, based on Fast PWM.
>
>Xtal=9830400Hz
>8bit Fast PWM
>This gives PWM frequency of (9830400/256) 19200Hz.
>
>So, if I wanted to generate a sine wave at 2200Hz, I'd only be changing the pulse width ~9 times per cycle. This seems completely inadequate.
>
>As this is my first attempt at sine wave generation, I have a couple of questions
>
>1) Does the math above seem correct, or am I mistaken in my limited understanding of using PWM

The division looks incorrect. 9830400/256 = 38400.

>2) Assuming my math is correct, then there must be one rule-of-thumb for how how a frequency one can generate using fast-pwm for a given Xtal frequency. What is the rule?

>3) Anything else you'd care to add/offer?

You could sacrifice resolution to get a faster pwm by setting the top to a lower value
than 255. Also the ATtiny26 has a 64MHz PLL clock for PWM giving a PWM frequency of 250KHz
at 8 bits. 

An alternative is a dedicated synth chip. The AD9833 is not too expensive and isn't
difficult to program. 
--
Ralph Hilton
http://www.ralphhilton.org
C-Meter: http://www.cmeter.org
FZAOINT http://www.fzaoint.net

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.