CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Wireless Communication Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
TheRidDler



Joined: 13 Jun 2007
Posts: 7

View user's profile Send private message

Wireless Communication Problem
PostPosted: Sun Jun 24, 2007 9:47 pm     Reply with quote

Hi all! I really love this forum, one learns quickly here Smile I have a small problem, i hope you can help me with this, as its my final year project and need to get this working or no degree :( Me and a friend wrote this code together specifically for our system.

This is our setup : We have a remote board with a GPS unit and a Radiometrix Transmitter. The board is controlled by a 18F252. The PIC takes the positional sentence from the GPS and transmits it to the local receiver board. The local board has a Radiometrix Receiver unit , 18F252 and a MAX232N. This board only relays the data to the PC so that a GUI can play with it.

When opening the console window however, absolutely nothing gets received. If possible could you guys that probably know a bit more about PIC coding than us please take a look at our code to see if you can maybe spot something that might be causing this? Thanks alot for all your help!

Remote (GPS) Board
Code:


#include <18F252.h>
#include <stdlib.h>
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,stop=1,parity=N,rcv=PIN_B4,bits=8,stream=GPS,errors)              //data from GPS to PIC

char ToBeSend[128],EncodedData[256];

void encodeRF()
{
       //Encoding of data byte to manchester coding
       char Send1,Send2,txbyte;
       int8 encode=0, index=0;

       while (encode < 128){
       txbyte = ToBeSend[encode];
       encode++;

       if (bit_test(txbyte,7)) { bit_set(Send1,7), bit_clear(Send1,6); }
       else { bit_clear(Send1,7), bit_set(Send1,6); }
       if (bit_test(txbyte,6)) { bit_set(Send1,5), bit_clear(Send1,4); }
       else { bit_clear(Send1,5), bit_set(Send1,4); }
       if (bit_test(txbyte,5)) { bit_set(Send1,3), bit_clear(Send1,2); }
       else { bit_clear(Send1,3), bit_set(Send1,2); }
       if (bit_test(txbyte,4)) { bit_set(Send1,1), bit_clear(Send1,0); }
       else { bit_clear(Send1,1), bit_set(Send1,0); }
       if (bit_test(txbyte,3)) { bit_set(Send2,7), bit_clear(Send2,6); }
       else { bit_clear(Send2,7), bit_set(Send2,6); }
       if (bit_test(txbyte,2)) { bit_set(Send2,5), bit_clear(Send2,4); }
       else { bit_clear(Send2,5), bit_set(Send2,4); }
       if (bit_test(txbyte,1)) { bit_set(Send2,3), bit_clear(Send2,2); }
       else { bit_clear(Send2,3), bit_set(Send2,2); }
       if (bit_test(txbyte,0)) { bit_set(Send2,1), bit_clear(Send2,0); }
       else { bit_clear(Send2,1), bit_set(Send2,0); }

       EncodedData[index] = Send1;
       EncodedData[index+1] = Send2;
       index = index+2;
       }
}       
       
void Transmit(char IDUnit)
{
   
       int8 i = 0;

       set_tris_a(0x00);
       
       #use rs232(baud=57600,parity=N,bits=8,xmit=PIN_A0,RESTART_WDT,errors)// data from PIC to PC
       
       //delay_ms(40); //Give Relay Unit time to change TRX to RX mode

       delay_ms(5);

       for (i=0;i<200;i++) //Training sequence
         putc(0xAA);

       putc(0xFF);
       putc(0xFF);
       putc(0xCC);         //Header #1
       putc(0x33);         //Header #2
       putc(0x55);         //Header #3
       putc(IDUnit);       //ID of unit to be communicated to
       
       for (i=0; i<256; i++)//Data
         putc(EncodedData[i]);
       
       putc(0x00);         //Ending Sequence of transmition
       putc(0x00);

       for (i=0;i<128;i++)
      {           //clear the TX buffer for next transmission
         ToBeSend[i] = 0x00;
       }
}

#define GPS_BUFFER_SIZE   128         

char gps_buffer[GPS_BUFFER_SIZE];                                               // A place to store the string

char *GPS_GetField( int index )
{
   char *p = gps_buffer;         
   int count = 0;               

   while( *p )                  
   {
      if( *p++ == ',' )         
      {
         if( count++ == index )   
         {
            return p;         
         }
      }
   }
   
   return p;
}


