Friday 22 July 2011

SIRC Part III - Encoding and Decoding


In this post, We examine sample codes for encoding and decoding using Arduino.
Encoding:
     The frequency of the carrier signal to be sent is 40 KHz. Hence, it takes 25 micro-seconds for one cycle. Each cycle consists of ON and OFF time. The duty cycle denotes the ratio between ON time and Time Period. The duty cycle recommended by SIRC protocol is 1/4 or 1/3. Here we use a duty cycle of 25% (1/4). Hence, the number of cycles required to complete each type of bit is listed below:
              Start bit: ( 2400(uS)/25(uS) ) = 96 cycles
               '1' bit : ( 1200/25 ) = 48 cycles
               '0' bit : ( 600/25 ) = 24 cycles
A sample program for encoding the signal is given below:

void setup()
{
           Serial.begin(9600);
           pinMode(14,OUTPUT);      // IR LED is connected to pin14
}
int sum=0;
void transmit(int command)
{
        // Here we dont add the start bit to the array
        //Array arr[] represents just the data bits
        // We create the start pulse manually using pulse(int) function
        // We declare an array containing all the data bits (arr[])
        // i--> index variable
        int arr[12],i=0;
        // Here we convert the decimal value of command to binary value,
        // And store it in the array arr[]
        // From LSB to MSB
        while(command>0){

                    arr[i]=command%2;
                    command/=2;                    
                    i++;
        }
        // Now we have converted the decimal to binary
        // But we need to fill the empty digits to '0'
       for( i ; i<7 ; i++ )

               arr[i]=0;

              //Adding the address bits
              // For testing purpose, we use TV, address for TV is 10000 (5 bit data)
              // Now value of i is 7
              // Which represents the 8th position
              arr[i]=1;
              i++;
             // Now value of i is 8
             // Which represents the 9th position (9,10,11,12)--> 4 iterations

            for( i ; i<12 ; i++ )
                  arr[i]=0;
                  // Last 5 bits now look like 10000 which denotes address of TV
                  //Manually pulsing the start bit (2400 uS)
                  pulse(96);              //96*25=2400;
                 //Pulsing all the data bits
                 for(i=0;i<12;i++) {
                       if(arr[i]) {
                              pulse(48);
                       } else {
                             pulse(24);
                       }
                 }
               //The total time of one set of data (one frame) is 45 mS (ie) 45000 uS                  
               digitalWrite(14,0);
               delayMicroseconds((45000-sum));
}

// The pulse function for pulsing bits
// 25% duty cycle
// followed by 600 uS space
void pulse(int dur)
{
               for(int j=0;j<dur;j++) {

                      digitalWrite(14,1);
                      delayMicroseconds(6);
                      digitalWrite(14,0);
                      delayMicroseconds(19);
               }
              delayMicroseconds(600);
}
void loop()
{
          transmit(21);
          transmit(21);
          transmit(21);
          delay(3000);
}


Decoding:
          Decoding is relatively easier than encoding. A sample coding which is self-explanatory is given below:


void setup()
{
          Serial.begin(9600);       //Setting Baud Rate
          pinMode(15,INPUT);    //IR TSOP receiver is connected to PIN 15                   
}

int remote()
{
            // i--> index variable
            // arr[]--> array that stores digital word
            // val--> stores the decimal format of arr[]
            int i=0,arr[8],val=0;
            //Checking the start bit
            if(pulseIn(15,0)>2000) {
                  //Storing the digital word in arr[]
                  for(i=0; i<7 ; i++,arr[i] = pulseIn(15,0)) ;
                  //Converting digital word into decimal value
                  //Storing it in val
                    for(i=0;i<7;i++) {
                           if(arr[i]>1000) {
                                   val=val+(1<<i);
                           }
                    }
            }
           return val;
}
void loop()
{
            int checkVal=remote();
            if(checkVal);
            Serial.println(checkVal);
}

Coming up next:

          * Practical model

          * Building a project ( An Universal Remote)


5 comments:

excellent job. appreciate it. waiting for universal remote !!!

Ya nice info. How to specifically operate a device with universal remote. whether they have any address

@Guruprasath:

Any device that follows SIRC protocol can be controlled......

The remote should be programmed to work in different possible modes based on devices to be controlled.......

and all the devices can be controlled by switching to different modes ( to avoid interference )......

I'm running on a tight schedule......
The universal remote will be ready on
OCT15-20.......

@ Guruprasath:

The last 5bits of the digital word transmitted represent the unique address of each device........
The addresses of different devices are listed out in the link below; Refer the link for more info.......

http://yengal-marumugam.blogspot.com/2011/06/sirc-part-i-basics.html

Post a Comment

தங்களது கருத்துக்களை இங்கே வெளியிடவும்...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More