
© 2010 Microchip Technology Inc. DS70205C-page 11-19
Section 11. Timers
Timers
11
11.7 32-BIT TIMER MODES OF OPERATION
11.7.1 Timer Mode
The 32-bit timer operates similarly to a 16-bit timer in Timer mode. Example 11-6 illustrates the
code sequence to set up Timer2 and Timer3 in 32-bit Timer mode.
Example 11-6: Initialization Code for 32-Bit Timer Mode
11.7.2 Gated Timer Mode
The 32-bit timer operates similarly to a 16-bit timer in Gated Timer mode. Example 11-7
illustrates the code sequence to set up Timer2 and Timer3 in 32-bit Gated Timer mode.
Example 11-7: Initialization Code for 32-Bit Gated Timer Mode
T3CONbits.TON = 0; // Stop any 16-bit Timer3 operation
T2CONbits.TON = 0; // Stop any 16/32-bit Timer3 operation
T2CONbits.T32 = 1; // Enable 32-bit Timer mode
T2CONbits.TCS = 0; // Select internal instruction cycle clock
T2CONbits.TGATE = 0; // Disable Gated Timer mode
T2CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
TMR3 = 0x00; // Clear 32-bit Timer (msw)
TMR2 = 0x00; // Clear 32-bit Timer (lsw)
PR3 = 0x0002; // Load 32-bit period value (msw)
PR2 = 0x0000; // Load 32-bit period value (lsw)
IPC2bits.T3IP = 0x01; // Set Timer3 Interrupt Priority Level
IFS2bits.T3IF = 0; // Clear Timer3 Interrupt Flag
IEC0bits.T3IE = 1; // Enable Timer3 interrupt
T2CONbits.TON = 1; // Start 32-bit Timer
/* Example code for Timer3 ISR */
void __attribute__((__interrupt__, no_auto_psv)) _T3Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T3IF = 0; // Clear Timer3 Interrupt Flag
}
T3CONbits.TON = 0; // Stop any 16-bit Timer3 operation
T2CONbits.TON = 0; // Stop any 16/32-bit Timer3 operation
T2CONbits.T32 = 1; // Enable 32-bit Timer mode
T2CONbits.TCS = 0; // Select internal instruction cycle clock
T2CONbits.TGATE = 1; // Enable Gated Timer mode
T2CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
TMR3 = 0x00; // Clear 32-bit Timer (msw)
TMR2 = 0x00; // Clear 32-bit Timer (lsw)
PR3 = 0x0002; // Load 32-bit period value (msw)
PR3 = 0x0000; // Load 32-bit period value (lsw)
IPC2bits.T3IP = 0x01; // Set Timer3 Interrupt Priority Level
IFS2bits.T3IF = 0; // Clear Timer3 Interrupt Flag
IEC0bits.T3IE = 1; // Enable Timer3 interrupt
T2CONbits.TON = 1; // Start 32-bit Timer
/* Example code for Timer3 ISR */
void __attribute__((__interrupt__, no_auto_psv)) _T3Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T3IF = 0; // Clear Timer3 Interrupt Flag
}
Komentarze do niniejszej Instrukcji