Shop OBEX P1 Docs P2 Docs Learn Events
PWMPAL Sine wave generation. — Parallax Forums

PWMPAL Sine wave generation.

VitorVitor Posts: 13
edited 2007-01-12 18:34 in BASIC Stamp
I purchased the PWMPAL to generate sine waves and after reviewing the documentation I did not find much besides the sample to light the tri-color LED.
I purchased the PWMPAL because it was advertised for producing sine waves.


I need to generate four sine waves from 1 Hz to 600 Hz (or 1,000 Hz, this is not critical). This is to simulate four wheel speed sensors in a car.
I need to ramp the signals up to 600 Hz over a period of 60 sec (not critical).
Then, I drop one of the signals about 50% (not critical) and keep all other three at original frequency (one wheel locks up while applying the brakes).
I do the same for the other 3 signals over a period of time.
I reduce the frequency down to zero (or close to) to simulate stopping the car.

With this code I get 200 Hz
SEROUT 0, 6, [noparse][[/noparse]"!PWMM1", 100, 0, 100, 0]
SEROUT 0, 6, [noparse][[/noparse]"!PWMSS", %00010000]
I have tried a resistor out of P12, pwmm1 and grounding the signal thru a 1uf capacitor as the sample in page 37, Understanding signals.
I get a shark fin wave.
I changed the frequency, resitors, capacitors to smooth the wave but the shark fin is the best I can do.

Am I missing something?

In the tri-color example, two outputs are used.

Do I need two outputs to generate one sine wave? If so, I will only be able to produce 2 sinewaves?
Can I generate a fairly decent sinewave out of every motor output?

Help will be greatly appreciated if you have a sample circuit and/or code.

Thank you.




·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-12 06:16
    The PWMPAL is not intended for generating sine waves. The documentation says it generates square waves and AC "square waves". The AC is generated by using two outputs of opposite polarity and connecting your load (an LED often) between the two outputs.

    The frequency (and PWM) is set by specifying the on time and off time in 25us units. Since you probably want a symmetric pulse, the on time and off time would be the same. To get 600 Hz, you would need a total cycle time of 1/600 or about 66 units. The on time should be 33 units and the off time should be 33 units which will give an output frequency of 606Hz. For a 1Hz frequency, you will need 10,000 units for the on time and 10,000 units for the off time. This is supplied as two bytes as you've noticed before in the documentation. To ramp this up, you will need to decrease the units from 10,000 to 33 over the 60 second period. You'll have to decide what kind of change curve you want and what kind of formula or table you need to get the changes you want.

    You might be able to take the triangle waveform (shark fin) and run it through a converter like this one (cut and paste the diagram and use a fixed width font like Courier to view it).
    Newsgroups: sci.electronics.design
    Subject: Re: Anyone know of a Squarewave to Sinewave converter IC?
    Date: Mon, 06 May 1996 17:35:32 -0400
    Organization: Lockheed Sanders
    
    > 
    > That's what I'm looking for--a chip that accepts a squarewave input of,
    > say, .1 to 1.0 Mhz and outputs a sine wave of the same frequency...
    > 
    > Dave
    
    Generating a true sinewave from an input square wave over an octave in
    frequency is not simple.  A sinewave oscillator would be easy.  Filtering
    to the fundimental would be easy for a frequency range less than an
    octive.
        The best I was able to come up with is the attached circuit which
    approximately generates the sine of the input voltage.  Integrating the
    squarewave to a triangle wave and inputting it to the circuit below 
    should work.  
        There is another way to approximate a sine wave using zener diodes
    in an op amp circuit to form a straight line approximation, but I
    couldn't find that circuit.
        Anyway, here is one method:
    
    The following is taken from "Functional Circuits" , Yu Jen Wong and 
    William Ott, McGraw-Hill (c) 1976 pp207-209
    
    
              +----------------------------------------------+
              |                                              |
              R1                                             |
              |                           +-R6-+-----R5------+
              +-----R3--+                 v    |             |
              |  | \    |       +--------+     |  | \        |
    e-in -+-R2+--|- \   |  A    |  mult  |     +--|- \       |
          |      |op >--+-------| x    xy|  C     |op >------+-e-out
          |    +-|+ /           |      --|---R4-+-|+ /
          |    | | /          +-| y    10|      | | /
          |    v              | +--------+      |
          |     +--------+    |                 |
          |     |  mult  |    |                 |
          +-----| x    xy| B  |                 |
          |     |      --|----+    +-R7---------+
          +-----| y    10|         |            |
          |     +--------+         |            R8
          |                        |            |
          +------------------------+            v
    
    e-in = triangle wave
    A =  -5.212*ein - 4.520*eout
    B =  ein^2/10
    C =  (-ein^2/100)* (5.212*ein+4.520*eout)
    e-out =  (1.0287*ein - 0.10423*ein^3) / (1 + 0.0904*ein^2)
          =  1.0287ein - ein^2(0.10423*ein+0.0904*eout)
    R1 = 11.06 K
    R2 = 9.594 K
    R3 = 50 K
    R4 = 4.857 K
    R5 = 10 K
    R6 = 3.333 K
    R7 = 9.442 K
    R8 = 10 K
    op - operational amplifier
    mult - four quadrant multiplier chip
    v  - ground
    
    IMPLICIT FEEDBACK AND FOUR QUADRANT SINE GENERATOR
    
    The Tchebysheff polynomials method .. can be applied to approximate a 
    sine function in all four quadrents.  Two terms are generally not enough 
    to achieve reasonable accuracies.  If three terms are retained, the 
    approximation, as given in [noparse][[/noparse]the equation below], will yield less than 
    0.75 percent error from -180 degrees to +180 degrees.
         eo=sin(ei) ~= 0.98402ei - 0.15328ei^3 + 0.0054523ei^5^
    However, if implicit feedback is utilized, the four quadrent sine 
    function can be approximated by:
         eo= sin(ei)
                 1.0287ei - 0.10423ei^3
            ~=  -------------------------
                     1 + 0.0904ei^2
    The approxmation has less than 1.25 percent error in four quadrents.  In 
    order to model [noparse][[/noparse]the above equation], rewrite it as
         eo = 1.0287ei - ei^2(0.10423ei + 0.0904eo)
    The circuit...requires two multipliers and two operational amplifiers.
    
    


    There's another converter described here (www.oldtemecula.com/theremin/sixthvoice/index.htm)

    If you search the web for "square wave to sine wave" you'll find lots of converters.
  • VitorVitor Posts: 13
    edited 2007-01-12 18:34
    Thank you.
    I have under control the portion for frequency and duty and curve for modifying.
    I missed that tiny little portion of the PWMPAL spec. AC square wave.

    In the tri-color led sample.
    I hooked my scope to it but I has having a problem·and I did not see the wave.

    What portion of the code prompts PWMM2 to generate the negative voltage (opposite polarity of PWMM1) and not just another PWM positive square signal?

    And, out of curiosity, why are the duration values in·hexadecimal·in the PWMPAL documentation samples?

    Thank you again.
    ·
Sign In or Register to comment.