Generating Sine Wave

Thread Starter

MCU88

Joined Mar 12, 2015
358
MCU driving an R2R ladder proceeded with an OPAMP for output ...

Q: How do I generate the table for an sine wave, what is the formula? Is it sin(theto) x VP I have forgotten...
 

#12

Joined Nov 30, 2010
18,224
Your satisfaction is entirely subjective.
Other people know how to do the math for % distortion per number of datapoints.
I will have to defer to them.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Your satisfaction is entirely subjective.
Other people know how to do the math for % distortion per number of datapoints.
I will have to defer to them.
My maths (discrete maths) -- is %55 pass mark at University level. So if I just pulled out paper, pen and a calculator and did say 128 points I might get away with it?
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Thanks buddy...
Short answer: what you can "get away with" in terms of DAC resolution and number of sine table entries depends entirely on what THD level you need to achieve.
Could I use some filter, say butterworth to smooth things out a little?
As for the sine table, just use a spreadsheet.
Do you have the MS Excel file by any chance?...
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
As an analog oriented person, I prefer a tuned LC tank.
Yeah perhaps I'll do an PIC-based digital function generator too. You wanna do the maths and get some credit in the project? I only have 256 bytes of RAM to play with, so I think say maybe 128 points for an lookup table? Probably get away with it.

I recall an project years back where a guy digitally created a rather crude stepped sine wave and feed it through an 3rd order butterworth and it came out nice and clean.
 

jpanhalt

Joined Jan 18, 2008
11,087
Why do you need to use RAM for a look-up table? You have 2048 words in program memory.

John

Edit: Those numbers are for a 16F628A, but that device only has 224 bytes of RAM, not 256 bytes. So, apparently the TS is using another chip and it may well have a different amount of program memory.
 
Last edited:

OBW0549

Joined Mar 2, 2015
3,566
Could I use some filter, say butterworth to smooth things out a little?
Generally, you'll HAVE to use a filter, or your output will have harmonics out the gazoo. Depending on how pure a sine wave you need to generate, anything from a first-order filter to an 8th-order filter might be necessary.

Do you have the MS Excel file by any chance?...
I used to have a whole bunch of 'em, for various DDS projects, but not anymore. They're easy to generate, though.

As Alec_T noted, your table only has to have the DAC values for the first quadrant of the sine wave. First step forward through the table, then step backward; then reverse the sign of the table values while stepping forward again, then stepping backward. Rinse and repeat for each cycle...
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
As Alec_T noted, your table only has to have the DAC values for the first quadrant of the sine wave. First step forward through the table, then step backward; then reverse the sign of the table values while stepping forward again, then stepping backward. Rinse and repeat for each cycle...
So I just need to plot value for 90 degrees of the wave?
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
Just wondering about processing resources / speed for a minute though... now I would need logic in my code to manipulate the table to go from 90 to 180, then to 270 and finally 360 degrees and repeat. Right?

Could I obtain an higher frequency if I did an 360 degree plot? That way I would just be fetching (n) value from the table in RAM and writing straight to the port. Seems like less computing.
 

#12

Joined Nov 30, 2010
18,224
Yeah perhaps I'll do an PIC-based digital function generator too. You wanna do the maths and get some credit in the project? I only have 256 bytes of RAM to play with, so I think say maybe 128 points for an lookup table? Probably get away with it.

I recall an project years back where a guy digitally created a rather crude stepped sine wave and feed it through an 3rd order butterworth and it came out nice and clean.
1) No.
2) When you change frequency, the tuned tank becomes less appropriate. There are digital signal processing chips that work wonders at changing filters according to the frequency.
 

Thread Starter

MCU88

Joined Mar 12, 2015
358
There are digital signal processing chips that work wonders at changing filters according to the frequency.
I don't want to use any specialized chips. I want DIY projects for under $100 that use dead easy to get parts anywhere in the world. The generator would of course be an toy and good for audio / hobby work... perhaps not audio if the distortion is >0.1% But still by far the best oscillator for audio work is the wein bridge. Distortion is like 0.001% or thereabouts?

Easy greasy...
 

OBW0549

Joined Mar 2, 2015
3,566
Just wondering about processing resources / speed for a minute though... now I would need logic in my code to manipulate the table to go from 90 to 180, then to 270 and finally 360 degrees and repeat. Right?
Yup.

Could I obtain an higher frequency if I did an 360 degree plot? That way I would just be fetching (n) value from the table in RAM and writing straight to the port. Seems like less computing.
Yes, representing the full sine wave cycle in your table would give the least amount of work for the CPU. When speed is paramount, that's what I've done. Do put your table in program memory, though...
 
Top