C codes for decoding RC5 signals using PIC microcontoller

Status
Not open for further replies.
microchip rc5

No problem, glad to be of help.

Try out my free RTOS for 16-bit pics posted on eda board here:
 
Re: PIC RC5 Code

Hi,

I am trying to port the code for C18 compiler but am not able to.

This is the code i have converted but its not working for RC5. Can someone please help me.

Code:
#include <p18cxxx.h>
#include <delays.h>

#pragma config WDT = OFF, LVP = OFF, OSC = XT, DEBUG = OFF

#define IR_pin PORTBbits.RB0
#define TIME_MIN  (5)
#define TIME_MAX  (11)
#define TIME_STEP (2)
#define LED0 PORTCbits.RC0
#define LED1 PORTCbits.RC1
#define LED2 PORTCbits.RC2
#define LED3 PORTCbits.RC3
#define LED4 PORTCbits.RC4

int RC5_receive(unsigned int *, unsigned int *);
void Delay100Us(unsigned char);
void applyIR(void);

void Delay100Us(unsigned char time)
{
	char x;
	for(x=0;x<time;x++)
		Delay10TCYx(9);
}

unsigned int IR_addr, IR_data = 0;

void applyIR(void)
{
	switch(IR_data)
	{
		case 0: LED0 ^= 1;break;
		case 1: LED1 ^= 1;break;
		case 2: LED2 ^= 1;break;
		case 3: LED3 ^= 1;break;		
		case 4: LED4 ^= 1;break;
	}
}	

void main(void)
{
	ADCON1 = 0x0F;
	TRISB = 0x01;
	TRISA = 0x00;
	PORTB = 0x00;
	TRISC = TRISD = TRISE = 0x00;
	PORTA = PORTC = PORTD = PORTE = 0x00;
	PORTC = 0x55;
	while(1)
	{
		RC5_receive(&IR_addr, &IR_data);
		applyIR();
	}
}

int RC5_receive(unsigned int * RC5addr, unsigned int * RC5data) 
{
    int           OffTime, OnTime, DevTime, DiTime, i, j, k;
    unsigned int  data = 0, addr = 0;
    unsigned int  bit_mask;

    if(!IR_pin)
    {
		for(OffTime = 0; !IR_pin && OffTime < TIME_MAX; OffTime += TIME_STEP){Delay100Us(TIME_STEP);}
		if (OffTime > TIME_MAX) return -1;
		if (OffTime < TIME_MIN) return -1;

		for(OnTime = 0; IR_pin && OnTime < TIME_MAX; OnTime += TIME_STEP){Delay100Us(TIME_STEP);}
		if (OnTime > TIME_MAX) return -1;
		if (OnTime < TIME_MIN) return -1;


		//* skip token
        DevTime = (2 * OffTime + OnTime + OffTime / 2);

        for (k = 0; k < DevTime; k += TIME_STEP){Delay100Us(TIME_STEP);}
	
		//* get address and data
		DiTime  = (OffTime + OnTime);
	
		for (i = 0; i < 5; i++)
		{
		    if(IR_pin) {addr <<= 1;} else{addr <<= 1; addr |= 1;}
		    for (k = 0; k < DiTime; k += TIME_STEP){Delay100Us(TIME_STEP);}
		}
		for (i = 0; i < 6; i++)
		{
		    if(IR_pin){data <<= 1;}else{data <<= 1; data |= 1;}
		    for (k = 0; k < DiTime; k += TIME_STEP){Delay100Us(TIME_STEP);}
		}
	
		*RC5data = data;
		*RC5addr = addr;
		return 0;
    }
    return -1;
}
 

Re: PIC RC5 Code

@Yager
HI
I want to ask a simple question that will your code work for every remote or not.
I am making this project and after going through the replies i am going to test your code.
But i have some confusion. I am making a project that take data from the remote and display it on the LCD.
Displaying on the LCD is other issue but is it work for every remote in the market or the specific remote.
My remote uses RC5 protocol.

Thanks

Added after 32 minutes:

I am using 18F452 and the max232 is attached to the 4 and 5 pin of 18F and IR is connected at the 13 pin. What changes that i have to make in your project to run it on my PIC board.

Thanks I will wait for your replay.
 

Re: +pic24 ir decoder code

hi mate can i use this code on a 16f690 pic
 

Re: rc5 pic example


hi @btpass
i am trying to use your rc5 code on pic16f690 but keep getting errors please can u help me see where i am going wrong in the code i am using high C compiler. thanks.


header /////////////**********
/*--- rc5decode.h ---*/

#ifndef RC5DECODE
#define RC5DECODE

