To share an experience here.
We are using the RTC counter of PA4 as a free run counter to calculate motor speed. The RTC counter is a 16bit counter split into a high byte and a low byte register.
We found the high byte will not carry immediately when the low byte is changed from 0xFF to 0x00.
For example: we got 0x00FE -> 0x00FF -> 0x0000 -> 0x0101…
It has a chance to get a 0x0000 value which is wrong and results in wrong speed calculation.
Solution: To read the low byte first, then high byte.
You can use a 16bit variable to store the result of the 16bit counter, but you must read the RTC_CNTL first, then RTC_CNTH.
It is also not recommend to use the register window or the memory window in the Codewarrior IDE to view these registers.
Instead, you can read the two SFRs in order and print it to terminal for check.
Reason: The reason is the design of lock mechanism.
The RTC_CNTL must be read first to lock the counter and then read RTC_CNTH to correctly read 16bit counter.
The design is to avoid the case: When RTC_CNT counter is 0x01FF, read RTC_CNTL to be 0xFF, the high byte of counter
is locked to be 0x01, then read RTC_CNTH will get 0x01. If the counter is not locked, 0x02 will be read from RTC_CNTH.
Then 0x02FF will be read which is wrong.