문제

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!

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top