Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Problem in NEC IR Decoding in PIC12F675

Status
Not open for further replies.

chayanforyou

Newbie
Newbie level 4
Joined
Aug 7, 2018
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
51
Hello

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.

My program is working fine with PIC16F877A which running with external OSC @ 16MHz. But when I using the same program for PIC12F675 its 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. Please, can someone help me to solve this problem for PIC12F675?

Thank you
Chayan


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
/* 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;
      }
    }
  }
}

 
Last edited:

Hi,

a delay() function s not very precise - at least not at very low delay times.

But even wors you run this in a loop .. and don´t care that the rest of the code additinally need some time to be processed.

Your program is built as if the complete loop takes 50us..but it need more time.

***
For precise timing use the built in hardware periferls. There´s a good reason why thy built this into the PIC.
* timer / counter
* interrrupts
* capture

Your code takes 100% of processing time - when yo use the built in periferals then you maybe need just 1% of processing power (depending on your solution and the level of optimisation) thus the microcontroller has plenty of power to do other things. (display, keypad, communication....)


Klaus
 

I'm already using Timer 0 and Pin change interrupt for my work. And I need to decode IR signal with GP3 pin which is MCLR pin.

Now what process i can use for my work?
 

Go to Page 53 of the PIC12F675 data sheet and read up on the MCLRE config bit.
Be careful when setting this as I recall that you then cannot use the low-voltage programming (which I'm not sure is available on that MCU anyway).
I would not try to do too much with a small device such as the one you have - it only has 1K words of FLASH for the program and the various vectors.
I assume that the MikroC compiler uses different names for the GPIO registers and bits than the XC8 compiler and the datasheet. Also I assume that the parts of the MikroC environment that you don't show (e.g.the config settings and the oscillator setup) provide you with the clock speed (and therefore delay times) that you expect (not forgetting Klaus's comments above).
Susan
 

Hi,

I'm already using Timer 0 and Pin change interrupt for my work.
The given code does not.
--> show us this code.

But when I using the same program for PIC12F675 its not working for me.
This is no error description.
Give a more detailed error description:
* what do you expect?
* how do you test this?
* what results do you see?
* what did you do to debug (encircle) the problem?

Klaus
 

Hello

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 compiled my code with MikroC Pro and using PicKit2 Clone as the burner.

I'm using the same code for PIC12F675 above I have pasted.

just change GPOI pin

IR_PIN to GP3_bit
LED_PIN to GP2_bit

and config

OPTION_REG = 0x80;
TRISIO = 0x08;

And not found any error in my code.
Can you give any solution to decode NEC Protocol in an easy way?

Thanks
 

Are you saying that the code you pasted at the start of this thread was for the PIC16F887A and not the PIC12F675? At least that would explain the differing pin names.
Also those are not CONFIG options - they are Special Function Registers (SFRs) that you set in your program.
The CONFIG settings MUST be changed as they are not compatible between the two devices. For a start the PIC16F887A has only 4 oscillator options (FOSC1:FOSC0) while the PIC12F692 as 8. A number of the other config bits with corresponding functions are in different locations within the word.
Are you also saying that the PIC12F675 will be doing other processing but you want to have accurate timing (without a timer) for the NEC Protocol decoding? If so then stop right there.
The NEC protocol (according to https://sibotic.files.wordpress.com...redtransmissionprotocol-281113-1713-47344.pdf which I found on the Internet) has pulses that are 562.5uSec in width and spaces of the same or double that length. To get accurate pulse/space detection you will need to sample at least twice within that period so you must sample at least every 281.25uSec. (More frequent sampling is better so that you can more accurately detect the edges.)
At 4MHz clock, you are executing 1 instruction every 1uSec (with a few exceptions for branching instructions and the like which take 2uSec) and you are both bit-banging the reading of the NEC protocol input and doing other work, all within 1K words of FLASH! (The PIC16F887A has 4 times that amount of program memory and you say it as running 4 times faster.)
Don't forget that any ISR processing will distort any non-hardware driven timing such as the 'delay' macros.
My advise is to use a larger processor or at least use Timer1 for the timing and a state machine for decoding the input. (Also try to use a newer MCU that has a timer with the PRx registers as these give much better timing on the interrupts.)
Susan
 

Thanks Susan.
I will try another way to solve my problem.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top