Cheap sine wave, and DAC

Go To Last Post
32 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Moderators, please post this to a more appropriate group if necessary. * Done. *

 

I've seen a few posts here about generating a sine wave from an AVR. These usually include a look-up table in flash and a DAC chip.  I'm using an Atmega128 at 16MHz.

 

1) If you don't need a perfect sine wave, but just want an audio tone output which isn't clipped, you can put a two-stage R/C filter on the output pin of a timer, say TIMER3_COMPA.  The output of the R/C filter will give an approximation of a sine wave for audio purposes.

 

2) A cheap SPI DAC can be made from a Microchip MCP4261, which has two digital potentiometers.  It can be accessed at 10MHz and requires no external components, though I put an Op Amp voltage follower after my wipers.

 

These two suggestions are not related; I'm not suggesting audio over DAC.

The comma in the title is an Oxford comma.

 

I can supply code and schematics if anyone is interested.

Last Edited: Wed. Jan 12, 2022 - 03:36 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Was there a question in that, or did I just miss it?

 

Jim

 

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No question, I just thought I'd share what I learned during my project.

If there is any interest in making a dual 256-bit SPI DAC for a couple of AU$, or a nice-sounding audio output for just a few lines of code and a dollar, I can post my tested code and schematic.

If my posting is inappropriate in some way, I can remove it.

I'm only trying to be helpful.

 

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

George and Mildred wrote:
a nice-sounding audio output

Could you quantify that?

 

What frequency range is achievable? What sample rate?

 

It sounds like it'd be a lot of work for the processor as it's going to have to be constantly updating the wiper position(s) at the sample rate.

 

How does it compare to PWM - which is probably the more "conventional" approach?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you or I may have caused confusion.

I'm offering a solution to a nice sinewave-ish output, and the DAC suggestion is a separate thing. 

I'm not offering a solution to audio over DAC; that's not implied in my post.

I'm sorry if you inferred that.

 

Peter

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the clarification - I wasn't sure.

 

But the questions/comments still stand:

 

What frequency range of the sinewave is achievable? What is the step rate?

 

It sounds like it'd be a lot of work for the processor as it's going to have to be constantly updating the wiper position(s) at the step rate.

 

How does it compare to PWM - which is probably the more "conventional" approach?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Perhaps if the OP just posted the code and schematics people could make up their own mind?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


George and Mildred wrote:

I think you or I may have caused confusion.

I'm offering a solution to a nice sinewave-ish output, and the DAC suggestion is a separate thing. 

I'm not offering a solution to audio over DAC; that's not implied in my post.

I'm sorry if you inferred that.

 

Peter

Are you aware of Atmel's application note AVR335 ? Aer you proposing something different to that? It shows how to add a filter to the output of a timer PWM to "smooth" the signal.

 

AVR335: https://community-storage.elemen...

 

specifically this bit...

 

Last Edited: Tue. Jan 11, 2022 - 12:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

He's just talking about a single sinewave; eg,

 

 

EDIT

 

Diagram stolen from here: http://www.learningaboutelectronics.com/Articles/Square-to-sine-wave-converter-circuit.php

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Jan 11, 2022 - 12:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

in #1, George and Mildred wrote:
Microchip MCP4261

in #3, George and Mildred wrote:
256-bit (sic?) SPI DAC

The MCP4261 is an 8-bit pot - giving 256 steps.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a lot of time for people who ask sensible questions.

 

What frequency range of the sinewave is achievable? What is the step rate?

 0 to 15,00 at lest, without interrupts.

It sounds like it'd be a lot of work for the processor as it's going to have to be constantly updating the wiper position(s) at the step rate.

If you had read my posts you'd realize I'm not suggesting audio over DAC.  

How does it compare to PWM - which is probably the more "conventional" approach?

Just a few lines of C to turn it on and off, set the frequency and use the output pin as an analogue input to test the volume.

 

Look folks, I'm just offering a few lines of code and a few dollars of hardware.

If you want to flame me, I'll just sit by and let you do it.  It doesn't really bother me, I just thought my post would be of interest.

 

peter

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes.  It doesn't need all that crap.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes.  My job here is done thank you.

 

Peter

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

George and MildredI wrote:

What frequency range of the sinewave is achievable? What is the step rate?

 0 to 15,00 at lest, without interrupts.

Was there a typo there? Is that frequency of the sinewave, or the step rate (ie, update rate of the pot) ?

 

 

George and Mildred wrote:
If you had read my posts you'd realize I'm not suggesting audio over DAC.  

I have and I do - but the observation still stands.

 

What you're doing, I presume, is updating the wiper position to give you the steps to approximate a sinewave:

 

 

So, at every step, you have to write to the pot to update it - and that sounds like quite a lot of work for the processor.

 

EDIT: fair enough, (probably) no more work than updating a DAC at each step.

 

 