/*--- Function prototype ---*/

unsigned char rc5Decode(void);

/*---- globals ---*/

extern unsigned char command; /* rc5 command code */
extern unsigned char address; /* rc5 system address */
extern unsigned char rc5valid; /* valid rc5 data flag */

#endif

/*--- Macros ---*/

#define IR_IN _PORTA,2 /* Infra Red input pin */
#define TMR _TMR0 /* Pulse timer */

/*--- rc5 infra red codes ---*/

#define SYSTEM_ADD 0x10 /* System address */

#define RC5_VOL_UP 0x10 /* VOL+ */
#define RC5_VOL_DOWN 0x11 /* VOL- */
#define RC5_LINEAR_FUNCTION_UP 0x4D /* LINEAR FUNCTION + */
#define RC5_LINEAR_FUNCTION_DWN 0x02 /* LINEAR FUNCION - */
#define RC5_NEMERIC_KEY_0 0x00 /* CNUMERIC KEY 0 */
#define RC5_NUMERIC_KEY_1 0x01 /* NUMERIC KEY 1 */
#define RC5_NUMERIC_KEY_2 0x02 /* AUX1 */
#define RC5_STANDBY 0x09 /* STANDBY */
#define RC5_MUTE 0x0D /* MUTE */


/*--- End of file ---*/



////////////c code**********


#include <htc.h>
#include <pic.h>
#include "RC_5header.h"
#define _XTAL_FREQ 4000000
#define _PWRTE_ON EQU H'3FEF' ; PWRT enabled
#define _MCLRE_ON EQU H'3FFF' ; MCLR pin function is MCLR
#define _WDT_ON EQU H'3FFF' ; WDT enabled
#define _CP_ON EQU H'3FBF' ; Program memory code protection is enable


__CONFIG( _CP_ON &_PWRTE_ON &_WDT_ON &_MCLRE_ON );
/*--- globals ---*/

unsigned char command;
unsigned char address;
unsigned char temp;
unsigned char timer;
unsigned char rc5valid;

void select_operation(void);

//*interrupt function ta call functionality when the remote input is detected*//
void interrupt isr(void);

/*--- rc5 decode function ---*/

unsigned char rc5Decode(void)
{
#asm

;------ calibrate timing

bcf 3,5 ;select bank 0
clrf TMR ;start timer
call Wlow ;get the pulse length
call Whigh

btfsc TMR,7 ;test for timer overflow
goto codeError ;bail out

movf TMR,w ;save pulse length
clrf TMR ;start timer

movwf _timer ;take the pulse length,
movwf _temp ;save it
bcf 3,0 ;clear carry
rrf _temp,f ;divide by 2
bcf 3,0 ;clear carry
rrf _temp,f ;divide by 4
movf _temp,w ;and subtract to get
subwf _timer,f ;3/4 pulse length

movlw 0x0c
movwf _temp ;init bit counter

;------ wait for start of data

nextBit movf _timer,w ;wait 3/4 pulse length
subwf TMR,w
btfss 3,2 ;take a look
goto nextBit

;------ start of data stream

btfss IR_IN ;if its high
goto waitlow ;gonna be an 0

;------ wait for high 1

call Whigh ;if its low
bsf 3,0 ;gonna be a 1
goto clock

;------ wait for low 0

waitlow call Wlow
bcf 3,0 ;gonna be an 0

;------ clock data

clock clrf TMR ;restart timer
rlf _command,f ;save the bit
rlf _address,f
decfsz _temp,f ;done all bits?
goto nextBit

;------ get system address, toggle bit and command code

movf _command,w ;copy low byte
movwf _temp ;into temp
rlf _temp,f ;need to shift 2 bits
rlf _address,f ;into address
rlf _temp,f
rlf _address,f ;system address is 5 bits

movlw 0x3f ;mask system address + toggle bit
andwf _address,f

movlw 0x3f ;mask command code
andwf _command,f ;command code is 6 bits
goto rc5ok ;done ok

;------ wait while low

Wlow btfsc TMR,7 ;test for timer overflow
return ;bail out
btfss IR_IN
goto Wlow
return

;------ wait while high

Whigh btfsc TMR,7 ;test for timer overflow
return ;bail out
btfsc IR_IN
goto Whigh
return

rc5ok movlw 0x01
movwf _rc5valid
goto rc5end
codeError clrf _rc5valid
rc5end nop

#endasm

return rc5valid;
}

