H
H
hitakiri2018-03-21 15:02:23
Arduino
hitakiri, 2018-03-21 15:02:23

How to transfer data from Arduino Mega to Atmega8A via UART?

The task is to transfer data from the Arduino Mega2560 to the Atmega8A AVR microcontroller .
Found the reverse problem and it works.
5ab247c6c74a7675578601.jpeg
Initially, I wrote with my hands according to the Atmega datasheet and materials one , two , three .
I tried to use ready-made libraries for Arduino and for Atmega .
Arduino code:

unsigned char n;
void setup() {
  Serial1.begin(9600);
  pinMode(19,INPUT);//RXD pin is set for INPUT
  pinMode(18,OUTPUT);
}

void loop() {
//  if (Serial1.available()) {
    //for(n=100; n<= 255;n++){
    //Serial1.print(n, BIN);
      Serial1.print(0b00001000, BIN);
      delay(1000);
      //}
//  }

}

Atmega code:
#define F_CPU 16000000UL

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    unsigned char m;
    m = 7;
    portIni();
    LCDIni();
    setPos(0,0);
    //unsigned char n; // Переменная общего счётчика
    char buffer [64];
  
  DDRD |= (1 << PIND0);//PORTD pin0 as INPUT

  DDRC=0xFF;//PORTC as OUTPUT

  int UBRR_Value = 6; // 9600 baud rate

  UBRRH = (unsigned char) (UBRR_Value >> 8);

  UBRRL = (unsigned char) UBRR_Value;

  UCSRB = (1 << RXEN) | (1 << TXEN);
  //UCSRC = (1 << USBS) | (3 << UCSZ0);
  UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
  //UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(TXCIE);
  
       /* Set frame format: 8data, 1stop bit */
  //UCSRC = (1<<URSEL)|(3<<UCSZ0);

  unsigned char receiveData;
  //sprintf(buffer, "Wait...", m);
  //LCDString(buffer);
  //_delay_ms(1000);

  while (1)
  {
    while (! (UCSRA & (1 << RXC)) );
    receiveData = UDR;
    if (receiveData >= 0x01){
      LCDClear();
      sprintf(buffer, "%3d", receiveData);
      LCDString(buffer);
      _delay_ms(2);
    } else {
      LCDClear();
      sprintf(buffer, "Wait...", m);
      LCDString(buffer);
      _delay_ms(1000);
    }
  }
}

The commented-out code was not specially removed, there are parameters that were also checked.
The result is the same - it transmits incorrect data. According to the proteus debugger, the main problem is the incorrect baud number .
Actually, the question is how to correct / select this parameter or is the problem something else?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vanyamba-electronics, 2018-03-21
@hitakiri

I see 2 errors.

DDRD &= ~(1 << PIND0); //PORTD pin0 as INPUT

UBRRH = 0;
UBRRL = 103;

The meaning of UBRR can be found here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question