Using the counter register for timing
groggory
Posts: 205
I am building basically a precision stopwatch. I have a temperature compensated crystal DS32khz that is outputting 32.768kHz with a pretty high precision.
I am outputting that to my propeller and telling it to count the pulses in a counter register.
When a timing event occurs I pull the current value of the counter register. When a stop event occurs I pull the new value of the register. The difference of the two is the time.
The problem is, how do I know if the register has rolled over its 32bit maximum? Right now I check this with a little extra logic, but it slows down my loop.
How should I best accomplish this?
Another idea I had was to use one register to count at full speed and another register to count at a lower speed, as fed by a frequency divider on my input timing chip.
What do you guys think?
I am outputting that to my propeller and telling it to count the pulses in a counter register.
When a timing event occurs I pull the current value of the counter register. When a stop event occurs I pull the new value of the register. The difference of the two is the time.
The problem is, how do I know if the register has rolled over its 32bit maximum? Right now I check this with a little extra logic, but it slows down my loop.
How should I best accomplish this?
Another idea I had was to use one register to count at full speed and another register to count at a lower speed, as fed by a frequency divider on my input timing chip.
What do you guys think?
Comments
Correct me if I'm wrong. A counter register overflows after 128K seconds when fed from a 32K clock (assuming you count edges). That's more than 36 hours.
Do you really need measurements beyond that? If not simply reset the counter every time you take/start a measurement. Even considering SPINs signed nature you'd still have 18 hours left without jumping through hoops.
DeltaTime = LastestSnap - PreviousSnap
It's all 32bit summing circuits so a rollover in the counter is naturally accounted for. As long as the interval between the two readings of the counter is less than half the rollover period of the counter then it'll work indefinitely.
Resetting the counter will destroy the counts between the latest sample and the reset, so it's best not to use that method.