Communicating with non-arduino AVR over serial

I have an Arduino Mega and I wanted to dive in and see how everything really works by following this tutorial with an Atmega644 and a usbASP programmer. After blinking an LED, I felt the next logical step would be to communicate some bytes over a serial connection with the Arduino. Unfortunately, regardless of what I do the Arduino only receives a random combination of 0 and 128 values.
Things I've tried:

  • Reading the serial data transmitted from the Atmega644 with the second Serial connection(since I have a Mega), and then with the SoftwareSerial library(on the 10 and 11 pins)
  • Changing the baud rate with no effect on the random 0 and 180 values
  • Confirmed that the timing is set correctly and is 16Mhz with a LED flashing program at 1 second intervals.

This is a cut down version of the program on the Atmega644 that sends bytes, but I have tried about 6 other programs provided in serial communication tutorials with no noticeable effect:

#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000
#define BAUD 9600
#include <util/setbaud.h>

void uart_init(void) {
    UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;

    #if USE_2X
        UCSR0A |= _BV(U2X0);
    #else
        UCSR0A &= ~(_BV(U2X0));
    #endif
    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); 
    UCSR0B = _BV(RXEN0) | _BV(TXEN0);
    
}
void uart_putchar(char c) {
    loop_until_bit_is_set(UCSR0A, UDRE0); 
    UDR0 = c;
}
int main(void) {    
    uart_init();
    
    for(;;;)
        uart_putchar('A');//I've also tried sending incremental values to try and affect the random output
        
    return 0;
}

Arduino sketch:

#include <SoftwareSerial.h>
SoftwareSerial portOne(10,11);

void setup()
{
  Serial.begin(9600);
  portOne.begin(9600);
}

void loop()
{
  portOne.listen();
  while (portOne.available() > 0) {
    Serial.println("Data from port one:");
    unsigned char inByte = portOne.read();
    Serial.print(inByte);
    Serial.println();
  }
}

At this point I'm fairly positive that I've just configured something wrong but I've exhausted every possibility I can think of. Could someone shed some light on why I might be only receiving random values of either 0 or 128, even when I change almost every condition possible in the programs? I would try and simplify this by trying to receive the serial data through a usb port but the usbASP sadly doesn't support serial communication from the AVR to the computer.

Thanks in advance :slight_smile:

SoftwareSerial portOne(10,11,true);

Why are you configuring for inverted signals? That will give strange results. Lose the "true" part unless you have a very good reason.

Arduino Mega

If you are using a Mega, why are you using SoftwareSerial?

Woops, I had added that in the last 10 minutes to see what would happen but it didn't affect it.

Because Serial1 didn't help the problem and I was running out of options? I'm a noob with this but I'm just trying to eliminate everything I can :confused:

The most frustrating part of this is that I recently misplaced my logic probe which would make all of this much easier to debug xD

Stick with hardware serial ports. They work much much better than software ones.

Are the grounds connected together?

Yes I've tried the hardware ports, I'm back to using them in the code now.

Yup

Also this stuff is what I'm getting back from continually sending 'A'

Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
128
Data from port one:
128
Data from port one:
128
Data from port one:
0
Data from port one:
128
Data from port one:
128
Data from port one:
128
Data from port one:
128
Data from port one:
128
Data from port one:
0

Can you show your modified sketch? And as for the sending device, can you prove it is sending? (I don't have the energy to work out if all those bits are right)

Like, start up the serial monitor and see you are getting a lot of As.