It is currently 26 May 2018 03:21

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: UART Interrupt problem
PostPosted: 09 May 2018 12:36 
Offline

Joined: 20 Oct 2014 12:33
Posts: 47
Hi,

I have problem when recieving UART data using Interrupt. I use EasyPIC Fusion V7 and PIC32MX795F512L.

Everything works well when I sending 8 or less caraters to board. When I try to send more then 8 ISR doesn't worke anymore.

I attached all project files in zip.

Does anyone have some suggestion how to solve this problem.

Thank you.

BR,
Ivo


Attachments:
Uart_Demo.zip [423.63 KiB]
Downloaded 10 times


Last edited by ikrajinovic on 16 May 2018 12:46, edited 2 times in total.
Top
 Profile  
 
PostPosted: 09 May 2018 13:17 
Offline

Joined: 27 Jun 2010 12:07
Posts: 917
Location: England
The idea of using interrupts for UART receive is that your interrupt routine puts all received characters into a buffer, updates the buffer pointer(s) and gets out of the interrupt routine as fast as possible.

Your "main" loop should then handle the receive buffer at leisure, in the safe knowledge that interrupts will handle receive data as fast as it is sent.

You are spending far too much time processing each interrupt, so that incoming serial data is no longer being processed. There is only a 4 byte hardware buffer (IIRC) so there is limited hardware buffering.

As a terrible cludge, you could change UART5_Write_Text("---UART while: "); to UART5_Write_Text(":");
This may allow a few more bytes to be sent consecutively before disaster happens, but this would be a bodge, not a solution.

In summary: Use receive interrupts to fill a buffer of sufficient size for your application and do processing within main loop. Avoid intensive or slow processing within your interrupt routine.


Top
 Profile  
 
PostPosted: 09 May 2018 13:59 
Offline

Joined: 20 Oct 2014 12:33
Posts: 47
Thank you.

Your suggestion solved my problem. I short ISR and everything works OK.

This is how it looks now:
void UART5interrupt() iv IVT_UART_5 ilevel 6 ics ICS_AUTO {
  while(UART5_Data_Ready())
  {
   uart_rd = UART5_Read();        // read the received data
   txt[dataCounter] = uart_rd;
   dataCounter++;
  }

    if (dataCounter == 128)
    {
      dataCounter = 0;
    }

  dataFlag = 1;

  U5RXIF_bit = 0;
}


Top
 Profile  
 
PostPosted: 16 May 2018 12:31 
Offline

Joined: 20 Oct 2014 12:33
Posts: 47
I have one more problem about reading UART data.
Everything works extremely well when i declare
volatile char txt[BUFLEN + 1];
where BUFLEN is less than 255. But when I try to increase array length I have a program that is not working after first message arrived that is longer than 255 bytes. I declare constant for array length.
const unsigned int BUFLEN = 256;


This is my ISR:
void UART5interrupt() iv IVT_UART_5 ilevel 6 ics ICS_AUTO {
  while(UART5_Data_Ready() == 1)
  {
   uart_rd = UART5_Read();        // read the received data
   if(dataCounter < BUFLEN)
   {
    txt[dataCounter] = uart_rd;
    dataCounter++;
   }
  }
  dataFlag = 1;
  U5RXIF_bit = 0;
}


I main I have this:
while(1)
  {
     if (dataFlag == 1 && UART5_Data_Ready() == 0){

       dataFlag = 0;
       txt[dataCounter] = '\0';
       
       if(dataCounter > 0)
       {
         UART5_Write_Text(txt);
         UART5_Write(13);
         UART5_Write(10);
       }
       else
       {
         UART5_Write_Text("Buffer is empty");
         UART5_Write(13);
         UART5_Write(10);
       }
       dataCounter = 0;
       memset( txt, 0, (BUFLEN+1));
     }
}


Top
 Profile  
 
PostPosted: 16 May 2018 12:41 
Offline

Joined: 27 Jun 2010 12:07
Posts: 917
Location: England
Did you change declaration of dataCounter from unsigned short (0 to 255) to unsigned int (0 to 65535)?


Top
 Profile  
 
PostPosted: 16 May 2018 13:18 
Offline

Joined: 20 Oct 2014 12:33
Posts: 47
Yes. Now it is
volatile unsigned int dataCounter = 0;


Top
 Profile  
 
PostPosted: 17 May 2018 14:42 
Offline

Joined: 20 Oct 2014 12:33
Posts: 47
The problem was in library function UARTx_Write_Text.

This function was unable to send string longer than 255 caracters.

I made custom function and now everything works great.

void UART5_Write_String(const char* str) {
    int i = 0;
    for (i = 0 ; str[i]!='\0' ; i++) {
        UART5_Write(str[i]);
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: