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 CCS Technical Support

simple receiver code

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



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

simple receiver code
PostPosted: Tue Aug 25, 2009 1:40 am     Reply with quote

Hey, I got a simple RF module and am trying to write code for it. I've written the transmitter code to send 0,0,0,STX,A,A,A,CRC,CR. I know it works fine because when I hook it up to my osc, I can see the signals being sent from the pic to the transmitter.

I also wrote code for the receiver, and it's meant to light up an led if the message was captured. when it didnt work, I broke the problem into smaller parts, and just added a few lines.

The code below is meant to light up an LED everytime the STX is encountered.
Code:

void main(){

set_tris_c(0b00100101);                         //setting input
setup_adc_ports( NO_ANALOGS );

set_tris_a(0b00000000);                         //set outputs
setup_adc_ports( NO_ANALOGS );


for(;;){                                       //forever

   InByte = fgetc(COM_A);          //store in InByte
   
   if(InByte == STX){                 //STX encountered

         output_high(PIN_A1); //light up LED on pin A1 for 3 sec
         delay_ms(1000);
         delay_ms(1000);
         delay_ms(1000);
         output_low(PIN_A1);
   
   }

} //end for loop

}


Is there something wrong with this code? I've tried to isolate the problem and I just can't seem to find where I'm going wrong.

When I hook the osc up to the receiver module, I can tell that its getting the signal. Its not exactly what was sent, but it does somewhat follow the pattern of the transmitted signal.

Any help/tips would be really useful,

Thanks
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 25, 2009 8:26 am     Reply with quote

I can't tell you what's wrong but how long do you think your led will remain in the off state with your code?

Looks like on for 3 seconds (which you can just use delay_ms(3000) instead of delay_ms(1000) three times), then led is off for a few instruction cycles since you have no delay after turning it off.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Aug 25, 2009 8:38 am     Reply with quote

It would help to know which PIC you are using. You don't show your #use rs232() statement or fuses. Personally, I would use the serial port hardward whenever trying to receive data. Do you have STX declared as 0x02 in your code? Setting the tris does no good unless you also use fast_io and you don't have that entered here.

Just a few things that I noticed.

Ronald
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

PostPosted: Tue Aug 25, 2009 9:53 am     Reply with quote

Hey thanks for the reply.

I am using a PIC 16F688 for both TX and RX.

Code:


#fuses XT, NOWDT, NOPROTECT,BROWNOUT, PUT
#use delay (clock = 4000000)
//------------------------------
#define WireTX PIN_C4
#define WireRX PIN_C5
//------------------------------
#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, STREAM=COM_A)

int InByte;

