Hello,
I cannot understand your reasoning for changing the modulo value from free-running mode, or for the use of a TPM prescale setting of 128. Both would seem quite unnecessary. Additionally, you seem to have a channel setting to toggle on output compare. You actually require software compare mode for this application.
Basically you require that a channel interrupt occurs every millisecond, by using software output compare mode for the TPM channel. When each interrupt occurs, I prefer to decrement the 'dmsec' counter, so that when timeout is reached the counter value will be zero, and further counting is inhibited. No other timeout flags would be necessary.
#define INCRVAL 4000 // Value for 1ms increment // assuming 4MHz bus, prescale 1// Global variable:volatile word dmsec;/*************************************************************************/// Initialisation for TPM2 modulevoid TPM2_init( void){ TPM2MOD = 0; // Free-running mode TPM2SC = 0x08; // Bus clock source, prescale 1 TPM2C0SC = 0x00; // Channel 0 initially disabled}/*************************************************************************/// Set delay timeout period (multiples of 1ms)void setdelay_ms( word time){ __asm sei; // Disable interrupts TPM2C0V = TPM2CNT + INCRVAL; // First compare value dmsec = time; TPM2C0SC = 0x50; // Software compare mode, interrupt enabled TPM2C0SC_CH0F = 0; // Ensure flag is clear __asm cli; // Enable interrupts}/*************************************************************************/// Wait for delay timeout periodvoid waitdelay_ms( word time){ setdelay_ms( time); while (dmsec) // Wait for timeout __RESET_WATCHDOG();}/*************************************************************************/// ISR for TPM2 channel 0interrupt void ISR_TPM2C0( void){ TPM2C0SC_CH0F = 0; // Clear flag TPM2C0V += INCRVAL; // Next compare value if (dmsec) { dmsec--; if (dmsec == 0) TPM2C0SC = 0x00; // Disable further TPM2C0 interrupts }}
Regards,
Mac