Sending RC5 using mBed

21 Dec 2010

Hi everybody

I am trying to send RC5 codes using my mBed. Bits are transmitted using a 36kHz carrier. To get this carrier, I used a ticker and set it to a time of 14uS. I attach or detach this carrier to send bits. I use the following code, but it doesn't work...

Does anyone have a clue what I'm doing wrong?

#include "mbed.h"

DigitalOut RC5led(p22);
Ticker RC5carrier;

//TV Address: 0
//Code 0-9: buttons 0-9

void toggleRC5led() { //function that toggles the led for the 36KHz carrier
    RC5led=!RC5led;
}
void RC5sendbit(int rc5bit) { //Function to transmit a bit
    //36kHz: ((1 / 36 000) * (10^6)) / 2 = 13.8888889µS
    if (rc5bit == 0) {
        //Logic 0: 36kHz Carrier 889µS on, 889µS off
        RC5carrier.attach_us(&toggleRC5led, 14);
        wait_us(889); //Wait 889µS
        RC5carrier.detach();
        wait_us(889);
        RC5led = 0;
    } else {
        //Logic 1: Carrier 889µS off, 889uS on
        wait_us(889);
        RC5carrier.attach_us(&toggleRC5led, 14);
        wait_us(889);
        RC5led = 0;
    }
}

void sendRC5code(int togglebit, unsigned int adres, unsigned int commando) {
    int copyadres;
    int copycommando;
    RC5sendbit(1); //startbit 1
    RC5sendbit(1); //extra commandbit, leave it 1
    //5 data bits from the adres:
    adres = adres & 0x1F; //1F = b00011111
    copyadres = (adres & 0x10) >>4;
    RC5sendbit(copyadres);
    copyadres = (adres & 0x08) >>3;
    RC5sendbit(copyadres);
    copyadres = (adres & 0x04) >>2;
    RC5sendbit(copyadres);
    copyadres = (adres & 0x02) >>1;
    RC5sendbit(copyadres);
    copyadres = adres & 0x01;
    RC5sendbit(copyadres);
    //6 data bits from the command:
    commando = commando & 0x3F; //3F = b00111111
    copycommando = (commando & 0x20) >>5;
    RC5sendbit(copycommando);
    copycommando = (commando & 0x10) >>4;
    RC5sendbit(copycommando);
    copycommando = (commando & 0x08) >>3;
    RC5sendbit(copycommando);
    copycommando = (commando & 0x04) >>2;
    RC5sendbit(copycommando);
    copycommando = (commando & 0x02) >>1;
    RC5sendbit(copycommando);
    copycommando = commando & 0x01;
    RC5sendbit(copycommando);
}

int main() {
    int togglebit;
    while(1) {
        togglebit = 1;
        sendRC5code(togglebit, 0, 5); //Send command 5 to address 0
        wait(0.5);
        togglebit = 0;
        sendRC5code(togglebit, 0, 6); //Send command 6 to address 0
        wait(0.5);
    }
}

Thanks in advance!

22 Dec 2010

It looks like a line of code got dropped from sendRC5code(). The "togglebit" part of the preamble isn't being sent. Shouldn't the code be:

void sendRC5code(int togglebit, unsigned int adres, unsigned int commando) {
    int copyadres;
    int copycommando;
    RC5sendbit(1); //startbit 1
    RC5sendbit(1); //extra commandbit, leave it 1
    RC5sendbit(togglebit); // toggle bit finishes preamble
    //5 data bits from the adres:
    adres = adres & 0x1F; //1F = b00011111
    etc...

An alternative approach, by the way, would be to use a PWM output set to 36KHz. You could then just set the duty cycle
to zero to turn off the IR signal, and set it to 50% (or, better yet, 33% per the spec) to turn the IR signal on.
It seems like that would have less overhead than attaching and detaching a ticker function. Just a thought.
22 Dec 2010

Hmm... You're right... A hardware PWM function would do better than some software emulated things. I'll try your changes and post the results. Thanks!