cbluetoothuartmicrochippic24

UART buffer not raising a flag on PIC24F


I am using an RN42-XV bluetooth module to send characters to the PIC24F from a computer. The module is connected/paired correctly and the characters that are sent across are also correct (used an oscilloscope).

This is how it is being initialized:

void initUART(){

   //Peripheral Pin Mapping
   RPINR19bits.U2RXR = 5; //pin 14 UART Receive
   RPOR5bits.RP11R = 3; //pin 17 UART Transmit

   //Configuring the UART
   U2BRG = BRGVAL;
   U2MODEbits.UARTEN = 1;
   U2MODEbits.UEN = 0;
   U2MODEbits.PDSEL = 0;// 8 bit no parity
   U2MODEbits.STSEL = 0; // 1 stop bit
   U2STAbits.UTXEN = 0;
   U2STAbits.URXISEL = 0;

   //Putting the UART interrupt flag down.
   IFS1bits.U2RXIF = 0;
 }

I am also using this function to get the contents of the buffer:

int waitForChar(){
   int receivedChar;
   // Use the UART RX interrupt flag to wait until we recieve a character.
   while(IFS1bits.U2RXIF == 1){
      // Clear the UART RX interrupt flag to we can detect the reception
      // of another character.
      IFS1bits.U2RXIF = 0;
      // U2RXREG stores the last character received by the UART. Read this
      // value into a local variable before processing.
      receivedChar = U2RXREG;
   }
return receivedChar;
}

The problem is that the program never goes into the while loop inside the function waitForChar() because the UART interrupt flag is never raised by the hardware. I have tried different PIC24Fs but all run into the same issue.


Solution

  • The UART initialization code was missing this line:

     AD1PCFG = 0xFFFF
    

    The ADC flags have priority over UART. This line disables them.