SKXBEE communicate with XBEE connected to PIC

Bluetooth, XBee, RF......

SKXBEE communicate with XBEE connected to PIC

Postby wayn » Tue Mar 20, 2012 12:29 am

I want to send data from SKXBEE to XBEE connected to PIC.
I m using XCTU and send the packet.
At the send packet box. I just send 1 byte that is 1 or in hex "0x31".

Below is my code for the PIC. Correct me if I had done the mistake.

CODE: SELECT_ALL_CODE
void uart_init(void)
{
   SPBRG=129;         //set baud rate as 9600 baud
   TXSTA.BRGH=1;            //baud rate high speed option
   TXSTA.TXEN=1;            //enable transmission
   TXSTA.TX9 =0;            //8-bit transmission
   RCSTA.RX9 =0;            //8-bit reception
   RCSTA.CREN=1;            //enable reception
   RCSTA.SPEN=1;            //enable serial port
}
unsigned char uart_rec(void)                        //receive uart value
{
        unsigned int rec_data;
        while(PIR1.RCIF==0);                                                //wait for data
        rec_data = RCREG;
        return rec_data;                                        //return the data received
}
void main() {
  ANSEL = 0;
  ANSELH = 0;
  T2CON = 0x04;
  TrisE = 0;
  PortE = 0;

  uart_init();

  Delay_ms(200);
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100)

  UART1_Write_Text("Start");
  UART1_Write(10);
  UART1_Write(13);

  while (1) {                   // Endless loop

    uart_rd = uart_rec();
    if(uart_rd == '1'){
      portE = 0x01;
      delay_ms(1200);
      portE = 0;
      }
      else if(uart_rd == '2'){
      portE = 0x02;
      delay_ms(1200);
      portE = 0;
      }
      else
      UART1_Write_Text("No input");
      UART1_Write(13);
      delay_ms(2000);

  }
}


I want to light up the LED at port E if receiving '1' or '2' from SKXBEE but it seem like couldnt work.
Can anyone gives me any advice or where should I troubleshoot. Thanks.
Attachments
fail.png
fail
wayn
Greenhorn
 
Posts: 2
Joined: Sun Jan 08, 2012 7:09 pm

Re: SKXBEE communicate with XBEE connected to PIC

Postby robosang » Tue Mar 20, 2012 9:55 am

How is XBee module connected to PIC? Can show us the hardware setup? Don miss out that XBee is 3.3V operation.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: SKXBEE communicate with XBEE connected to PIC

Postby wayn » Tue Mar 20, 2012 12:09 pm

robosang WROTE:How is XBee module connected to PIC? Can show us the hardware setup? Don miss out that XBee is 3.3V operation.


I had double check the hardware setup ady.. shouldnt be a problem since previously i used it for data receive and transmit before. The xbee is power up with 3.3V.

I'm not quite understand how come when the 2 xbee communicated, no matter what data I send through by XCTU, then the other xbee will reply sending back the same data to XCTU.

Is it in my code I need to write as this:
CODE: SELECT_ALL_CODE
 while (1) {                   // Endless loop

    uart_rd = uart_rec();
    if(uart_rd = 0x7E){
for (i=0; i< 18; i++)
data_discard = UART1_Read();     // Read the unused data
data_receive = UART1_Read();     // Read the command send
      if(data_receive == '1'){
      portE = 0x01;
      delay_ms(1200);
      portE = 0;
      }
}


By the way, when I read the data, can I just use uart_read at the receiver side to read the command i sent from the transmitter?
Or at the receiver side, I need to discard the unused byte and grab the data that being sent from transmitter?
Attachments
12345.png
wayn
Greenhorn
 
Posts: 2
Joined: Sun Jan 08, 2012 7:09 pm

Re: SKXBEE communicate with XBEE connected to PIC

Postby shahrul » Tue Mar 20, 2012 1:06 pm

XBee is not big problem if you understand the UART transmit and receive. You may testing with wired first to check your coding.
This is picture the way i do. Here.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 20 guests

cron