0

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!

Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
Ammar
  • 1,203
  • 5
  • 27
  • 64
  • @Carl Norum asynchronous in this context does not mean "without" a clock, but without a common clock. The transmission of data and framing bits from the 2 ends of the serial channel occur at independent rates (asynchronous) that only need to be _approximately_ the same (theory 5%, practically 2%). – chux - Reinstate Monica Jul 19 '13 at 00:39

2 Answers2

1

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.

  • When BRGH is set to 0 (default), the timer is incremented every 16th cycle of peripheral bus clock.
  • When BRGH is 1, the timer increments every 4th cycle.

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:

  • When BRGH = 0:

    UxBRG = FPB / (16 * BAUDRATE) - 1
    
  • When BRGH = 1:

    UxBRG = FPB / (4 * BAUDRATE) - 1
    

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
makes
  • 6,438
  • 3
  • 40
  • 58
  • Can you clarify what the 12 represents again? (I'm confused) Let's say I have 20MHz clock frequency, what should the baud rate be ? Thanks! – Ammar Jul 17 '13 at 21:50
  • @Ammar The value in the UxBRG register represents how many clock cycles a data pulse lasts: the smaller the value, the faster the baud rate and vice versa. Use the formulas in this post and in the linked reference manual to calculate this value. I added some extra details into my answer. – makes Jul 17 '13 at 22:23
  • So I should get the UxBRG as close as possible to 1 ? I will do the math for now, I appropriate your help. – Ammar Jul 17 '13 at 22:48
  • @Ammar no, you decide the baud rate you want, then just calculate the value for UxBRG. You set UxBRG to whatever it needs to be for your desired baud rate. Study the linked reference manual and try out the formulas there for UxBRG and baud rate error on paper to learn how the values interact. – makes Jul 17 '13 at 23:07
  • Your example results in `259.4166...`, not `259`. Of course BRG must be loaded with an integer. To get the best BRG value, use a "round_to_nearest" rather than a "floor" value that results from direct `int` implementation of the formula. `C` recommendation: `BRG = (FPB + (2* BAUD))/(4 * BAUD) - 1;`. The example rounds and floors to 259 either way, but this would be meaningful with other FPB, BAUD. – chux - Reinstate Monica Jul 19 '13 at 00:53
0

If you are using some of the latest development libraries and code examples from Microchip you may find that there are already UART methods in the libraries that will set up the PIC for your needs. If you dig deep into the new compiler directory structures you will find help files in the microsoft format (no fear, if you are on a Unix type computer there are Unix utilities that read these types of files.). There you can drill down into the help to find the documentation of various ready made methods you can call from your program to configure the PIC's hardware. Buyer Beware, the code is not that mature. For instance I was working on a PIC project that needed to sample two analog signals. The PIC hardware A/D converter was very complex. But it was clear the ready made code only covered about 10% of that PIC's abilities.

-good luck

st2000
  • 286
  • 1
  • 16