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

Bizzare Timing

vgplayervgplayer Posts: 10
edited 2010-10-21 11:18 in Propeller 1
This has been bugging me for a while and I can't seem to find the answer. This always returns 160M cycles instead of 80M cycles like I thought it should. Instead of P16 being high and low for 1 second it is 2 seconds. It seems that dT is added twice somewhere. What am I doing wrong?
CON

  _CLKMODE = XTAL1 + PLL16X
  _XINFREQ = 5_000_000

  Pin = 16

OBJ

  DEBUG : "FullDuplexSerial"

VAR

  long T, dT, oldT

PUB Main

  dira[Pin]~~
  DEBUG.start(31,30,0,115200)

  dT:=clkfreq 
  T:=cnt
  T+=dT

  repeat
     if (||(||cnt - ||T) => dT)
          !outa[Pin]
          oldT:=T
          T:=cnt
          T+=dT
          DEBUG.dec(||(||T - ||oldT))
          DEBUG.tx(13)

Comments

  • BeanBean Posts: 8,129
    edited 2010-10-21 08:34
    You don't want to do the second "T+=dT". cnt has already advanced by 80M, and adding dT will add "another" 80M to it.

    Bean
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-10-21 08:35
    This variant does as you expect -- and, I think, is easier to follow.
    pub main | dt, t, delta
    
      debug.start(31, 30, %0000, 115200)
    
      dira[pin] := 1
    
      dt := clkfreq
      t := cnt
      repeat
        delta := ||(cnt - t)
        if (delta > dt)
          !outa[pin]
          debug.dec(delta)
          debug.tx(13)
          t += delta
    
  • vgplayervgplayer Posts: 10
    edited 2010-10-21 10:30
    Thank you all for the solutions (removing T:=cnt in the loop) but I still don't understand the problem as I had it before.

    If statement is true
    Pin is toggled
    T is set to current cnt
    Add 80M to T

    Current cnt + dT should be one second.

    So it should be high for 1 second and low for 1 second (0.5Hz).

    What is it that I don't see?
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-10-21 10:37
    I think it works fine like that, but there is execution time being wasted to get cnt, and add the value. If you just add dT to T, then the contents of the IF statement will happen (on average) exactly every second. Instead, your meathod would be every second, plus the time it takes to check the IF statement, and get cnt, and add dT to that. So the frequency may actually be 0.499Hz.
  • vgplayervgplayer Posts: 10
    edited 2010-10-21 10:41
    Bobb Fwed wrote: »
    I think it works fine like that, but there is execution time being wasted to get cnt, and add the value. If you just add dT to T, then the contents of the IF statement will happen (on average) exactly every second. Instead, your meathod would be every second, plus the time it takes to check the IF statement, and get cnt, and add dT to that. So the frequency may actually be 0.499Hz.

    I understand that it is not good time keeping but I am not getting 0.5Hz, I am getting 0.25Hz with my original code.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-10-21 10:49
    My bad. It's because of your IF statement:
    if (||(||cnt - ||T) => dT)
    'better as:
    if ((cnt - T) => dT)
    
    This is looking for a difference of dT, but you are adding a dT to T after you get your cnt.

    So you should remove the T+=dT (in and outside of the loop -- or just the T:=cnt inside the loop :-P).
  • vgplayervgplayer Posts: 10
    edited 2010-10-21 11:18
    Bobb Fwed wrote: »
    My bad. It's because of your IF statement:
    if (||(||cnt - ||T) => dT)
    'better as:
    if ((cnt - T) => dT)
    
    This is looking for a difference of dT, but you are adding a dT to T after you get your cnt.

    So you should remove the T+=dT (in and outside of the loop -- or just the T:=cnt inside the loop :-P).

    Got it. Thank you.
    T = cnt_old + dT
    (cnt_new - T) => dT
    cnt_new - (cnt_old + dT) => dT
    cnt_new - cnt_old - dT => dT
    cnt_new - cnt_old => 2dT
Sign In or Register to comment.