NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Changing Uart Baud Rate

June 01, 2009
by Starwarslegokid
Starwarslegokid's Avatar

Hello Everyone!

Iv decided to start working on a wireless connection between two nerdkits and I found a wireless transmitter and receiver for really cheap.

http://www.sparkfun.com/commerce/product_info.php?products_id=8945

http://www.sparkfun.com/commerce/product_info.php?products_id=8948

I want to use the uart that Nerdkits has supplied, and hook the transmitter and recievers to the TX and RX on the microcontrollers. The only problem is that the baud rate for the transmitter/reciever is 2400 bps, and the uart is set up for 115,200bps. I was wondering if anybody knows how to change the uart baud rate to 2400bps.

I poked around the uart code and found this line of code: UBRR0L = 7; // for 115200bps with 14.7456MHz clock

Does anybody know what the UBRR0L is and how exactly this controls the baud rate?

Scott B-)

June 01, 2009
by TBNK
TBNK's Avatar

I don't know what UBRR0L is, but:

14.7456 MHz = 14,745,600

14,745,600 / 115,200 = 128

128 = 2 to the 7th power exactly

Probably not a coincidence.

June 02, 2009
by Starwarslegokid
Starwarslegokid's Avatar

Thanks TBNK! You got me pointed in the right direction B-) I did some searching and I found a tutorial for the wireless transmitters I bout with a ATmega8

http://winavr.scienceprog.com/example-avr-projects/running-tx433-and-rx433-rf-modules-with-avr-microcontrollers.html

I found this in the code example provided, It helps to explain allot:

//set desired baud rate

#define BAUDRATE 1200

//calculate UBRR value

#define UBRRVAL ( (F_CPU / (BAUDRATE*16UL))-1 )

If you do the math, then the 14.7456 MHz processor at 115200bps equals the 7 that UBRR0L equals. I like how the online example did theirs with the defined constant for the rate so I modified the nerdkits code.

The modified Nerdkits code works great when the baudrate is still 115200bps, when I try and change the rate to 1200 like I was hoping, I cannot send messages to the microcontroler using the windows HyperTerminal anymore. Ohh also, I'm doing all my tests using wires, once iv perfected the 1200bps message over a wired signal, then ill try and put in the wireless devices.

Any help would be great, I'm not sure why it works when the baud is 115200bps, but not when its 1200.

Scott B-)

July 05, 2009
by BobaMosfet
BobaMosfet's Avatar

If you look at the datasheet for the ATMEGA168, page 174-176, it describes UBRRn and how it is set internally according to certain settings you can adjust.

July 07, 2009
by Ethanal
Ethanal's Avatar

Starwarslegokid, I have been looking all over the place for a really cheap wireless communication solution and now I've found one thanks to you. Now I don't have to waste my money on an expensive xBee module. Thanks a million.

August 15, 2009
by Starwarslegokid
Starwarslegokid's Avatar

Thank you so much BobaMosfet, That was what I was looking for B-). I will have to try using one of the table values on page 196 soon.

Hey Ethanal, glad you found them. When I saw it was only going to be afew bucks too, I couldn't resist lol. Did you get yours working yet?

Post a Reply

Please log in to post a reply.

Did you know that you can make a spooky Halloween Jack-O-Lantern with a microcontroller? Learn more...