1

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.

3 Answers3

1

The function type is declared as void so it does not return anything. You should be getting compiler warning if you try to assign its return value. Moreover it does not wait for a char. It is "non blocking" that is it returns anyway, but you need a return value to tell you whether it has a char or whether it does a not. If you want it to wait and return a char, it could be like this

int waitForChar(){                           // declare a return type
    int receivedChar;
    while(IFS1bits.U2RXIF == 0);             // wait
    receivedChar = U2RXREG;
    IFS1bits.U2RXIF = 0;                     // clear status
    return receivedChar;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

I notice a couple of things:

  1. you enable the module (UARTEN) before entirely configuring it.

  2. Shouldn't U2STA.URXDA used as flag to test for receiving?

  3. You don't configure several bits in both registers. That is ok though if you are absolutely sure the startup state is what you like.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
0

The UART initialization code was missing this line:

 AD1PCFG = 0xFFFF

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