ERICOO
Joined: 13 Jun 2011 Posts: 14 Location: NIGERIA
|
SIRC decoding |
Posted: Sat Apr 21, 2012 4:14 am |
|
|
Somebody out there please help. I had been trying to decode Sony TV remote control all to no avail. I have read so much on this on the internet, but the theory seem far different from the practice. I used pic kit2 to analyze the data. It was ok, start 2.52 ms, short bit = 600,640 us. I found a code written in MikroC which I translated to CCS PCW with little modification. But the Timer1 readings are not as expected. What am I doing wrong ? Someone please help. Here is the code:
Code: |
#include <16F887.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#include <Flex_LCD.c>
#BYTE PIR1 = 0x0c
//#BIT INTF = 1
unsigned input_data, bit_count,i;
int16 CAPT = 0,CAPT1 = 0,CAPT2=0;
enum {
Idle,
Start_bit,
Capture_bit
};
char Current_state = Idle;
char got_data = 0;
char Command_code, Device_code;
#int_ext // Interrupt name
void interrupt(){
if(interrupt_active(int_ext)){
switch (Current_state){
case Idle:
set_timer1(0);
ext_int_edge(L_TO_H); //interrupt on rising edge.
Current_state = Start_bit;
break;
//found the rising edge, check lenght for 2.4ms
case Start_bit:
CAPT = get_timer1();
set_timer1(0);
//correct signal, move on to next state
if((CAPT>=2000)&&(CAPT>=2800)) {
//output_high(PIN_B1);// this pin will only go high when //CAPT is b/w 44520 and 44590, dont know why
bit_count = 0;
input_data = 0;
Current_state = Capture_bit;
} else {
//fault signal, reset to Idle
Current_state = Idle;
}
break;
case Capture_bit:
//check plus length for 0 or 1, 0 = 1.2ms and 1 = 1.8ms
CAPT1 = get_timer1();
set_timer1(0);
if((CAPT>=900)&&(CAPT<=1500)){
//output_high(PIN_B2);// this pin will only go high when //CAPT is b/w 44520 and 44590, dont know why
input_data >>= 1; // add 0 to received data
bit_count++;
}else {
if((CAPT>=1500)&&(CAPT<=2100)){
//output_high(PIN_B3);// this pin will only go high when //CAPT is b/w 44520 and 44590, dont know why
input_data >>= 1;
input_data |= 0x8000; //add 1 to received data
bit_count++;
} else {
//error occurs, reset to Idle state
ext_int_edge(H_TO_L); //interrupt on falling edge.
Current_state = Idle;
}
}
//compleat 12 bit
if(bit_count >= 12){
got_data = 1;
input_data >>= 4;
ext_int_edge(H_TO_L); //interrupt on falling edge.
Current_state = Idle;
}
break;
default: Current_state = Idle;
}
clear_interrupt(int_ext); //clear interrupt flag.
}
enable_interrupts(int_ext);
enable_interrupts(global);
}
//********************************************
void main(){
//set_tris_b(0x01);
enable_interrupts(int_ext); // Enable named interrupt
clear_interrupt(int_ext);
ext_int_edge(H_TO_L); // Interrupt signal polarit
enable_interrupts(global); // Enable all interrupts
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
set_timer1(0);
lcd_init();
printf(lcd_putc"\f");
// lcd_gotoxy(11,1);
lcd_putc ("IR DECODING");
lcd_gotoxy(1,2);
lcd_putc ("Command:");
//**********************************************************//// Introduced this section of the code to test timer1 and crystal
// if they are functioning properly using PIC KIT2 the result was OK
//*********************************************************** for(i=0;i<4;i++){
set_timer1(63136); // Timer1 interrupt flag will set in 2.4 ms.
output_low(PIN_B4); // this pin was connected to PIC KIT2 for the test
//result of test gave 2.38 ms @ 40 ms window
while(!(PIR1&(1<<0))){}// wait until interrupt flag is set
output_high(PIN_B4);
clear_interrupt(int_timer1);
delay_ms(1);// test result gave 1.04 ms, @40ms window
}
//***********************************************************//***********************************************************
while(1)
{
//***********************************************************//I included this section to show the detected times which happens to differ from my expectations
lcd_gotoxy(12,2);
printf(lcd_putc,"%lu",CAPT);
delay_ms(2000);
lcd_gotoxy(12,2);
printf(lcd_putc,"%lu",CAPT1);
delay_ms(2000);
lcd_gotoxy(12,2);
printf(lcd_putc,"%lu",CAPT2);
delay_ms(2000);
//*********************************************************** if(got_data){
printf(lcd_putc"\f");// clear LCD
// lcd_gotoxy(11,1);
lcd_putc ("IR DECODING");//print "IR DECODING"
lcd_gotoxy(1,2);
lcd_putc ("Command:");// print "Command:"
Command_code = input_data & 0x7F;//extract command code from input_data
Device_code = input_data >> 7;//get Device code from input data
got_data = 0;
if(Device_code == 1){
switch (Command_code){
case 0:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
case 1:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
case 2:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
case 3:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
case 4:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
case 5:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
case 6:
lcd_gotoxy(12,2);
printf(lcd_putc,"%U",Command_code); break;
}
}
}
}
} |
|
|