Newbie trying to understand clkfreq
Tony B.
Posts: 356
I am trying to understand the program attched to this post.· The program is from the Spin Stamp for BOE-BOT object from the propeller object exchange.· I am wrestling with understanding how I know the value of clkfreq.· I found a helpful chart on page 175 oif the Propeller Manual V1.0 in which clkfreq has a value of 1000.· When I use that value the program works out.· Does clkfreq always have a value of 1000 no matter the _clkmode and ·_xinfreq settings?· IF they are related, how?· Thanks for the help
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tony
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tony
Comments
...
CON
_clkfreq = 80_000_000
_clkmode = xtal1 + pll16x
...
Pretty good rule of thumb to include that in all your top level objects or life will be a pain when nothing works.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
-Phil
I am new too, but from what I understand is that clkfreq is the number of ticks per second.
In your specific code you have 10 MHz (10,000,000 Hz) times the PLL number which is 8, which is 80,000,000 ticks per second. So in this case clkfreq=80,000,000
The code then takes clkfreq/1000 (which is called "pause") then multiplies "pause" by 20... which is the same as saying clkfreq divided by 50
80,000,000 / 1000 * 20 = 1,600,000
OR
80,000,000 / 50 = 1,600,000 (which just means you want it to wait 1/50th of a second)
The thing to remember is that clkfreq is the total number of ticks per second, cnt is the current count of ticks. In 1 second, cnt will have counted up to clkfreq.
Examples:
waitcnt(clkfreq + cnt) means to wait for 1 second. You are saying wait until 1 second's worth of ticks has been counted before continuing.
waitcnt(clkfreq / 2 + cnt) means to wait for .5 seconds. Wait until half of 1 second's worth of ticks has been counted before continuting.
waitcnt(clkfreq / 4 + cnt) means to wait for .25 seconds
waitcnt(clkfreq * 2 + cnt) means to wait for 2 seconds
clkfreq = _clkmode * _xinfreq
Clkfreq is the number of clock cycles per second. So clkfreq really equals one second - always.
If you want 1 millisecond you divide clkfreq by 1,000. This is a much better approach than calling 1 millisecond "80_000".
Those two are exactly the same so long as the clock frequency is 80mhz. If you need to change the clock frequency for some reason (low power) the top one will still be 1 mS but the bottom would change to be 2 mS.
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Servo Boss, a 12 channel servo tester kit from Gadget Gangster.
Actually, I think what you meant to say is this: in one second the cnt will increment itself clkfreq number of ticks. After waiting 1 second, the value of cnt is NOT necessarily going to be the value of the clkfreq. The value of cnt itself is something like an odometer that is always running as the Propeller is running. Once the cnt reaches 2,147,483,647, it "rolls over" and starts counting all over again. So waitcnt() is useable because you are telling the Propeller to wait until the value inside the parentheses has reached cnt plus some desired number of ticks.
(The roll over starts at -2,147,483,648)
If you re-write that as (cnt + clkfreq * 2) you will get slightly more accurate timing.
In a simplistic fashion, the variables are pushed on to the stack left to right, so by doing it your way you have the extra delay of the (clkfreq*2) calculation prior to reading and storing the
value of cnt. If you make cnt the leftmost variable, it will *always* be read and stored first. This just makes your timing calculation slightly more accurate (probably in the order of 20-50uS) depending on the complexity of the calculation.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lt's not particularly silly, is it?
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Servo Boss, a 12 channel servo tester kit from Gadget Gangster.
Post Edited (W9GFO) : 9/9/2009 6:10:01 AM GMT
In general there is min. number of 'whatever' that you can use in SPIN, as in SPIN waitcnt plus the calculations have a runtime and the cnt-difference you use must always be bigger or equal to the runtime, so that you don't miss the calculated counter value. I bet someone of the gurus here can remember the min. value .. I can't (was it around 600?). One can easyly test it.
Just wanted to mention this little pitfall, so that you can't say "Never heard of that ;o)" in case it happens to you that the waitcnt actually waits for ~50 seconds.
No guru required. Look two posts up ^^^^
Rich H
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The Servo Boss, a 12 channel servo tester kit from Gadget Gangster.
But Brads point is still valid. Doing it the other way around·adds a little accuracy. Doing it in PASM adds a lot of accuracy. As always the solution depends on your needs.
Post Edited (MagIO2) : 9/9/2009 6:25:32 AM GMT