I am working on a code for Atmel Microcontroller and using ATMEL Studio.
You can check the toolchain and studio version from here.
*AtmelStudio7.0toolchainavr8avr8-gnu-toolchainlibgccavr5.4.0*
I have a code in my two programs.
PROGRAM_1:
#define USART_BAUD_RATE(BAUD_RATE) ((float)(5000000 * 64 / (16 * (float)BAUD_RATE)) + 0.5)
USART1.BAUD = (uint16_t)USART_BAUD_RATE(300);
PROGRAM_2:
#define USART_BAUD_RATE(BAUD_RATE) ((float)(5000000 * 64 / (16 * (float)BAUD_RATE)) + 0.5)
/* uint32_t my_BaudRate = 300; // i set this value in program, normally its 115200, 2400, but sometime can be 300*/
USART1.BAUD = (uint16_t)USART_BAUD_RATE(my_BaudRate );
I found out that the USART.BAUD has different values in both programs although it should have same if i set the my_BaudRate to 300.
In Program 1: USART1.BAUD has the value 0xFFFF, although it should have overflown and should have had 0x046B. I tried changing the BAUD_RATE value to 280 or less, it always sets it to 0xFFFF. Only after i set the value to 306, it sets the actual value which is less than 0xFFFF.
In Program 2: it works as expected, USART1.BAUD has the value 0x046B.
I think it has something to with preprocessor or compiler optimization, but i am not sure if this is a known behavior or if i need to be careful about these kind of macros.
Would be grateful for any insights.
Best Regards.