Hi,
The problem is you need some synchronization between overflow ISR and reading TPM1CNT.
There is nice old HC11 reference manual, which explains how to "sync" your time measurements with timer overflow, with few asm examples http://www.nxp.com/docs/en/reference-manual/M68HC11RM.pdf
Didn't verify it, but you need something like this
UINT GetTimer( void )
{
UINT tcnt;
UINT ovcnt;
__asm("SEI");
tcnt = TPM1CNT; // capture CNT once!
ovcnt = m_TPM1OFCtr;
// increment ovcnt if 1) TOF is set and
// 2) sampled TPM1CNT was closer to previous overflow than to next overflow
if( TPM1SC_TOF && (tcnt < TPM1_MOD_VALUE / 2u))
ovcnt ++;
__asm("CLI");
return (ovcnt * TPM1_MOD_VALUE + tcnt - m_StartTimer);
}
void ResetTimer( void )
{
__asm("SEI");
m_TPM1OFCtr = 0;
m_StartTimer = TPM1CNT;
// decrement ovcnt if 1) TOF is set and
// 2) sampled TPM1CNT was closer to previous overflow than to next overflow
if( TPM1SC_TOF && m_StartTimer < TPM1_MOD_VALUE / 2u)) // (m_StartTimer should be unsigned)
m_TPM1OFCtr --;
__asm("CLI");
}
Unless TPM1 is used for PWM, you should keep TPM_MOD_VALUE at its max, lowering interrupt rate. For timer duration <= 2 ^ 16 you wouldn't need timer overflow at all. For longer durations you need to switch to wider integer than UINT anyway
Regarding if(m_StartTimer < TPM1_MOD_VALUE / 2u) 16 bit compare. For mod value /2 = 500, you can test against close value 2 ^ 9 = 512. if( (m_StartTimer & 512) == 0 ) may be faster, at least with some compiler switches
Regards
Edward