'large integer implicitly truncated to unsigned type' compiler warning when calculating baud rate

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

Hi guys - as the title states, I get 'large integer implicitly truncated to unsigned type' in expansion of my CALCULATED_BAUD macro.  What can I do to avoid this error?  It specifically calls out the UBBRL = CALCULATED_BAUD line, which makes sense.  This calculation and setting of the UBBR registers worked fine for higher baud rates, but came across this warning as I tried to go lower.  How else should I calculate my baud/set it in the registers to avoid this warning?

 

#define F_CPU 16000000UL
#define BAUD_RATE 2400
#define CALCULATED_BAUD ((F_CPU / 16 / BAUD_RATE) - 1)

#include <avr/io.h>

void usart_transmit(unsigned char data);

int main(void)
{
    /*
     UBRR is a 16-bit register, but only the 4 least significant bits of UBBRH are valid.
     Let's put our calculated baud rate into these registers.
    */
    UBRRH = (CALCULATED_BAUD >> 8);
    UBRRL = CALCULATED_BAUD;

    /*
      Set the TXEN bit in the USART control register B to enable transmission off the TX pin.
    */
    UCSRB = (1 << TXEN);

    /*
      Set these bits to set 8-bits in the USART control register C to set a character size of 8.
    */
    UCSRC = (1 << UCSZ0) | (1 << UCSZ1);

    while (1)
    {
        usart_transmit('t');
    }
}

void usart_transmit(unsigned char data)
{
    // Wait for transmit buffer to be empty
    while ( !(UCSRA & (1 << UDRE)) );

    UDR = data;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I believe you should add the suffix "UL" to the value 16 to avoid integer truncation (16-bit value).

 

Are you aware of the setbaud.h utility? You may want to use that instead:

http://www.nongnu.org/avr-libc/u...

 

It uses this code:

#define UBRR_VALUE (((F_CPU) + 8UL * (BAUD)) / (16UL * (BAUD)) -1UL)

//...

#  define UBRRL_VALUE (UBRR_VALUE & 0xff)
#  define UBRRH_VALUE (UBRR_VALUE >> 8)

 

/Jakob Selbing

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Tylerlee12 wrote:
What can I do to avoid this error?

UBRRL = CALCULATED_BAUD & 0xFF;

or

UBRRL = (uint8_t)CALCULATED_BAUD;

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The truncation occurs on assignement to UBRRH and UBRRL. The correct code looks as follows:

UBRRH = (uint8_t)(CALCULATED_BAUD >> 8);
UBRRL = (uint8_t)CALCULATED_BAUD;

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely the "correct code" is something like... 

UBRR = (uint16_t) CALCULATED_BAUD;

;-) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suggest a name change: from CALCULATED_BAUD to CALCULATED_UBRR.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While Tylerlee12 uses this ( which is correct but produces the warning "Warning  integer overflow in expression [-Woverflow])
 

#define CALCULATED_BAUD ((F_CPU / 16 / BAUD_RATE) - 1)

​I have always used this as it matches the formula in most of the data sheets (UL added to prevent the warning)

 

#define CALCULATED_UBRR (F_CPU / (16UL * BAUD_RATE)) - 1

also +1 for the last two posts

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." - Marcus Aurelius               

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

larryvc wrote:
While Tylerlee12 uses this ( which is correct but produces the warning "Warning integer overflow in expression [-Woverflow])
No, it does not produce that warning. Never, no matter what the actual values of F_CPU or BAUD_RATE are.

larryvc wrote:
​I have always used
Yes, that formula is prone to overflows, because of the multiplication.

 

The warning the OP gets has nothing to do with which formula is used or any UL. He simply gets it because the baud rate is so low that the actual result of it (the UBBR value) is bigger than 255. So the assignment of it to the 8 bit lvalue produces the "'large integer implicitly truncated" warning.

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, you are right, I stand corrected.  Don't know why the error was occurring earlier, but it is gone now.  Thanks for the explanation Stefan.

 

 

 

 

 

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." - Marcus Aurelius               

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I always wonder why more people don't use <util/setbaud.h>

http://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html

 

#define F_CPU 16000000
#include <avr/io.h>
.
.
.
    #define BAUD 9600
    #include <util/setbaud.h>
    UBRR = UBRR_VALUE;
    #if USE_2X
    UCSRA |= (1 << U2X);
    #else
    UCSRA &= ~(1 << U2X);
    #endif

Job done.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]