Xbee Problem

Bluetooth, XBee, RF......

Xbee Problem

Postby Kromuald13 » Fri Dec 23, 2011 11:45 am

hello guys
i have developped some codes to enable data transfer between two xbee modules with the help of two microcontroller , pic16f877a
the codes are compilling well , but practically , can see anything happening besides the welcoming message " Robot Wating uPON signal "
here below follows , the codes pls do have a look give me a feedback
thanks a lot

Transmission
CODE: SELECT_ALL_CODE
//===================================================================================
//   include
//===================================================================================
#include <pic.h>

//===================================================================================
//   configuration
//===================================================================================
__CONFIG (0x3F32);
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif 
/* For Delay use : __delay_ms(1000);
               CLRWDT();
                     or
                   __delay_us(1000);
               CLRWDT();
*/

//===================================================================================
//   define
//===================================================================================
#define   trigger      RA0
#define AdressL    RA1
#define   AdressH      RA2
#define display      PORTB  // Use for Statsion number display

#define SYNC_DATA      0x00
#define HEADER         0xaa

//===================================================================================
//   function prototype
//===================================================================================
//void send_packet(unsigned char data);
void uart_send(unsigned char data);
void uart_str(const char *s);
unsigned char uart_rec(void);
// Global Variable
//==================================================================================

 const char *s;
 unsigned char data ;

//   main function
//===================================================================================
void main(void)
{
   
   ADCON1= 0x06;            //configure PortA as digital I/O
   TRISA = 0b111111;         //configure PORTA input
   TRISB = 0b00000000;         //configure PORTB as output
    TRISC = 0b00000000;
   
   PORTA = 0x00;
   PORTC = 0xff;
   PORTB = 0x00;
 

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


  //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 &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atwr");// send &quot;WR&quot; (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atdl2222");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atwr");//send &quot;WR&quot; (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
//}

 
   unsigned char num = '5';   
   //unsigned char no= 0;
   //display=_7seg[num];

   while(1)                  //infinity loop
   {
      if(AdressL==0&&AdressH ==0)      //checking DIP switch state : Only using 2 pins here for we have 4 stations
      {
          num='1';            // Station One is on Fire
         

      //   display=_7seg[num];      //display Station number on 7 segment

         
      }


   
      else if(AdressL==1&&AdressH ==0)   //checking DIP switch state : Only using 2 pins here for we have 4 stations
      {
          num ='2';            // Station Two is on Fire
         

         //display=_7seg[num];      //display Station number on 7 segment


                  }

                 
                   else if(AdressL==0&&AdressH ==1)   //checking DIP switch state : Only using 2 pins here for we have 4 stations
      {
          num = '3';            // Station Three is on Fire
         

      //   display=_7seg[num];      //display Station number on 7 segment


                  }
                 
                 
                   else if(AdressL==1&&AdressH ==1)   //checking DIP switch state : Only using 2 pins here for we have 4 stations
      {
          num = '4';            // Station Four is on Fire
         

         //display=_7seg[num];      //display Station number on 7 segment


                  }



   while(trigger == 1)
      {
         data = num;
                        uart_send(data);         //continuous send data
         
      }   
      
      
      
   }      



}






//===================================================================================
//   functions
//===================================================================================

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
}

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
}





Reception


CODE: SELECT_ALL_CODE
//===================================================================================
//   include
//===================================================================================
#include <pic.h>

//===================================================================================
//   configuration
//===================================================================================
__CONFIG (0x3F32);
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif 
/* For Delay use : __delay_ms(1000);
               CLRWDT();
                     or
                   __delay_us(1000);
               CLRWDT();
*/

//===================================================================================
//   define
//===================================================================================

#define   Dip_Switch1      RB1
#define Dip_Switch2     RB2
#define   Dip_Switch3      RB3


#define      lcd         PORTD
#define      RS         RB4
#define      E         RB5

#define SYNC_DATA      0x00
#define HEADER         0xaa

//===================================================================================
//Global Variable
//===================================================================================

 unsigned char rec_data ;
 unsigned char  i;
 unsigned char j;
//const char S;
 const char *F;
const char *s;


//===================================================================================
//   function prototype
//===================================================================================

