Shop OBEX P1 Docs P2 Docs Learn Events
Sine wave generator — Parallax Forums

Sine wave generator

PhilldapillPhilldapill Posts: 1,283
edited 2008-07-07 11:56 in Propeller 1
I'm trying to make a type of signal generator that produces a varying PWM signa. The duty cycle of this varying pulse train will be a function of the trig function sine. The following picture should show what I'm talking about.
http://en.wikipedia.org/wiki/Image:Pwm.png

In essence, if you were to overlay a sinewave, like the one shown, the high amplitude parts would coorespond to a longer pulse. When the sinewave is at it's absolute peak, the duty cycle would be 100%, and at it's lowest - 0%. What I've done is created a table in Excel that has all the computed values of the sinewave. I would use the ROM sine values, but in this application, I don't think that will work for various reasons. Now, the values in the excel sheet have been computed and divided up into·128 divisions. These values only represent 90 degrees of the sinewave, since, by definition, the function is symmetrical about the x axis and 90 degree points.

What I've been trying to do is code a PASM program that produces these pulses on TWO pins. One pin will do 180 degrees of the sinewave, then the other pin will repeat the exact same pulsetrain. These two pins will then drive a couple of high speed opto isolators, which will then drive a HIGH power H-bridge(yet to be designed). I am very new to PASM(1 week into it) and am not too experienced, yet, so forgive me for any lack of understanding.

My program flow goes something like this...

Fetch the period of the PWM signal
   repeat
      Fetch the new ON time(duty cycle * period which is already stored as a table)
      Calculate the OFF time(period - ON time)
      Turn pin on for ON clock ticks
      Turn pin off for OFF clock ticks
      Flip the output pin(A to B, or B to A)


Now, this may seem easy enough at first glance, which I thought at first. However, if you start picking apart the flow, you might see a problem. Not only does it take a few clocks to execute the instruction, thus increasing the time the pin is off, but it also leaves very little room to execute the code in the first place when the duty cycle approaches 100% or 0%. If you turn the pin on, then make calculations, then·turn the pin off, you must do all the instructions during the on time. The same applies for the off time vs. on time. The problem comes in either way.

I'm looking for a way to get all the instructions done both ways. The duty cycle MUST approach 0% AND 100%.

My period will always be 2604 clocks. This gives a freq of 30.72kHz, which produces the sinewave at 60Hz(do the math if you want - 30.72kHz/512 periods). Now, if I split my code up into·4 nearly identical segments, each being from 0-45­°, 45°-90°, 90°-135°, and 135°-180°, I think I could make it work.

In the first segment, I would be getting the next ON time value in the hub. In this segment, I would be doing the calculations during the off time, as I have more than 1302 clocks(actually anywhere from 2604 to 1302 since the duty will be from 0-50%). The next segment, again getting the NEXT value, but doing the calculations during the ON time since that time will always be greater than the off time. The same goes for the third, but it will be getting the PREVIOUS value, and the fourth, again the previous, but doing the calcuations during the OFF time. The cycle will then repeat on the other pin and again repeat on the other, etc.



