Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Baud rate calculation

Status
Not open for further replies.

ghostman11

Well-Known Member
can anyone explain in idiot language how you go about calculating what baud rate to use with the usart on a pic. its something i just dont seem able to get my head around :D thanks guys
 
thought i should add an edit...... the code i have is


Code:
OpenUSART ( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW,51);

i am using internal osc at 8mhz and would like 9600 baud rate.
i found this calculator online (its free) micromagic systems - home

but i dont seem to be doing something right as i just get rubish on the screen. hence the reason for asking someone clever how its done :D
 
ive been looking at the datasheet but nothing is sinking in tonight so i am going to leave it untill tommorow! is frustrating when i get dense days like this :D
 
ive used the calculator but just get rubish on the terminal. probaly best wait till i have had some sleep :D
 
The calcs look scary in the datasheet but it's actually pretty easy.

Depending on bit BRGH you divide the xtal clock by 16 or 64.

So for a 4 Mhz xtal, BRGH=1, to get 9600 baud;
4000000 / 64 / 9600 = 6

For a 4 Mhz xtal, BRGH=0, to get 9600 baud;
4000000 / 16 / 9600 = 26

Obviously 26 is the better choice (better resolution). Then the last task is you always subtract 1 from the result;

so 6-1 = 5
or 26-1 = 25

That is the value you use in the BRG.
 
Common baud rates are usually listed in the tables in the device datasheets. For example, in the PIC16F877A datasheet, 9.6Kbaud SPBRG is listed as having a value of 25 with 0.16% error at 4Mhz in Asynchronous mode (SYNC=0).

RB has explained how to quickly and easily calculate the SPBRG value for any Asynchronous baud rate.

Obviously for Synchronous mode you simply divide the xtal freq by 4 then your desired baud rate and subtract 1 from the result (SPBRG = xtal freq / 4 / desired baud - 1).
In either case, you obviously want to calculate for the lowest error to guarantee framing stays put.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top