PIC16f628 radio link

Discussion in 'Embedded Systems and Microcontrollers' started by cykrus, Oct 1, 2012.

  1. cykrus

    Thread Starter New Member

    Oct 1, 2012
    4
    0
    Hello all,

    I am very new to the micro-controller world so bare with me.

    I want to make a remote control to be able to send data between t PIC's (16f628). I know that there are already products out there (i have tested h12e/d rd600 d/e and they work great). My scope is to learn and play with PIC and not to use already made products :)

    Before this project i did the "flash led" type of tutorials to learn a little bit about PIC's. I have a picckit3 programmer and I'm using the PIC C compiler.

    Environment setup :
    I have 2 16f628 pic's and a set of 434 Mhz RX/TX. I also have a max 232
    connected to my serial port and receiver PIC in order to be able to debug data. At the hardware level have no problems setting it up.

    Software setup:

    The idea is that I want to be able to send a data package (comming from input pins) from PIC1(connected to the TX 434) to PIC2(connected to rx 434).

    An here comes the code I have already wrote. Again ... this is my first real program so bare with me :)

    DECODER

    Code ( (Unknown Language)):
    1.  
    2. #include <16F628.h>
    3. #fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
    4. #use delay(clock=4000000)
    5. #use rs232(baud=19200, xmit=pin_B2, rcv=pin_B1)
    6.  
    7.  
    8. void display_binary(int c){
    9. int i;
    10.    i = 8;
    11.    do
    12.      {
    13.        if( c & 0x01 )
    14.          { putc('1'); }
    15.        else
    16.          { putc('0'); }
    17.  
    18.        c >>= 1;  i--;
    19.      } while(i);
    20. }
    21.  
    22.  
    23. int sync(int a){
    24.    int found;
    25.    int string;
    26.    int bit;
    27.    
    28.    int original;
    29.    
    30.    original = a;
    31.    
    32.    do{
    33.       a = original;
    34.       found = 0; // found the string
    35.       string = 0x00;
    36.          
    37.       do{
    38.            if( input(PIN_A0) ){
    39.                bit = 0x01;
    40.            }
    41.            else{
    42.                bit = 0x00;
    43.            }
    44.            string = string | bit;
    45.            string <<= 1;                    
    46.            
    47.            if( ((a & 0x01) ^ bit ) == 0 ){ // right bit == input
    48.                a >>= 1; // shift 1 position right
    49.            }
    50.            else{
    51.               return 0; // error
    52.            }
    53.            
    54.            if( a == 0x00){
    55.                 found = 1;
    56.            }
    57.            
    58.            delay_us(500); // wait until next bit
    59.            
    60.       }while( found == 0 );  
    61.  
    62.   }while( found == 0 );
    63.  
    64.   return 1;
    65. }
    66.  
    67. int getdata(){
    68.    
    69.    int string = 0x00;
    70.    int i;
    71.    
    72.    for(i=0;i<8;i++){
    73.       string <<= 1;
    74.       if( input(PIN_A0) ){
    75.          string |= 0x01;
    76.       }
    77.       else{
    78.          string |= 0x00;
    79.       }  
    80.       delay_us(500);
    81.    }
    82.    
    83.    return string;
    84. }
    85.  
    86.  
    87. void main(void) {
    88.  
    89.    int prev, k, i;
    90.    unsigned int data;
    91.  
    92.    while(1) {
    93.      
    94.       k=0; // until i get 3 time the same result
    95.       do{
    96.      
    97.          i=0;
    98.          do{
    99.             if( sync(0xAB) == 1 ){
    100.                i++;
    101.                delay_us(500);
    102.             }
    103.             else{
    104.                i = 0;
    105.             }
    106.          }while(i<3);
    107.          
    108.          data = getdata();
    109.          
    110.          if( k > 0 && data != prev) k=1; // reset
    111.          else k++; // one more good result
    112.          
    113.          prev = data; // previous result used in the next loop
    114.          
    115.       }
    116.       while(k<3);
    117.      
    118.       if( data == -1 ) output_high(PIN_A2);
    119.       if( data == 63 ) output_high(PIN_A1);
    120.      
    121.       printf("Found %d = ", data);
    122.       display_binary(data);
    123.       printf("\r\n");
    124.      
    125.       delay_ms(250);
    126.       output_low(PIN_A2);
    127.       output_low(PIN_A1);
    128.    }
    129.  
    130. }
    131.  
    ENCODER

    Code ( (Unknown Language)):
    1.  
    2. #include <16F628.h>
    3. #fuses INTRC_IO, NOLVP, NOWDT, PUT, BROWNOUT
    4. #use delay(clock=4000000)
    5.  
    6. void send1(){
    7.    output_high(PIN_A0);
    8.    delay_us(500);
    9.    output_low(PIN_A0);
    10. }
    11. void send0(){
    12.    output_low(PIN_A0);
    13.    delay_us(500);
    14.    output_high(PIN_A0);
    15. }
    16.  
    17. void sendbyte(int x){
    18.    int i=8;
    19.    do{
    20.       if(x & 0x01){
    21.          send1();
    22.       }
    23.       else{
    24.          send0();
    25.       }
    26.       x >>= 1;
    27.       i--;
    28.    }while(i);
    29. }
    30.  
    31.  
    32. main() {
    33.  
    34.    while(true) {
    35.       if(input( PIN_B0) ){
    36.          // sync
    37.          sendbyte(0xAB);
    38.          delay_us(500);
    39.          sendbyte(0xAB);
    40.          delay_us(500);
    41.          sendbyte(0xAB);
    42.          delay_us(500);
    43.          sendbyte(0xFF);
    44.          delay_us(500);
    45.  
    46.       }
    47.       else if(input( PIN_B1) ){
    48.          // sync
    49.          sendbyte(0xAB);
    50.          delay_us(500);
    51.          sendbyte(0xAB);
    52.          delay_us(500);
    53.          sendbyte(0xAB);
    54.          delay_us(500);
    55.          sendbyte(0xFC);
    56.          delay_us(500);
    57.      }
    58.    }
    59. }
    60.  
    The idea is the encoder is sending 3 bytes (AB) as a sync (in order to align it with the RX) and then it send a byte of data depending on which pin B0 or B1 is high. The decoder searched for the 3 sync packages and then reads the byte (I'm reading it until i get 3 times the same value .. as i have experienced some errors due to radio noise i guess). Everything work so far but I'm sure it's not the perfect way to do it.

    My questions are :

    1. I have set a delay of 500us between bits and bytes as well. Is this correct ? What should be an optimal value ?
    2. I coded 2 function for sending 1 or 0 bits. Is there another way to send directly a full byte to a pic PIN ?
    2a. I have seen that in manchester it says that 1 is 0to1 transition and 1 is 1to0 ... does this mean that for sending a 1 I should do the following code ?
    Code ( (Unknown Language)):
    1.  
    2. output high;
    3. delay 250us;
    4. output_low;
    5. delay 250us
    6.  
    How do i read this data (from port) on the other end ?


    3. I have studied for 1 week a lot about the manchester encoding, i have understood how it works but i dont know how to apply it to hardware. Meaning how to I put the bits on the PORTS and how do i read them ? What is the delay between bits ? I searched a whole week but did not find any c sample of this ... only the manchester encoding process.

    4. Is the way that i'm reading data from 434RX correct ? I have setup on both tx and rx same timing (delays between bits) and this is the way i "sync" .. is there another way do to this ?

    Please feel free to correct/adjust my code and tell me what do I do wrong.

    I want to learn and experimenting is for me the best way to do this.

    Thanks,
    Vlad
     
  2. cykrus

    Thread Starter New Member

    Oct 1, 2012
    4
    0
    Another question just came into my mind. The way that i'm syncronising the encoder/decoder is my seting the same delay between bits. Now my question is this ... how much time does it take to execute operations between sending bits ? For example is the encoder has a code like

    Code ( (Unknown Language)):
    1.  
    2. output high
    3. delay 10us
    4. output low
    5. delay 10us
    6.  
    and the decoder has the code


    Code ( (Unknown Language)):
    1.  
    2. read input
    3. delay 10us
    4. <=== other 10 instructions come here
    5. read input
    6. delay 10us
    7.  
    How much does it take for the "10 instructions" to execute ... can that cause a delay and with the 10us added on top the second "input" would no longer be aligned with the encoder's timing ?

    How to solve this issue ... I suppose there is a way with the clock ? ... like in the so if we say "we'll meet there in 10 minutes" ... it is not so accurate as saying "we'll meet there at 2:50 PM" .. right ?
     
  3. cykrus

    Thread Starter New Member

    Oct 1, 2012
    4
    0
    OOps ... i did some more reading and it looks like my code reinvented the wheel :)))) ... sorry for that ... i just discovered ... "USART" ...

    It looks like my code for sending/receiving bits is a "stoneage" version of USART ...
     
  4. cykrus

    Thread Starter New Member

    Oct 1, 2012
    4
    0
    So .. after another day spent on forums i got the idea. I am using the UART and I have learner about all the registeres and bits for the rx buffer, error, etc etc. Great stuff ...

    So I managed to implement a fully working link with a 4 bit address and 8 bit (can be extended to 10 - two pins are still left as rx/tx for the wireless link ) data between 2 pic16f628.

    Next question just for fun ... can i save one more pin ? Is there a way when init the UART to only assign a pin (the tx) and rx to leave it undefined OR assigning it to some kind of NULL ?
     
Loading...