Shop OBEX P1 Docs P2 Docs Learn Events
Program Loop Timing Jitter — Parallax Forums

Program Loop Timing Jitter

DjimDjim Posts: 4
edited 2008-09-18 17:20 in BASIC Stamp
Hi, I was trying to make a repeating PWM loop on a BS2 through an output pin that is 50us ON and 9000us OFF.· Tried this using pulsout for the 50us and pause for the 9ms: these are put into a program loop.· I need the repetition rate to be very precise, i.e. I need something like 50us +-10us for on and the 9500us +-10us for off.·· Apparently pulsin is triggered by an interrupt and faces significant timing variation and the program loop execution cycles may also be adding to timing jitter.·

Any suggestions on how this might be accomplished?· I also looked at PWM, SEROUT; would those be better possibilities?

thanks

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-18 03:54
    Show us your code.

    -Phil
  • DjimDjim Posts: 4
    edited 2008-09-18 04:03
    Its basically this: (BS2)

    Setup:
    HIGH 0 ' make P0 low (LED off)
    Main:
    PULSOUT 0, 25 '50us pulse
    PAUSE 9 ' Pause 9ms
    GOTO main
    END
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-18 06:10
    I'm looking at the output from your program on a scope, and it's rock-steady — not a bit of jitter. But this is what I would expect from a BASIC Stamp, since it's completely deterministic, with no interrupts.

    -Phil

    BTW, which is it for "off"? 9000µsec or 9500µsec. Your first post gives both figures. Once I have that, I can give you a way to nail the timing a lot closer.

    Post Edited (Phil Pilgrim (PhiPi)) : 9/18/2008 6:20:42 AM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-09-18 12:05
    +1 Phil. Once you "get it right" there's no jitter in the timing. Now, reading the tokens and executing them takes some time, so you'll probably have to adjust the PAUSE command to 8 mSec, then put in a PULSOUT to some un-used pin to get the exact part of a millisecond you need so that the whole loop meets your timing requirements.

    Oh, and usually "jitter" means a back-and-forth inaccuracy in some timing signal. If the signal is rock-steady, just not quite correct, that's "inaccurate", not "jitter".
  • DjimDjim Posts: 4
    edited 2008-09-18 12:12
    Hi,

    Thanks for the info.·· For the purposes of this discussion the off time can just be 9ms.· Buts thanks for the valuable suggestion on using pulsout on another pin if you need off time more or less than 1ms that will be valuable.

    I am going back to the scope this am to make sure there is not a mistake.·· I was measuring the jitter with an NI card running 100mhz but maybe there is something funny going on there.··

    How many command lines per sec can a BS2 run?

    thanks
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-18 16:09
    allanlane5 beat me to it with the second PULSOUT suggestion. Actually, you can get rid of the PAUSE altogether and use the second PULSOUT to take up all the slack.

    -Phil
  • DjimDjim Posts: 4
    edited 2008-09-18 17:01
    Thats what I tried, good suggestion. In looking at this more closely on the scope, it appear to be the GOTO loop that is adding to the timing jitter. Still looking at this more closely. Note for clarity: I am looking for repeatability, not accuracy.
    thanks
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-09-18 17:14
    I was going to say the BS2 does not use a timing interrupt -- but I believe the PAUSE statement DOES in fact use an internal timer interrupt to 'wake up' every so often, which will inject some 'jitter' into the process.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-18 17:17
    I don't know what kind of jitter your scope is seeing. Mine shows less than 2 µsec (0.02%), as the attached time-exposed trace reveals. It could be that your scope is not getting a clean trigger, which would cause it to display an apparent jitter. But I can assure you, it's not coming from the Stamp.

    Here's the program I used for the scope trace:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    HIGH 0
    DO
      PULSOUT 0, 25 '50us pulse
      PULSOUT 1, 4364
    LOOP
    
    
    


    But your original program didn't jitter either.

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 9/18/2008 5:23:53 PM GMT
    640 x 480 - 11K
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-09-18 17:20
    allanlane5,

    PAUSE does not use a timing interrupt. You might be thinking of SLEEP and NAP.

    -Phil
Sign In or Register to comment.