long GPS_Min_To_Dec( char *p )
{
   long value;
   int  decimal;

   value = atol(p) + 3;                                                     // to round to the nearest minute
   decimal = value % 100;
   value -= decimal;
   value /= 10;
   value += decimal / 6;

   return value;
}

void Send_To_PC()
{
   int t;
   for(t=0;t<GPS_BUFFER_SIZE;t++)
      ToBeSend[t]=gps_buffer[t];
   encodeRF();
   Transmit(0xAA); 
}

void main()
{
   char ch;
   int checksum1, checksum2, index;

   set_tris_b( 0xFF );
   port_b_pullups( 1 );
   
   for(;;)
   {
      ch = getch(GPS);

      switch( ch )
      {
         case '\n':
         continue;
         
         case '$':
         index = checksum1 = checksum2 = 0;
         break;

         case '*':
         checksum2 = 1;
         break;

         case '\r':
            ch = gps_buffer[index-2];
            checksum2 = ((ch < 'A') ? (ch - '0') : (ch - 'A' + 10)) << 4;
         
            ch = gps_buffer[index-1];
            checksum2 += ((ch < 'A') ? (ch - '0') : (ch - 'A' + 10));
         
            if( checksum1 == checksum2  )
            {
               gps_buffer[index] = 0;

               // the first unique letter is R
               if( gps_buffer[3] == 'R' )
               {
                  Send_To_PC();
               }
             }

         default:
         if( checksum2 == 0 )
         {
            checksum1 ^= ch;
         }
         break;
      }
      
      if( index < GPS_BUFFER_SIZE - 1 )
      {
         gps_buffer[index++] = ch;
      }
   }
}


Local Reveiver Board
Code:


#include <18F252.h>
#include <stdlib.h>
#use delay(clock=20000000)
#fuses NOWDT,HS, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT

const char Address = 0xAA;

char Received[256],RFIndicator = 0x00,DecodedData[128];

int16 timeout = 0;

char decodeRF (char P1,P2)
{
      char value = 0x00;
      //Decoding of manchester encoded received information

      disable_interrupts(GLOBAL);

      if( bit_test(P2,0) && !bit_test(P2,1) ) { bit_clear ( value,0); }
      else if( !bit_test(P2,0) &&  bit_test(P2,1) ) { bit_set ( value,0); }
      else goto ERROR;
      if(  bit_test(P2,2) && !bit_test(P2,3) ) { bit_clear ( value,1); }
      else if( !bit_test(P2,2) &&  bit_test(P2,3) ) { bit_set ( value,1); }
      else goto ERROR;
      if(  bit_test(P2,4) && !bit_test(P2,5) ) { bit_clear ( value,2); }
      else if( !bit_test(P2,4) &&  bit_test(P2,5) ) { bit_set ( value,2); }
      else goto ERROR;
      if(  bit_test(P2,6) && !bit_test(P2,7) ) { bit_clear ( value,3); }
      else if( !bit_test(P2,6) &&  bit_test(P2,7) ) { bit_set ( value,3); }
      else goto ERROR;
      if(  bit_test(P1,0) && !bit_test(P1,1) ) { bit_clear ( value,4); }
      else if( !bit_test(P1,0) &&  bit_test(P1,1) ) { bit_set ( value,4); }
      else goto ERROR;
      if(  bit_test(P1,2) && !bit_test(P1,3) ) { bit_clear ( value,5); }
      else if( !bit_test(P1,2) &&  bit_test(P1,3) ) { bit_set ( value,5); }
      else goto ERROR;
      if(  bit_test(P1,4) && !bit_test(P1,5) ) { bit_clear ( value,6); }
      else if( !bit_test(P1,4) &&  bit_test(P1,5) ) { bit_set ( value,6); }
      else goto ERROR;
      if(  bit_test(P1,6) && !bit_test(P1,7) ) { bit_clear ( value,7); }
      else if( !bit_test(P1,6) &&  bit_test(P1,7) ) { bit_set ( value,7); }
      else goto ERROR;

      // value decoded
      return value;

      ERROR:
      return 'E';
}

void Send_To_GUI()
{
   int t;
   
   #use rs232(baud=57600,stop=1,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8,errors)
   
    output_high(PIN_B3);
   
   for(t=0;t<128;t++)
      putc(DecodedData[t]);
}   

