embeddedmicrocontrollermicrochippic32

How to get UART to work in PIC32 with correct clock frequency and baud rate?


I am working on UART with pic32mx5xx. All I need is to send a message from pic to terminal (Putty), but it is not working as I would get invalid characters appearing. The baud rate is set to 19200, how do I calculate the clock frequency?

Is it true that the clock frequency of the UART is 16 times the baud rate. If I do the math the clock frequency should be 307200, but this is doesn't seem right.

Can someone help me understand how baud rate and clock frequency relate to each other ? Also how to calculate both?

Thanks!


Solution

  • The baud rate generator has a free-running 16-bit timer. To get the desired baud rate, you must configure its period register UxBRG and prescaler BRGH.

    It is usually better to set BRGH to 1 to get a smaller baud rate error, as long as the UxBRG value doesn't grow too large to fit into the 16-bit register (on slower baud rates).

    The value in the period register UxBRG determines the duration of one pulse on the data line in baud rate generator's timer increments.

    See the formulas in section 21.3 - UART Baud Rate Generator in the reference manual to learn how to calculate a proper value for UxBRG.

    To compute the period of the 16-bit baud rate generator timer to achieve the desired baud rate:

    Where FPB is the peripheral bus clock frequency.

    For example, if FPB = 20 MHz and BRGH = 1 and the desired baud rate 19200, you would calculate:

    UxBRG = 20000000 / (4 * 19200) - 1
          = 259