Hello, I am communicating two microcontrollers serially. one of them is atmega328p and the other one is atmega2560. The baudrate is 9600. According to data sheet for a 16MHz crystal and 9600 Baudrate value. The value for UBRR register should be 103. Now I have created a Usart_init() function which takes UBRR value as argument. The UART is working fine when I hard code 103 as the argument like this in the code below
main() { USART_Init(103); /******************/ } void USART_Init( unsigned int ubrr) { UBRR0H = (unsigned char)(ubrr>>8); UBRR0L = (unsigned char)ubrr; UCSR0B = (1<<RXEN0)|(1<<TXEN0); UCSR0C= (1<<USBS0)|(1<<UCSZ00)|(1<<UCSZ01); }
But when I replace these with macro I only get ???? on the serial monitor. I am sure I am making some basic mistake here due to my lack of knowledge. I am using macros as the following
#define FOSC 16000000 #define BAUD 9600 #define UBRR_VAL ((FOSC/(16 * BAUD)) -1 ) main() { USART_Init(UBRR_VAL); /******************/ } void USART_Init( unsigned int ubrr) { UBRR0H = (unsigned char)(ubrr>>8); UBRR0L = (unsigned char)ubrr; UCSR0B = (1<<RXEN0)|(1<<TXEN0); UCSR0C= (1<<USBS0)|(1<<UCSZ00)|(1<<UCSZ01); }
I even try to hardcode 103 as parameter and then print this UBRR_VAL macro but I still get ??? instead of this value. Everything else is printing normally.
Thanks in Advance