Generating Sine Wave

Discussion in 'The Projects Forum' started by MCU88, Mar 30, 2015.

  1. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    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...
     
  2. #12

    Expert

    Nov 30, 2010
    16,253
    6,752
    Vo = Vp sin theta
     
  3. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    So how many plots should I do to get an satisfactory wave?
     
  4. #12

    Expert

    Nov 30, 2010
    16,253
    6,752
    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.
     
  5. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    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?
     
  6. OBW0549

    Well-Known Member

    Mar 2, 2015
    1,301
    880
    MCU88 likes this.
  7. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    Thanks buddy...
    Could I use some filter, say butterworth to smooth things out a little?
    Do you have the MS Excel file by any chance?...
     
  8. Alec_t

    AAC Fanatic!

    Sep 17, 2013
    5,773
    1,103
    Your table only needs to contain sine values for one quadrant.
     
  9. #12

    Expert

    Nov 30, 2010
    16,253
    6,752
    As an analog oriented person, I prefer a tuned LC tank.
     
  10. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    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.
     
  11. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,675
    899
    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: Mar 30, 2015
  12. OBW0549

    Well-Known Member

    Mar 2, 2015
    1,301
    880
    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.

    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...
     
  13. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    Yeah I guess. Many ways to skin an cat.
     
  14. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    So I just need to plot value for 90 degrees of the wave?
     
  15. OBW0549

    Well-Known Member

    Mar 2, 2015
    1,301
    880
    Yup.
     
  16. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    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.
     
  17. #12

    Expert

    Nov 30, 2010
    16,253
    6,752
    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.
     
  18. MCU88

    Thread Starter Member

    Mar 12, 2015
    360
    35
    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...
     
  19. #12

    Expert

    Nov 30, 2010
    16,253
    6,752
    I believe that is achievable. I bought my EICO 390 signal generator in 1977.
     
  20. OBW0549

    Well-Known Member

    Mar 2, 2015
    1,301
    880
    Yup.

    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...
     
Loading...