Hiện tại mình sử dụng 2 ngắt Timer1, Timer2 và 5 ngắt ngoài (INT0, INT1, INT2, INT3, INT4). Đã cấu hình được các ngắt đó rồi nhưng không biết bằng cách nào để thiết lập thứ tự ưu tiên ngắt (interrupt priority). Bạn nào biết thì bày cho mình với nhé. Xin cảm ơn!
Khai báo ngắt Timer:
PHP Code:
void Init_Timer1( void )
{
/* ensure Timer 1 is in reset state */
T1CON = 0;
/* reset Timer 1 interrupt flag */
IFS0bits.T1IF = 0;
/* enable Timer 1 interrupt */
IEC0bits.T1IE = 1;
/* set Timer 1 period register */
PR1 = 0x8000;
/* select external timer clock */
T1CONbits.TCS = 1;
/* enable Timer 1 and start the count */
T1CONbits.TON = 1;
}
Ngắt Timer
PHP Code:
void __attribute__((__interrupt__)) _T1Interrupt( void )
{
//User code
/* reset Timer 1 interrupt flag */
IFS0bits.T1IF = 0;
}
Khai báo ngắt ngoài:
PHP Code:
void INTx_IO_Init(void)
{
INTCON2 = 0x001E; /*Setup INT1, INT2, INT3 & INT4 pins to interupt */
/*on falling edge and set up INT0 pin to interupt */
/*on rising edge */
IFS0bits.INT0IF = 0; /*Reset INT0 interrupt flag */
IEC0bits.INT0IE = 1; /*Enable INT0 Interrupt Service Routine */
IFS1bits.INT1IF = 0; /*Reset INT1 interrupt flag */
IEC1bits.INT1IE = 1; /*Enable INT1 Interrupt Service Routine */
IFS1bits.INT2IF = 0; /*Reset INT0 interrupt flag */
IEC1bits.INT2IE = 1; /*Enable INT0 Interrupt Service Routine */
IFS2bits.INT3IF = 0; /*Reset INT1 interrupt flag */
IEC2bits.INT3IE = 1; /*Enable INT1 Interrupt Service Routine */
IFS2bits.INT4IF = 0; /*Reset INT1 interrupt flag */
IEC2bits.INT4IE = 1; /*Enable INT1 Interrupt Service Routine */
}
Chương trình phục vụ ngắt ngoài
PHP Code:
void __attribute__((__interrupt__)) _INT0Interrupt(void)
{
//User code
IFS0bits.INT0IF = 0; //Clear the INT0 interrupt flag or else
//the CPU will keep vectoring back to the ISR
}
void __attribute__((__interrupt__)) _INT1Interrupt(void)
{
//User code
IFS1bits.INT1IF = 0; //Clear the INT1 interrupt flag or else
//the CPU will keep vectoring back to the ISR
}
void __attribute__((__interrupt__)) _INT2Interrupt(void)
{
//User code
IFS1bits.INT2IF = 0; //Clear the INT2 interrupt flag or else
//the CPU will keep vectoring back to the ISR
}
void __attribute__((__interrupt__)) _INT3Interrupt(void)
{
//User code
IFS2bits.INT3IF = 0; //Clear the INT3 interrupt flag or else
//the CPU will keep vectoring back to the ISR
}
void __attribute__((__interrupt__)) _INT4Interrupt(void)
{
//User code
IFS2bits.INT4IF = 0; //Clear the INT4 interrupt flag or else
//the CPU will keep vectoring back to the ISR
}