avr-gcc-list
[Top][All Lists]
Advanced

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

[avr-gcc-list] integer overflow compiler warning?


From: Sander Pool
Subject: [avr-gcc-list] integer overflow compiler warning?
Date: Tue, 31 Dec 2002 00:53:18 -0800

Hello,

I've been trying to get the USART going on my mega128. To prepare I figured
I'd first get a mega163 going on my STK500. That turned out to be an
adventure, as these things typically are :-)

I started with the gcctest4 example program. I compiled it with the old
avrfreaks release earlier this year ('02) and it worked fine. So I looked up
the UBR setting formula in the datasheet and it didn't look anything like
what's in the example. I remember looking at that earlier but don't recall
figuring it out.

In any case, the formula in the datasheet works out to be:

UBR = ((Fck/BR)/16) - 1

I plug in my values, 4 MHz and 9600 baud and UBR has to be set to 25. The
same as shown in the table on page 74.

So I change my code to be like this:

#define F_CPU            4000000      /* 4Mhz */
#define UART_BAUD_RATE      9600      /* 9600 baud */


#define UART_BAUD_SELECT ((F_CPU/(UART_BAUD_RATE*16))-1)

When I compile this I get a warning:

uart_test1.c:89: warning: integer overflow in expression

where line 89 is:

        UBRR =  (u08) UART_BAUD_SELECT;


Running this code in the simulator shows that UBRR is set to 0xB0 and I
verified with the STK500 LEDs that that is indeed the value.

If I use Volker's definition (F_CPU/(UART_BAUD_RATE*16l)-1) then the value
is set correctly and no warning is generated. When I do the math by hand
using this formula I don't get 25 at all.

What's going on? Anyone have any ideas? Is gcc so limited in its constant
calculations that it overflows that easily? How does Volker's macro work?

I hope someone can shed some light on this, thanks in advance,

        Sander

avr-gcc-list at http://avr1.org



reply via email to

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