# PIC - Problem in NEC IR Decoding in PIC12F675

I'm using PIC12F675 with internal OSC running @ 4MHz. I'm using MikroC Pro compiler version 6.6.2.

To Decode NEC protocol, I'm not using Timer or Interrupt. I used delay command to make the code as simple as possible with time-out property and the code checks the IR signal with resolution of 50Âµs.

I'm using Timer 0 and Pin change interrupt for my work. And I need to decode IR signal with a normal way using the GP3(MCLR) pin.

My program is working fine with PIC16F877A which running with external OSC @ 16MHz. But when I using the same program for PIC12F675 it's not working for me.

I have disabled Pull-ups and MCLR pin function for PIC12F675. And using the GP3 pin as the input for IR receiver.

I'm using the example from here.

Below is my program for PIC16F877A. Please, can someone help me to solve this problem for PIC12F675?

Thank you

Chayan

/* Pin Configuration */
#define IR_PIN                RB0_bit       /**< IR Signal Reception Pin.*/
#define LED_PIN               RC5_bit

unsigned long ir_code;
unsigned short command, inv_command;

unsigned long count = 0, i;
// Check 9ms pulse (remote control sends logic high)
while ((IR_PIN == 0) && (count < 190)) {
count++;
delay_us(50);
}
if ( (count > 189) || (count < 120))     // NEC protocol?
return 0;

count = 0;
// Check 4.5ms space (remote control sends logic low)
while (IR_PIN && (count < 95)) {
count++;
delay_us(50);
}
if ( (count > 94) || (count < 30))       // NEC protocol?
return 0;

for (i = 0; i < 32; i++) {
count = 0;
while ((IR_PIN == 0) && (count < 14)) {
count++;
delay_us(50);
}
if ( (count > 13) || (count < 2))     // NEC protocol?
return 0;

count = 0;
while (IR_PIN && (count < 40)) {
count++;
delay_us(50);
}
if ( (count > 39) || (count < 4))     // NEC protocol?
return 0;

if ( count > 20)                      // If space width > 1ms
ir_code |= 1ul << (31 - i);         // Write 1 to bit (31 - i)
else                                  // If space width < 1ms
ir_code &= ~(1ul << (31 - i));      // Write 0 to bit (31 - i)
}
return 1;
}

void main()
{
TRISB0_bit = 1;
TRISC5_bit = 0;

while(1) { //Loop executed infinite times

//while (IR_PIN);                //Wait until IR pin falls
command = ir_code >> 8;
inv_command = ir_code;

switch(command){
case 0x48:
LED_PIN = 1;
break;
case 0x80:
LED_PIN = 0;
break;
}
}
}
}

• What are you struggling with exactly? What values do you expect, and what do you receive? – Hein Wessels Aug 8 '18 at 6:12
• Actually, I don't want to use External Interrupt or Pin Change Interrupt and Timer0 for this decoding purpose. Because I am using Timer0 and Pin change interrupt for handling another work. I'm receiving garbage value probably If I would get the actual value LED connected with the GP2 pin in PIC12F675 will be Turn on and Turn off by sending the command from the remote. I compiled my code with MikroC Pro and using PicKit2 Clone as the burner. There is no error in my code. But it is not possible to check received value from remote in PIC12F675 because it has not UART and LCD interfacing option. – Chayan Mistry Aug 8 '18 at 7:18
• "when I using the same program for PIC12F675 it's not working... I have disabled Pull-ups and MCLR pin function for PIC12F675. And using the GP3 pin as the input for IR receiver." - so not actually the same. Please show us the exact code you are using on the 12F675. – Bruce Abbott Aug 8 '18 at 9:36
• if you are using interrupts for other work then your count loop timing will be off. why cant you use the timer for both purposes, are you changing the time duration between interrupts? you dont need the interrupt necessarily for this, but you can still poll the timers current count yes and use that as a more accurate time measurement. If you dont have any other code or interrupts running does the above work? what is the real question/problem here? – old_timer Aug 8 '18 at 12:37