void interrupt isr(void)
{

if(rc5Decode())
{
switch(PORTB)
{
case 'RC5_NUMERIC_KEY_0':
PORTC = 0x03;
break;
case 'RC5_NUMERIC_KEY_1':
PORTC = 0x0C;
break;
default :
PORTC = 0x0F;
}
}
INTF = 0;
}

void select_operation(void)
{
PORTC = 0x0F;
}


void main()
{
PORTA = 0xFF;
TRISA = 0;

PORTB = 0xFF;
TRISB = 0;

PORTC = 0x00;
TRISC = 0;

GIE = 1;
INTE = 1;

while(1);


}


thanks in advance
 

Re: PIC RC5 Code

Nice RTOS btbass.

Any recommendations as to reference material, books, literature, etc, that aided in coding it?
 

Re: PIC RC5 Code

Try this for your rc5 decoder.
 

Attachments

  • Rc5_Decoder.zip
    6.3 KB · Views: 123

Re: PIC RC5 Code

Hi bigdogguru,
The RTOS was inspired by an old book I have called 'Born to Code in C' by Herbert Schildt which describes a multitasking system for PC's in DOS.
I used his code on several projects and it worked very well.

The book I used to write the program was 'dsPIC30F Programmers Reference Manual' which is a Microchip publication I have in hardcopy. I don't think they do it any more, as now you can download it as a pdf.

The assembly language for the Pic 16-bit family is really powerful.
In my opinion it is the cleanest most logical assembly language Ive used.

I have posted a light version on eda board here
https://www.edaboard.com/threads/203855/
 
Last edited:

Re: rc5 code yager



hi @CMOS i am trying to use the same code on pic16f690 but i keep coming up with errors can u help me look at my code to see where the errors lie thanks ;


#include <htc.h>
#include <pic.h>
#include "RC_5header.h"
#define _XTAL_FREQ 4000000
#define _PWRTE_ON EQU H'3FEF' ; PWRT enabled
#define _MCLRE_ON EQU H'3FFF' ; MCLR pin function is MCLR
#define _WDT_ON EQU H'3FFF' ; WDT enabled
#define _CP_ON EQU H'3FBF' ; Program memory code protection is enable


//CONFIG'()';// _CP_ON);// &_PWRTE_ON &_WDT_ON &_MCLRE_ON );
/*--- globals ---*/

unsigned char command;
unsigned char address;
unsigned char temp;
unsigned char timer;
unsigned char rc5valid;

void select_operation(void);

//*interrupt function ta call functionality when the remote input is detected*//
void interrupt isr(void);

/*--- rc5 decode function ---*/

unsigned char rc5Decode(void)
{
#asm

;------ calibrate timing

bcf 3,5 ;select bank 0
clrf TMR ;start timer
call Wlow ;get the pulse length
call Whigh

btfsc TMR,7 ;test for timer overflow
goto codeError ;bail out

movf TMR,w ;save pulse length
clrf TMR ;start timer

movwf _timer ;take the pulse length,
movwf _temp ;save it
bcf 3,0 ;clear carry
rrf _temp,f ;divide by 2
bcf 3,0 ;clear carry
rrf _temp,f ;divide by 4
movf _temp,w ;and subtract to get
subwf _timer,f ;3/4 pulse length

movlw 0x0c
movwf _temp ;init bit counter

;------ wait for start of data

nextBit movf _timer,w ;wait 3/4 pulse length
subwf TMR,w
btfss 3,2 ;take a look
goto nextBit

;------ start of data stream

btfss IR_IN ;if its high
goto waitlow ;gonna be an 0

;------ wait for high 1

call Whigh ;if its low
bsf 3,0 ;gonna be a 1
goto clock

;------ wait for low 0

waitlow call Wlow
bcf 3,0 ;gonna be an 0

;------ clock data

clock clrf TMR ;restart timer
rlf _command,f ;save the bit
rlf _address,f
decfsz _temp,f ;done all bits?
goto nextBit

;------ get system address, toggle bit and command code

movf _command,w ;copy low byte
movwf _temp ;into temp
rlf _temp,f ;need to shift 2 bits
rlf _address,f ;into address
rlf _temp,f
rlf _address,f ;system address is 5 bits

movlw 0x3f ;mask system address + toggle bit
andwf _address,f

movlw 0x3f ;mask command code
andwf _command,f ;command code is 6 bits
goto rc5ok ;done ok

;------ wait while low

Wlow btfsc TMR,7 ;test for timer overflow
return ;bail out
btfss IR_IN
goto Wlow
return

;------ wait while high

Whigh btfsc TMR,7 ;test for timer overflow
return ;bail out
btfsc IR_IN
goto Whigh
return

rc5ok movlw 0x01
movwf _rc5valid
goto rc5end
codeError clrf _rc5valid
rc5end nop

#endasm

return rc5valid;
}

