USART Baudrate

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am working on a code for AT90CAN128 with USART functionality. XTAL frequency is 8MHz and the desired Baudrate is 9600 Baud.

I am just curious - if I write:

uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(16*UART_BAUDRATE) - 1);

like found somewhere on the net, I get a Baudrate of 10.

If I simply write ubrr=51, i.e. the result from a calculator using the equation above, suddenly it starts working like it should with speed of around 9600 Baud.

What is wrong with the first definition?

In config.h it is written:

#define FOSC           8000        // 8.000 MHz External cristal
#define F_CPU          (FOSC*1000) // Need for AVR GCC

#define USE_UART      BOTH_UART
#define UART_BAUDRATE 9600
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your (FOSC*1000) expression is probably your problem. You need to make sure that it is a 32-bit expression:

#define F_CPU          (FOSC*1000uL) // Need for AVR GCC

Untested. Try it for yourself. As you realise, the formula is fine. But if 8000*1000 is calculated as an int expression, casting it up to 32-bit afterwards does not help!

David.