Shop OBEX P1 Docs P2 Docs Learn Events
Why can't I get a higher frequency square wave? — Parallax Forums

Why can't I get a higher frequency square wave?

W9GFOW9GFO Posts: 4,010
edited 2009-04-27 17:26 in Propeller 1
I am in the process of going through the PE Kit labs again. This code is essentially the same as in the lab only I have increased (decreased) the values to see where the prop couldn't keep up.

16 khz is the highest freq where I could get a wave with equal high and low time - more or less. That makes 5,000 clock cycles to go through the repeat loop.

Here I have a square wave at 16 khz. the scope shows it high for 30 uS and low for 32 uS.

So, I am wondering;

Why can't I get a higher freq to work?

Why is it low longer than high?

And yes, I know there are better ways to make a square wave - says so right in the lab...

Rich H

CON

  _xinfreq = 5_000_000
  _clkmode = xtal1 + pll16x

VAR

    long swStack[noparse][[/noparse]40]
    byte swCog

PUB Launch

    swCog := cognew(SquareWave(24,clkfreq/32000, clkfreq/16000), @swStack )

PUB SquareWave( pin, tHigh, tCycle )| tC, tH


  dira[noparse][[/noparse]pin]~~
  outa[noparse][[/noparse]pin]~

  tC := cnt

  repeat                     ' (pin still low)
    outa[noparse][[/noparse]pin]~~           ' make pin high
    tH := tC + tHigh     ' (re)capture a time in the future where the pin will go low
    tC += tCycle         ' (re)capture a time in the future where the pin will go high
    waitcnt(th)             ' sit tight until time to go low
    outa[noparse][[/noparse]pin]~             ' make pin low
    waitcnt(tC)            ' wait again until time to go high
                                ' end - go back and do it again



Edited to add comments to code, it makes sense now. smile.gif

Post Edited (W9GFO) : 4/27/2009 6:39:08 AM GMT

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-04-27 06:12
    It's low longer than it's high because your waitcnt(tC) is separated from the subsequent outa[noparse][[/noparse]pin]~~ by the repeat loop overhead. Put the outa[noparse][[/noparse]pin]~~ at the end of the loop, and you should see a more symmetrical waveform.

    -Phil
  • W9GFOW9GFO Posts: 4,010
    edited 2009-04-27 06:18
    Thanks Phil,

    ...I get it now, it took commenting each line for the answer to reveal itself. Original post edited to include comments.

    Rich H

    Post Edited (W9GFO) : 4/27/2009 6:44:17 AM GMT
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-04-27 07:56
    SPIN is interpreted. So, it needs a lot of PASM instructions to execute one SPIN instruction, which of course reduces the max. speed possible.

    If you want symmetrical signal, your only chance is to reduce number of instructions in the loop by using xor operator. My first guess is that accessing the outa without [noparse][[/noparse] ] is faster than giving a bit number.

    If you have to create plain squarewaves this could also be done with one of the counters each COG has. This still can be done in SPIN if you are afraid of using PASM.

    Or you can switch to PASM of course.
  • W9GFOW9GFO Posts: 4,010
    edited 2009-04-27 16:30
    This is just experimenting. I found that the following code gives me 22.222 khz.

    3,600 1,800 clock cycles to toggle a pin!
    CON
    
      _xinfreq = 5_000_000
      _clkmode = xtal1 + pll16x
    
    pub SquareWaveFastest
    outa[noparse][[/noparse]24]~
    dira[noparse][[/noparse]24]~~
    
    repeat
      !outa[noparse][[/noparse]24]
    

    Post Edited (W9GFO) : 4/27/2009 4:57:52 PM GMT
  • LeonLeon Posts: 7,620
    edited 2009-04-27 16:58
    Spin is very slow. Try it in assembler.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • Mike GreenMike Green Posts: 23,101
    edited 2009-04-27 17:26
    Spin is actually pretty fast for a general purpose high level interpretive language. Any interpreted language will be slower than native instructions. Catalina is a free C compiler under development by a forum member that produces "semi-interpreted" native Propeller instructions (http://forums.parallax.com/showthread.php?p=795326). The code produced is much larger than the equivalent Spin interpretive code and the interpreter does an instruction in 5-10 Prop instruction times on average. There are several Forth interpreters including one that's free from Cliff Biffle (www.cliff.biffle.org/software/propeller/forth/).

    Doing high speed stuff in assembly is the best way to do it. There are some good 3rd party tools (not free) like ImageCraft's C compiler and JDForth (www.jacobsdesign.com.au/software/jdforth/jdforth.php).
Sign In or Register to comment.