Problem with Receive Interrupt in MikroC for dsPIC

General discussion on mikroC PRO for dsPIC30/33 and PIC24.
Post Reply
Author
Message
ngodinhnhan
Posts: 4
Joined: 23 Feb 2023 07:12

Problem with Receive Interrupt in MikroC for dsPIC

#1 Post by ngodinhnhan » 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
}

hexreader
Posts: 1784
Joined: 27 Jun 2010 12:07
Location: England

Re: Problem with Receive Interrupt in MikroC for dsPIC

#2 Post by hexreader » 25 Aug 2023 08:43

1) The biggest problem is that you have failed to read a character out of the UART receive buffer on each interrupt. This will cause the buffer to fill after just a few characters.

2) It is a bad idea to have a delay within an interrupt. You might get away with it when manually typing in from a terminal, but in a real application, many characters may be missed during the 100ms that you are stuck in interrupt routine.

3) please use code tags to make your code readable, or better still, zip up the whole project folder and attach like below.

Attached is my attempt at a fix:
This project is designed for 10MHz external 2-pin crystal and x8 PLL to give Fosc of 80MHz
Attachments
forum.zip
(52.24 KiB) Downloaded 51 times
Start every day with a smile...... (get it over with) :)

ngodinhnhan
Posts: 4
Joined: 23 Feb 2023 07:12

Re: Problem with Receive Interrupt in MikroC for dsPIC

#3 Post by ngodinhnhan » 29 Nov 2023 02:46

Hi hexreader,
thanks for your comments. I fixed it. But one prolem arises. Please look at my code belowand attached image. I am using UART Terminal for testing. If I send 1, I will recieve CONECTEDCONNECTED instead of just only CONNECTED as I expected. if I send 1 again, I will get CONECTEDCONNECTED. That mean the microcontroller send two times or three time of text "CONNECTED". Could you take the time to help me ?

Code: Select all

#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

// global variables
unsigned char tempchar;

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();
    UART2_Write_Text("\r\n\nUART test 25 August 2023\r\n");
    while(1){
    }
}
void UART2_Interrupt() iv IVT_ADDR_U2RXINTERRUPT ics ICS_AUTO{
    IFS1bits.U2RXIF = 0;                                                        // Clear the interrupt flag
    LED_Signal = ~LED_Signal;                                                   // toggle LED each character
    if(U2STAbits.OERR == 1){
        U1STAbits.OERR = 0;                                                     // Clear Overrun Error to receive data
    }
    tempchar = UART2_Read();                                                    // one character from buffer
    UART2_Write_Text("CONNECTED");                                                      // echo to terminal
}
Attachments
UART error.png
UART error.png (51.23 KiB) Viewed 293 times

User avatar
IvanJeremic
mikroElektronika team
Posts: 316
Joined: 05 Sep 2022 14:32

Re: Problem with Receive Interrupt in MikroC for dsPIC

#4 Post by IvanJeremic » 29 Nov 2023 09:51

Hi,

The code you sent worked fine for me, i have attached the entire project i used try and see if it works for you.
forum.7z
(33.45 KiB) Downloaded 25 times
Regards,

Ivan.

Post Reply

Return to “mikroC PRO for dsPIC30/33 and PIC24 General”