Shop OBEX P1 Docs P2 Docs Learn Events
Constant (CON) Declaration question — Parallax Forums

Constant (CON) Declaration question

RobertWRobertW Posts: 66
edited 2010-01-20 16:43 in Propeller 1
Can anyone explain why I get an error message with the following code:

CON
· ScanTime = (clkfreq / 100) + cnt··············· '10 millisecs scan time

I am looking to set a constant for a delay that I can use in different parts of my program.· This way I would not have to retype 'clkfreq / 100 + cnt' everytime.· Is this possible?· Thanks.

-Rob W.

Comments

  • BradCBradC Posts: 2,601
    edited 2010-01-20 03:38
    RobertW said...
    Can anyone explain why I get an error message with the following code:


    CON

    ScanTime = (clkfreq / 100) + cnt '10 millisecs scan time



    I am looking to set a constant for a delay that I can use in different parts of my program. This way I would not have to retype 'clkfreq / 100 + cnt' everytime. Is this possible? Thanks.



    -Rob W.

    cnt is a register in the chip, not a constant (in that context anyway). clkfreq is a value stored by the compiler in location $0 of the hub ram. The interpreter inserts this value when you use the "clkfreq" command. Although in theory it should be available at compile time, none of the current compilers are geared to make that happen.

    Every call you use that value you will need the current cnt value. To speed things up you could define a long in VAR and assign that the value of clkfreq / 100 when your program starts up. Every time you want to use that you'd use WAITCNT(cnt + ScanTime).

    Despite almost every example people post to the forum where it's "some calculation" + cnt, you should really put "cnt" as the first value inside the brackets. This just makes your time calculation that little bit more accurate as the cnt value gets pushed onto the stack prior to any of the other calculations taking place.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Life may be "too short", but it's the longest thing we ever do.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-01-20 03:44
    Neither clkfreq nor cnt are constants. clkfreq is a hub location that's loaded with the clock frequency when the Propeller resets. cnt is a register that increments at the clock frequency's rate. If you know ahead of time that the clock frequency will be, say 80 MHz, you can always do:

    CON
    
      ScanTime = 80_000_000 / 100
    
    
    


    But you still have to do your delays with something like this:

      waitcnt(ScanTime + cnt) [i]...or...[/i]
      waitcnt(time += ScanTime) [i]where [b]time[/b] gets initialized from [b]cnt[/b] at some point.[/i]
    
    
    


    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 1/20/2010 3:49:18 AM GMT
  • KyeKye Posts: 2,200
    edited 2010-01-20 03:56
    Use functions.

    PUB pauseForSeconds(number)
     
      result := cnt 
     
      repeat (number #> 0)
        result += clkfreq
        waitcnt(result)  
     
    PUB pauseForMilliseconds(number) 
     
      result := cnt
      
      repeat (number #> 0)
        result += (clkfreq / 1000)
        waitcnt(result)      
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-01-20 04:23
    I agree with Kye, but don't perform the math once and before the loop -- like this:

    pub pause(ms) | tix, base
    
      tix := clkfreq / 1_000
      base := cnt
      repeat ms
        waitcnt(base += tix)
    


    In actual fact I tend to create delay constants as Phil suggested above, based on my XIN and PLL settings.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • RobertWRobertW Posts: 66
    edited 2010-01-20 16:43
    Thanks guys. I will give it a try this evening.
    -Rob
Sign In or Register to comment.