XBee radio communication between PICs

From Mech
Revision as of 23:12, 19 February 2008 by Hwang (talk | contribs) (→‎ZigBee Mode)
Jump to navigationJump to search

Original Assignment

Two wired PICs can communicate by RS-232. We can replace the wires with a wireless link. In this project, that wireless link will be implemented using Zigbee radio modems that use the IEEE 802.15.4 protocol. The demonstration will show two PICs talking to each other over wireless, and a PIC talking to a PC by wireless. This demonstration has the user typing in data into hyperterminal and the PIC responding. For example, the user might type "1+5 [enter]" and the PIC responds with "6".

Overview

It is common for two pics to communicate by RS-232, a wired transmission. However, it may be desirable to communicate via a wireless link. This wiki page discusses this option by using XBee radio modems using the IEEE 802.15.4 protocol. This will allow for wireless communication between two PICs and between a PIC and a computer.

File:XBeePinOut.jpg

The IEEE 802.15.4 is a point-point/point-multipoint communications protocol(similar to Bluetooth) designed for low-power devices. Like Bluetooth, the IEEE 802.15.4 specification also uses the 2.4 GHz ISM band. The ZigBee protocol, which deals with mesh networking and routing, is built upon the IEEE 802.15.4 specification.

The XBee radios, made by Digi (formerly Maxstream), are shipped with firmware implementing the IEEE 802.15.4 protocol, but can be loaded with the ZigBee protocol stack, which can be downloaded from the Digi website. Note that ZigBee is still in its infancy and devices from different manufacturers may not be compatible. The range for an XBee Pro indoors is up to 300 feet while line of site, outdoor communication is up to a mile. (XBee Manual)