We're not trying to flame you - just get a clear idea of what you're suggesting/offering.

 

As Brian said, posting the code & schematic should make it all clear ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Jan 11, 2022 - 12:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Peter,

 

Please upload your schematic and the source code. I am interested.

 

Cheers,

 

Ross

 

Ross McKenzie, Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hum.

Last Edited: Tue. Jan 11, 2022 - 02:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm not suggesting audio over DAC. 

What you're doing, I presume, is updating the wiper position to give you the steps to approximate a sinewave:

I'm not. Please do me a courtesy and read my posts.

Pots and audio are separate; I have made this clear.  Your presumption is incorrect.

 

I'm frankly getting tired of this thread.

 

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I believe everyone here is an expert on my subject so I'll just exit stage left.

What a wonderful day it has been sharing an idea with everyone.

 

Peter

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

George and Mildred wrote:

What a wonderful day it has been sharing an idea with everyone.

 

Hi Peter, I think people are interested but you haven't actually shared anything. That information vacuum has simply prompted people to speculate, sometimes incorrectly.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
but you haven't actually shared anything.
That. ;-)

 

(we can only guess at what you are talking about/suggesting until something is actually presented - I suspect this post started life on the tail end of something else which might have explained more context which we are not missing as the thread has been split off).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

George and Mildred wrote:
I can supply code and schematics if anyone is interested.

You have our interest as witnessed by the questions, will you please post the these items.....

 

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

to answer my own question

George and Mildred wrote:
 MCP4261 ... can be accessed at 10MHz

that's the SPI max clock (at 5V).

 

Write command seems to be 16 bits, so max update rate ~ 625kHz (or somewhat less, as there's some delays required after CS is asserted, and before it's released)

 

There are 8-bit commands for increment/decrement, so could go faster in cases where they could be used.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should get some of these I2S DAC ICs:https://www.ebay.com/itm/1916742...

 

16-bit dual serial DAC CD-audio quality for less than $1 each.  Why fool around with software waveforms?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

But no AVR has I2S so you would have to bit-bang it? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
CD-audio quality

I think George & Mildred may have hinted that he/she/they aren't actually talking about audio - "CD-Quality" or otherwise.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil wrote:
George & Mildred may have hinted that he/she/they aren't actually talking about audio

but here's the thing:

George and Mildred wrote:
but just want an audio tone output which isn't clipped,
George and Mildred wrote:
sine wave for audio purposes.

I fear I have been misled by the (multiple) use of the word "audio" ?!?

 

I really would like to see some more details in this thread to get some idea about what we can only just speculate about so far!

Last Edited: Tue. Jan 11, 2022 - 03:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OP's first suggestion was fairly clear until the end of its second sentence.

"for audio purposes" seems to have driven some off the deep end.

In this case, "audio" just means relating to sound,

not multi-channel high fidelity audio equipment.

 

OP's other suggestion might require more detail,

but I expect those details are in the Microchip MCP4261 spec sheet.

Everyone here knows how to use SPI, correct?

 

OP should post the offered code and schematics.

The code and schematics for OP's first suggestion should be in one post.

The code and schematics for OP's second suggestion should be in a separate post.

 

I really do not understand why posters on this thread are determined to conflate the two suggestions.

OP explicitly stated that the two suggestions are separate.

The questions assuming otherwise seemed to have caused an understandable frustration.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:
OP explicitly stated that the two suggestions are separate.

I think that part was added later - it wasn't in the original OP ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Jan 11, 2022 - 04:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

or SPI (create codec frame signal from SPI clock and a port bit)

Interfacing an I2S Device to an MSP430 Device - slaa449a.pdf (Texas Instruments)

[mid-page 6]

6 Conclusion

...

...  and can be implemented simply and inexpensively using a 4-bit counter, a 'D' type flip-flop, and a dual inverter gate.

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
skeeve wrote:
OP explicitly stated that the two suggestions are separate.

I think that part was added later - it wasn't in the original OP ...

The conflation continued after 6:14 AM,

but that probably just shows why one should not edit posts for more than tipos and such.

 

edit: the "and such"es should probably be labeled.

Moderation in all things. -- ancient proverb

Last Edited: Tue. Jan 11, 2022 - 05:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 or a nice-sounding audio output for just a few lines of code and a dollar

would be much clearer to say

 

 or a nice-sounding sinewave output for just a few lines of code and a dollar  

 

That seems to be what momentarily sent things hurtling off the train track.

A nice offer & gesture that unfortunately landed in the ditch of history.

 

hmmm ???

https://en.wikipedia.org/wiki/Ge...

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Jan 11, 2022 - 07:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As Peter has left the room there appears to be little or no benefit in continuing with endless speculation.

 

The key is in the lock. If Peter wants it unlocked, he can PM me.

 

Ross (Moderator)

Ross McKenzie, Melbourne Australia

Topic locked