RF Link Using a 16 bit PIC and UART

Implementing RF Link using UART

I’m going to talk about RF Links and how to implement them using very inexpensive transmitters and receivers. I’m going to start with the Linx modules. These are sold by Linx Technologies. They are simple to use and work rather well. An RF link will allow you to communicate with a device over a hundred feet or more depending on the conditions and the amount of obstacles in the way. This can be very convenient when you are unable to run wires to the device to communicate with it.

We are going to use a UART to transmit characters to the transmitter and a UART to receive characters from the receiver. But, we aren’t able to just send characters using the UART and expect the RF link to send these characters. Due to the RF link requiring a 0 DC bias we are going to encode the characters we send to Manchester encoding. This encoding will require an encoding process and a decoding process. To encode the data we can use the following routine:

void 
sendData(unsigned char txbyte)
{
    int i,j,b,me;
    
    b = txbyte;
        
    for (i=0; i<2; i++) { 
        me = 0;         // manchester encoded txbyte
        for (j=0 ; j<4; j++) {
            me >>=2;
            if (b & 1)
                me |= 0b01000000; // 1->0
            else
                me |= 0b10000000; // 0->1
            b >>= 1;
        }
        uart1put(me);
    }
}

To decode the sent data we can use the following routine:

unsigned char 
decodeData(unsigned char encoded)
{
    unsigned char i,dec,enc,pattern;
    
    enc = encoded;
    
    if (enc == 0xf0)    //  start/end condition encountered
        return 0xf0;
    
    dec = 0;    
    for (i=0; i<4; i++) {
        dec >>=1;
        pattern = enc & 0b11;        
        if (pattern == 0b01)        // 1
           // bit_set(dec,3);
           dec |= 0x08;
        else if (pattern == 0b10)
           // bit_clear(dec,3);       // 0
           dec &= ~0x08;
        else 
            return 0xff;            // illegal code
        enc >>=2;
    }
    return dec;
}