void time2()
{
/* The following code example will enable Timer2 interrupts, load the Timer2
Period register and start Timer2.
When a Timer2 period match interrupt occurs, the interrupt service
routine must clear the Timer2 interrupt status flag in software.
*/
T2CON = 0x00; //Stops the Timer2 and reset control reg.
TMR2 = 0x00; //Clear contents of the timer2 register
PR2= 0x4E20; //Load the Period register with the value 0x4E20
T2CONbits.TCKPS = 1;//1:8 prescale value
IPC0bits.T2IP = 0x04; //Setup Timer2 interrupt for desired priority level
// (This example assigns level 4 priority)
IFS0bits.T2IF = 0; //Clear the Timer2 interrupt status flag
IEC0bits.T2IE = 1; //Enable Timer2 interrupts
T2CONbits.TON = 1; //Start Timer2 with prescaler settings at 1:1 and
//clock source set to the internal instruction cycle
/* Example code for Timer2 ISR*/
void __attribute__((__interrupt__, __shadow__)) _T2Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T2IF = 0; //Reset Timer2 interrupt flag and Return from ISR
}
}
文章评论(0条评论)
登录后参与讨论