Anyway, now that the explaining is done. I will be needing some advice, code wise, through this post. Please reply if you have ANY suggestions whatsoever. Thanks guys!

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-07-02 19:27
    My PWM object might prove useful here.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-07-02 19:41
    That's Awesome Phil!

    Buuuut, does it have the resolution needed for this? My table is filled with values ranging from 0 to 2604. Plus, this PWM train is at 30.72kHz. I suppose I could reduce my resolution to 15.36kHz, but thats a big difference in steps.
  • LeonLeon Posts: 7,620
    edited 2008-07-02 19:58
    A software DDS with a couple of DACs might be better than using PWM.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-07-02 20:10
    Leon, what is a software DDS? I am unfamiliar with the term. If you are suggesting using some sort of DAC to output a sinwave directly, I've done that before. Easy stuff. What I need to do is ultimately use this chopped sinewave signal to drive a big H-bridge and feed the current through a transformer to step it up. I will then use some filtering to obtain a fairly clean power sinewave output to power things with.

    The hardware will come later, once I get this darn thing to work right...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-07-02 20:13
    Philldapill,

    If this is for your DC-AC inverter, I doubt you'll need more than 8 bits of resolution, if that much. Can you scale the values in your table to fit?

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-07-02 20:20
    I can scale them yes. Currently, I am using 8-bit resolution for the number of pulses per half phase. Well, actually, come to think of it, I am using 8-bit for the amplitude as well. In my spreadsheet, I specifically chopped the amplitude values into 256 increments, and rounded them to the nearest integer. I guess this will work after all. The only thing about this, is that with every pulse, the duty cycle changes for the next one. Is your object fast enough to do this?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-07-02 20:44
    Fast enough? Good question. You'll just have to try it and see...

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Still some PropSTICK Kit bare PCBs left!
  • LeonLeon Posts: 7,620
    edited 2008-07-02 20:47
    Philldapill said...
    Leon, what is a software DDS? I am unfamiliar with the term. If you are suggesting using some sort of DAC to output a sinwave directly, I've done that before. Easy stuff. What I need to do is ultimately use this chopped sinewave signal to drive a big H-bridge and feed the current through a transformer to step it up. I will then use some filtering to obtain a fairly clean power sinewave output to power things with.

    The hardware will come later, once I get this darn thing to work right...

    A software DDS uses a phase accumulator etc. to generate waveforms:

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

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • ValidatorValidator Posts: 5
    edited 2008-07-03 12:31
    The DDS is OK for low level signal generation but the output is analogue and not suited to PWM drive.

    I am following this thread with interest as I currently have a project where I have to generate a reasonably clean·sine wave of 40 to 80Hz in 0.1Hz steps.· The output voltage has to be variable up to 150 volts RMS.· Luckily the power required is only about 20 watts at full output.· The unit has to be as·light and efficient as possible so analogue techniques are not acceptable.

    I was looking at a centre tapped primary for the transformer as the drive requirement is simpler however the efficiency is reduced.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-07-03 17:07
    Well, Validator, I feel very validated in using PWM. icon9.gif

    I am not very familiar with whole harmonic part of this, but I have a feeling that I will have to eventually change the number of pulses to something with less harmonic generation. If anyone has some experience in this area, please, let me know. I just think that I will need an odd number of pulses in this, instead of even...

    By the way, Validator, what are you using yours for? Mine is for a DC-AC inverter.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-03 17:57
    Don Lancaster has made a study of ways to digitally generate quality sine waves (www.tinaja.com/glib/msintro1.pdf and www.tinaja.com/glib/msinexec.pdf). His website has a calculator for the values needed and Spin or assembly language should be able to handle this nicely at 60Hz depending on the purity you want.

    Post Edited (Mike Green) : 7/3/2008 6:07:21 PM GMT
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-07-03 18:38
    This stuff is amazing, Mike! I don't have a strong background in fourier analysis, but things he talks about in this are right on track with what I have been thinking, and have found. He mentioned the problem with the timing issue I brought up, and suggests a solution. I love it. Thanks again Mike!
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2008-07-03 21:05
    There was another thread some time ago about Don Lancaster's magic sine waves you might like to look at. It has a link to Spin code and also to a timing calculator:

    http://forums.parallax.com/forums/default.aspx?f=25&p=1&m=174316

    Given the hype, though, I have yet to see any real practical applications described with the magic sinewave approach either for motor drive or for power inverters.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ValidatorValidator Posts: 5
    edited 2008-07-04 12:43
    The basic need is for a sine wave generator with a 'bit of power' behind it and the output has to be isolated from the incoming line voltage so I was thinking along the lines of a SMPS to 24 volts DC then a switcher to generate the input to a step up transformer.

    I have also been looking at doing it with a class-D amplifier but·all the·designs I have seen so far·require positive and negative supplies which would add to the cost.

    I only need two units so designing transformers and·complex electronics·would not be viable.

    Thanks for the links Tracey.· I will have a play with the information.· As you understand the program already can you explain please·how I might·adapt the code for·the range I need? (40 to 80Hz in 0.1Hz steps)
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2008-07-05 17:13
    The output frequency is given by the formula
    FRQx = 2^32 * Fout/CLKFREQ

    So for example, 60 Hz is
    FRQx = 2^32 * 60 / 80E6 = 3221
    and the range of FRQx values is 2147 to 4295 for an output frequency of 40-80 Hz.

    If you need to calculate that in real time, here is one way to do it in Spin,

    afreq := frqVal(myfreq, 80_000_000) + 5  / 10 ' with myfreq in tenths, e.g. 603 for 60.3 Hz, then round off 
    
    PRI frqVal(a, b) : f            ' calculate a/b * 2^32, binary long division
      repeat 32                      'calculate f: a/b = f/(2^32)
        a <<= 1
        f <<= 1
        if a => b
          a -= b
          f++
    



    Don't get too far into it though without trying it to see if you can get the required power output. I've heard from a respected correspondent that magic sines look good on paper but don't give the expected efficiency in practice.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-07-05 20:05
    Tracy, what is a good way to drive these? If I were to drive the sinewave off of a 12V battery, and would only need to draw about 5A from the battery, would a simple H-bridge do? What kind of filtering is needed?
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2008-07-05 23:29
    Well, yes, I think it would take and H bridge, but it has to be one that has three states, forward, reverse and off. The magicsines2.spin code as posted works on two Prop output pins, two half bridges. Both pins are low when the output is supposed to be off, and one pin goes high for forward and the other pin goes high for reverse.

    One advantage of the magic sines approach in theory is that filtering is needed only for high harmonics. Typically the first significant harmonic is the 23rd , and the amplitude of the harmonic is always less than the fundamental. Don has said in reference to motors that the motor inductance provides one pole and the intertia another pole and additional filtering may not be necessary. An inverter may or may not need additional filtering. Is this for something like a telescope drive?

    The trouble is, I don't want to offer practical advice about the load. I came into this because I've always been intrigued with the math, ever since Don started writing about it, and it was clear that the Propeller is preeminently suited to meeting the tight timing requirements with cogs to spare. I haven't really built anything with it and am interested in your experiences if you do pursue it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • ValidatorValidator Posts: 5
    edited 2008-07-07 11:56
    Thank's Tracey, maths is definately my weak point, much appreciated.
Sign In or Register to comment.