The XBee requires a 3.3 voltage regulator and the chip is designed to be mounted in specific sockets (Note: These sockets don't fit in a standard bread board!)

For the pin locations, see page 7 of XBee Manual or the figure to the right.

For PIC to computer interface, a terminal program such as X-CTU needs to be used. Although other terminal programs might work as well, X-CTU software was designed specifically for the XBee, and in addition to its terminal functions, it also has functions for testing signal strength, reading, saving, and writing the state of the XBee, and updating firmware. The X-CTU program is run on the PC while connected to a X-Bee via a serial port. The X-CTU software can be downloaded from X-CTU Site.


Circuit

Communicating Pic to Pic
Communicating Computer to Pic



The XBee module can be connected directly to the UART port on the PIC. To connect it to a RS2-32 port, one must use a voltage shifting transceiver chip because RS232 signals are from -15V to + 15V. The MAX232/ST232 chip will convert voltage level from RS-232 to TTL logic levels and vice versa. The chip requires 4 external capacitors (the fifth is a bypass capacitor) in order to operate. The transceiver data sheet can be found here.

In the circuit shown above, the serial port is a DE9F 9-pin serial port, which is used to connect the computer to the circuit.

In order to communicate PIC to PIC, two of the circuits shown on the left should be used.

In order to communicate PIC to computer, one of each of the circuits should be used.

Code

Communication PIC to Computer

The code below will wait for the character '+' to appear on the serial port, and when it does, it will add the next two single digit numbers that are entered and return the result back to the computer. In order to connect with the PIC, the program X-CTU must be used on the computer as discussed above.

#include <18f4520.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT        
#use delay(clock=20000000)       // 20 MHz crystal on PCB
//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1)    // you can use any pins for software uart...
#use rs232(baud=9600, UART1)       // hardware uart much better; uses  RC6/TX and RC7/RX 
// characters tranmitted faster than the pic eats them will cause UART to hang.

#include <stdlib.h>

int a;
char abuff[2]={0};
int b;
char bbuff[2]={0};

char x;
int sum;

void main() {
   while (TRUE) { 
       if (kbhit()) {
         x = getc();
       }
       if (x=='+'){
         while (!kbhit()){}
         abuff[0] = getc();
         a = atoi(abuff);
         while (!kbhit()){}
         bbuff[0] = getc();
         b = atoi(bbuff);
         
         sum = a+b;
         printf("sum=%u\r\n",sum);
         x=0;
       }
   }
}

Communication PIC to PIC

In this communication, each PIC has its own code, which are shown below.

Transmitter

/* pic2pic_transmit.c
ME333 Lab 5
The transmitter checks to see if PIN A0 is high or low. 
If the  pin is high, the transmitter sends the signal 'a' to the receiver and 
turns on an LED. If the pins is low, the transmitter sends the signal 'b'. 
*/

#include <18f4520.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT        
#use delay(clock=20000000)       // 20 MHz crystal on PCB
//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1)    // you can use any pins for software uart...
#use rs232(baud=9600, UART1)       // hardware uart much better; uses  RC6/TX and RC7/RX 
// characters tranmitted faster than the pic eats them will cause UART to hang.

#include <stdlib.h>

#define LED_0 PIN_D0

void main() {
   while (TRUE) { 
      if (input(PIN_A0)){
         output_high(LED_0);
         printf("a"); //sends signal a
       }
       else{
         output_low(LED_0);
         printf("b"); //sends signal b
       }
       
       }
   }

Receiver


/* pic2pic_receive
ME 333 Lab 5
The receiver checks the signal from the XBEE and if the signal is 'a', the receiver turns on an LED.
*/

#include <18f4520.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT        
#use delay(clock=20000000)       // 20 MHz crystal on PCB
//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1)    // you can use any pins for software uart...
#use rs232(baud=9600, UART1)       // hardware uart much better; uses  RC6/TX and RC7/RX 
// characters tranmitted faster than the pic eats them will cause UART to hang.

#define LED_0 PIN_D0

#include <stdlib.h>

char x;

void main() {
   while (TRUE) { 
       if (kbhit()) {
         x = getc();
       }
       if (x=='a'){
         output_high(LED_0);
         
       }
       else{
         output_low(LED_0);
         
       }
   }
}


Using the XBee Radio

Parameters AT Commands

AT commands enable you to set the parameters of the XBee radio. To enter AT command mode, open X-CTU (program discussed in overview) and type in "+++" (without the quotes). The chip should respond with an "OK". Then, you can enter the commands. If the radio doesn't receive a commands for a while (default 10s), then it will exit command mode and return to normal operation mode. All parameters are in hexadecimal format.

Commands are usually entered in the following formats (after entering command mode):

To read parameters: AT**

To write parameters: AT** value

For example, to read the ID of the radio, you could enter:

+++ ATMY

To set the value of the radio's ID to 1 you could enter:

+++ ATMY 1


Specific instructions and descriptions of allowable parameters to send can be found in Section 3 of the XBee Product Manual.

Some commonly used commands:

  • +++ Enter command mode
  • ATCN Exit command mode
  • ATAC Apply changes
  • ATWR Write current parameter values to non-volatile memory (must reset or use ATAC for changes to take effect)
  • ATRE Restore defaults
  • ATFR Reset
  • ATCH Set channel
  • ATID Set network ID
  • ATDH Set desination ID (high byte)
  • ATDL Set destination ID (low byte)
  • ATMY Set device ID
  • ATBD Set baud rate
  • ATAP Set transparent/API mode

Transparent Operation

By default, the radios are set to work in transparent mode (as opposed to API mode).

Unicast/Multicast Mode

(see section 2.4.1 in the manual)

A radio will be able to send send data to any other radio having the same channel (CH parameters), PAN-ID (ID parameter), and has a source address (MY in 16-bit address mode, SL+SH parameters in 64-bit address mode. See 2.4 in the manual) equal to the destination address (DL parameter in 16-bit addressing mode, DL+DH in 64-bit addressing mode) of the sending radio. You can set these parameters using the AT commands.

Broadcast Mode

(see section 2.4.2 in the manual) To make your XBee broadcast packets, set the DL parameters to FFFF and the DH parameter to 0.


Proprietary API mode

To use the API mode, set the AP parameter to 1 or 2, depending on whether you will need escape characters (see 3.4 in the manual for more information).

The API mode allows users to send data in a packet structure. Commands and specific destination addresses can be embedded into packets, which allows you to give the radio commands without having to enter the AT command mode. The packet also inclues a checksum (the packet won't be sent/received if this is wrong), and receiving packets will have things like source address and signal strength embedded.

Writing software for the API mode may be more difficult, as you have to assemble a packet and calculate a checksum. You may wish to use API mode if you need to be able to detect corrupt data or if you need to communicate to many different radios individually.