I am writing a program to send data from one microcontroller and receive the data using on another PIC32 microcontroller and display the received data on LCD.
I am using UART2 of both PIC32MX to send and receive data and I am using UART interrupt at receiver side. I am not getting any data on receiving side and the LCD display shows nothing. The code is as follows:
The buffer size is 20.
Function to Initialise UART:
void UARTinitialization(void)
{
PPSUnLock;
PPSOutput(2, RPC13, U2TX); // Assign U2TX to pin RPC13
PPSInput(1, U2RX, RPC14); // Assign U2RX to pin RPC14
PPSLock;
TRISCbits.TRISC14 = 1;
TRISCbits.TRISC13 = 0;
U2MODE = 0;
// enable TX and RX
U2STA = 0x1400;
U2BRG = Baud2BRG(115200, pbclk);
// enable UART2
IFS1bits.U2RXIF = 0;
IPC9bits.U2IP = 2;
IPC9bits.U2IS = 1;
IEC1bits.U2RXIE = 1;
U2MODESET = 0x8000;
rxIndex = 0;
// RS485 = READ;
} // END of UART Initialization
Function to display received char:
void Display(void)
{
if (UART_Flag == 1) // check whether the program enters into the ISR
{
int i;
for (i=0; i<BUFFER_SIZE; i++)
{
LCDinitWriteChar(rxBuffer[i]);
if (rxBuffer[i] == '\0')
break;
}
UART_Flag = 0;
}
}
Function to send the data (used at transmitter side):
void writeUART(char *string, size_t size)
{
while(size > 0)
{
while (U2STAbits.UTXBF);
// put data in to transmit register
U2TXREG = *string;
string++;
size--;
}
// wait for complete transmission of data
while(!U2STAbits.TRMT);
}
ISR code:
void __ISR(_UART_2_VECTOR, IPL6SRS) _U2Interrupt(void)
{
//Receive Buffer Overrun Error Status bit.
if (U2STAbits.OERR)
U2STAbits.OERR = 0;
else if (IFS1bits.U2RXIF)
{
rxBuffer[rxIndex] = U2RXREG;
// rxIndex++;
if (rxBuffer[0] != '*')
{
rxIndex = 0;
}
else
{
rxIndex++;
}
UART_Flag = 1;
}
IFS1bits.U2RXIF = 0;
}
Main function at receiver side:
int main(int argc, char** argv)
{
pbclk = SYSTEMConfigPerformance(80000000);
UARTinitialization();
LCDInit(); // LCD Intialization
clearScreen();
while(1)
{
if (U2STAbits.OERR) U2STAbits.OERR = 0;
if (U2STAbits.PERR) U2STAbits.PERR = 0;
if (U2STAbits.FERR) U2STAbits.FERR = 0;
Display(); // function to display received data
LCDinitClear();
}
return (EXIT_SUCCESS);
}