Is there a way to make a Millisecond Counter WITHOUT using a cog?
DavidM
Posts: 630
Hi,
I have a Millisecond Timer Object that used throughout my current project, and to do this I use a COG, that simply uses the system counter. This millisecond counter simply updates a Global variable passed to it via a pointer in from MAIN LOOP (Cog 0)
Q1) Is there a way to make a Millisecond Counter WITHOUT using a cog?
As I need more cogs for other tasks.
I was looking at the other features of the counters (i.e CNTB, FRQA,FRQB, PHSA, PHSB ) but I don't understand what they do. Can any of these be used?
Thanks
Dave M
I have a Millisecond Timer Object that used throughout my current project, and to do this I use a COG, that simply uses the system counter. This millisecond counter simply updates a Global variable passed to it via a pointer in from MAIN LOOP (Cog 0)
Q1) Is there a way to make a Millisecond Counter WITHOUT using a cog?
As I need more cogs for other tasks.
I was looking at the other features of the counters (i.e CNTB, FRQA,FRQB, PHSA, PHSB ) but I don't understand what they do. Can any of these be used?
Thanks
Dave M
Comments
Thanks for the reply.
So you say it CAN be done, But dividing the counter to get 1000 hz is not a good choice?
So How does this work?
Dave M
* if your clock rate isn't an exact multiple of 1000, you'd probably want to handle the integer math better
* you need to make sure you call this more frequently than the counter wrapping
* the lock lets you call this from multiple cogs
* you need to call ms_register before using the ms function
* the variables are in DAT instead of VAR, so only a single copy of this object will ever run.
Jonathan
What are your requirements on accuracy and unique-ness (i.e. how long until roll-over is acceptable)? And do you really need 1ms at this level, i.e. would any increasing timestamp do (which could then be adjusted later)?
Exactly. I have a similar problem and those questions are exactly what you need to answer in order to narrow down the huge number of ways you can tackle this problem.
you said "(unfortunately eating a pin)",So to use these counters you need a spare pin? I actually don't have any spare pins in my project! They are all used up on my current PCB.
Thanks anyway!
Dave M
Do you have any cogs doing ordinary things?
What is your RTC idea?
Thanks
Dave M
It always interests me to see how people use their cogs and configure their projects.
Would you be willing to sketch out your cog use and roughly outline the devices hooked up to the prop?
HI ,
Funny you mention this, I have a mud map I made the other day showing my OBJECTS vs COGS vs I/O, I need to "REORGANISE" My code to free up some cogs for my RS485 Protocol I am halfway through!
I will see if I can "Draw " up a COG "Mud Map" soon,
Dave M
-Phil
things that should be repeated at a slower rate can be started by a second "timer-variable" like shown in the example-code
best regards
Stefan
-Phil
http://forums.parallax.com/showthread.php?129913-Using-counters-for-long-term-seconds-time-keeping
The same considerations apply to your case, even if you must count faster.
If you have a fast loop somewhere you can check the cnt, without needing to use the waitcnt.
This counts seconds.
Massimo
Let me bring my example into this just a bit...
If I have an external 32768 Hz clock source that feeds into p26 ... let's just say. And I have a cog that reads this clock internally in order to do some of it's timing routines using it's cog's counter module. If another cog wanted to access that counter value also, I don't think it can directly read the other cog's counter module. You could have that cog update a variable in hub RAM with that variable, but that takes some overhead. You could instantiate both cogs at approximately the same time and start reading that counter at the same time so they would have independent, but NEARLY identical clock values.
Is there a way to 'reset' a counter in the propeller's counter module?
Any counter can be reset by setting its phsx register to zero.
-Phil
My eprom is being accessed throughout the running of the main loop at various times ( reading and writing Variables as well as large chunks of DATA), So the SDL Pin is not available.
I run an LCD of the i@C line wi=which is updated all of the time, So basically I don't have a spare pin.
My Main loops runs at about 10hz, and is over 1200 lines of code, it does a lot, so all my TIME CRITICAL Processing is done in the other cogs.
I guess my project uses ALL the PINS , ALL the COGS, and now almost ALL the RAM!
Another thing is that my MILLISECOND Timer is working VERY WELL and is used in MANY places and is the most important part of my project, So I really don't want to "Play Around" with it.
Anyhow I have worked out a way to remove my BUZZER COG and merge its functionality with the MILLISECOND Timer code which has ample "Cycle time" for switching on/off a buzzer.
Thanks for all the replies.
I gather that these other counter features were NOT really designed for BASIC TIMING, It would have been nice to have some simple timers for each cog, Does the PROP 2 have something like this?
OR It would be nice if we could SPLIT a cog, In 2 or more "INTERVALS", then we could theoretically have more cogs, Remember sometimes we need the Advantage of a cog running independently but does not always has to run at FULL SPEED if you know what I mean.
Dave M
Hyperthreading like you describe was discussed, but I think Chip discarded the idea due to pipeline complexities. I believe there might be some sort of cooperative threading mechanism available, though.
-Phil
if you eat a few i/o's from the get go....so what....at least you know where you stand up front....
I would personally ditch the internal clock (or crystal ref.) and never rely on the inadequate Vref on chip.....
every good design should start with ROCK SOLID voltage regulation and a clock with the ability to poll NTP (or at the very least not drift a significant amount)
I think there is no way around the problem of poor clock implementation in the OP's project....just consider it a hard lesson learned....
Works perfect for me!
Dave M
I was presenting you with a way to not use a cog for timing.....which is what you asked for....take it or leave it David....whatever you are doing it obviously isn't working for you, or it's less than ideal....so how can you say it's a problem in your original post....and then say it's perfect later on?
Oh wait David....I remember you form the other thread....my bad, I know exactly what the problem is....
I only wanted to know this because I need more cogs, When I realised that I could get rid of another cog, and put that functionality in the existing timer cog, there's no need for other outside components. And besides , My PCB is already designed and out in the "field" so to speak, I am working on an upgrade that requires 2 more cogs.
Dave M
Dave,
I think the only way to do this, would be to break up your timer into a set of objects.....then nest those objects within other tasks (or "call" them)....I described this in Humanoidio's topic....and "coined" the term program bouncing....I have no demonstration of this code for you (I would gladly make it public if I did), but I have no doubts that it can be done....as I am doing something similar to control full color gamut LED's....it is possible to nest a broken up object (into multiple callable objects) within diverse functions....I imagine the same could be done with your timer...just as long as it was first in line....
I still think an external clock ref is the way to go, but I understand your situation.....
this issue is, of course, where to store your "chunks" and how to time them (especially for a linear function).....to put this more simply (it's not simple) just split your timer up among cogs....this could streamline some code, and possibly make extra resources available to you (maybe)....
not sure if this is helpful, or just confusing....good luck