Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Xtal and Baud Rate

Xtal and Baud Rate

2009-03-09 by David VanHorn

The normal equation for setting the UBRR isn't working for me.

This is an ATMEGA32, with 4 MHz external crystal.
I verified that the CPU is running at 4 MHz, full output mode.

#ifndef F_CPU
# warning "F_CPU not defined MagSensor.c"
# define F_CPU 4000000
#endif

#define USART_BAUDRATE 115200 // Has to be fast enough to get rid of the data
#define UBRRVAL (((F_CPU/(USART_BAUDRATE*4UL)))) // using the double baud flag

//******************************************************************************************
//
// Unit Usart
//
void Init_USART(void)
{
	// Set baud rate divider
	UBRRL = UBRRVAL; 		//low byte
	UBRRH = (UBRRVAL>>8);	//high byte

	UCSRA |= (1<<U2X);									// Double baud rate
	UCSRC |= ((1<<URSEL) | (3<<UCSZ0));
	UCSRC &= (~(1<<UPM1) & ~(1<<UPM0) & ~(1<<USBS));	// 8N1
	UCSRB |= (1<<RXEN) | (1<<TXEN);						// Enable RX and TX
}
//


Normally, this should be USART_BAUDRATE * 16UL, but that results in
way too fast!
I AM using the double speed flag, and I expected to shift the equation to *8.

I'm using Brays terminal on the PC end, and getting correct data with
it set to 115200.

What did I miss?

I'm running it under Jtag ice, so I can see the values programmed.

After init is completed, I have:
UBRR = 0x0008
UCSRA = 0x22
UCSRB = 0x0A
UCSRC = 0x00

Looks fine to me, for 8N1


-- 

"The very powerful and the very stupid have one thing in common. Instead of
altering their views to fit the facts, they alter the facts to fit their
views... which can be very uncomfortable if you happen to be one of the
facts that needs altering." Doctor Who, Face of Evil

Re: [AVR-Chat] Xtal and Baud Rate

2009-03-11 by Bob Paddock

On Mon, Mar 9, 2009 at 4:59 PM, David VanHorn <microbrix@gmail.com> wrote:
> The normal equation for setting the UBRR isn't working for me.

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_wrong_baud_rate
was just added in 1.6.5 of AVR-LibC.


-- 
http://www.wearablesmartsensors.com/
http://www.softwaresafety.net/
http://www.designer-iii.com/
http://www.unusualresearch.com/

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.