
dsPIC33F/PIC24H Family Reference Manual
DS70205C-page 11-12 © 2010 Microchip Technology Inc.
11.4.2 Gated Timer Mode
When the Timer module operates with the internal clock (TCS = 0), Gated Timer mode can be
used to measure the duration of an external gate signal. In this mode, the timer increments by
one on every rising edge of the input clock as long as the external gate signal at the TxCK pin is
high. The timer interrupt is generated on the falling edge of the TxCK pin. Figure 11-5 illustrates
Gated Timer mode operation.
To configure the Gated Timer mode:
• Set the TGATE control bit (TxCON<6>) to enable gated timer operation
• Clear the TCS control bit (TxCON<1>) to select the internal clock source
Setting the TSYNC bit (TxCON<2>) has no effect because the internal clock is always
synchronized.
Example 11-2 illustrates the code sequence to measure the pulse-width (T1CK) in Gated Timer
mode.
Example 11-2: Initialization Code for 16-Bit Gated Timer Mode
Figure 11-5: Gated Timer Mode Operation
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 1; // Enable Gated Timer mode
T1CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
TMR1 = 0x00; // Clear timer register
PR1 = 9; // Load the period value
IPC0bits.T1IP = 0x01; // Set Timer1 Interrupt Priority Level
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Start Timer
/* Example code for Timer1 ISR */
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; // Clear Timer1 Interrupt Flag
}
Timer Clock
Input
(Internal)
TxIF
TMRx
1
2
3
8
90
PRx
9
Cleared by user software
Cleared by user software
TxCK
(Gate Input)
4
56
7
1
2
0
Comentarios a estos manuales