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

RF module code

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jack// ani
Guest







RF module code
PostPosted: Sat Nov 12, 2005 1:05 am     Reply with quote

Hi all,

I need codes for data transmission....between PIC uC, connected to 433MHz RF module. Basically I want to control a motor...the Tx module will send the commands and RX will act accordingly. Please help me......

Thanks a lot
specialk



Joined: 12 Nov 2005
Posts: 27

View user's profile Send private message

PostPosted: Sat Nov 12, 2005 5:11 pm     Reply with quote

Look up "manchester encoding" to find information on wireless data transmissions.

-special [k]
jack// ani
Guest







PostPosted: Mon Nov 14, 2005 11:13 am     Reply with quote

Thanks......I cannot get any well working codes. I'll appreciate a lot if anyone could please share his codes here.....please.

thanks a lot
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Mon Nov 14, 2005 12:03 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=23953
jack// ani
Guest







PostPosted: Tue Nov 15, 2005 9:44 am     Reply with quote

Hi newguy,

Thanks for that. I understood whole of the codes....except two of these lines....please explain its purpose

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

and

if (got_id && ++valid_data_count == 5)

thanks

Source code for the reciever is.....

Code:


#include <18F8621.h>
#device *=16 ADC=8
#define Fosc 40000000
#define WireTX PIN_C6
#define WireRX PIN_C7
#define ConsTX PIN_G1
#define ConsRX PIN_G2
#use delay(clock = Fosc,RESTART_WDT)
#fuses EC_IO, BROWNOUT, BORV20, PUT, STVREN, NOLVP
#use rs232(baud=9600, xmit=WireTX, rcv=WireRX, ERRORS, STREAM=Wireless)
#use rs232(baud=9600, xmit=ConsTX, rcv=ConsRX, ERRORS, STREAM=Console)             //Setup RS232

#define RX_BUFFER_SIZE 80
#define TX_BUFFER_SIZE 80

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;

#int_RDA
void RDA_isr(void) {
   rx_buffer[rx_wr_index] = getc();
   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)
      // also going to reset the buffer write index back to 0, so that I know where my valid data will be
      rx_wr_index = 0;
   }
   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;
   }
}

#int_TBE
void TBE_isr(void) {
   if (tx_counter != 0) {
      putc(tx_buffer[tx_rd_index]);
      if (++tx_rd_index > TX_BUFFER_SIZE) {
         tx_rd_index = 0;
      }
      tx_counter--;
      if (tx_counter == 0) {
         disable_interrupts(INT_TBE);
      }
   }
}

void bputc(int c) {
   int restart = 0;

   while (tx_counter > (TX_BUFFER_SIZE - 1));

   if (tx_counter == 0) {
      restart = 1;
   }
   tx_buffer[tx_wr_index++] = c;
   if (tx_wr_index > TX_BUFFER_SIZE) {
      tx_wr_index = 0;
   }
   tx_counter++;
   if (restart == 1) {
      enable_interrupts(INT_TBE);
   }
}

void main() {

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_ON);
   setup_timer_0(RTCC_INTERNAL|RTCC_OFF|RTCC_8_bit);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   enable_interrupts(INT_RDA);
   //enable_interrupts(INT_TBE);
   enable_interrupts(global);

   set_tris_e(0);
   fprintf(Console,"working");
   while (TRUE) {
      restart_wdt();
      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]);
         }
      }
   }
}

newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Tue Nov 15, 2005 10:08 am     Reply with quote

jack// ani wrote:
Hi newguy,

Thanks for that. I understood whole of the codes....except two of these lines....please explain its purpose

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

and

if (got_id && ++valid_data_count == 5)

thanks



valid_data_count is what its name implies, just a count of the valid characters. In this simple example, the valid data is "Rocks" (minus the quotes). I initially set valid_data_count to 0xff, which is 255 because valid_data_count is an 8 bit integer.

Now the next line, if (got_id && ++valid_data_count == 5) executes. got_id is now true, and "++valid_data_count" means first increment valid_data_count (255 wraps to 0), and then check to see if the new value of valid_data_count is equal to 5. If both of these conditions are true, we've received the 5 bytes of the message we're expecting.

Now that I think of it, it is quite confusing. Would it help if I told you that you can change valid_data_count to be initialized to 0 (instead of 255), and change the "== 5" to "== 6". Might be a bit easier to follow.

Hope this helps.
jack// ani
Guest







PostPosted: Tue Nov 15, 2005 2:51 pm     Reply with quote

Thanks a lot.......you are very helpful. I really appreciate it a lot.

thanks again
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