Author Topic: [SOLVED] USART0 on Arduino Uno just sending garbage  (Read 335 times)

0 Members and 1 Guest are viewing this topic.

Offline ksoviero

  • Contributor
  • Posts: 8
  • Country: us
[SOLVED] USART0 on Arduino Uno just sending garbage
« on: July 18, 2020, 04:57:22 pm »
Hi all,

I've been using the Arduino libraries for a while now, but I want to start making my own projects that use MCUs directly rather than relying on the Arduino libraries. The first thing I'm trying to do is get USART0 up and running so that I can use it to debug everything else.

Here's the code I have so far:

Code: [Select]
#include <avr/io.h>
#include <util/delay.h>

#define BAUD 9600
#define UBRR ((F_CPU / (16UL * BAUD)) - 1)

int main() {
  UBRR0H = UBRR >> 8;
  UBRR0L = UBRR;

  UCSR0B = 1 << TXEN0;

  while (true) {
    unsigned char data[] = "Hello\r\n";

    for (int i; data[i] != 0; ++i) {
      while (!( UCSR0A & (1 << UDRE0)));
      UDR0 = data[i];
    }
  }
}

And (a truncated version of) what I'm getting back:

Code: [Select]
o��M�y�_�ӏ�a����'��%�~��{['�P�L�␋�Dx���}�����|c␎��
Things I've tried so far:

- I've tried implementing a delay per some forum posts I found, but I still get garbage, just... slower.

- Tried explicitly setting UCSR0C to 0x06, same result.

- Also tried explicit setting F_CPU to 16000000, no dice.

I'm also developing this using PlatformIO on VS Code, just in case that affects things.

Any help would be greatly appreciated.

Oh, and just to be clear, this is an Arduino Uno, connected over USB.
« Last Edit: July 18, 2020, 05:14:27 pm by ksoviero »
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2785
  • Country: fi
Re: USART0 on Arduino Uno just sending garbage
« Reply #1 on: July 18, 2020, 05:06:05 pm »
You are not initializing i to 0. You are over- or underindexing data, reading from random memory locations.
 
The following users thanked this post: Ed.Kloonk

Offline ksoviero

  • Contributor
  • Posts: 8
  • Country: us
Re: USART0 on Arduino Uno just sending garbage
« Reply #2 on: July 18, 2020, 05:14:06 pm »
You are not initializing i to 0. You are over- or underindexing data, reading from random memory locations.

Well... shit. It works now. That was obvious in hindsight.

Thanks for the help!
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 2785
  • Country: fi
Re: [SOLVED] USART0 on Arduino Uno just sending garbage
« Reply #3 on: July 19, 2020, 07:44:33 am »
Add -Wall to the compiler command line (assuming GCC). You will get compiler warning from this kind of obvious mistake. It happens even to more experienced.
 

Offline newbrain

  • Frequent Contributor
  • **
  • Posts: 886
  • Country: se
Re: [SOLVED] USART0 on Arduino Uno just sending garbage
« Reply #4 on: July 21, 2020, 12:23:49 pm »
Add -Wall to the compiler command line (assuming GCC). You will get compiler warning from this kind of obvious mistake. It happens even to more experienced.
:-+ This is good advice, and I would also add -Wextra to get warning for some more sublte traps for young (and old) players, such signed/unisgned compares.
Nandemo wa shiranai wa yo, shitteru koto dake.
 
The following users thanked this post: Ed.Kloonk, agehall


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf