Shop OBEX P1 Docs P2 Docs Learn Events
Spin programing that I cannot figure out — Parallax Forums

Spin programing that I cannot figure out

msiriwardenamsiriwardena Posts: 301
edited 2014-02-22 18:12 in Propeller 1
I am trying to figure out How the "Time constants were drived" and how the " dt := clkfreq /4620 - How the divisor(4620) was derived.


This comes from an exercise from I/O timing Basics in the "Propeller Education Kit Labs"


''File: LedFrequenciesWithoutCogs.spin
''Experience the discomfort of developing proceses that could otherwise run
''indendently in separate cogs.  In this example, LEDs blink at 1, 2, 3, 5,
''7, and 11 Hz.

CON

    _xinfreq = 5_000_000                     ' 5 MHz external crystal 
    _clkmode = xtal1 + pll16x                ' 5 MHz crystal multiplied → 80 MHz

    T_LED_P4 = 2310                          ' Time increment constants
    T_LED_P5 = 1155
    T_LED_P6 = 770
    T_LED_P7 = 462
    T_LED_P8 = 330
    T_LED_P9 = 210
    
PUB Blinks | T, dT, count

    dira[21..16]~~                             ' Set LED I/O pins to output

       dT := clkfreq / 4620                  ' Set time increment 
       T  := cnt                             ' Mark current time

    repeat                                   ' Main loop

       T += dT                               ' Set next cnt target
       waitcnt(T)                            ' Wait for target

       if ++ count == 2310                   ' Reset count every 2310
         count := 0                        

       ' Update each LED state at the correct count.
       if count // T_LED_P4 == 0             
         !outa[16]
       if count // T_LED_P5 == 0
         !outa[17]
       if count // T_LED_P6 == 0
         !outa[18]
       if count // T_LED_P7 == 0
         !outa[19]
       if count // T_LED_P8 == 0
         !outa[20]
       if count // T_LED_P9 == 0
         !outa[21]

Thank you
Siri

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-22 15:27
    My guess is it's about how long the loop would take if every "if" condition were true.

    They want the loop to be fast as possible without missing the time window for the waitcnt.

    Edit: I don't know if they measured the time directly or if they just kept trying smaller time intervals and kept the smallest interval that would work.
  • msrobotsmsrobots Posts: 3,709
    edited 2014-02-22 15:36
    Just look at the numbers...

    4620 =

    02 x 2310
    04 x 1155
    06 x 770
    10 x 462
    14 x 330
    22 x 210

    Enjoy!

    Mike
  • msiriwardenamsiriwardena Posts: 301
    edited 2014-02-22 15:59
    @msrobots
    Just the numbers do not explain the logic.
    Where did the #4620 derived from why not any other number.How are the other numbers related to 1Hz,2,3,7 and 11Hz.

    Siri
  • kuronekokuroneko Posts: 3,623
    edited 2014-02-22 16:49
    Since all 6 frequencies/LEDs have to share one loop you first need the lowest common multiple, i.e. 1*2*3*5*7*11 = 2310 which is what count is about (this gives you the initial timing granularity, clkfreq/2310). Since the slowest LED (1 Hz) needs two toggles during that time we have to divide by two and finally arrive at clkfreq/4620.

    Not that it matters much but count will start with an undefined value and could glitch when it overflows. From then on it's confined to 0..2309.
  • msiriwardenamsiriwardena Posts: 301
    edited 2014-02-22 18:12
    @kuroneko

    Thank you very much - now it is crystal clear ,how these values calculated.

    Siri
Sign In or Register to comment.