UART issue in ATmega2560 and Microchip Studio

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

Answers (1)

Mohamed ZIEDAN
Mohamed ZIEDAN

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:

Step-by-Step Debugging

  1. Check String Initialization:

    • Ensure that the string passed to Terminal_SendString is properly null-terminated.
  2. Verify Character Transmission:

    • Since 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.
  3. Add Debug Statements:

    • Add debug statements or breakpoints to inspect the value of each character before it is transmitted.

Example Debugging and Fixed Code

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
    }
}

Points to Verify

  1. Correct Baud Rate:

    • Make sure the 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.
  2. String Null-Termination:

    • Ensure the string you pass to Terminal_SendString is properly null-terminated. In C, strings are arrays of characters ending with a null character \0.
  3. Memory Corruption:

    • Ensure there is no memory corruption or overwriting happening in your code that might alter the string.

Example of Properly Null-Terminated String

char example_str[] = "Hello, World!";

Additional Checks

  • If you have access to a debugger, place breakpoints in the Terminal_SendString function and inspect the values of str[chr] and debug_char.
  • Use a simpler string to ensure there are no hidden characters causing issues.

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

Related Questions