Shop OBEX P1 Docs P2 Docs Learn Events
Does anyone have signal generation code thats output matches the propscope readings? — Parallax Forums

Does anyone have signal generation code thats output matches the propscope readings?

turbosupraturbosupra Posts: 1,088
edited 2010-10-21 20:16 in Propeller 1
I am trying to write some signal generation code that the frequency per second calculated in my code will match what I'm seeing on my propscope.

Currently I'm seeing ~5.4khz on my propscope when by math from the code it should be 6khz

If someone has a known good method that produces a solid XXkhz on their prop scope, I'd appreciate being able to test it. Thanks for reading.

Comments

  • hover1hover1 Posts: 1,929
    edited 2010-10-21 15:00
    With your DAC board plugged in, you should be able to generate your waveforms
    in the Oscilloscope mode. Look on the right where it says generate. Plug in you frequency and push the generate button.

    The output from the DAC card is the BNC connector closest to the Propscope box. Just connect a BNC to BNC (X1) from the DAC card output to Chan 1 Input so you can verify, as seen in the attachment.

    Jim
    turbosupra wrote: »
    I am trying to write some signal generation code that the frequency per second calculated in my code will match what I'm seeing on my propscope.

    Currently I'm seeing ~5.4khz on my propscope when by math from the code it should be 6khz

    If someone has a known good method that produces a solid XXkhz on their prop scope, I'd appreciate being able to test it. Thanks for reading.
    1596 x 792 - 279K
  • hover1hover1 Posts: 1,929
    edited 2010-10-21 15:21
    You could also try Beau's Frequency Synthesis object to produce two independent square waves :

    http://obex.parallax.com/objects/47/

    Jim
    hover1 wrote: »
    With your DAC board plugged in, you should be able to generate your waveforms
    in the Oscilloscope mode. Look on the right where it says generate. Plug in you frequency and push the generate button.

    The output from the DAC card is the BNC connector closest to the Propscope box. Just connect a BNC to BNC (X1) from the DAC card output to Chan 1 Input so you can verify, as seen in the attachment.

    Jim
  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-21 15:22
    Hi Hover1,

    That is perfect except for one thing, I need to be able to generate the signal from my 40pin DIP prop chip output through a pin, and then verify the signal generation math is correct with an identical signal on the oscope.

    For example, if I wanted to generate 6khz I should be able to take the clock frequency and divide it by 12000, or 6000 lows + 6000 highs like in the code below, correct?

    I should see 6khz? But instead I'm seeing 5.53khz with this code as shown in the screen capture.

    I have to change the divisor to about 13070 on both waitcnts to get 6khz? I'm guessing I'm doing something wrong here? Any ideas? (I've also tried it where I'm just using "cnt" instead of a variable set to cnt.)





     
    PUB SixKhzSimulator
    
      'DesiredRPM := 1000
      toothcount := 1
      previouscnt := cnt 
      dira[c_CrankSensorOutputSimPin] := 1
    
    
      repeat
    
        outa[c_CrankSensorOutputSimPin] := 1                ' output high 
        Waitcnt((clkfreq/12000) + previouscnt)           ' wait half the tooth count or 5 degrees of rotation
        toothcount := toothcount + 18
        previouscnt := cnt
        outa[c_CrankSensorOutputSimPin] := 0                ' output low
        Waitcnt((clkfreq/12000) + previouscnt)           ' wait half the tooth count or 5 degrees of rotation
        previouscnt := cnt
    
    
    1258 x 714 - 185K
  • hover1hover1 Posts: 1,929
    edited 2010-10-21 15:34
    See my second post for generating frequency off the Prop chip.

    Also , upgrade Propscope software to 2.00. You are pretty far behind.

    http://forums.parallax.com/showthread.php?t=123627

    Jim


    turbosupra wrote: »
    Hi Hover1,

    That is perfect except for one thing, I need to be able to generate the signal from my 40pin DIP prop chip output through a pin, and then verify the signal generation math is correct with an identical signal on the oscope.

    For example, if I wanted to generate 6khz I should be able to take the clock frequency and divide it by 12000, or 6000 lows + 6000 highs like in the code below, correct?

    I should see 6khz? But instead I'm seeing 5.53khz with this code as shown in the screen capture.

    I have to change the divisor to about 13070 on both waitcnts to get 6khz? I'm guessing I'm doing something wrong here? Any ideas? (I've also tried it where I'm just using "cnt" instead of a variable set to cnt.)





     
    PUB SixKhzSimulator
    
      'DesiredRPM := 1000
      toothcount := 1
      previouscnt := cnt 
      dira[c_CrankSensorOutputSimPin] := 1
    
    
      repeat
    
        outa[c_CrankSensorOutputSimPin] := 1                ' output high 
        Waitcnt((clkfreq/12000) + previouscnt)           ' wait half the tooth count or 5 degrees of rotation
        toothcount := toothcount + 18
        previouscnt := cnt
        outa[c_CrankSensorOutputSimPin] := 0                ' output low
        Waitcnt((clkfreq/12000) + previouscnt)           ' wait half the tooth count or 5 degrees of rotation
        previouscnt := cnt
    
    
  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-10-21 15:36
    Read page 62 and 63 of Propeller Education Kit Labs: Fundamentals
    You can find it under the help menu of the Prop Tool.

    John Abshier
  • Daniel HarrisDaniel Harris Posts: 207
    edited 2010-10-21 16:13
    Hello TurboSupra,

    Look at this code and understand the math behind it. The most of what you need to do is find the number of clock ticks you need to wait before you toggle the output pin. Also, with your code, there is overhead associated with doing math inside of your waitcnt s. This will cause "clock" drift because you are not syncing up to a common point. That is, it takes time to do the math and your code does not take this extra time into account.

    Take a look at this code:
    {
      Some of the timing code has been borrowed from Jeff Martin's
      Clock.spin file.  You can find the full file in your Propeller Library
      folder.
    }
    con
    
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    
    
      WMin  =       381     'WAITCNT-expression-overhead Minimum
    
      outFreq  = 6000       'the frequency we want to generate
    
      c_CrankSensorOutoutSimPin     = 1
    
    pub generateFrequency | syncPoint, ticks
    
      dira[c_CrankSensorOutoutSimPin]~~                     'make pin an output
    
      
    {  
      Some Math:
    
      First, we need to divide or time up into little pieces.  This way
      we can figure out how many clock counts we need to wait in between toggling
      the pin to get our desired frequency.
    
      ticks := clock_frequency / (desired frequency * 2)
    
      the number of clock ticks to wait is the clock frequency divided by your desired
      frequency.
    
      In this example, we are using a 5 MHz xtal with a 16x PLL to wind the clock
      frequency up to 80_000_000.  That is, it takes 80 million ticks to get
      one second.  
      
      We multiply the desired frequency by two because we need to toggle the pin
      at twice the frequency to get a full cycle (low -> high -> back to low).
    
      Therefore, at 6 KHz * 2, (80 million ticks per second / 12_000 pin toggle per second)
      gives us ~6_667 ticks per pin toggle.
      
    }
    
    
      ticks := (clkfreq / (outFreq<<1)) #> WMin    'calculate the number of ticks to wait to generate the desired frequency
    
      syncPoint := cnt              'take a picture of the current clock count.  This way, we can always sync to this point.
    
      repeat
        !outa[c_CrankSensorOutoutSimPin]     'toggle the pin
        waitcnt(SyncPoint += ticks)   ' wait the appropriate number of clock counts to generate
                                      ' the proper square wave
    
    
  • Daniel HarrisDaniel Harris Posts: 207
    edited 2010-10-21 16:18
    BTW, the ideal way to generate square waves like this is to set up one of the counters in a cog to generate the signal for you. This way, the Propeller just outputs the desired frequency without having to do any processing (other than setting up the counter). John Abshier has the right idea - the PE kit has tons of examples on this kind of timing.

    Doing this in Spin is OK, but its not so accurate. Writing assembly will get you the accuracy, but a counter module is the best way to go in my opinion.
  • turbosupraturbosupra Posts: 1,088
    edited 2010-10-21 16:27
    @Hover, John and Daniel

    Thank you very much.

    Shamefully enough I am not familiar with how to use the counters, but I will read the suggested pages tonight and hopefully be a little familiar with using them tomorrow.

    Thank you again! :D
  • AribaAriba Posts: 2,690
    edited 2010-10-21 20:16
    turbosupra

    The error comes from the time which the instructions need between the Waitcnt((clkfreq/12000) + previouscnt).

    If you make it all relative to previouscnt, without reading the cnt register every time, the frequency should be correct:
    PUB SixKhzSimulator
    
      'DesiredRPM := 1000
      toothcount := 1
      previouscnt := cnt 
      dira[c_CrankSensorOutputSimPin] := 1
    
      repeat
    
        outa[c_CrankSensorOutputSimPin] := 1                ' output high 
        previouscnt += clkfreq/12000
        Waitcnt(previouscnt)                                ' wait half the tooth count or 5 degrees of rotation
        toothcount := toothcount + 18
        outa[c_CrankSensorOutputSimPin] := 0                ' output low
        previouscnt += clkfreq/12000
        Waitcnt(previouscnt)                                ' wait half the tooth count or 5 degrees of rotation
    

    Andy
  • @Ariba

    Question about this post. I have an idea on something and would like to know if this code example is accurate enough to work with 27 Mhz frequencies?
    Thanks for the good example.
    Martin

  • AribaAriba Posts: 2,690

    Hello Martin

    No, Spin is too slow for 27 MHz, even Assembly on the P1 is too slow. The only chance is to setup a counter to output a 27 MHz square wave.

    Andy

  • evanhevanh Posts: 15,910

    Andy,
    Do you feel 13 years older? ;)

  • AribaAriba Posts: 2,690

    No, 14 years wiser :)

  • evanhevanh Posts: 15,910

    Happy birthday then I guess.

Sign In or Register to comment.