Electrical – pic24 UART receive data is invalid

picuart

Hello i m writng a program for UART receive interrupt for PIC24FJ64GA306

i m receiving a data which is in format of 0xAAXXXXXXXX, where data after AA can be anything… the data transmityted through host and received by pic doesnt match

Also i observed a strange issue: when i transmit for ist time the data is not fully received, only 2 bytes of data is received, where in when i transmit 2nd time i get a OVER run error.

The code is as follows:

static void uart1_DebugSerial_init(void)
{
  /*Setting the RPINRbits to map the peripheral port pins*/
   RPINR18bits.U1RXR = 26;   /*Selects RP26 as UART1 RX pin*/
   /*Setting the RPORbits to map the peripheral port pins to UART TX pin*/    
   RPOR10bits.RP21R = 3;   /*Assign U1TX to RP21*/  

   U1MODEbits.STSEL = 0; // 1-Stop bit
   U1MODEbits.PDSEL = 0; // No Parity, 8-Data bits
   U1MODEbits.ABAUD = 0; // Auto-Baud disabled
   U1MODEbits.BRGH = 0; // Standard-Speed mode
   U1BRG = BRGVAL; // Baud Rate setting for 9600

   //   U1STAbits.UTXISEL0 = 0;        // Interrupt after one TX character     is transmitted
  //    U1STAbits.UTXISEL1 = 0;
  IEC0bits.U1TXIE = 0;           // Disable UART TX interrupt
    IEC0bits.U1RXIE = 1;            // Enable the UART RX interrupt
  U1STAbits.URXISEL = 0;         // Interrupt after one RX character is   received

  U1MODEbits.UARTEN = 1; // Enable UART
  U1STAbits.UTXEN = 1; // Enable UART TX

  /* Wait at least 105 microseconds (1/9600) before sending first char */
  DELAY_105uS
  //U1TXREG = 'a'; // Transmit one character
  }

Uart transmitt code:

int uart1_DebugSerial_send(char *buf)
 {
    int len, buf_length;

   buf_length = strlen(buf);
   len = buf_length;

   while(buf_length--)
   {
       while(!(U1STAbits.TRMT));   //stay here until the UXTSR register is empty
       U1TXREG = *buf++;
    }

   return len;

}

UART Receive ISR

void __attribute__((__interrupt__, no_auto_psv)) _U1RXInterrupt(void)
 {
   static char str[50];
  unsigned char RxChar;
  static uint8_t uart_header_rcvd_flag = false;
  static uint8_t i = 0;

  IFS0bits.U1RXIF = 0;    /*Clear the Rx interrupt flag*/
  /* Check for receive errors */
  if(U1STAbits.FERR == 1)
   {

//          continue;
  }
  /* Must clear the overrun error to keep UART receiving */
  if(U1STAbits.OERR == 1)
  {

    U1STAbits.OERR = 0;
  //        continue;
}
/* Get the data */
if(U1STAbits.URXDA == 1)
{

    RxChar = U1RXREG;

}
/*If the Received char is 0xAA and Header received flag is false, then
 start receiving the packet*/

if (RxChar == 0xAA && uart_header_rcvd_flag == false)
{

    uart_header_rcvd_flag = true;
    i = 0;
}
/*If the Packet reception is already commenced, then check for the PKT_SIZE
 to stop populating the host_rx_buf
 */
if (uart_header_rcvd_flag == true)
{

    host_rx_buf[i++] = RxChar;

    if (i == HOST_PKT_SIZE)
    {

        host_pkt_rcvd_flag = true;
        uart_header_rcvd_flag = false;
    }
}    
 }

Please help me, where i m going wrong

Thanks in advance

Best Answer

the data transmityted through host and received by pic doesnt match

Check to make sure if both the baud rates for Transmitter and receiver are the same.

Also i observed a strange issue: when i transmit for ist time the data is not fully received, only 2 bytes of data is received, where in when i transmit 2nd time i get a OVER run error.

As the type of error indicates. Your Buffer is overflowing. Which means your not reading out of the buffer faster than new data arrives.Check the receiver buffer size and use hardware or software flow control appropriately. Looking at your ISR,reduce the number of operations performed in ISR to avoid overflow and it must surely work. Do all processing checks in main().