avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] ATMega UART question


From: Bob Paddock
Subject: Re: [avr-chat] ATMega UART question
Date: Sun, 21 Dec 2008 06:57:15 -0500


> This is the code I have:
> UBRRL = (XTAL / (16L * BAUDRATE)) - 1;      //set baud rate;

Does that work?  

Not in all cases.  I have found this to work in all cases that I've tried:

/*
 * #define UART_BAUD_SELECT (F_CPU/(UART_BAUD_RATE*16L)-1)
 *
 * When doing integer divide it is usually better to round to the nearest
 * integer, rather than to the lowest (which the above _expression_ does).
 *
 * Add 0.5 (i.e. half the value of the denominator)
 * to the numerator before the division.  This can be achieved in the
 * following way:
 *
 * #define UART_BAUD_SELECT ((F_CPU + UART_BAUD_RATE * 8L) /\
 *                             (UART_BAUD_RATE * 16L) - 1)
 * - Neil Johnson AVR-GCC List
 */

#define UART_BAUD_CALCULATE(UART_BAUD_RATE) ((XTAL_FREQ + UART_BAUD_RATE * 8L) / (UART_BAUD_RATE * 16L) - 1)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]