Hi All,
I am trying to implement RC5 protocol in one of my IR based project .
I got a reference code based on MSP430G2553 ,kindly help me to explain the code or code flow is ok.
Please find below the the reference code i got from ti website for IR implementation using MSP430G2553.
#include "msp430G2553.h"
void Calculate_IR_code(void);
unsigned int timer0=0, deltat = 0;
unsigned char IR_Toggle;
unsigned char result[100];
unsigned char IR_address;
unsigned char IR_command;
unsigned char signal_IR_ready;
void main(void){
unsigned char i=0;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set DCO to calibrated 1 MHz.
DCOCTL = CALDCO_1MHZ;
P1OUT = 0;
P1DIR &= ~(BIT1|BIT2|BIT3);
P1DIR |= (BIT0|BIT4|BIT5|BIT6|BIT7);
P1SEL = 0x0;
P1SEL2 = 0x0;
P1SEL |= (BIT1|BIT2);
TA0CCR0 = 0;
TA0CTL = TASSEL_2 + ID_0 + MC_2 + TACLR;
TA0CCTL0 = CM_1 + CCIS_0 + SCS + CAP + CCIE;
TA0CCTL0 &= ~CCIFG;
_enable_interrupt();
while(1){
asm(" NOP");
if (signal_IR_ready==1){
Calculate_IR_code();
// Toggle GPIO11 if ON/OFF IR remote command has been received
if(IR_command == 12) P1OUT ^= BIT6;
// Prepare next run
for(i=0;i<100;i++)result[i] = 0;
signal_IR_ready=0;
}
}
}
void Calculate_IR_code(void){
// compute device address and command code
unsigned char i,ir_code=0;
unsigned char current_bit,prev_bit;
unsigned char error=0;
if (result[0] != 'P'){
error = 1; // sequence must start with a 'P'
}else{
current_bit = 1;
ir_code = (ir_code<<1)+current_bit;
prev_bit = current_bit;
for(i=1;result[i]!='P';i++) {
// short duration
if((result[i] == 'S') && (result[i+1] == 'S')){
current_bit = prev_bit;
ir_code = (ir_code <<1)+current_bit;
prev_bit = current_bit;
i=i+1;
}else{
// long duration duration, toggle bit
if (result[i] == 'L'){
current_bit = (~prev_bit) & 0x0001;
ir_code = (ir_code <<1)+current_bit;
prev_bit = current_bit;
}
}
}
}
IR_Toggle = (ir_code & 0x800)>>11;
IR_address = (ir_code & 0x7C0) >> 6;
IR_command = (ir_code & 0x3F);
return;
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void CCR0_ISR(void) {
deltat = TA0CCR0 - timer0;
timer0 = TA0CCR0;
static unsigned char begin_of_frame = 0; // switch
static unsigned char end_of_frame = 0;
static unsigned char result_idx = 0;
if (begin_of_frame == 0){ // not yet found
if (deltat > 5000){ // 5 ms as idle separator (RC-5 : 113ms)
begin_of_frame = 1;
result_idx = 0;
result[result_idx++] = 'P';
}
if(deltat < 1250) // 1.25 ms
result[result_idx++] = 'S'; // short width
else
result[result_idx++] = 'L'; // large width
}else{ // begin_of_frame == 1
if (end_of_frame == 0){ // not yet received
if (deltat < 1250){
result[result_idx++] = 'S';
}else{
if (deltat > 5000){
end_of_frame = 1;
result[result_idx++] = 'P';
}else{
result[result_idx++] = 'L';
}
}
}
}
if (end_of_frame == 1){
signal_IR_ready = 1;
end_of_frame = 0;
begin_of_frame = 0;
result_idx = 0;
}
P1OUT ^= BIT0;
TA0CCTL0 &= ~CCIFG;
return;
}