I am encountering an issue with USART communication on an ATmega328P microcontroller and would appreciate some assistance with debugging.
The problem I'm facing is as follows: I have implemented USART communication on an ATmega328P microcontroller to transmit the string "HE" over serial. However, when using the following code snippet, the output on the serial console repeats "H H H H H ..." endlessly:
#define FOSC 16000000UL
#define BAUD 9600
#define UBRR FOSC/16/BAUD-1
void USART_init(unsigned int ubrr)
{
// Setting Baud rate
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
//Enable receiver and transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set frame format: 8data, 2stop bits
UCSR0C = (1 << USBS0) | (1 << UCSZ00) | (1 << UCSZ01);
return;
}
void USART_Transmit(unsigned char data)
{
while(!(UCSR0A & (1 << UDRE0)));
UDR0 = data;
return;
}
void print_serial(char* data)
{
USART_Transmit(data[0]);
USART_Transmit(data[1]);
USART_Transmit(data[2]);
return;
}
int main()
{
USART_init(UBRR);
print_serial("HE");
return 0;
}
However, when using the following modified code snippet, the output is as expected, printing "HE" only once:
#define FOSC 16000000UL
#define BAUD 9600
#define UBRR FOSC/16/BAUD-1
void USART_init(unsigned int ubrr)
{
// Setting Baud rate
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
//Enable receiver and transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set frame format: 8data, 2stop bits
UCSR0C = (1 << USBS0) | (1 << UCSZ00) | (1 << UCSZ01);
return;
}
void USART_Transmit(unsigned char data)
{
while(!(UCSR0A & (1 << UDRE0)));
UDR0 = data;
return;
}
void print_serial()
{
char *data = "HE";
USART_Transmit(data[0]);
USART_Transmit(data[1]);
USART_Transmit(data[2]);
return;
}
int main()
{
USART_init(UBRR);
print_serial();
return 0;
}
It seems that the issue arises when passing the string "HE" as an argument to the print_serial function. However, when defining the string within the function itself, the output is correct.
Can anyone please help me identify the root cause of this issue and suggest any potential fixes?
Thank you in advance for your assistance.
Observation: It seems there might be an issue related to the string's scope. Allocating memory for the string allows the correct output "HE" to be printed on the serial console.
{
USART_init(UBRR);
char *data = (char*)malloc(3);
data[0] = 'H';
data[1] = 'E';
data[2] = '\0';
print_serial(data);
free(data);
return;
}
main()? Does it just run your code again? Most embedded systems do not return from main. They would have some kind of infinite loop.void main() { USART_init(UBRR); print_serial("HE"); while(1); return; }asm volatile ("nop");inside the while-loop.