MikroC, dsPIC UART receive interrupt issue

1.9k views Asked by At

I'm communicating with an other device over UART. I'm requesting certain information by sending commands to the device. I'm repeating the command until I receive a usable answer.

The problem I have is that the ISR I use works fine when you're dealing with static and known string lengths. But in this case the strings I'm receiving could have different lengths at any time. Depending e.g. on if the device has an error or not. So lets say that the first 5 requests result in an error string which is shorter or longer than the expected length. And at attempt 6 it does result a in a string with the expected length, the (software) buffer seems corrupted some way. The bytes aren't on the right place, like some framing problem.

This is the ISR as I it is now:

void UART1RXInterrupt() iv IVT_ADDR_U1RXINTERRUPT {

  uart_rd2[LoopVar0] = UART1_Read();
  LoopVar0++;
  if (LoopVar0 >= q) //Fill array until certain lenght
  {
   LoopVar0 = 0;
   ready0 = 1;
  }
  if (U1STA.OERR = 1)
  {
    U1STA.OERR = 0;
    U1STA.FERR = 0;
  }
  U1RXIF_bit = 0;
  }

And the way I handle it in the main code:

UART1_Write_Text("A"); //Command
          if (ready0 == 1)//Data received
          {
            if (uart_rd2[0] == 0x4F && uart_rd2[1] == 0x4B) //Check message ID
            {
              //Found answer that I was looking for
            }
            ready0 = 0;
          }
          else
          delay_ms(2000); //Wait and try again
          }

How should my code be setup to handle this situation better?

1

There are 1 answers

0
jolati On

In your interrupt routine when LoopVar0 >= q you reset the queue index LoopVar0 = 0 and set a flag ready0 = 1 to process the queue data in main(). The problem is that if new interrupts are handled before the queue has been processed in main() they will overwrite the queue contents. Implement a circular queue that you process in main() whenever the start of the queue does not equal the end. This will give you time to process all incoming data.