unsigned char uart_rec(void);
void e_pulse(void);
void delay(unsigned short i);
void send_char(unsigned char data);
void send_config(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void dis_num(unsigned long data);
void increment(unsigned long data);
void send_string(const char *F);
void uart_send(unsigned char data);
void uart_str(const char *s);


//===================================================================================
//   Main
//===================================================================================

void main(void)
 {
 ADCON1= 0x06;            //configure PortA as digital I/O
 TRISB = 0x00;
 TRISC = 0xff;
 TRISD = 0x00;
 TRISA = 0x00;

PORTA = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTB = 0x00;
send_config(0b00000001);      //clear display at lcd
send_config(0b00000010);      //Lcd Return to home
send_config(0b00000110);      //entry mode-cursor increase 1
send_config(0b00001100);      //diplay on, cursor off and cursor blink off
send_config(0b00111000);      //function set
      
lcd_goto(0);               //cursor start from beginning
      

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_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 &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atwr1111");// send &quot;WR&quot; (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atdl22");// send command to setting Destination address
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atwr");//send &quot;WR&quot; (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);
 
uart_str("atcn");// send command for Exit AT Command Mode
uart_send(0xD);//0XD is hexa code of &quot;Enter&quot;key
__delay_ms(200);



while(1)
 {
 
   lcd_clr();
    lcd_goto(0);
    send_string ("Robot Waiting ");
    lcd_goto(20);
    send_string ("Upon Signal ");

  unsigned char uart_rec(void);
   __delay_ms(100);

 if (rec_data == '1')
   {
     Dip_Switch1 = 1;
     Dip_Switch2 = 1;
     Dip_Switch3 = 1;
    lcd_clr();
    lcd_goto(0);
    send_string (" Station A on Fire");
   }


 else if (rec_data == '2')
   {
     Dip_Switch1 = 0;
     Dip_Switch2 = 1;
     Dip_Switch3 = 1;
    lcd_clr();
    lcd_goto(0);
    send_string (" Station B on Fire");
   }

  else if(rec_data == '3')
   {
     Dip_Switch1 = 1;
     Dip_Switch2 = 0;
     Dip_Switch3 = 1;
    lcd_clr();
    lcd_goto(0);
    send_string (" Station C on Fire");
   }
     
 
else if(rec_data == '4')
   {
     Dip_Switch1 = 0;
     Dip_Switch2 = 0;
     Dip_Switch3 = 1;
    lcd_clr();
    lcd_goto(0);
    send_string (" Station D on Fire");
   }

}

}


//===================================================================================
//Functions
//===================================================================================

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
}


//==================subroutine LCD setting ==========================

void send_config(unsigned char data)
{
    RS=0;
   lcd=data;
   __delay_ms(500);
   e_pulse();
}

void e_pulse(void)
{
   E=1;
   __delay_ms(500);
   E=0;
   __delay_ms(500);
}

void send_char(unsigned char data)
{
    RS=1;
   lcd=data;
   __delay_ms(500);
   e_pulse();
}


void lcd_goto(unsigned char data)
{
    if(data<16)
   {
       send_config(0x80+data);
   }
   else
   {
       data=data-20;
      send_config(0xc0+data);
   }
}


void lcd_clr(void)
{
   RS=0;
    send_config(0x01);
   __delay_ms(600);
}


 void send_string ( const char *F)
  {
    while(F&&*F) send_char(*F++);
  }



void dis_num(unsigned long data)
{
   unsigned char hundred_thousand;
   unsigned char ten_thousand;
   unsigned char thousand;
   unsigned char hundred;
   unsigned char tenth;

   hundred_thousand = data/100000;               
   data = data % 100000;
   ten_thousand = data/10000;
   data = data % 10000;
   thousand = data / 1000;
   data = data % 1000;
   hundred = data / 100;
   data = data % 100;
   tenth = data / 10;
   data = data % 10;

   if(hundred_thousand>0)
   {
      send_char(hundred_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(ten_thousand + 0x30);
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }

   else if(ten_thousand>0)
   {
      send_char(ten_thousand + 0x30);   //0x30 added to become ASCII code
      send_char(thousand + 0x30);
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(thousand>0)
   {
       send_char(thousand + 0x30);   //0x30 added to become ASCII code
      send_char(hundred + 0x30);
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(hundred>0)
   {
       send_char(hundred + 0x30);   //0x30 added to become ASCII code
      send_char(tenth + 0x30);
      send_char(data + 0x30);
   }
   else if(tenth>0)
    {
      send_char(tenth + 0x30);   //0x30 added to become ASCII code
      send_char(data + 0x30);
   }
   else send_char(data + 0x30);   //0x30 added to become ASCII code
}

void increment(unsigned long data)
{   
   unsigned short j;
   for(j=10;j>0;j--)
   {   lcd_goto(32);
      data=data+1;
      dis_num(data);
      __delay_ms(5000);
   }

}
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Xbee Problem

Postby shahrul » Fri Dec 23, 2011 12:43 pm

It's hard to checking program without hardware.
You try display rec_data. Is that your rec_data receive signal or not? Is that your rec_data receive right value as '1' , '2' , '3' , '4' or other else.
You can see example here, XBee Lesson but the transmitter is from VB and PIC is the receiver.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: Xbee Problem

Postby Hollowaykeanho » Fri Dec 23, 2011 12:45 pm

So what's the problem?


Also, can you simplify your codes to only those particular related parts?

Thanks.
Hollowaykeanho
Newbie
 
Posts: 12
Joined: Wed Jun 22, 2011 6:39 am
Location: KL

Re: Xbee Problem

Postby Hollowaykeanho » Fri Dec 23, 2011 1:06 pm

I'll assume your problem is you can't get a command transmission works, here's a tip for debugging, hopefully it helps:


1) Between 2 Xbee, use the XTU software to check the modem settings (destination add etc etc.) and try use its manual communications to communicate each other. You can open multiple XTU at a time but each XTU can open a specific port.
--> Goal: Eliminate Xbee Modem handshake problem
--> Desired Outcome: Like a loopback test. You should be able to manually send char from one side to another with no flaws.


2) Next, use either one XTU software talks to transmitter PIC side.
--> Goal: To check transmission PIC is working properly and eliminate bug/errors contributed by transmitter. (Finding bug for debug)
--> Desired Outcome: XTU should be able to display proper transmission output. Each message must be correct as accordance to your design.


3) Next, continue use that XTU talks to reception PIC side.
--> Goal: To check reception side is working properly. (Rooting out problems)
--> Explanation: Since both modem and transmitter problem are busted, errors form factors are most probably came from reception side instead. You might want to do the following with other output display (as mentioned by shahrul, LCD is good choice):

