Shop OBEX P1 Docs P2 Docs Learn Events
Beginner trying to figure out a longer interval for blinking LED — Parallax Forums

Beginner trying to figure out a longer interval for blinking LED

fsbfsb Posts: 24
edited 2011-04-16 07:19 in Propeller 1
Probably a dumb question but I am trying to learn spin on the propeller.

I've been playing with blinking LEDs and, using the following, can get a delay up to about 50 seconds between blinks, but beyond this, the timing is way off. I was shooting for 5 minutes between blinks. What am I doing wrong?

partial code:

dira [4] := 1
repeat
outa[4] := 1
pause_ms (5000) 'msDelay = 5000
outa[4] := 0
pause_ms (50,000) 'msDelay = 50,000
PRI pause_ms (msDelay)
waitcnt (cnt + ((clkfreq/1000) * msDelay

Thanks for any help.
fsb

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-04-16 06:41
    For one thing, you have "repeat" there but no number follows it,
    so it's going to "repeat" for... ever...
  • pgbpsupgbpsu Posts: 460
    edited 2011-04-16 06:42
    fsb-

    If you are running the prop with a 5Mhz crystal and a PLL of 16 your system clock will be 80Mhz. The 32-bit width of the internal counter means you can store 0xFFFF_FFFF. When counting up at 80Mhz, that register will fill and roll-over at ~53 seconds. To get the delays you want, use cascading delays. There is also a timer object in the OBEX. When run in its own cog it can give decent timing for long periods. I don't remember the specs exactly but I think you might be able to do better than one second per day error if you have a decent crystal.

    Try using the code delimiters so the spacing on your inline code is preserved [ code ] [ / code ] - remove the spaces.

    Peter
  • pgbpsupgbpsu Posts: 460
    edited 2011-04-16 06:45
    PJ Allen wrote: »
    For one thing, you have "repeat" there but no number follows it,
    so it's going to "repeat" for... ever...

    PJ- I suspect that the formatting got dropped when posted. I assume they mean:
    repeat
      outa[4] := 1 
      pause_ms (5000) 'msDelay = 5000
      outa[4] := 0
      pause_ms (50,000) 'msDelay = 50,000
    

    ???
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-04-16 06:47
    Hi fsb,

    this is your 22nd post but anyway welcome to the propeller-forum!
    You can ask whatever you want. You are asking the exact way like we (the forum-members) like it.

    You tried something on your own made some success but now there is a question.

    the command waitcnt uses the 32bit hardware-counter which can count up to 2^32 - 1. This is 4.294.967.295

    Each clock-tick of the propeller the counter increments by one. This means for counting up to 4.294.967.295 at a clockfrequency of 80MHz means after

    4.294.967.295 / 80.000.000 = 53.68 seconds the counter has counted up to his maximum.
    The waitcnt command works that way: the execution of the cog is stopped until the new value is matched.
    If you try to wait for more than 53 seconds the value does not match after the amount of time it should because
    the counter can't count up to a 33-bitvalue wich would be nescessary to count up to 60 seconds

    This works always even if the counter is almost at his maximum as he just wraps around to zero and starts counting up again
    until the value matches. you can imagine this like a milecounter in a car if it is at its max it wraps around to zero and starts counting up again

    if you code a waitcnt-command this means take actual number of counter and add a certain number (if this means exceeding the max value
    it counts up to max and then to the rest of the "distance"

    If you want to blink slower or wnat to wait longer you have to divide the waiting into pieces smaller than 53 seconds
    and do more than one waitcnt command

    in the obex there is a timer object which provides a realtime clock that offers form milliseconds up to years.

    also start reading the manual about the command waitcnt and the systemcounter "CNT" If you don't understand
    anything from the manual came back with questions

    best regards

    Stefan
  • pgbpsupgbpsu Posts: 460
    edited 2011-04-16 06:59
    fsb-

    StefanL38's suggestion about the manual is a great one. In the downloadable version of the prop manual (available on the Parallax website) there is a good section on using waitcnt. Page 219 is a good place to start.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-04-16 07:19
    Using a synchronized delay loop in the pause() method will eliminate the roll-over problem you're experiencing. This will let you pause up to 2,147,483,648 milliseconds which, in practical terms, is about 25 days!
    pub pause(ms) | t
    
    '' Delay program ms milliseconds
    
      t := cnt                                                      ' sync with system counter
      repeat (ms #> 0)                                              ' delay must be > 0
        waitcnt(t += clkfreq/1_000)
    
Sign In or Register to comment.