Shop OBEX P1 Docs P2 Docs Learn Events
LED frequencies without cogs — Parallax Forums

LED frequencies without cogs

cy88cy88 Posts: 3
edited 2008-10-14 22:31 in Propeller 1
Hi, I'm looking at this code taken from Parallax's Fundamentals: I/O and Timing Basics v.1.0.

For "dT := clkfreq / 4620", my question is why 4620? 80,000,000 divided by 4620 isn't exactly a nice number. Is there something special about 4620 that I'm missing?

Thanks,
cy88

''File: LedFrequenciesWithoutCogs.spin 
''Experience the discomfort of developing processes that could otherwise run 
''independently 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[noparse][[/noparse]9..4]~~                             ' 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 
       if count // T_LED_P5 == 0 
         !outa 
       if count // T_LED_P6 == 0 
         !outa[noparse][[/noparse]6] 
       if count // T_LED_P7 == 0 
         !outa[noparse][[/noparse]7] 
       if count // T_LED_P8 == 0 
         !outa[noparse][[/noparse]8] 
       if count // T_LED_P9 == 0 
         !outa[noparse][[/noparse]9] 

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-14 05:02
    4620 is twice 2310. Since a count of 2310 represents a half cycle (the output pins are toggled), 4620 is a full cycle.

    1155 is half of 2310 and 770 is 1/3rd of 2310. 462 is 1/5th of 2310. 330 is 1/7th of 2310. 210 is 1/11th of 2310.
  • cy88cy88 Posts: 3
    edited 2008-10-14 05:08
    so 2310 is just an arbitrary number?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-14 05:39
    Not really. It's 1 * 2 * 3 * 5 * 7 * 11, the product of the first 6 prime numbers.
  • BaggersBaggers Posts: 3,019
    edited 2008-10-14 10:52
    CY88,

    since all LED's are flashing at different frequencies, with only one counter, that gets reset back to 0 to make sure all LED's keep their pulse the number has to be divisible by it's frequency value, eg, 1,2,3,5,7,11 and so by multiplying them ALL together, you will get a nice non-fractional value that can be divided by either of the frequencies [noparse]:)[/noparse]
    so that when the count gets reset, it won't affect the frequencies of any of the LED's [noparse];)[/noparse] if you see what I mean, as 2310 is divisible by all values and gives integer results.

    As you can probably guess I'm not a teacher, but I hope it's clear enough lol if not let me know.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • cy88cy88 Posts: 3
    edited 2008-10-14 22:31
    ah, i get it. Thank you guys.
Sign In or Register to comment.