3.1) Display received message to LCD upon successful transmission
-->Goal: Bust out the UART setting problem etc.
-->Desired outcome: the display on LCD should be exactly the same as the message being sent from XTU.

3.2) Manual issue the command from XTU to reception PIC side
-->Goal: To ensure your program works as the way it should.
-->Desired outcome: Reception side should do the functions accordingly as you're manually issuing the command to the system.

3.3) Integrate both transmitter PIC and reception PIC
--> Last try attempts.


Hope this helps.

Cheers,
Holloway
Hollowaykeanho
Newbie
 
Posts: 12
Joined: Wed Jun 22, 2011 6:39 am
Location: KL

Re: Xbee Problem

Postby Kromuald13 » Fri Dec 23, 2011 1:31 pm

shahrul WROTE:It's hard to checking program without hardware.
You try display rec_data. Is that your rec_data receive signal or not? Is that your rec_data receive right value as '1' , '2' , '3' , '4' or other else.
You can see example here, XBee Lesson but the transmitter is from VB and PIC is the receiver.




hi shahrul ..
hanks for ur reply
humm actually , i am receiving , the characters . '1' , '2' , '3' and absed pn the characters recieved a ccertains message willl appear on the lcd screen ...
let me have a look on ur link , thx aagain
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: Xbee Problem

Postby Kromuald13 » Fri Dec 23, 2011 1:33 pm

Hollowaykeanho WROTE:I'll assume your problem is you can't get a command transmission works, here's a tip for debugging, hopefully it helps:


1) Between 2 Xbee, use the XTU software to check the modem settings (destination add etc etc.) and try use its manual communications to communicate each other. You can open multiple XTU at a time but each XTU can open a specific port.
--> Goal: Eliminate Xbee Modem handshake problem
--> Desired Outcome: Like a loopback test. You should be able to manually send char from one side to another with no flaws.


2) Next, use either one XTU software talks to transmitter PIC side.
--> Goal: To check transmission PIC is working properly and eliminate bug/errors contributed by transmitter. (Finding bug for debug)
--> Desired Outcome: XTU should be able to display proper transmission output. Each message must be correct as accordance to your design.


3) Next, continue use that XTU talks to reception PIC side.
--> Goal: To check reception side is working properly. (Rooting out problems)
--> Explanation: Since both modem and transmitter problem are busted, errors form factors are most probably came from reception side instead. You might want to do the following with other output display (as mentioned by shahrul, LCD is good choice):

3.1) Display received message to LCD upon successful transmission
-->Goal: Bust out the UART setting problem etc.
-->Desired outcome: the display on LCD should be exactly the same as the message being sent from XTU.

3.2) Manual issue the command from XTU to reception PIC side
-->Goal: To ensure your program works as the way it should.
-->Desired outcome: Reception side should do the functions accordingly as you're manually issuing the command to the system.

3.3) Integrate both transmitter PIC and reception PIC
--> Last try attempts.


Hope this helps.

Cheers,
Holloway





thx chew , let me proceed that ......
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am


Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 11 guests

cron