Emanuele Corini

MC9S08DZ16 Timer Overflow interrupt

Discussion created by Emanuele Corini on Jul 13, 2017
Latest reply on Jul 20, 2017 by Emanuele Corini

Hello, 

 

I'm using S08DZ16 microcontroller, and i'm trying to use timer overflow interrupt to get time a measuring function.

I've problems to get the correct number of overflows.

 

Timer initialization:

#define TPM1_MOD_VALUE (1000U) ///< TPM1 Module value used to get 1ms period.

 

// Module Counter @1MHz, Period value 1ms

/* TPM1SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */
TPM1SC = 0x00; // Stop and reset counter
TPM1MOD = TPM1_MOD_VALUE ; // Period value setting
(void)(TPM1SC == 0); // Overflow int. flag clearing (first part)
/* TPM1SC: TOF=0,TOIE=1,CPWMS=0,CLKSB=0,CLKSA=1,PS2=0,PS1=1,PS0=1 */
TPM1SC = 0x4B; // Int. flag clearing (2nd part) and timer control register setting

 

Timer start function:

void ResetTimer( void )
{
   m_TPM1OFCtr = 0;
   m_StartTimer = TPM1CNT;
}

 

Timer get function:

UINT GetTimer( void )
{
   return (m_TPM1OFCtr * TPM1_MOD_VALUE + TPM1CNT - m_StartTimer);
}

 

IRQ for timer overflow:

INTERRUPT TPM1Overflow( void )
{
   (void) TPM1SC;
   TPM1SC_TOF = 0;
   //Increment overflow counter
   m_TPM1OFCtr++;
}

 

In particular sometimes happens that the number of overflow is 1 less or 1 more than expected.

Outcomes