xbee example
DESCRIPTION
xbeeTRANSCRIPT
-
XBee/XBee Pro Wireless Communicate with Microcontroller
Published by: pheysia on July 30, 2011.
26
Share
Tweet
2
Share
Share
1.0 Introduction
The XBee and XBee Pro radio is made by Digi (formerly Maxstream) which is shipped with
firmware implementing the IEEE 802.15.4 protocol. These modules use the IEEE 802.15.4
networking protocol for fast point-to-multipoint or peer-to-peer networking. However, the most
different part between XBee and XBee Pro is they have different cover distance range for
communicate with own module. XBee can be covers around 30m at indoor and 100m at outdoor.
Inversely, XBee Pro can cover higher distance range than XBee which is 100m at indoor and
1500m at outdoor. Both devices that has a UART interface can connect directly from
microcontroller to pins of RF Module (XBee/XBee Pro). Using UART interface, we can use this
wireless devices to communicate between microcontroller to microcontroller (two PICs) or
between PC to microcontroller. Here, we will discuss how to use two XBee/XBee Pros to
interface with microcontroller and how to send command for configuration XBee/XBee Pro
Module.
2.0 Xbee/XBeePro hardware interface with microcontroller
Bear in mind, XBee is designed for 3.3V system, those use system will require extra work and
component before XBee module could be embedded in the system. So that, we used SKXBEE
(XBee Pro Starter Kit) as our example in this article. SKXBee can be used to interface for 5V
microcontroller board (SK40C+PIC16F877A). Any microcontroller with UART peripheral can be
used to interface with SKXBee. In this case, we have to connected XB_RX pin to PIC
Transmitter pin TxD (RC6) and XB_TX pin to PIC Receiver pin RxD (RC7). However, the
-
RESET button of SKXBee is optional case depend of user who would like the microcontroller to
reset SKXBee during run time. For more detail, you can refer to SKXBee User Manual on
Cytron website page (www.cytron.com.my).
The below figure has been shown as example hardware connection of SKXBee with
SK40C+16F877A:
-
The configuration of UART is complete, SKXBee is ready for embedded wireless
development. Trasmitting and receiving data require software or firmware development
on particular microcontroller. Before develop software programming, we should know
how to setting XBee Module Source address and Destination address, it is very
important for us to transfer data by transparently from one XBee module (Source) to
another XBee module (Destination).Two ways to setting ID address which is using X-
CTU software or using programming code to send specified command for XBee ID address
setting. We will discuss both ways in this article. In fact, user only need select one of
both ways to do it.
3.0 Using X-CTU software to setting Source address or Destination address.
X-CTU which is a computer based software to communicate with Xbee, to change configuration
or Transmit/Receive data. Double click the setup_x-ctu.exe file to install this software.
-
NOTE: Using X-CTU for address setting, SKXBee is no necessary
connect to microcontroller before the setting process is done.
Launch the X-CTU Software and connect the USB cable from SKXBee to computer USB port
(without connect to microcontroller). Select PC Setting tab> select serial COM Port >click
TEST/QUERY:
The box will show as figure below, click OK for close this box:
-
Basically, Xbee/XBee Pro has two command mode options which is AT Command Mode
and API Command Mode. Here, we only use AT Command Mode. The figure below AT
Command table is provided some commonly used for setting address:
All parameter are in Hexadecimal
format. For more detail of Xbee/XBee Pro command, please refer to User Manual of XBee/XBee-Pro
OEM RF Module.To send AT command and parameter, use syntax shown below:
NOTE: The failure to enter AT Command Mode is most commonly due to baud rate un-
match. Ensure the Baud setting on the PC Settings tab matches the interface data rate of the Xbee Module. By default, the baud rate should be 9600bps.
The below example shown how to check the previous Destination ID address of Xbee
module and how to change a new Destination ID address using WRITE command. To store the new value to non-volite (long term) memory, ATWR or atwr should be used
in AT Command Mode.
-
For modified parameter values to persist in the modules registry after a reset, changes must be saved to non-volatile memory using the WR (Write) command. Otherwise, parameters will be restored to previously saved values after reset. When a command is
send to a module, the module will parse and execute the command.
Upon successful execution of a command, the module return an OK message. If execution of a command results in an error, module will return ERROR message. For setting two SKXbee Source address and Destination address for communicate each
other, the command sending should same (parameter 1111 and 2222 can be change)
as below figure:
-
Now, we can write the coding to send command for setting SKXBee Source address and
Destination address. However, we have to refer the AT Command Mode table. The main reason
is the command code exactly same with the code we have using X-CTU software for setting the
parameter.
4.0 Using programming source code to setting Source address or Destination address.
If we want to use coding to send the command to SKXBee, we have to ensure the
hardware connection between SKXBee to microcontroller is complete. It can refer back
to the previous sub topic on 2.0 Xbee/XBee Pro hardware interface with microcontroller.
Due to this case, we are using SK40C and 40 pins PIC16877A to interface with SKXBee.
UART data flow will be use for them to communicate each other. So that, we will
initialize the UART configuration in programming code. This figure below has been
shown as an example code:
void uart_init(void)
{
SPBRG=129; //set baud rate as 9600 baud
BRGH=1; //baud rate high speed option
-
TXEN=1; //enable transmission
TX9 =0; //8-bit transmission
RX9 =0; //8-bit reception
CREN=1; //enable reception
SPEN=1; //enable serial port
}
UART Transmit and Receive coding configuration:
unsigned char uart_rec(void) //receive uart value
{
unsigned int rec_data;
while(RCIF==0); //wait for data
rec_data = RCREG;
return rec_data; //return the data received
}
void uart_send(unsigned char data)
{
while(TXIF==0); //only send the new data after
TXREG=data; //the previous data finish sent
-
}
void uart_str(const char *s)
{
while(*s)uart_send(*s++); // UART sending string
}
Now, we can write the coding to send command for setting SKXBee Source address and
Destination address. However, we have to refer the AT Command Mode table. The main reason
is the command code exactly same with the code we have using X-CTU software for setting the
parameter.
For initialize the Source address and Destination address to SKXBee1 module, we can
write the coding in void main() code as:
void xbee_init(void)
{
uart_str("+++");//send command to enter XBee Pro command mode
delay_ms(200);// waiting for finish sending and XBee respond
uart_str("atmy1111");//send command to setting Source address
uart_send(0xD);// 0XD is hexa code of "Enter"key
delay_ms(200);
-
uart_str("atwr");// send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atdl2222");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atwr");//send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
}
For initialize the Source address and Destination address to SKXBee2 module, we can
write the coding in another microcontroller PIC source code void main() as:
void xbee_init(void)
{
-
uart_str("+++");//send command to enter XBee Pro command mode
delay_ms(200);// waiting for finish sending and XBee respond
uart_str("atmy2222");//send command to setting Source address
uart_send(0xD);// 0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atwr");// send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atdl1111");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
uart_str("atwr");//send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
-
uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of "Enter" key
delay_ms(200);
}
Using UIC00B programmer to loading source code into PIC16F877A SK40C:
When we have finished load the coding into PIC, we should press RESET button to
ensure the address will store in Xbee/XBee Pro module. Otherwise, the Xbee/XBee Pro
will store with previously address value.
NOTE: Please remember the Source Address and Destination address only need one time
setting process for ID address, the Xbee/XBee Pro module will store the address in
module. User no need to setting it every time when using Xbee/XBee module, except
that user want to change a new Source address or Destination address to these RF
module.
7.0 Conclusion:
XBee/XBee Pro is a user friendly RF module for wireless communication between two PICs or
between a PIC and a computer. When we know how to use it with wisely, we can use this
wireless device to control a robot or equipment, data collection, security system and etc. By the
way, we have to know the command protocol for this RF module, then we can used and handle it
-
easily with microcontroller or computer. Please do post your inquiry in our technical forum as we
seldom check the comment section in tutorial site.