0
\$\begingroup\$

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 int address;
unsigned short command, inv_command;


short nec_remote_read() {

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;

// Read code message (32-bit)
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
if (nec_remote_read()) {
address = ir_code >> 16;
command = ir_code >> 8;
inv_command = ir_code;

switch(command){
  case 0x48:
  LED_PIN = 1;
  break;
  case 0x80:
  LED_PIN = 0;
  break;
   }
  }
 }
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ What are you struggling with exactly? What values do you expect, and what do you receive? \$\endgroup\$ Commented Aug 8, 2018 at 6:12
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 8, 2018 at 7:18
  • \$\begingroup\$ "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. \$\endgroup\$ Commented Aug 8, 2018 at 9:36
  • \$\begingroup\$ 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? \$\endgroup\$
    – old_timer
    Commented Aug 8, 2018 at 12:37

1 Answer 1

1
\$\begingroup\$

The PIC12F675 requires four oscillator cycles for each instruction cycle. This means that while running at 4 MHZ, your instruction cycle is 1 uSec per instruction. Look at the disassembly listing from your compiler to see how many instructions your while loop must execute and I think you will see that you are introducing and additional 20-25 uSec to your delay depending on where you exit the while loop.

You could run the PIC12F675 faster, or switch to an interrupt-driven design, or setup and read a counter to give you the correct timing. Good luck!

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.