Shop OBEX P1 Docs P2 Docs Learn Events
Timing Question — Parallax Forums

Timing Question

ajwardajward Posts: 1,130
edited 2012-07-11 09:10 in Propeller 1
I started going back through the PE Kit book tweaking and twiddling with the code to see what happens and looking at the waveforms. The code for LedOnOffP4 made a decent square wave.
I ran this sample to approximate a 1 kHz signal....
PUB One_kHz_Output


    dira[4] := 1
    
    repeat
        outa[4] := 1
        waitcnt(clkfreq/2000 + cnt)
        outa[4] := 0
        waitcnt(clkfreq/2000 + cnt) 

When I looked at the waveform on the scope, the signal was about 1.22 mS. Ran the code on 4 propellers and the results were very similar... 1.21 to 1.23 mS.

I'm thinking the extra time comes from the overhead of the square wave being generated in software. Am I close or way off base?

Anyhow... thanks for any advice!!

Amanda

Edit: In my early morning, caffeine not yet kicked in yet haze, I confused kHz with mS. Problem's the same... just different! ;-)

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2012-07-11 07:52
    Yes, the extra time is caused by overhead. One way to improve that is to sync the toggling to the system clock like this:
    PUB One_kHz_Output | time, interval
       dira[4] := 1
       interval := clkfreq / 2000
       time := cnt
       repeat
          waitcnt(time += interval)
          outa[4] := 1
          waitcnt(time += interval)
          outa[4] := 0
    
    This will produce a 1KHz signal with the signal edges coming at a fixed interval from the wait times. The WAITCNTs occur every 500us and any overhead does not accumulate.
  • turbosupraturbosupra Posts: 1,088
    edited 2012-07-11 07:55
    Hi Amanda,

    You have to account for the overhead in spin and have something that is not skewed by the time it takes the interpreter to execute the spin instructions and translate them into machine code. With this you have a single starting point and you're always adding clk/2000 to it every time, so it's not being skewed by the additional ~.22kHz to execute the code and then move to the next wait command, it instead just builds off of the initial creating a perfect wait of clkfreq/2000 + the last which is based on the initial, every single time.
    cntHolder := cnt
    repeat
            outa[4] := 1
            waitcnt(clkfreq/2000 + cntHolder )
            outa[4] := 0
            waitcnt(clkfreq/2000 + cntHolder )
    
    
  • tonyp12tonyp12 Posts: 1,951
    edited 2012-07-11 08:59
    I think 90% of all new Prop users run in to this.
    Say you have to wait 1000 cnt to wait one second.

    Loop control: 20 cnt.
    set up the wait: 20 cnt.
    wait the 1000 cnt.

    You have now paused for 1040 cnt.

    So if you instead set up a wait so it always look the next 'even' 1000 cnt
    What you did between these waits does not matter, as long they NEVER take more than a 1000 cnt to do.

    a=cnt+1000
    loop:
    wait for cnt=a
    do my thing
    a=a+1000
    jmp loop
  • hover1hover1 Posts: 1,929
    edited 2012-07-11 09:10
    Hi Amanda,
    If you are running through the PEK again, go to page 64 in the Ver 1.2 PDF.

    It is discussed there.

    Jim
Sign In or Register to comment.