Problem with Receive Interrupt in MikroC for dsPIC
Posted: 25 Aug 2023 06:24
Hi all,
I am using dsPIC30F4013 and mikroC for dsPIC compiler. I am trying to use UART interrupt. For test UART, I used UART Terminal tools in the complier. But actually, my interrupt only occurred in limited times.
For example, if I send "A" from UART Terminal, at the 1st, 2nd, 3rd, and 4th, the interrupt occured. However, at 5th sending, the interrupt did not occur. Please help me to solve this problem. Below is my code:
#define TXD_dir TRISF5_bit
#define RXD_dir TRISF4_bit
#define TXD LATF5_bit
#define RXD LATF4_bit
// LED signal
#define LED_Signal_dir TRISD1_bit
#define LED_Signal LATD1_bit
void UART2_Config()
{
TXD_dir = 0; // TXD direction
RXD_dir = 1; // RXD direction
TXD = 1; // TXD pin
RXD = 1; // RXD pin
U2STAbits.UTXEN = 1; // 1 = UART transmitter enabled, UxTX pin controlled by UART (if UARTEN = 1); 0 = UART transmitter disabled
U2MODEbits.LPBACK = 0; // 0 = Disable Loopback mode
IFS1bits.U2RXIF = 0; // Clear the interrupt flag
// Settings for Receive Interrupt priority
IPC6bits.U2RXIP0 = 1; // Reference dsPIC30F Family Reference - Page 160/772
IPC6bits.U2RXIP1 = 1;
IPC6bits.U2RXIP2 = 1;
// Settings for Receive Interrupt mode
IEC1bits.U2RXIE = 1; // Enable UART2 receive interrupt
U2STAbits.URXISEL1 = 0;// 0x :Interrupt flag bit is set when a character is received; Page 5/34 SEction34 UART-Part 2
U2STAbits.URXISEL0 = 0;
// Enable the UART module
U2MODEbits.UARTEN = 1; // 1 = Enable UARTx; UARTx pins are controlled by UARTx module
}
void main() {
ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
LED_Signal_dir = 0;
UART2_Init_Advanced(9600,_UART_8BIT_NOPARITY,_UART_ONE_STOPBIT);
Delay_ms(1000); // Wait for UART module to stabiliz
UART2_Config();
while(1)
{
}
}
void UART2_Interrupt() iv IVT_ADDR_U2RXINTERRUPT ics ICS_AUTO
{
LED_Signal = 1;
Delay_ms(100);
LED_Signal = 0;
if(U2STAbits.OERR == 1)
{
U1STAbits.OERR = 0; // Clear Overrun Error to receive data
}
IFS1bits.U2RXIF = 0; // Clear the interrupt flag
}
I am using dsPIC30F4013 and mikroC for dsPIC compiler. I am trying to use UART interrupt. For test UART, I used UART Terminal tools in the complier. But actually, my interrupt only occurred in limited times.
For example, if I send "A" from UART Terminal, at the 1st, 2nd, 3rd, and 4th, the interrupt occured. However, at 5th sending, the interrupt did not occur. Please help me to solve this problem. Below is my code:
#define TXD_dir TRISF5_bit
#define RXD_dir TRISF4_bit
#define TXD LATF5_bit
#define RXD LATF4_bit
// LED signal
#define LED_Signal_dir TRISD1_bit
#define LED_Signal LATD1_bit
void UART2_Config()
{
TXD_dir = 0; // TXD direction
RXD_dir = 1; // RXD direction
TXD = 1; // TXD pin
RXD = 1; // RXD pin
U2STAbits.UTXEN = 1; // 1 = UART transmitter enabled, UxTX pin controlled by UART (if UARTEN = 1); 0 = UART transmitter disabled
U2MODEbits.LPBACK = 0; // 0 = Disable Loopback mode
IFS1bits.U2RXIF = 0; // Clear the interrupt flag
// Settings for Receive Interrupt priority
IPC6bits.U2RXIP0 = 1; // Reference dsPIC30F Family Reference - Page 160/772
IPC6bits.U2RXIP1 = 1;
IPC6bits.U2RXIP2 = 1;
// Settings for Receive Interrupt mode
IEC1bits.U2RXIE = 1; // Enable UART2 receive interrupt
U2STAbits.URXISEL1 = 0;// 0x :Interrupt flag bit is set when a character is received; Page 5/34 SEction34 UART-Part 2
U2STAbits.URXISEL0 = 0;
// Enable the UART module
U2MODEbits.UARTEN = 1; // 1 = Enable UARTx; UARTx pins are controlled by UARTx module
}
void main() {
ADPCFG = 0xFFFF; // Configure AN pins as digital I/O
LED_Signal_dir = 0;
UART2_Init_Advanced(9600,_UART_8BIT_NOPARITY,_UART_ONE_STOPBIT);
Delay_ms(1000); // Wait for UART module to stabiliz
UART2_Config();
while(1)
{
}
}
void UART2_Interrupt() iv IVT_ADDR_U2RXINTERRUPT ics ICS_AUTO
{
LED_Signal = 1;
Delay_ms(100);
LED_Signal = 0;
if(U2STAbits.OERR == 1)
{
U1STAbits.OERR = 0; // Clear Overrun Error to receive data
}
IFS1bits.U2RXIF = 0; // Clear the interrupt flag
}