# Interface of XBee module with ATMEGA 168

I have configured 2 xbee series 1 modules such that one will receive serial data from any other module of same pan ID and the other will send data to the previous mentioned one.

Now when I use a terminal software (XCTU) to check data transmission its fine. I want to send data serially using a ATMEGA 168 using xbee as transmitter. And another xbee will receive the data and send it to an ATMEGA 2560 based platform. I do not have any platform for atmega 168 so I kinda built a burner (bootloader circuit). The bootloader works. I have connected pin 3 (TXD of USART0) of atmega 168 with data in (pin 3) of xbee module. I have made necessary power connections as well (Vcc=3.3V & GND ). But the xbee does not seem to send anything. Is there something I am missing ? Please help, as you may already figured out I am a complete newbie.

PS : before reducing my reputation please let me know what I am doing wrong so I may rectify that.

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

void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x5F; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x98;
}

void init_devices()
{
cli();
uart0_init();
sei();
}

int main(void)
{
unsigned char data;
init_devices();
data =1;

while(1)
{
data =0x21;
UDR0 = data;
_delay_ms(2000);
data =0x55;
UDR0 = data;
_delay_ms(2000);

}

}
}


This is the code I used.

• perhaps post your code as well? you could be communicating at the wrong baud rate Dec 22 '13 at 6:44
• @geometrikal thanks for replying. i have attached the code too. Dec 22 '13 at 16:51
• It seems you are not calling init_devices() from your main routine, so the UART is not getting setup. I found this on the web which could be useful www2.ee.ic.ac.uk/t.clarke/projects/Resources/zigbee/AVR/… ... it has example code for a serial port. Also, have you thought about burning the Arduino boot loader and then using the Arduino IDE / libraries etc? Dec 22 '13 at 22:12
• I am going to try & run the program in another device today. I have written similar programs before and they have worked on other platforms. Can there be any fault with voltage levels or hardware configurations? Dec 23 '13 at 2:19
• XBee requires 3.3V and UART level signals. P.S. did you try adding init_devices into main? Dec 23 '13 at 7:38

The problem was in the programming. Unless the fuse settings are changed the microcontroller uses its default 8MHz oscillator with a pre-scaler of 8. So effectively you should have a 1 MHz system frequency instead of 12 MHz. So for 9600 baud you have to put:

UBRR0L = 0x06; //set baud rate lo


UBRR0L = 0x5F; //set baud rate lo


This should take care of the problem. Though I do not know how to configure the fuses or whatever.

A few problems:

• init_devices() must be called from inside main() otherwise it won't be run

• data = 0; and data = 1; do not send out the characters '0' and '1' respectively. They send out NUL and SOH: see http://www.asciitable.com, and these characters will show as nothing in a terminal emulator. Instead, enclose the values in quotes, e.g. data = '0';, to send the characters '0' and '1' respectively.

• Check the baud rate. With UCSR0A = 0x00; UBRR0 should be set to: (F_CPU / 8 / baud - 1) / 2; (source: Arduino serial library) where F_CPU is your CPU frequency in Hertz. With that in mind 005F seems like a funny value. Check the MCU frequency and calculate accordingly.

Some suggestions:

• Funny characters on the serial output do suggest incorrect baud rate, however they do not always appear.

• Download the Arduino IDE and have a look at / borrow their serial code.

So my guess is that there is something wrong with hardware setup

• Don't preempt your trouble shooting but deciding on what the problem is ahead of time, or you might miss the real issue. I still make this mistake! :)
• Thanks again will check again following your suggestions !! Dec 24 '13 at 0:00
• Nice, please let us know if it doesn't work. I have a very similar setup running but with atmega328p. Also, to check baud rate you could connect the atmega168 to a PC? Dec 24 '13 at 0:15
• @Geometrical .Thanks for all your replies. I have revised the program as you said (please check above ) and it was a very silly mistake to make. But still I am getting no output. I have a 12MHz crystal oscillator and I have my xbees configured for 9600 Baud rate. Now from the link below wormfood.net/… I have checked that with UBRR = 77 (ie 4D in hex ) I can get 9600 Baud with a 11.98 MHz crystal. Will that be ok or the slight mismatch is enough to throw my connection haywire? Dec 30 '13 at 7:25
• I think that is ok, I forget the UART timing tolerances but less than 1% should be fine. Try connecting the 168 UART directly to the 2560 UART ( tx to rx, rx to tx, gnd to gnd ), bypassing the XBee completely and just check that. Do you have an oscilloscope? You could check the tx output with that as well. Dec 30 '13 at 11:45
• Will do that thanks. Tried to simulate with Proteus but failed there too. Will try with a real oscilloscope in the new year !! Dec 30 '13 at 11:52