[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] UART transmitting problem
From: |
Neil Johnson |
Subject: |
Re: [avr-gcc-list] UART transmitting problem |
Date: |
Fri, 28 Nov 2003 14:08:06 +0000 (GMT) |
Hi,
On Fri, 28 Nov 2003, Anderas Strodl wrote:
> Laurence Anderson wrote:
> > #define UART_BAUD_SELECT (F_CPU/(UART_BAUD_RATE*16l)-1)
> ^
> Are you sure this is correct? As far as I see it should be:
> #define UART_BAUD_SELECT (F_CPU/(UART_BAUD_RATE*16)-1)
The "l" tells the preprocessor to treat the 16 as a long int, rather than
just a plain int. This then forces the type of the result of the
multiplication to a long int, and then the type of the divide as long int.
Hopefully this should prevent overflows.
I'd like to suggest a slight change in the above macro (note: this is
untested at the moment). When doing integer divide it is usually better
to round to the nearest integer, rather than to the lowest (which the
above expression does).
A better expression would 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)
Thus a numerical result of, say, 12.7 would be rounded up to 13, while
12.2 would be rounded down to 12.
Anyone care to give this a try? I don't have my dev. tools to hand at the
moment, so am unable to verify this myself (althought have written such
code many times in the past).
Comments?
Neil
--
Neil Johnson :: Computer Laboratory :: University of Cambridge ::
http://www.njohnson.co.uk http://www.cl.cam.ac.uk/~nej22
---- IEE Cambridge Branch: http://www.iee-cambridge.org.uk ----
Re: [avr-gcc-list] UART transmitting problem, Anderas Strodl, 2003/11/28
- Re: [avr-gcc-list] UART transmitting problem,
Neil Johnson <=