RF Half Duplex communication using PIC16F877A

Bluetooth, XBee, RF......

RF Half Duplex communication using PIC16F877A

Postby Kromuald13 » Thu Dec 15, 2011 3:52 am

Hi to All
i am Romuald , and i am doing a RF based project . My aim is to send a string of character ( 4 bit code) in a wireless manner .
to do so , i used DIP switch to produce the 4 bit code on the transmitter side and connect them to PORT A , then i want to send t a receiver .
i have already programmed it , and it compiles succesfully , but i don't know why when i check at the receiver side , but using LEDs i can 't see anything
it is seen no to be working at all

here below follows my codes

TRANSMISSION
CODE: SELECT_ALL_CODE
//==========================================================================
//   Author            : Cytron Technologies      
//   Project            : Sending Data using RF Module
//   Project description   : The transmitter will send 0->F (data in Hexadecimal)
//==========================================================================

//===================================================================================
//   include
//===================================================================================
#include <pic.h>

//===================================================================================
//   configuration
//===================================================================================
__CONFIG (0x3F32);

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



#define SYNC_DATA      0x00
#define HEADER         0xaa
#define send_button         RB0

//===================================================================================
//   function prototype
//===================================================================================
void uart_send(unsigned char data);
void send_packet(unsigned char data);

//   main function
//===================================================================================
void main(void)
{
   //assign variable
   unsigned char Adress,no;
                           

   ADCON1= 0x06;            //configure PortA as digital I/O

   TRISA = 0b111111;         //configure PORTA input
   TRISB = 0b00000001;         //configure PORTB as output
   
   //setup USART
   BRGH = 0;               //baud rate low speed option
   SPBRG = 255;            //set boud rate to 1200bps for 20Mhz crystal
   TX9 = 0;               //8-bit transmission
   TXEN = 1;               //enable transmission
   SYNC = 0;               //asynchronous
   
   SPEN = 1;               //enable serial port
   
   Adress = PORTA;   
   no=0;


   while(1)                  //infinity loop
   {
   no = Adress;

   if (send_button ==0){
         
      send_packet(no);
                       }    
   }      
}

//===================================================================================
//   functions
//===================================================================================
void uart_send(unsigned char data)
{   
   while(TXIF==0);            //only send the new data after
   TXREG=data;               //the previous data finish sent
}


void send_packet(unsigned char data)
{
   unsigned char i;
   
   // Buffer for the data in one packet.
   unsigned char buffer[3];
   
   // Byte 0 is the header.
   buffer[0] = HEADER;
   
   // Byte 1 is the data.
   buffer[1] = data;
   
   // Byte 2 is the checksum.
   buffer[2] = (unsigned char)(HEADER + data);
   
   // Clocking for a while before sending the data so that the Tx and Rx are in sync.
   for (i = 0; i < 7; i++) uart_send(SYNC_DATA);
   
   // Transmit the packet using UART.
   for (i = 0; i < 3; i++) uart_send(buffer[i]);
}



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

//===========================================================================
//    configuration
//============================================================================
__CONFIG (0x3F32);

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

#define HEADER         0xaa
# define Adress                 PORTA
//=============================================================================
//   function prototype
//=============================================================================
unsigned char uart_rec(void);
unsigned char read_packet(void);

//============================================================================
//   main function
//============================================================================
void main(void)
{
   //assign variable
   unsigned char Adress,no;
                              

   //set I/O input output
   TRISB = 0b00000000;         //configure PORTB as output
         TRISA = 0b000000;
   
      
   //setup USART
   BRGH = 0;               //baud rate low speed option
   SPBRG = 255;            //set boud rate to 1200bps for 20Mhz crystal
   SPEN = 1;               //enable serial port
   RX9 = 0;               //8-bit reception
   CREN = 1;               //enable reception

   Adress = 0 ;
   

   while(1)               //infinity loop
   {
      CREN=1;                     //enable continuos receive
      if(OERR==0) no=read_packet();       //receive sata if overrun error free
      else CREN=0;               //if overrun error, disable continuos receive
      
      Adress=no;         
     
   }
}

//==================================================================================
//   functions
//===================================================================================
unsigned char uart_rec(void)   //receive uart value
{
   unsigned char rec_data;
   while(RCIF==0);            //wait for data
   rec_data = RCREG;
   return rec_data;         //return the received data
}



unsigned char read_packet(void)
{
   // Buffer for received byte.
   unsigned char received_byte;
   
   // Counter to indicate the current position of the received data packet.
   static unsigned char counter = 0;
   
   // Buffers for the data and checksum.
   unsigned char data;
   unsigned char checksum;
   
   
   // We loop until the checksum is correct.
   do {
      // We will ignore the sync data and assume the header byte is the start of packet.
      // Keep reading until the header byte is received.
      while (uart_rec() != HEADER);
      
      // The following byte shoulde be the data byte.
      data =    uart_rec();
      
      // Then the last byte is the checksum.
      checksum =    uart_rec();
   } while (checksum != (unsigned char)(HEADER + data));
   
   // If the checksum is correct, return the data.
   return data;
}
Kromuald13
Novice
 
Posts: 26
Joined: Wed Feb 09, 2011 2:03 am

Re: RF Half Duplex communication using PIC16F877A

Postby Kromuald13 » Thu Dec 15, 2011 3:13 pm

Hi i forgot to mention that , i took the program form Cytron project PR16 and modified it . The Thing that , i am using a 433MHz RF module instead of the 315 MHz module used by Cytron , so wondering if there is any setting modifications to do when initializing the USUART in the program

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

Re: RF Half Duplex communication using PIC16F877A

Postby sich » Fri Dec 16, 2011 4:44 pm

The configuration for both 315 and 433 are the same. It's just the frequency of the wireless link. Try connect your PICs UART together, Tx to Rx, Rx to Tx and Gnd to Gnd. If your program is okay, this should work. Other wise, troubleshoot your code.
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 603
Joined: Tue Apr 21, 2009 2:15 pm


Return to Wireless Device

Who is online

Users browsing this forum: No registered users and 1 guest

cron