Sine wave


Closed Thread
Results 1 to 9 of 9

Thread: Sine wave

  1. #1
    Join Date
    Mar 2004
    Posts
    74

    Default Sine wave

    Hi all,
    I am trying to generate a sine wave between 50Hz and 300 Hz. I am trying to understand the concept better so that I can write the code. I have set up a basic resistor ladder using the following values 12k,24k,47K,91K. I have calculated all possible votage output voltages and put into excel to see if I can chart a sine wave. It does not look like a sine wave. Do I need to vary the timing between each sample based on the inv sine? or do I need to calculate all sine values and use the closed value that I can get from the resistor ladder?
    some help to go in the right direction would be appreciated
    thanks
    Darren

  2. #2
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    I have tried to plot out the voltages that I found on this website when searching for information. http://www.ke4nyv.com/picprojects.htm
    The plot that excel created is not a sine wave(see attached). Do I need to have variable timming between each output? Is the information correct? can some one let me know where I might be going wrong in understanding?
    Thanks
    Attached Files Attached Files

  3. #3
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default

    Darren

    The basic technique is to quantize and store the waveform in a table. This is how I do it.

    Write a small program in any language of your choice and print out a table of values that define a sine wave. You specify the number of steps in the waveform(time slices) and you specify the maximum value the quantum can have(in your case 16).

    Once you have this table, all you have to do is to setup a timer to interrupt you at a fixed rate (timeslice) and you put out one element of the table at each slice. In this way, you will be able to approximate the waveform you created in memory.

    To change the frequency of this waveform, all you have to do is change the timeslice interval. Smaller intervals will give you a higher frequency. However, be warned, code takes a finite time to execute and you will need to be sure you can generate the 300Hz that you need. I am pretty sure the PIC can do upto around 4KHz with its internal 4MHz oscillator.

    This is just one technique of creating the sine wave. There are other techniques like phase accumulator and Direct Digital Synthesis which you may like to read up.

  4. #4
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Pwm

    How about doing sine wave with PWM or HPWM?
    Google Search: pwm sine wave

    BR,
    -Gusse-

  5. #5
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    I understand how to create a sine wave ( I have used the sin function in excel to create a table of values, then graphed the values to display sine wave). I am just not sure how to convert this to the required output. If I step through the output sequence it appears to be linear. Do I need to convert the values from my excel to the nearest equivelent in the values from my resistor ladder? or is there some other way to do this?

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Darren,

    I've played with the numbers from the website you started with.
    And either I got it wrong, or that's one "messed up" example.

    To start with, it's not the way an R2R network is supposed to be laid out. Which accounts for the lower amplitude.
    A description of a proper R2R network can be found here ...
    http://en.wikipedia.org/wiki/Resistor_Ladder
    But his way does have fewer parts.

    Barring the fact that his binary numbers are all backwards ...
    With the program from that web page, it doesn't make a Sine wave.
    But it's not a bad "Triangular" wave.

    Here's a spreadsheet of my results. (html)

    http://www.pbpgroup.com/Calculators/...R_SineWave.htm

    The top section calculates the voltage output for each of the digital possibilities.
    These numbers seem quite different than what the web page author came up with.

    The resistor values can be changed to test different conditions. If you change them to exact multiples (1000, 2000, 4000, 8000), you can see that Vout is very linear, although still low amplitude.

    The section below the graphs calculates Vout for the "SIN" sequence.
    These numbers can be changed, and might help test your calculated SIN values.

    But I have to wonder if a 4-bit sine wave at 50hz is going to be what you want. It'll look more like a staircase than a sine wave. You could add a lot more capacitance to the filter, but at 300 hz it may be too much.

    hth,
    DT

  7. #7
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    Thanks Daryl,
    I will give it a go when I get a spare hour. One concept that I had completely overlooked is the fact that the outputs go low (silly me). I was only calculating using the hi 5v state and not the low 0v (GND) state which will completly change my calculations.
    Thanks, I will let you know how I go

  8. #8
    Join Date
    Mar 2004
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    Daryl,
    I had a go using your data and got the same result (not what I was hoping for a sine wave). I decided to increase to 6 bit and have another stab at it. I got the same triangle wave that you had on your example. (still not the expected sine wave). I then divided 180 degrees down to the same number of samples, used the sine function, added 1 (then no negative voltages), and scaled to same as actual voltages. I had a new set of data that I used to base my output on. essentialy I picked the closest actual value to the calculated value. I graphed the new data and got the attached waveform (more like a sine wave). This was acheieved by repeating some of the samples with the same voltage and on some skiping voltages. Now I am not sure how the best way to implement it is? do I create a table using this data and simply step through? or do I use the sine function in the pic basic to calculate the output?
    Attached Files Attached Files
    Last edited by Darrenmac; - 18th May 2009 at 02:01. Reason: forgot attachment

  9. #9
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    The SIN function will be the easiest.
    It's what the FREQOUT statement uses to generate its sine waves.

    It's essentially a pre-made lookup command. So it'll save you some work.
    The result is an 8-bit number so you could just shift right twice and have your 6-bit value. But 8-bit would be better at such low frequencies.

    You may also want to look at this from sirvo ...
    http://www.picbasic.co.uk/forum/show...5843#post65843

    I've pointed to the video of the finished project, but further up you can see how he did it.

    It uses a lookup, and 10-bit PWM (with 8-bit values) instead of R2R.
    I wish I had a scope like that.
    DT

Similar Threads

  1. To Generate Sine Wave Using PCPWM of PIC18F4331 (Issue)
    By Cyborg in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd March 2014, 13:39
  2. Generating 3-phase sine wave..
    By sirvo in forum mel PIC BASIC Pro
    Replies: 38
    Last Post: - 6th November 2008, 14:07
  3. square to sine wave help
    By TONIGALEA in forum General
    Replies: 6
    Last Post: - 2nd July 2006, 09:32
  4. Replies: 1
    Last Post: - 16th February 2005, 20:05
  5. PBP Dampened Sine Wave
    By Keith55555 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 3rd April 2004, 20:01

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts