0
votes

I am trying to setup up a basic serial communcation link between an ATMEGA8515 and an Arduino Uno. I am Putty connection to the correct com port I can see the Arudino print "got:got:" in burst of two going with the delay. So the problem is that the Arduino is recognizing that something is being transmitted but not getting any characters.

I've been trying to use the transmit code on page 143 of the datasheet in my main loop:

int main(void)
{
    //Baud is 9600
    USART_Init(USART_BAUDRATE); 
    //init interrupt
    sei();
    
    DDRA |= _BV(DDA6); 
    while(1)
    {
        USART_Transmit("a\n");
        _delay_ms(5000);
    }
}

And trying to recieve the serial using this code taken from here:

char val; // variable to receive data from the serial port
void setup() {
  Serial.begin(9600);       // start serial communication at 9600bps
}
void loop() {
  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
    Serial.print("got:"+val);
  }
  
  delay(100);                    // wait 100ms for next reading
}

This results in "got:" being printed twice every 5 seconds: enter image description here

I hooked up an LED to the TX line of the AVR and can see when its tranmitting. The TX line of the AVR is hooked into the RX line of the arduino and the same fasion for AVR RX.

Any ideas why I can't see the char that i'm trying to transmit?

One odd thing is that the RX light never blinks on the Arduino making it seem that its not getting anything but the TX always blinks as its printing to the com port.

EDIT: This is the code i'm using for transmit:

void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}

and have changed my main code to USART_Transmit("a"); I also tried Serial.print(val, HEX); on the arduino which prints 0s.

4
Are you feeding the TTL serial output of the ATmega straight into the RS232 port on the Arduino? The logic levels are inverted between TTL and RS232.PeterJ

4 Answers

2
votes

It's not a good practice to have a delay() in your loop, much less if you're expecting to receive serial data.

For your arduino code, instead of relying Serial.available() to be run just after you receive the serial data, you should use serialEvent() function.

You just have to implement this function

void serialEvent() {
    //write your code here
}

Keep in mind that this function will be called whenever serial data arrives thru the corresponding serial port.

For arduino mega, as there are more than one serial port you can also use serialEvent1(), serialEvent2(), serialEvent3().

it is also important to know that currently these functions are not compatible with the Esplora, Leonardo, or Micro.

You can read more about this in the arduino reference

Here's an example

2
votes

The problem was the Baud rate was wrong.

After measuring it on an oscilloscope I found that it was wrong. I used the baud rate prescaler:

#define BAUD_PRESCALE ( (F_CPU / (USART_BAUDRATE * 16UL)) - 1 )  

Which gave me garbage chars but a step in the right direction. I then knocked down the baud to 2400 and got the char and strings that I wanted printed to putty.

0
votes

This page has some warnings about using the string + operator. The examples that don't work correctly look something like yours.

Try

String got = "got:";
Strint toPrint = got + val;
Serial.print(toPrint);
0
votes

Try printing val as a hex or decimal value. You may be trying to print a non-printable character.

Serial.print(val, HEX);
Serial.print(val, DEC);