23.4.3.2 Gated Timer Initialization Steps

The following steps must be performed to configure the timer for Gated Timer mode:

  1. Clear the ON bit (TxCON[15] = 0) to disable the timer.
  2. Clear the TCS bit (TxCON[1] = 0) to select the System Clock source.
  3. Set the TGATE Control bit (TxCON[7] = 1) to enable Gated Timer mode.
  4. Select the desired prescaler.
  5. Clear the Timer register, TMRx.
  6. Load the Timer Period register, PRx, with the desired 32-bit match value.
  7. If interrupts are used:
    1. Clear the TxIF Interrupt Flag bit in the IFS register.
    2. Configure the Interrupt Priority Levels in the IPC register.
    3. Set the TxIE Interrupt Enable bit in the IEC register.
  8. Set the ON bit (TxCON[15] = 1) to enable the timer.

Gated Timer Example Code

T1CON = 0x0;		    // Stop timer and clear control register
T1CON = 0x00000080;		      // Gated Timer mode, prescaler at 1:1, internal clock source
TMR1 = 0x0;				// Clear timer register
PR1 = 0xFFFFFFFF;			 // Load period register with 32-bit match value
T1CONbits.ON = 1;			// Start timer