Reputation: 77
Using Microchip Studio, I have implemented a code snippet to send a character via UART using ATmega2560 and a USB-to-TTL device.
The code for sending a character is as follows:
void USART0_Init(unsigned int ubrr){
/* Set baud rate */
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
/* Set frame format: 8data, 2stop bit */
UCSR0C |= (1<<USBS0)|(3<<UCSZ00);
/* Enable transmitter */
UCSR0B |= (1<<TXEN0);
}
void USART0_Transmit(char data)
{
/* Wait for empty transmit buffer */
while(!(UCSR0A & (1 << UDRE0)));
/* Put data into a buffer and send the data */
UDR0 = data;
}
The previous code snippet sends a character SUCCESSFULLY to my PC to be seen on a terminal software tool (I am using Docklight), however when attempting to send a whole string using the following function:
void Terminal_SendString(char* str)
{
unsigned short int chr = 0;
while(str[chr] != '\0')
{
USART0_Transmit(str[chr]);
++chr;
}
}
it gives me a garbage value, to be precise it gives me the decimal value "255" but in its representation in ASCII code.
Despite the simplicity of this issue, it's been more than a whole week in investigating it, with no signs of success !!
Upvotes: 0
Views: 171
Reputation: 9
It sounds like the code for sending a single character is working correctly, but sending a string results in garbage values. This issue is likely due to an issue with the string handling in your code.
Here's a step-by-step approach to debug and fix the issue:
Check String Initialization:
Terminal_SendString
is properly null-terminated.Verify Character Transmission:
USART0_Transmit
is working for a single character, it's important to verify that each character in the string is being correctly passed to this function.Add Debug Statements:
Here is a modified version of your code with added debug output to help trace the issue:
#include <avr/io.h>
// Function to initialize USART0
void USART0_Init(unsigned int ubrr) {
/* Set baud rate */
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1 << USBS0) | (3 << UCSZ00);
/* Enable transmitter */
UCSR0B = (1 << TXEN0);
}
// Function to transmit a single character
void USART0_Transmit(char data) {
/* Wait for empty transmit buffer */
while (!(UCSR0A & (1 << UDRE0)));
/* Put data into a buffer and send the data */
UDR0 = data;
}
// Function to send a string via USART0
void Terminal_SendString(char* str) {
unsigned short int chr = 0;
while (str[chr] != '\0') {
// Debug output to verify each character
char debug_char = str[chr];
USART0_Transmit(debug_char);
++chr;
}
}
int main(void) {
// Example usage
unsigned int ubrr = 103; // Example for 9600 baud rate with 16MHz clock
USART0_Init(ubrr);
// Example string to send
char example_str[] = "Hello, World!";
Terminal_SendString(example_str);
while (1) {
// Main loop
}
}
Correct Baud Rate:
ubrr
value is correctly calculated based on your clock frequency and desired baud rate. For a 16MHz clock and 9600 baud, ubrr
should be 103.String Null-Termination:
Terminal_SendString
is properly null-terminated. In C, strings are arrays of characters ending with a null character \0
.Memory Corruption:
char example_str[] = "Hello, World!";
Terminal_SendString
function and inspect the values of str[chr]
and debug_char
.By following these steps, you should be able to identify and fix the issue with sending a string over UART on your ATmega2560.
Upvotes: -1