I tried to implement UART by using registers on Arduino mega. But i'm not getting any significant output. The same code works on Atmega 128 for some reason.
#define BAUD 9600
#define UBRR_VALUE ((16000000 / (16UL * BAUD)) - 1)
#include <stdio.h>
#include <avr/io.h>
uint16_t x;
void UART_init(){
UCSR0C|=(1<<UCSZ00)|(1<<UCSZ01); //8 bit data
UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
UBRR0L|=(uint8_t)(UBRR_VALUE); //9600 Baud rate
UCSR0B|=(1<<TXEN0)|(1<<RXEN0); //UART1 Tx Rx enable
}
void UART_send(char y){
while(!(UCSR0A && (1<<UDRE0))); //Check if Tx buffer is empty.
UDR0=y;
}
int main(){
UART_init();
while(1){
UART_send('p');
_delay_ms(100);
}
}
This prints question marks, and any integer type that I send prints boxes. I have verified the baud rate to be correct. what might be the error?