Hi,
I'm running MSP430G2553 TimerA from a 32768 Hz watch crystal. When an event happens on one of the micro pins, I want to read the value of TimerA. The micro core itself is running from DCO a lot faster (4 MHz).
The User Guide says that it is best to read the value in TA1R when the clock is stopped, to avoid a race condition. Apart from wondering what this race condition is (does it mean the micro or timer might spuriously start running wildly, or does it mean that the value of TA1R might be read while it is in the middle of changing, possibly changing several bits somewhat wildly?), I'm wondering what the safest way to deal with this would be.
I don't really want to stop the timer to read it as it could inhibit a valid increment of the timer, if the 32768 Hz crystal just happens to flip over at that moment. As the period of this is some 30.5 us, that's quite a significant error that could be introduced.
Another suggestion in the User Guide is to read it a number of times and take a majority vote. Has anyone tried this with success? How many times should it be read?
I was thinking to read it twice (reads 1 and 2) and if the values are the same, then all well and good - use that value. But if the values are different, then do another read (no 3) and compare this with no 2. If they are the same, then use the value of number 3 read (or 2, since they are the same). But if no 3 is different to no 2, is it worth comparing it to no 1 read and, if they are the same, using this value? It would seem a bit cavalier to use no 1 read to corroborate no 3 read if the intervening read says something different.
Also, is three reads enough? I suppose it depends on how quickly the timer flips over from one count to another, with no 'rubbish' counts in between. (More likely if many bits are changing simultaneously.) It may also depend on the speed of the micro and how quickly it can 'grab' reads. Could it be read so quickly that two successive reads give the same result, while it is in fact 'in process' of changing and the values read are not valid?
Anyone with any experience on this? I'd be grateful to know!