baileychic
Advanced Member level 3
SIRC Decoding not working. What is the problem. Find the attached circuit. This is the code of hexreader. I found this code at libstock and mikroe forum. I modified it for PIC12F683 but decoder is not working.
Transmitter code.
Receiver code.
Transmitter code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 sbit SW1 at GP0_bit; sbit SW2 at GP1_bit; sbit SW3 at GP3_bit; sbit IR_Encode_Pin at GP2_bit; unsigned int irCode = 0; unsigned char flagRegister = 0; sbit startPwmFlag at flagRegister.B0; void sendIRData(unsigned int irData) { unsigned char i = 0; unsigned int mask = 0x02; IR_Encode_Pin = 1; //PWM1_Start(); Delay_us(2400); IR_Encode_Pin = 0; //PWM1_Stop(); Delay_us(600); for(i = 0; i < 12; i++) { if(irData & mask){ IR_Encode_Pin = 1; //PWM1_Start(); Delay_us(1200); IR_Encode_Pin = 0; //PWM1_Stop(); } else { IR_Encode_Pin = 1; //PWM1_Start(); Delay_us(600); IR_Encode_Pin = 0; //PWM1_Stop(); } Delay_us(600); mask <<= 1; } Delay_ms(45); } void main(){ asm clrwdt OPTION_REG = 0x8F; CMCON0 = 0x07; ANSEL = 0x00; TRISIO = 0b00001011; GPIO = 0x00; Delay_ms(100); //PWM1_Init(38000); //PWM1_Set_Duty(127); while(1){ asm clrwdt if(SW1 == 0){ Delay_ms(50); if(SW1 == 0){ irCode = 0b1010001100101; //0110010 10100 startPwmFlag = 1; } } else if(SW2 == 0){ Delay_ms(50); if(SW2 == 0){ irCode = 0b1110001100111; startPwmFlag = 1; } } else if(SW3 == 0){ Delay_ms(50); if(SW3 == 0){ irCode = 0b1111001101001; startPwmFlag = 1; } } if(startPwmFlag){ sendIRData(irCode); startPwmFlag = 0; } } }
Receiver code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 sbit LED1 at GP0_bit; sbit LED2 at GP1_bit; sbit LED3 at GP4_bit; //sbit IR_Decode_Pin at GP2_bit; sbit ir_in at GP2_bit; // Infrared receiver input sbit ir_in_direction at TRISIO2_bit; // Infrared receiver input // global variables char fb_string[10]; // just a place to convert numbers to strings char irbyte; // assemble a byte of IR char cmd; // current command character from UART or PORTD unsigned int cnt; // expands 16 bit counter to 32 bit - not used here unsigned int retint; // general purpose integer //char keephex[3]; // store hex value as ascii unsigned char ir_byte; char ir_toggle; // toggle bit value unsigned char ir_result[11]; char mmc_zeros; void sony_print(void); int get_mark(void); int get_space(void); unsigned char get_sony_byte(char nbits); //Timer1 //Prescaler 1:1; TMR1 Preload = 64936; Actual Interrupt Time : 600 us void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xFD; TMR1L = 0xA8; TMR1IE_bit = 1; } void interrupt() { // interrepts are here, but serve no function yet if((TMR1IE_bit) && (TMR1IF_bit)) { // handle timer 1 overflow cnt++; // Increment counter - gives extra 2 bytes counter option TMR1IF_bit = 0; // Clear TMR1IF TMR1H = 0xFD; TMR1L = 0xA8; } if((INTE_bit) && (INTF_bit)) { // handle interrupt on B0 pin INTF_bit = 0; // clear interrupt INTEDG_bit = ~INTEDG_bit; } } /* // Sony Remote 40kHz 7 bits command (LSB first) then 5 bits address // there are also 15 bit and 20 bit versions of Sony protocol // // 40kHz 7 bits command (LSB first) then 5 bits address // 2.4ms mark=start // 0.6sp, 0.6 mark = 0 // 0.6sp, 1.2 mark = 1 // // tv address=01 // vcr1 address=02 // vcr2 address=03 // radio/cd address=04 // laser disc address=06 // surround sound address=0c // tuner address=0d // amp address=10 (theatre / cassette / tuner) // cd player address=11 // equaliser address=12 // HDMI control address=17 // dvd address=1a // print unsigned char as hex void hexout(unsigned char hexval){ int temp_hex; char hextxt[7]; temp_hex = hexval; // to integer IntToHex(temp_hex, hextxt); // integer to ASCII hex keephex[0] = hextxt[2]; // store ascii hex for later keephex[1] = hextxt[3]; keephex[2] = 0; // null terminate } // print out Sony values void sony_print(void){ signed int temp_ir; while(1){ while(ir_in); // wait for IR to go low temp_ir = get_mark(); if ((temp_ir > 130) && (temp_ir < 200)){ // expect 2.4mS mark ir_result[0] = get_sony_byte(7); // 7 bit command ir_result[1] = get_sony_byte(5); // 5 bit address Lcd_Out(2,1, "cmd= addr= "); // Print text to Lcd, 2nd row, 1st column hexout(ir_result[0]); Lcd_Chr(2,5,keephex[0]); Lcd_Chr(2,6,keephex[1]); hexout(ir_result[1]); Lcd_Chr(2,13,keephex[0]); Lcd_Chr(2,14,keephex[1]); Delay_ms(10); // wait for repeat messages to clear } } } */ void sony_print(void){ signed int temp_ir; while(1){ while(ir_in); // wait for IR to go low temp_ir = get_mark(); if ((temp_ir > 130) && (temp_ir < 260)){ // expect 2.4mS mark ir_result[0] = get_sony_byte(7); // 7 bit command ir_result[1] = get_sony_byte(5); // 5 bit address Delay_ms(10); // wait for repeat messages to clear if(ir_result[0] == 0b1010001) { LED1 = ~LED1; } else if(ir_result[0] == 0b1110001) { LED1 = ~LED1; } else if(ir_result[0] == 0b1111001) { LED1 = ~LED1; } ir_result[0] = 0; } } } // get one byte of Sony unsigned char get_sony_byte(char nbits){ char n; unsigned char ir_byte; signed int temp_ir; ir_byte = 0; // initialise for(n = 0; n < nbits; n++){ temp_ir = get_space(); // expect ~510uS temp_ir = get_mark(); // 650uS = 0, 1.2uS = 1 ir_byte >>= 1; if(temp_ir > 60) { // ir_byte += 0x80; } } ir_byte >>= 8 - nbits; // convert 5/7 bits to 8 return ir_byte; } // get IR mark time (low) int get_mark(void){ TMR1H = 0; TMR1L = 0; while(!ir_in); retint = TMR1L; retint += (unsigned int)TMR1H << 8; return retint; } // get IR space time (high) int get_space(void){ TMR1H = 0; TMR1L = 0; while(ir_in); // wait for IR to go low retint = TMR1L; retint += (unsigned int)TMR1H << 8; return retint; } void main(){ asm clrwdt OPTION_REG = 0x8F; CMCON0 = 0x07; ANSEL = 0x00; TRISIO = 0b00000100; GPIO = 0x00; Delay_ms(100); InitTimer1(); INTE_bit = 1; PEIE_bit = 1; GIE_bit = 1; cnt = 0; while(1) { asm clrwdt sony_print(); } }