Communication between laptop and atmega2560 via two Xbee S2C

I want to send data to atmega 2560 from my laptop using two Xbee S2C.

• The first Xbee , let's label it A , is to be interfaced with the laptop.
• The second Xbee , let's label it B , is to be interfaced with the atmega 2560.

I want to know how to configure these Xbees to communicate with each other. I am a beginner , I know only few things :

• The Xbee A will be interfaced with the laptop using Xbee usb adapter , which will facilitate the UART communication between Laptop and Xbee A.
• The Xbee B will be interfaced with the Atmega2560 by providing Xbee B with power and connecting Tx and Rx of the micro controller to that of Xbee B.These two will then communicate via UART.

I am also not clear that the controller's Tx and Rx pin will connect to Xbee B's Tx and Rx pin respectively or the other way around. Please guide.

Here's the code I am burning to Atmega 2560 , initially for turning a led on/off on PB6

#define F_CPU 14745600
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>

unsigned char data; //to store received data from UDR1

//Function to initialize ports
void port_init()
{
DDRB = 0x40;

}

//Function To Initialize UART0
// desired baud rate:9600
// actual baud rate:9600 (error 0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
// UBRR0L = 0x47; //11059200 Hz
UBRR0L = 0x5F; // 14745600 Hzset baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;
}

ISR(_VECTOR(26))        // ISR for receive complete interrupt
{
data = UDR0;                //making copy of data from UDR0 in 'data'  variable

UDR0 = data;                //echo data back to PC

if(data == 0x37)
PORTB = 0x00;
if(data == 0x39)
PORTB = 0x40;

}

//Function To Initialize all The Devices
void init_devices()
{
cli(); //Clears the global interrupts
port_init();  //Initializes all the ports
uart0_init(); //Initailize UART0 for serial communiaction
sei();   //Enables the global interrupts
}

//Main Function
int main(void)
{
init_devices();
while(1);
}


There are good instructions here for set up of the modules:

http://www.hobbytronics.co.uk/xbee-s2c-configuration

The way the UART is set up is clearly outlined in the datasheet here:

https://www.digi.com/resources/documentation/digidocs/pdfs/90002002.pdf

You should note that you might want CTS and RTS as well as Tx and Rx. You can use with or without, but you have to really take care if you do not implement it. If you are sending low amounts of data and not very often you'll probably get away without it.

Din on the module is connected to Tx of the micro's UART and the Tx and the PC's UART. However, please note that the UART of the PC will be at RS232 levels and you will need to use a level converter to get them to 3V3.

Similarly, Dout will go to the Rx pins of UARTs at each end, with you needing level conversion at the PC end.

You should read up on RTS and CTS and how they are used. Some micros have UARTs with dedicated flow control, others you have to implement in software. The Mega2560 does not have hardware flow control, so you'll have to implement in software. This is a nice little document: