UART communication not working with higher baud rate values

baudratecpicuart

I have a PIC32MX795F512L which is connected to 8MHZ crystal. UART program is working with 9600 baudrate value but not working with higher baud values like 115200.

Following is the code:

#define FCY 72000000UL //PIC32 working on 72MHZ
#define FPB (FCY/2)    // Peripheral clock is half of FCY
#define BAUDRATE    9600 // Baud Rate Value
#pragma config POSCMOD=HS,FNOSC=PRIPLL //Using High speed mode with primary Oscillator 
#pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, FPLLODIV=DIV_1
#pragma config FPBDIV=DIV_2, FWDTEN=OFF
int main()
{
  OpenUART1( UART_EN | UART_NO_PAR_8BIT | UART_1STOPBIT  , UART_RX_ENABLE | UART_TX_ENABLE, (FPB/16/BAUDRATE)-1 );
  while(1)
  {
   putsUART1("hello\n");
   DelayMs(1000);
  }
}

Is this could be the crystal not working properly. What other reasons can cause the UART not work with higher baudrates?

Best Answer

I'm guessing that the micro can't hit the higher baud rates accurately enough given the rather low clock speed of 8 MHz. This illustrates one of the drawbacks of blindly using a canned library. Your library apparently has no means to tell you what baud rate it actually ended up with and at run time it's too late to do anything about it.

Most UARTs need a clock at 16x the baud rate. At 9600 baud, that means the UART needs to get 153.6 kHz out of its baud rate generator. If you're starting with 8 MHz and constrained to only divide that by integers, then the best you can do is 52, which comes out to a 16x clock of 153.8 kHz, and a baud rate of 9615. The system will work fine with that 0.16% error, and you aren't noticing it.

At 115.2 kBaud you need a UART clock of 1.843 MHz. That is 8 MHz / 4.34. The closest you can come is divide by 4, which yields a UART clock of 2 MHz, and a baud rate of 125 k. That is 8.5% error, which is too large to allow the system to work.

When you need high baud rates, and especially when you want to keep the clock of the microcontroller low, you usually want to use a baud rate crystal, like 7.3728 MHz. That is deliberately a multiple of all the common baud rates times 16.