void interrupt isr(void)
{

if(rc5Decode())
{
PORTC = 0x0F;
}

INTF = 0;
}

void select_operation(void)
{
PORTC = 0x0F;
}


void main()
{
PORTA = 0xFF;
TRISA = 0;

PORTB = 0xFF;
TRISB = 0;

PORTC = 0x00;
TRISC = 0;

GIE = 1;
INTE = 1;
INTEDG = 0;

while(1);


}

---------- Post added at 06:18 ---------- Previous post was at 04:59 ----------

thanks @btpass for the code Built it successful but still not getting response from led's when pressing button on remote , i am using a vishay tsop1236 infra red receiver , tried the code with inverted and direct input to the RA2/INT pi but still no result , using RC5-R remote control. after i build the project ((NULL)) was put into all the assciated files in the files window.. any advice please thanks
 

Re: rc5 code yager

testing the code to illuminate led's at the C port when there's a valid transmission and still not getting any indicative output
 

Re: PIC RC5 Code

You are setting all your ports to outputs?
Also check the ANSEL register is clear

Code:
void main()
  {
  ANSEL = 0;            /* Configure pins as digital I/O */

  PORTA = 0xFF;
  TRISA = 0;		/* All outputs ??? */

  PORTB = 0xFF;
  TRISB = 0;

  PORTC = 0x00;
  TRISC = 0;

  GIE = 1;
  INTE = 1;
  INTEDG = 0;

  while(1); 
 }
 

Re: PIC RC5 Code

ok thanks alot btpass, but now i am gettin errors for the __CONFIG line saying unidentified symbol "MCLREN", "WDTEN", "PWRTEN" , "CP", "XT"
 

Re: PIC RC5 Code

Code:
  __CONFIG(XT & CP & PWRTEN & WDTEN & MCLREN);

This config line compiles ok for me.
The config line should be the first line in the code after the include files.
The symbols are defined in the 'pic16f685.h' file which is conditionally called from the 'pic.h' file.
You can look in the file and see the definitions at the end of the file.

You only need to include the <htc.h> file as that calls the <pic.h> file.
 

Re: PIC RC5 Code

__CONFIG( CP_ON & PWRTE_ON & MCLRE_ON); used this and the code compiles ok ,
the intended function for the isr i wrote is for led's in the c port to come on for a valid transmission but i still am not seeing the led's come on. is this a wrong piece of code or is the rc5 not working

void interrupt isr(void)
{
unsigned char PORTC;

if(rc5Decode())
{
switch(command)
{
case RC5_NEMERIC_KEY_0:
PORTC == 0x03;
break;

case RC5_NUMERIC_KEY_1:
PORTC == 0x0C;
break;

default :
PORTC = 0x0F;
break;
}
}
INTF = 0;
}

---------- Post added at 13:50 ---------- Previous post was at 13:50 ----------

i'm using pic16f690
 

Re: PIC RC5 Code

Make sure you have disabled the watchdog timer or periodically reset it.

Code:
void interrupt isr(void)
  {
  if(rc5Decode())
    {
    switch(command)
      {
      case RC5_NEMERIC_KEY_0:
        PORTC = 0x03;
        break;

      case RC5_NUMERIC_KEY_1:
        PORTC = 0x0C;
        break;

      default :
        PORTC = 0x0F;
        break;
      }
    }

  INTF = 0;
  }


---------- Post added at 14:28 ---------- Previous post was at 14:00 ----------

If you are using more than one interrupt, you need to check the interrupt source.
Code:
void interrupt isr(void)
  {
  if(INTF)
    {
    if(rc5Decode())
      {
      switch(command)
        {
        case RC5_NEMERIC_KEY_0:
          PORTC = 0x03;
          break;

        case RC5_NUMERIC_KEY_1:
          PORTC = 0x0C;
          break;

        default :
          PORTC = 0x0F;
          break;
        }
      }

    INTF = 0;
    }
  }
 

Re: PIC RC5 Code

thanks alot btpass really appreciate your time ... but i want to ask will this code also respond to input from a universal remote control?? or does it specifically have to be rc5 remote thanks
 

Re: PIC RC5 Code

So hopefully it is now working?

The code is only good for RC5 protocol. It has to be RC5.
 

Re: PIC RC5 Code

thanks mate
 

Hi,
I have written an interrupt driven RC5 decoder for CCS C compiler.
You can get it here:
**broken link removed**

Regards!
 
Reactions: adidav

    adidav

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…