Accurate Timekeeping
SRLM
Posts: 5,045
Hi,
I'm working on a clock project, and I'm looking for input on the execution of spin code. I wrote the bit of code below, and it's launched into a new cog and I just read the variables (hour, minute, and second) as needed from other parts of the program. However, I suspect that the spin execution code and the time it takes will be a problem. Even if it misses a single clock cycle every time, that adds up the longer the project runs. I'd like to avoid that if possible. Any ideas on how to make it determinate and execute in constant time?
Thanks!
I'm working on a clock project, and I'm looking for input on the execution of spin code. I wrote the bit of code below, and it's launched into a new cog and I just read the variables (hour, minute, and second) as needed from other parts of the program. However, I suspect that the spin execution code and the time it takes will be a problem. Even if it misses a single clock cycle every time, that adds up the longer the project runs. I'd like to avoid that if possible. Any ideas on how to make it determinate and execute in constant time?
PUB Clock repeat repeat until hour => 12 'hour repeat until minute => 60 'min repeat until second => 60 'sec waitcnt(clkfreq + cnt) second++ second := 0 minute++ minute := 0 hour++ hour := 0
Thanks!
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
This will be a good start in making your clock as accurate as the crystal.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
But what should I replace 12 with?
So, every time clk += clkfreq is executed, the value of clk is increased by 80M.
WaitCnt will wait until the value of CNT = clk.
There's a good description (including pictures) in the Propeller Manual for WaitCNT.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Repeat command with variable comparison (? number of clock cycles)
addition and subtraction, assignment (? number of clock cycles)
waitcnt command execution (? of clock cycles)
clkfreq constant (clkfreq number of clock cycles)
increment operator (? number of clock cycles)
I can infer that the there must be at least clkfreq number of cycles in the above command, and some number more to execute all the other commands. My question is: how do I get that number, and make it stay the same every time?
The method fixMainClock is simply a method that rolls the numbers over so I don't get 60 seconds displayed. Thanks for all the help! [noparse]:)[/noparse]
it's always a good thing to make a quick search with some keywords in the obex
Anyway by creating methods on your own you have learned a lot about the use of the systemcounter
best regards
Stefan
http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/Web-PELabsFunBook-v1.0.pdf·(6.93 MB)
Full text and source code is available from: www.parallax.com -> Propeller -> Downloads & Articles
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Andy Lindsay
Education Department
Parallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 1/18/2009 6:03:34 AM GMT