void main(){

for(;;){                            //forever

   InByte = fgetc(COM_A);          //store in InByte
   
   if(InByte == STX){              // STX encountered

   
         output_high(PIN_C1); //light up LED on pin A1 for 3 sec
         delay_ms(3000);
         output_low(PIN_C1);
         delay_ms(3000);
   
   }
   

} //end for loop



The STX has been declared in ascii.h as 0x02. The code itself is longer, but I'm trying to isolate the problem.

As for the LED remaing off, I presumed it would turn off for the few intruction cycles and then come on again. But in this case, it doesn't come on at all. I've connected the osc to the rx and tx, and it seems like the message is somewhat being captured.

I'll have to keep playing around I guess. Maybe put a longer preamble.

But am I on the right track in terms of search for the STX?

I've attached my TX code for reference if need be.

Code:


#include <16F688.h>
#include <ascii.h>

//-------------------------------------------------------------------------------
#define WireTX PIN_C4 //
#define WireRX PIN_C5
//-------------------------------------------------------------------------------
#fuses XT, NOWDT, NOPROTECT,NOBROWNOUT, PUT
#use delay (clock = 4000000)
#use rs232(baud=2400,xmit=WireTX , rcv=WireRX , STREAM=COM_A )

int CRC = 85;

void main()
{

set_tris_c(0b00000101);                         //set rco & rc2 as input
setup_adc_ports( NO_ANALOGS );                  //set RC0 & rc2 to digital input

   for(;;){     

         if(input(PIN_C2)==0)                   //if button pressed
         {
       
                output_high(PIN_A1);            //output high Led
                delay_ms(20);
               
                  fputc(NUL,COM_A);
                  fputc(NUL,COM_A);
                  fputc(NUL,COM_A);
                  fputc(STX,COM_A);
                  fputc('A',COM_A);
                  fputc('A',COM_A);
                  fputc('A',COM_A);
                  fputc(CRC,COM_A);
                  fputc(CR,COM_A);
 
                delay_ms(20);
                delay_ms(1000);                 //delay some ms
                output_low(PIN_A1);             // output low led
                delay_ms(1000);
         }
         
         else{     
               
               output_high(PIN_A1);  // if button not pressed then just on off led on pin D1
               delay_ms(50);           
               output_low(PIN_A1);
               delay_ms(50);
         }
   }
}



Thanks
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Aug 25, 2009 2:37 pm     Reply with quote

Maybe try using the serial ISR and see if it will work.

Code:
#fuses HS, NOWDT, NOPROTECT,BROWNOUT, PUT
#use delay (clock = 4000000)
//------------------------------
#define WireTX PIN_C4
#define WireRX PIN_C5
//------------------------------
#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, STREAM=COM_A, ERRORS)

int InByte;
int1 execute = 0;

#int_RDA
RDA_isr()
{

  InByte = fgetc(COM_A);          //store in InByte
  if(InByte == STX){              // STX encountered
    execute = 1;
  }

}



void main(){

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);


for(;;){                            //forever

       
   if(execute){              // STX encountered

         execute = 0;
         output_high(PIN_C1); //light up LED on pin A1 for 3 sec
         delay_ms(3000);
         output_low(PIN_C1);
         delay_ms(3000);
   
   }
   

} //end for loop


I haven't tried this out but it might work.

Ronald
hobby_85



Joined: 17 Aug 2009
Posts: 50

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 8:18 am     Reply with quote

hey ronald..i tried your code and no luck.

it had the same problem as mine. It lit once and thats it. When i hooked it up on the oscilloscope, i found that the receiver DOES receive the message. somewhat..it follows the signal pretty well. but the led does not light up constantly

any suggestions?

thanks man
mmprestine



Joined: 13 Jan 2004
Posts: 29
Location: Green Bay, Wisconsin

View user's profile Send private message

PostPosted: Wed Aug 26, 2009 8:40 am     Reply with quote

Good grief! Use google and do some research, these modules have been used many times by others.

It just so happens that I have used these exact modules and have had the exact issue. Turns out that I was using a fairly large bread board and that gave me all kinds of grief with these RF modules. All the capacitance that bread boards have was the issue I believe. I have the system working very well with two separate very small bread boards, separate supplies, and proper grounding on the uC and module. I would recommend testing without antennas to reduce your chances of picking up other signals on the same freq. since they are widely used. And finally send a string of 0xBA,0xBE,0xFA,0xCE (BABE FACE)to let the receiver to lock on and stabilize before sending good data. This is known as LAM (Loc On Module).

Here is some PIC code I found somewhere that works well.

Receiver:
Code:

int8  rx_wr_index = 0,
      tx_rd_index = 0,
      tx_wr_index = 0,
      tx_counter = 0,
      received = 0;

int8  lock_state = 0,
      rxd,
      i,
      valid_data_count;

unsigned int8  rx_buffer[RX_BUFFER_SIZE + 1],
               tx_buffer[TX_BUFFER_SIZE + 1];

int1  data_avail = FALSE,
      got_id = FALSE;

// Setup RS232
#use rs232(baud=2400, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=Wireless)
#use rs232(baud=9600, xmit=ConsTX, rcv=ConsRX, ERRORS, STREAM=Console)

#int_RDA
void RDA_isr(void) {

   rx_buffer[rx_wr_index] = getc(Wireless);

   rxd = rx_buffer[rx_wr_index]; // this just makes it easier typing-wise later on

   rx_wr_index++;

   if (rx_wr_index > RX_BUFFER_SIZE) {
      rx_wr_index = 0;
   }

   // now look for unique ID: "Dave "
   if (rxd == 'D' && lock_state == 0) {
      lock_state++;
   }

   else if (rxd == 'a' && lock_state == 1) {
      lock_state++;
   }

   else if (rxd == 'v' && lock_state == 2) {
      lock_state++;
   }

   else if (rxd == 'e' && lock_state == 3) {
      lock_state++;
   }

   else if (rxd == ' ' && lock_state == 4) { // got the entire string "Dave ", in that order

      lock_state = 0; // reset our "combination lock"

      got_id = TRUE;

      valid_data_count = 0xff; // get ready to count the number of data bytes - we know we have to expect 5 (Rocks)

      rx_wr_index = 0; //also going to reset the buffer write index back to 0, so that I know where my valid data will be
   }

   else { // we didn't receive "Dave ", so reset the lock back to the beginning
      lock_state = 0;
   }

   if (got_id && ++valid_data_count == 5) {
      data_avail = TRUE;
      got_id = FALSE;
   }
}

void main() {

   while (TRUE) {

      if (data_avail) {

         data_avail = FALSE;

         fprintf(Console,"\r\n\r\nData is now available\r\nData: ");

         for (i = 0; i < 5; i++) {
            fprintf(Console,"%c",rx_buffer[i]);
            printf(lcd_putc,"%c",rx_buffer[i]);
         }
      }
   }
}



Sender:

Code:

void main(void)
{
   while(TRUE)
   {
      fprintf(Wireless, "%c", 0xBA);   // LAM - something for the RX's USART to "lock onto"
      fprintf(Wireless, "%c", 0xBE);   // LAM - something for the RX's USART to "lock onto"
      fprintf(Wireless, "%c", 0xFA);   // LAM - something for the RX's USART to "lock onto"
      fprintf(Wireless, "%c", 0xCE);   // LAM - something for the RX's USART to "lock onto"
      fprintf(Wireless,"Dave Nice\r"); // I'm going to get the RX code to look for "Dave " as the ID and "Rocks" as the command/data
      delay_ms(5);
   }
}


Hope this helps.
Matt
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