void Receive()
{
   int  counter = 0x00, position = 0x00;
   char Header[3] = {0x00,0x00,0x00};
   char Identifier = 0x00;
   int  RFtimeout = 20;

   for (counter=0;counter<128;counter++)
       Received[counter] = 0x00;

   counter = 0x00;

   #use rs232(baud=57600,stop=1,parity=N,rcv=PIN_C7,bits=8,RESTART_WDT,errors)

   //WAITING RX ROUTINE..
   set_timer3(0x0000);
   
   timeout = 0;
   
   enable_interrupts(int_TIMER3);
   enable_interrupts(global);

   WAIT:
   //WAITING RX  ROUTINE

         Header[0] = getchar();
   
         if(timeout > RFtimeout)
         {
            disable_interrupts(int_TIMER3);
            timeout = 0;
            RFIndicator = 0x01; //This is the timeout and well received indicator
            goto OUT;
         }
   
         if(Header[0] != 0xCC)
            goto WAIT;

         Header[1] = getchar();

         if(timeout > RFtimeout)
         {
            disable_interrupts(int_TIMER3);
            timeout = 0;
            RFIndicator = 0x01; //This is the timeout and well received indicator
            goto OUT;
         }
   
         if(Header[1] != 0x33)
            goto WAIT;

         Header[2] = getchar();
   
         if(timeout > RFtimeout)
         {
            disable_interrupts(int_TIMER3);
            timeout = 0;
            RFIndicator = 0x02; //This is the timeout and well received indicator
            goto OUT;
         }
   
         if(Header[2]!= 0x55)
            goto WAIT;

         Identifier = getchar();
   
         if(timeout > RFtimeout)
         {
            disable_interrupts(int_TIMER3);
            timeout = 0;
            RFIndicator = 0x03; //This is the timeout and well received indicator
            goto OUT;
         }
   
         if(Identifier != Address)
            goto WAIT;

         Identifier = 0x00;
   
         disable_interrupts(int_TIMER3);
   
         while(counter < 128)
            {
                Received[counter] = getchar();
                counter++;
            }
       
       counter = 0x00;

         while (counter < 64)
         {
           DecodedData[position] = decodeRF(Received[counter],Received[counter+1]);
           position++;
           counter = counter + 2;
         }

         RFIndicator = 0x00; //This is the timeout and well received indicator

         OUT:
            disable_interrupts(global);
}

void main()
{
   output_high(PIN_B2);
   while(1)
      {
         Receive();
         Send_To_GUI();
         output_low(PIN_B3);
      }
}      


Some Additional Info :
Remote Board -> GPS Receive(Pin B4), RF Xmit(Pin A0) [GPS=9600,RF=57600]
Local Receiver Board -> RF Receive(Pin C7), Max232 Xmit(Pin A0),Max232 Rcv (Pin A1), B2&B3 - LED's [RF=57600, Max232=57600]
Both run at 20Mhz.

Thnx again! Embarassed
grasspuddle



Joined: 15 Jun 2006
Posts: 66

View user's profile Send private message

PostPosted: Tue Jun 26, 2007 6:39 am     Reply with quote

Is your for loop suppose to be empty?

Quote:
Code:

void main()
{
   char ch;
   int checksum1, checksum2, index;

   set_tris_b( 0xFF );
   port_b_pullups( 1 );
   
  for(;;)
   {
      ch = getch(GPS);
[/b]
TheRidDler



Joined: 13 Jun 2007
Posts: 7

View user's profile Send private message

PostPosted: Tue Jun 26, 2007 8:42 am     Reply with quote

Hey, thnx for the reply. Yes, that part of the code is working fine, the problem lies somewhere in the RF section... Laughing
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 26, 2007 1:16 pm     Reply with quote

Quote:
enable_interrupts(int_TIMER3);
enable_interrupts(global);

You're enabling interrupts, but you don't have an #INT_TIMER3
interrupt service routine. This means your program will crash
the first time it gets a Timer3 interrupt.


Quote:
set_tris_a(0x00);

You're setting the TRIS, but you haven't specified #use fast_io(a) mode.
The default mode of the compiler is "standard io" mode. In that mode
it sets the TRIS for you. Because the compiler is in standard i/o mode,
the library code and the CCS pin i/o functions will override your
set_tris_a(0x00) statement as required.


I didn't check the rest of your code because there's just too much of it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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