Xbee Problem
Posted: 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
Reception
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 "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);
//}
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 "Enter"key
__delay_ms(200);
uart_str("atwr1111");// send "WR" (write)command to SKXBee
uart_send(0xD);//0XD is hexa code of "Enter"key
__delay_ms(200);
uart_str("atdl22");// 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);
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);
}
}