Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
Makerdoer
PostPosted: Mar 11, 2010 - 09:15 AM
Newbie


Joined: Mar 11, 2010
Posts: 6


So what might be the simplest way to create sine from uC (except DDS IC). Frequency scale: few kHz to over 100 kHz. Low output voltage (mV). What kind of limitations are related to uC PWM output to sine considering this case (frequency?). How about triangular wave to sine, limitations?.

Thanks already!
 
 View user's profile Send private message  
Reply with quote Back to top
RES
PostPosted: Mar 11, 2010 - 09:26 AM
Resident


Joined: Dec 31, 2005
Posts: 632


Use a phase accumulator (adder) and a sine table and output PWM.

_________________
RES - http://www.attiny.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
MartinM57
PostPosted: Mar 11, 2010 - 09:34 AM
Resident


Joined: Dec 15, 2005
Posts: 532
Location: Bristol, UK

(that's sound like PWM, not a sine wave)
 
 View user's profile Send private message  
Reply with quote Back to top
cmhicks
PostPosted: Mar 11, 2010 - 09:56 AM
Hangaround


Joined: Nov 20, 2008
Posts: 333
Location: Cambridge, UK

MartinM57 wrote:
(that's sound like PWM, not a sine wave)
Pass PWM generated like that through a low-pass filter (eg a resistor and capacitor) and you'll get a reasonable approximation to a sine wave. However, it strikes me that 100kHz is pushing it a bit for an AVR using this method. Might be possible if you're not doing much else with the AVR.

There's a simple DDS signal generator project based on an AVR around these parts somewhere which should show you just what you need to do.

CH
==
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 11, 2010 - 09:59 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 33138
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

(that's sound like PWM, not a sine wave)

What makes PWM and sine mutually exclusive then?


_________________
 
 View user's profile Send private message  
Reply with quote Back to top
MartinM57
PostPosted: Mar 11, 2010 - 10:10 AM
Resident


Joined: Dec 15, 2005
Posts: 532
Location: Bristol, UK

It's OK CL, I'm completely with you Smile

RES omitted the somewhat important aspect of passing the PWM into a LPF, which given the OP's apparent knowledge would seem important to add - as well as seeming to claim that it would be good for the required frequency range, whereas it might be a struggle at the top end...
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Mar 11, 2010 - 10:14 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 33138
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:
passing the PWM into a LPF

As always I'd recommend AVR335 as a starting point for folks to understand about how to do analogue generation with a CPU that doesn't have an accessible DAC. That explains how the LPF converts the varying width digital to an analogue waveform.

Or, as an alternative, it's always worth mentioning building an external DAC with an R-2R:

http://www.myplace.nu/avr/minidds/index.htm



Oh but these days I guess the Xmega's are worth a mention too as they have 12-bit DACs built in.

(there's one or two of the tiny/megas that have DAC too - I think they the "lighting" devices?)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
Visovian
PostPosted: Mar 11, 2010 - 10:21 AM
Resident


Joined: Aug 07, 2007
Posts: 517
Location: Czech

Quote:
How about triangular wave to sine, limitations?.

There are methods to adjust triangular to sinus using several diodes or low signal mosfet.
The plus is that you can get higher frequencies (say 1 MHz).
The minus is a greater distortion.
 
 View user's profile Send private message  
Reply with quote Back to top
Makerdoer
PostPosted: Mar 11, 2010 - 10:57 AM
Newbie


Joined: Mar 11, 2010
Posts: 6


Thanks everyone! uC+10 bit serial DAC+filter seems interesting at this point. How about the code, is it going to be heavy? I suppose 100k freq for uC PWM output to sine method wound be too high? I have to look more closely that triangular to sine more closely. If that works, I guess the code wound be lighter. Anymore suggestions?
 
 View user's profile Send private message  
Reply with quote Back to top
cmhicks
PostPosted: Mar 11, 2010 - 11:09 AM
Hangaround


Joined: Nov 20, 2008
Posts: 333
Location: Cambridge, UK

Makerdoer wrote:
Thanks everyone! uC+10 bit serial DAC+filter seems interesting at this point. How about the code, is it going to be heavy? I suppose 100k freq for uC PWM output to sine method wound be too high? I have to look more closely that triangular to sine more closely. If that works, I guess the code wound be lighter. Anymore suggestions?
I think the limiting factor is going to be how fast you can shovel data out to the DAC.

The project Cliff pointed out is the one I was thinking of. This uses 9 cycles per iteration to update an accumulator, lookup an 8-bit value from a 256-point lookup table, and output the value to a port.

You can probably count on needing 12-15 cycles per iteration if you're going to send 10-bit data to a serial DAC. If you use a 20MHz AVR that gives a sample rate of a bit over 1MHz. You should be able to get reasonable sinewaves up to 100kHz using this method with fairly simple filters.

The waveform doesn't make much difference to the computation required, since you just read the waveform values out of a lookup table, but getting a good-looking squarewave at 100kHz is much more of a challenge than a sinewave at the same frequency, because of the sharp edges.

Basically, the design looks OK if you don't want to do anything else much with the AVR.

CH
==
 
 View user's profile Send private message  
Reply with quote Back to top
leon_heller
PostPosted: Mar 11, 2010 - 02:05 PM
Raving lunatic


Joined: Jul 27, 2001
Posts: 5142
Location: St. Leonards-on-Sea (UK)

Here's my version of Jesper's MiniDDS:

http://webspace.webring.com/people/jl/leon_heller/minidds.html

I used the Atmel assembler.

I've got a couple of other versions of the code, including one which uses a timer interrupt. That can do some other stuff while it is running.

_________________
Leon Heller
G1HSM
leon355@btinternet.com
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
sparrow2
PostPosted: Mar 11, 2010 - 03:43 PM
Resident


Joined: Oct 07, 2002
Posts: 978
Location: Skørping Denmark

If you need it you can speed the software DDS up so it only take 6 CLK (7 for same resolution).
Max freq at 20 MHz will then be 1.6 MHz but to call it a sine wave you need about 20 updates pr. periode so 167 KHz so you can make your 100 KHz this way.
 
 View user's profile Send private message  
Reply with quote Back to top
stu_san
PostPosted: Mar 11, 2010 - 05:15 PM
Raving lunatic


Joined: Dec 30, 2005
Posts: 2041
Location: Longmont, CO USA

sparrow2 wrote:
Max freq at 20 MHz will then be 1.6 MHz but to call it a sine wave you need about 20 updates pr. periode so 167 KHz so you can make your 100 KHz this way.
Laughing And, apparently, not much else! Twisted Evil

As long as this is the only task your AVR is doing, this is a pretty slick solution.

Stu

_________________
Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.
(My boss always chooses Cheap. Twice.)
 
 View user's profile Send private message  
Reply with quote Back to top
Makerdoer
PostPosted: Mar 11, 2010 - 06:05 PM
Newbie


Joined: Mar 11, 2010
Posts: 6


So in practise serial DAC is out of question for 100kHz sine and higher? How about parallel DAC or R-2R ladder then, takes one whole port but should be ok. I suppose it is easy to change sine freq by using this DAC method, eh. What about that tri wave to sine, any comment? Limitations? Btw. nice projects, miniDDS etc.


Last edited by Makerdoer on Mar 11, 2010 - 06:13 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Mar 11, 2010 - 06:13 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 18830
Location: Wisconsin USA

Quote:

So in practise serial DAC is out of question for 100kHz sine and higher?

Quote:

(except DDS IC)

??? If you are going add an external chip, why the prejudice against a DDS?
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 11, 2010 - 07:58 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 13373
Location: Orlando Florida

If the freq doesnt have to change, just output a square wave at whatever freq you want, add an RC lp filter and an RC hp filter, see if looks 'good enough'. (You cant see less than a couple percent distortion on a scope). If not, I bet an LC filter will make it look perfect.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
Makerdoer
PostPosted: Mar 11, 2010 - 08:12 PM
Newbie


Joined: Mar 11, 2010
Posts: 6


I need few frequencies. Sine frequencies like 5kHz, 50kHz and 100kHz (or higher). So frequency has to be selectable.
 
 View user's profile Send private message  
Reply with quote Back to top
Simonetta
PostPosted: Mar 12, 2010 - 12:55 AM
Resident


Joined: Mar 12, 2003
Posts: 620
Location: Portland, Oregon USA

Are you trying to make a 'function generator' that will give you control of the amplitude and the frequency of the sine wave? This is probably impossible to do with an AVR alone. If this is the goal then use the AVR to control the parameters of a MAX038 or an XR2206 chip.
I believe, (but I haven't done it) that the pulse width modulation method that AVRs use to make a DC voltage level can be used instead of the resistors that control the frequency and amplitude on the function generator chips.
I have had some success controlling the frequencies of the old Texas Instruments SN94281 complex sound generator chip that Radio Shack sold long ago. I used Maxim serial DACs with an AVR on the digital side and the DAC output (with a small resistor) on the 94281's SLF and VCO resistor inputs.

If you only need a good sine wave at one specific frequency then do PWM on the AVR and adjust the AVR clock speed. Or use a 74HC4046 VCO to make the AVR system clock and use a pot on the 74HC4046 VCO input control.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Mar 12, 2010 - 02:44 AM
10k+ Postman


Joined: Sep 04, 2002
Posts: 13373
Location: Orlando Florida

If you have an 8 bit parallel dac, you can probably feed it in a 1 or 2 usec loop using a 16 or 20 mhz avr. Lets say 1 usec... 20 instructions or so.... A 10 step sin wave will look steppy in front of the filter, but it should look pretty good after it, at about 100khz. You can get 6db per bit attenuation amplitude control by shifting right just before stuffing the sample in the dac.

_________________
Imagecraft compiler user
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
Simonetta
PostPosted: Mar 12, 2010 - 04:10 PM
Resident


Joined: Mar 12, 2003
Posts: 620
Location: Portland, Oregon USA

If you only need three or four selectable frequencies in the range of 5KHz, 50KHz, and 100KHz, then a microprocessor shouldn't be the foundation of your circuit. It would be better to use op-amps, resistors, capacitors, and a flashlight bulb to make an old-fashioned radio-style oscillator circuit. One circuit for each frequency. Or, if I may repeat my suggestion from a previous post, use a function-generator chip like the MAX038 or XR2206 with a fixed resistor/capacitor combination for each frequency needed.
This frequency range is too slow to use specialized digital RF signal generators and too fast to be generated by microcontrollers. The cost of a serial DAC would be the same for an XR2206 and you still have to write, debug, and document all the code to get it to work.

Best of luck.
 
 View user's profile Send private message  
Reply with quote Back to top
Makerdoer
PostPosted: Mar 12, 2010 - 06:26 PM
Newbie


Joined: Mar 11, 2010
Posts: 6


How about attiny26? It has fast PWM included. Could this be used for 100kHz sine formation? ...and yes certain measurements are done at the same time.
 
 View user's profile Send private message  
Reply with quote Back to top
leon_heller
PostPosted: Mar 12, 2010 - 06:44 PM
Raving lunatic


Joined: Jul 27, 2001
Posts: 5142
Location: St. Leonards-on-Sea (UK)

A software DDS can easily cope with those frequencies.

_________________
Leon Heller
G1HSM
leon355@btinternet.com
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
Makerdoer
PostPosted: Mar 12, 2010 - 06:56 PM
Newbie


Joined: Mar 11, 2010
Posts: 6


Yea, i know. But i'm looking for other options now.
 
 View user's profile Send private message  
Reply with quote Back to top
RES
PostPosted: Mar 12, 2010 - 07:41 PM
Resident


Joined: Dec 31, 2005
Posts: 632


Makerdoer wrote:
How about attiny26? It has fast PWM included. Could this be used for 100kHz sine formation? ...and yes certain measurements are done at the same time.


Using the AVR's High-speed PWM
http://www.avrfreaks.net/index.php?modu ... tem_id=498

_________________
RES - http://www.attiny.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
ganzziani
PostPosted: Mar 12, 2010 - 08:07 PM
Hangaround


Joined: Jun 16, 2006
Posts: 229
Location: Palmetto, FL

Simonetta wrote:
use a function-generator chip like the MAX038 or XR2206
The MAX038 has been obsolete for a long time, I used to like this part.
The XR2206 only goes to 1MHz, which should be fine for the OP. Another solution is using an XMEGA which has an integrated DAC. I have implemented an arbitrary waveform generator using an XMEGA. You can look at the code and see some videos if interested:
http://www.gabotronics.com/development-boards/xmega-xmultikit.htm
The great thing about it is that it uses the DMA, so once you set it, up it just runs in the background without CPU intervention, unlike the DDS.

_________________
www.gabotronics.com
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits