RF communication using PIC 18F4520

Discussion in 'Embedded Systems and Microcontrollers' started by areeckal, Feb 28, 2011.

  1. areeckal

    Thread Starter New Member

    Feb 28, 2011
    1
    0
    I need to set up RF communication from a pic 18F4520 to another pic 18f4520.
    I have developed transmitter and receiver code in mplab using hitech C compiler.
    Now I need to debug the code. The transmitter is working fine and I am getting the USART output from USART I/O tab.
    But while I am debugging receiver code, RCIF tag is not getting set even I am supplying input file through USART.
    Transmitter
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include "usart.h"
    4.  
    5. //Chip Settings
    6. __CONFIG(1,0x0200);
    7. __CONFIG(2,0X1E1F);
    8. __CONFIG(3,0X8100);
    9. __CONFIG(4,0X00C1);
    10. __CONFIG(5,0XC00F);
    11.  
    12. void main()
    13. {
    14.     unsigned int data;
    15.     //Initialize the USART
    16.     USARTInit();
    17.     //Now Read some input
    18.     while(1)
    19.     {
    20.         data=0x01;
    21.         /*
    22.         Now send a Packet
    23.         Packet Format is AA<data><data inverse>Z
    24.         total Packet size if 5 bytes.
    25.         */
    26.         //Stabilize the Tx Module By Sending JUNK data
    27.         //J for Junk
    28.         USARTWriteByte('J');
    29.         _delay(100);
    30.  
    31.         //Send 'A'
    32.         USARTWriteByte('A');
    33.         _delay(100);
    34.         //Send Another 'A'
    35.         USARTWriteByte('A');
    36.         _delay(100);
    37.         //Send the data;
    38.         USARTWriteInt(data,4);
    39.         _delay(100);
    40.         //Send inverse of data for error detection purpose
    41.         USARTWriteInt(~data,4);
    42.         _delay(100);
    43.         //End the packet by writing 'Z'
    44.         USARTWriteByte('Z');
    45.         _delay(100);
    46.  
    47.     }
    48. }
    49.  
    Receiver
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #include "usart.h"
    4.  
    5. //Chip Settings
    6. __CONFIG(1,0x0200);
    7. __CONFIG(2,0X1E1F);
    8. __CONFIG(3,0X8100);
    9. __CONFIG(4,0X00C1);
    10. __CONFIG(5,0XC00F);
    11.  
    12. void main()
    13. {
    14.     unsigned int packet[2],data;
    15.     TRISC=0x00;
    16.     //Initialize the USART
    17.     USARTInit();
    18.     //Get data from Tx
    19.     while(1)
    20.     {
    21.            
    22.         //Wait for a packet
    23.         if(USARTReadByte()=='A')
    24.         {
    25.             if(USARTReadByte()=='A')
    26.             {
    27.                 packet[0]=USARTReadByte();
    28.                 packet[1]=USARTReadByte();
    29.                 if((packet[0]!=~(packet[1]))&&(USARTReadByte()!='Z'))
    30.                     continue;
    31.                 else
    32.                 {
    33.                     data=packet[0];
    34.                     PORTC=data;
    35.                 }
    36.             }
    37.         }
    38.    
    39.  
    40.        
    41.     }
    42. }
    43.  
    usart.c
    Code ( (Unknown Language)):
    1.  
    2. /*****************************************************************
    3.  
    4. Most Basic USART (RS232 Serial) Communication Support File.
    5. Simple reading and writing of data without using
    6. Interrupts.
    7.  
    8. BAUD RATE:57600 Bits per Second
    9. CRYSTAL Frequency: 20MHz
    10.  
    11. Target Chip: PIC18F4520
    12. Target Compiler: HI-TECH C For PIC18 (http://www.htsoft.com/)
    13. Project: MPLAP Project File
    14.  
    15.  
    16. *****************************************************************/
    17.  
    18. #include <htc.h>
    19.  
    20. #include "usart.h"
    21.  
    22. void USARTInit()
    23. {
    24.     //Baud Rate = 57600 Bits per Second
    25.     //*** Note: Valid On 20MHz Crystal ONLY ***
    26.     //For other crystal freq calculate new values for SPBRG
    27.     SPBRG=86;
    28.  
    29.     //TXSTA REG
    30.     TXEN=1;
    31.     BRGH=1;
    32.    
    33.     //RCSTA
    34.     SPEN=1;
    35.     CREN=1; //Enable Receiver (RX)
    36.    
    37.     //BAUDCON
    38.     BRG16=1;
    39. PEIE=1;
    40.  
    41. }
    42.  
    43. void USARTWriteByte(char ch)
    44. {
    45.     //Wait for TXREG Buffer to become available
    46.     while(!TXIF);
    47.  
    48.     //Write data
    49.     TXREG=ch;
    50. }
    51.  
    52. void USARTWriteString(const char *str)
    53. {
    54.     while((*str)!='\0')
    55.     {
    56.         //Wait for TXREG Buffer to become available
    57.         while(!TXIF);
    58.  
    59.         //Write data
    60.         TXREG=(*str);
    61.  
    62.         //Next goto char
    63.         str++;
    64.     }
    65. }
    66.  
    67. /*
    68.  
    69. Writes a line of text to USART and goes to new line
    70. The new line is Windows style CR/LF pair.
    71.  
    72. This will work on Hyper Terminal Only NOT on Linux
    73.  
    74. */
    75.  
    76. void USARTWriteLine(const char *ln)
    77. {
    78.     USARTWriteString(ln);
    79.     USARTWriteString("\r\n");
    80. }
    81.  
    82. void USARTWriteInt(int val,unsigned char field_length)
    83. {
    84.     if(val<0)
    85.     {
    86.         USARTWriteByte('-');    //Write '-' sign for negative numbers.
    87.         val=(val*(-1));             //Make it positive.
    88.     }
    89.  
    90.     //Convert Number To String and pump over Tx Channel.
    91.     char str[5]={0,0,0,0,0};
    92.     int i=4,j=0;
    93.     while(val)
    94.     {
    95.         str[i]=val%10;
    96.         val=val/10;
    97.         i--;
    98.     }
    99.     if(field_length>5)
    100.         while(str[j]==0) j++;
    101.     else
    102.         j=5-field_length;
    103.    
    104.     for(i=j;i<5;i++)
    105.     {
    106.         USARTWriteByte('0'+str[i]);
    107.     }
    108. }
    109.  
    110.  
    111. unsigned char USARTReadByte()
    112. {
    113.     while(!RCIF);   //Wait for a byte
    114.    
    115.     return RCREG;
    116. }
    117.  
    118. [/i][/i]
     
Loading...