|
|
View previous topic :: View next topic |
Author |
Message |
Chienyee
Joined: 05 Mar 2013 Posts: 2 Location: Malaysia
|
Error when compiling source code (xbee series 1) |
Posted: Mon Mar 11, 2013 1:27 am |
|
|
Hi, I would like to ask for help in compiling programming code of xbee.
I am done with the coding of communication between 2 xbee series 1 (point to point communication). But I am having difficulties in compiling the code.
There seems so many error in the UART transmit and receiving part especially function part.
Hope that someone can help and guide. Thank you.
This is my code for SKXBEE 1:
Code: |
#include <18f4550.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal on PCB
#use rs232(baud=9600, UART1) // hardware uart much better; uses RC6/TX and RC7/RX
#include <stdlib.h>
#define sensor_1 PIN_B0 // LED 1
#define sensor_2 PIN_B1 // LED 2
#define LED_0 PIN_B6 // sensor 1
#define LED_1 PIN_B7 // sensor 2
#define sensor_1on 56 //ascii code char'8'
#define sensor_2on 50 //'2'
void uart_init(void);
unsigned char uart_rec(void); //receive uart value
void uart_send(unsigned char data);
void uart_str(const char *s);
void xbee_init(void);
//==========================================================================
//========================== global variable ===========================
//==========================================================================
unsigned int rec_data,a;
//////////////////////////
/////main function////////
/////////////////////////
void main()
{
set_tris_b (0x0F);
uart_init();
xbee_init();
while(TRUE)
{
if(!input(PIN_B0))
{
output_high(LED_0);
uart_send(sensor_1on);
delay_ms(200);
}
if(!input(PIN_B1))
{
output_high(LED_1);
uart_send(sensor_2on);
delay_ms(200);
}
else
{
output_low(LED_0);
output_low(LED_1);
}
}
}
//==========================================================================
//============================ function ================================
//==========================================================================
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);
}
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
}
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
}
|
This is the coding for SKXBEE2:
Code: |
#include <18f4550.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal on PCB
#use rs232(baud=9600, UART1) // hardware uart much better; uses RC6/TX and RC7/RX
#include <stdlib.h>
#define LED_0 PIN_B6 // sensor 1
#define LED_1 PIN_B7 // sensor 2
void uart_init(void);
unsigned char uart_rec(void); //receive uart value
void uart_send(unsigned char data);
void uart_str(const char *s);
void xbee_init(void);
unsigned int rec_data,a;
//////////////////////////
/////main function////////
/////////////////////////
void main()
{
set_tris_b (0x00);
uart_init();
xbee_init();
LED_0=0;
LED_1=0;
while(TRUE)
{
a=uart_rec();
if(a=='8')
{
output_high(LED_0);
delay_ms(100);
}
if(a=='2')
{
output_high(LED_1);
delay_ms(100);
}
else
{
output_low(LED_0);
output_low(LED_1);
}
}
}
//==========================================================================
//============================ function ================================
//==========================================================================
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);
}
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
}
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
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Mar 11, 2013 1:59 pm |
|
|
Read about the limitations of the PIC, and constants.
Change your uart_str routine to:
Code: |
void uart_str(int s){
putc(s); // UART sending string
}
|
and the prototype as well.
Then read the manual for 'why' this will work....
Add the keyword 'ERRORS' to the RS232 declaration. Read again 'why'.
Your uart_init code is not needed. This is done automatically by #use RS232.
Just use putc for 'uart_send'.
Just use getc for 'uart_rec'.
RTM, and look at the examples.
Best Wishes |
|
|
Chienyee
Joined: 05 Mar 2013 Posts: 2 Location: Malaysia
|
|
Posted: Sat Mar 16, 2013 8:12 am |
|
|
I have modify some of my code for few times but there is still no data transmission occurred. Though there is no more errors in my coding but still if I put errors in rs232 declaration there is a warning "variable never use rs232_errors". Please help or guide me in my coding so that data transmission can happen between 2 zigbee.Thanks.
This is my new coding for :
Transmit data:
Code: |
//==========================================================================
//============================ include ================================
//==========================================================================
#include <18f4550.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal on PCB
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,UART1,errors) // hardware uart much better; uses RC6/TX and RC7/RX
#include <stdlib.h>
//==========================================================================
//============================ define =================================
//==========================================================================
#define sensor_1 PIN_B0 // LED 1
#define sensor_2 PIN_B1 // LED 2
#define LED_0 PIN_B6 // sensor 1
#define LED_1 PIN_B7 // sensor 2
//==========================================================================
//======================== function prototype ==========================
//==========================================================================
void uart_str(int s);
void xbee_init(void);
//////////////////////////
/////main function////////
/////////////////////////
void main()
{
set_tris_b (0x0F);
xbee_init();
while(TRUE)
{
if(!input(PIN_B0))
{
output_high(LED_0);
putc('a');
delay_ms(200);
}
if(!input(PIN_B1))
{
output_high(LED_1);
putc('b');
delay_ms(200);
}
else
{
output_low(LED_0);
output_low(LED_1);
}
}
}
//==========================================================================
//============================ function ================================
//==========================================================================
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
putc(0xD);// 0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atwr");// send "WR" (write)command to SKXBee
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atdl2222");// send command to setting Destination address
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atwr");//send "WR" (write)command to SKXBee
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atcn");// send command for Exit AT Command Mode
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
}
void uart_str(int s)
{
putc(s); // UART sending string
}
|
Receiving data:
Code: |
//==========================================================================
//============================ include ================================
//==========================================================================
#include <18f4550.h>
#fuses HS,NOLVP,NOWDT,NOPROTECT
#use delay(clock=20000000) // 20 MHz crystal on PCB
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,UART1,errors) // hardware uart much better; uses RC6/TX and RC7/RX
#include <stdlib.h>
//==========================================================================
//============================ define =================================
//==========================================================================
#define LED_0 PIN_B6 // sensor 1
#define LED_1 PIN_B7 // sensor 2
//==========================================================================
//======================== function prototype ==========================
//==========================================================================
void uart_str(int s);
void xbee_init(void);
char x;
//////////////////////////
/////main function////////
/////////////////////////
void main()
{
set_tris_b (0x00);
xbee_init();
while(TRUE)
{
x=getc();
if(x=='a')
{
output_high(LED_0);
delay_ms(100);
}
if(x=='b')
{
output_high(LED_1);
delay_ms(100);
}
else
{
output_low(LED_0);
output_low(LED_1);
}
}
}
//==========================================================================
//============================ function ================================
//==========================================================================
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
putc(0xD);// 0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atwr");// send "WR" (write)command to SKXBee
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atdl1111");// send command to setting Destination address
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atwr");//send "WR" (write)command to SKXBee
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
uart_str("atcn");// send command for Exit AT Command Mode
putc(0xD);//0XD is hexa code of "Enter"key
delay_ms(200);
}
void uart_str(int s)
{
putc(s); // UART sending string
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Sat Mar 16, 2013 8:38 am |
|
|
As a diagnostic, I'd replace the zigbee modules with a PC running a terminal program.This way you could verify that the PICs are sending/receiving the correct data.
Not having those modules, I can't confirm that the 'init' for them is correct.
You're just sending commands and not listening for the responses.Maybe one of them is bad or not initialized correctly.You have no way of knowing that.
Another quick test would be to connect the PICs together without the zigbee modules.Again that will test the 'main' PIC codes and eliminate any 'problems' associated with the modules.
Once you've confirmed these 2 test work, then install the zigbee modules and providing their 'init' commands are correct, it should work.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Sat Mar 16, 2013 9:20 am |
|
|
The RS232_ERRORS thing is just a warning.
Basically when you add 'ERRORS', the compiler adds code to stop the UART getting hung if you don't service it in time. This is essential, unless you do this yourself. However it also adds a 'diagnostic' variable called 'RS232_ERRORS' to allow you to read that this is happening. If you don't use it you get the warning that it is not being used. It is only telling you 'look this feature is not being used', not that there is anything wrong. If you search the forum on this, you will find a lot about it. An annoyance, nothing more.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|