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.

C codes for decoding RC5 signals using PIC microcontoller

Status
Not open for further replies.
My code is working ok.
With the lower prescale I had to increase the timeout in two functions.

Code:
/*--- Timer 4 one shot Interrupt ---*/

void __attribute__((__interrupt__, no_auto_psv)) _T4Interrupt(void)
  {
  if(!Decoder.error)
    {
    if(Decoder.bit_counter == 0x0b){
      save_data(0);
      }

    RC5_Code.address = (Decoder.rc_data >> 6) & 0x001f;
    RC5_Code.command = Decoder.rc_data & 0x003f;
    RC5_Code.toggle = (Decoder.rc_data >> 11) & 0x0001; 
    RC5_Code.valid = 1;
    }

  IFS1bits.T4IF = 0;
  IEC1bits.T4IE = 0; 
  T4CONbits.TON = 0; 
  [B]PR4 = 0x2000;		  !!!!!! increase timeout[/B]
  Decoder.State = START_S; 
  }

/*--- Initialise Timer 4 (Used as RC timer) ---*/

static void init_timer_4(void)
  {
  T4CON = 0;  
  IPC6bits.T4IP = 6;    /* High priority */     
  [B]T4CONbits.TCKPS = 1;  /* Prescale 1:8 */ [/B]
  [B]PR4 = 0x2000;          /* Time out */   !!!!!! increase timeout   [/B]
  IFS1bits.T4IF = 0;
  IEC1bits.T4IE = 0;    /* Interrupt enable bit */
  T4CONbits.TON = 0;
  }

/*--- End of File ---*/

This code is working ok.

Code:
#include "rc5_decoder.h"


_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) 
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_NONE & FNOSC_FRC)

/*--- RC5 infra red codes ---*/

#define RC5_ADDRESS     20U

#define STOP            54U
#define PLAY            53U
#define PAUSE           48U
#define NEXT_TRACK      32U
#define PREV_TRACK      33U
#define SCAN_FORWARDS   52U
#define SCAN_BACKWARDS  50U

/*--- Program entry ---*/

int main(void)
  {
  init_rc5_decoder(); /* Initialise decoder */

  TRISA = 0;
  AD1PCFG = 0xffff;

  while(1)
    {
    if(RC5_Code.valid)
      {
      PORTA = RC5_Code.command;
      RC5_Code.valid = 0;
      }
    }
  }
    
/*--- End of File ---*/
 
Last edited:

Wow..Now it's working like a charm.....Thanks aaaaaaaaaa lot btbass...!!...

For 8mhz..Prescale 1:64
T4CONbits.TCKPS = 2;
 

Good, now its working here are the files for future users.
 

Attachments

  • Rc5_C30.zip
    2.4 KB · Views: 99
@btbass...

Now, I am using this code to display VOLUME UP n DOWN using 2x16 LCD.....I have used PLAY for VOLUP and STOP for VOL DOWN...Can u please check my code? I am getting some errors...
Attache work space...Thanks in Advance...!!
 

Attachments

  • Rc5_Decoder.zip
    6.3 KB · Views: 74

I think you have posted the wrong code???
 

Ups..This one..All .c and .h files
 

Attachments

  • rc5_decoder.zip
    5.7 KB · Views: 79

Change the rc5_decoder.h file for this one.
Code:
#ifndef RC5_DECODER_H
#define RC5_DECODER_H


/*--- RC code structure ---*/

typedef struct
  {
  unsigned char toggle;
  unsigned char address;
  unsigned char command;
  unsigned char valid;
  }RC5_CODE;

extern volatile RC5_CODE RC5_Code;

/*--- Function prototypes ---*/

void init_rc5_decoder(void);

#endif

You have two functions both called write?
You need to rename one of them.
 

Getting Error like


In file included from main.h:2,
from lcd.c:1:
rc5_decoder.h:21:7: warning: no newline at end of file
In file included from main.h:5,
from lcd.c:1:
conu2.h:24:7: warning: no newline at end of file
In file included from main.h:7,
from lcd.c:1:
volume.h:10:7: warning: no newline at end of file
lcd.c: In function 'uwrite':
lcd.c:5: error: syntax error before '{' token
lcd.c:107:2: warning: no newline at end of file
 

void write(int handle, void *buffer, unsigned int len); ????

missing semicolon in lcd.h
 

Thanks btbass...SOLVED...!!!

Now getting display..I have removed extra files..
 

Code:
RC5_Code.toggle = (Decoder.rc_data >> 11) & 0x0001;

what this statement do?
 

The toggle bit is for detecting a new keypress.

If you press and hold a key, the code is repeatably sent, but the toggle bit does not change.

If you let the key go, then repress it, the toggle bit changes to indicate a new keypress.

So, every time you press a new key, the toggle bit toggles.
 

You can use the toggle bit so you dont keep repeating the same command if someone is holding a key down.

psuedo code
Code:
unsigned char last_toggle;

if(code_valid)
  {
  if(toggle != last_toggle)
    {
    do command;
    last_toggle = toggle;
    }
  }
 
@btbass...Yh..It's really cool...I adopted..!!
 
Last edited:

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

Regards!

where i wrong
Code:
/*** FILEHEADER ****************************************************************
 *
 *    FILENAME:    RC5 IR Remote.c
 *    DATE:        07.10.2011
 *                       Uses RC5 driver by Christian Stadler
 *
 *    DESCRIPTION: IR Remote, Controls 8 Relays
 *
 *    16F676 Configuration, I=Input, O=Output
 *                         _________
 *                   Vdd   | 1  14 |  Vss
 *       (O) xxx <-- RA5   | 2  13 |  RA0 --> LED (O)
 *       (I)  IR --> RA4   | 3  12 |  RA1 --> RL7 (O)
 *                  MCLR   | 4  11 |  RA2 --> RL8 (O)
 *       (O) RL6 <-- RC5   | 5  10 |  RC0 --> RL1 (O)
 *       (O) RL5 <-- RC4   | 6   9 |  RC1 --> RL2 (O)
 *       (O) RL4 <-- RC3   | 7   8 |  RC2 --> RL3 (O)
 *                         ---------
 *
 ******************************************************************************/

/*** HISTORY OF CHANGE *********************************************************
 *
 *   07.10.2011 Initial Version
 * 
 * 
 * 
 * 
 ******************************************************************************/
#include <RC5 IR Remote.h>
#include "types.h"

/* --- RC5 driver configuration --- */
#define RC5_DATA_PIN          PIN_A4        /* IR sensor (SFH5110-36) connected to RB1 */
#define RC5_TICKS_PER_MS      (1000/32)      /* timer increments every 31.25 us */
                                            /* i.e. around 1000 ticks per millisecond */
#define RC5_GetTimer()        get_timer0()  /* timer0 shall be used for RC5 decoding */
                          

/* --- macros to switch on/off LED --- */
#define Led_On()        output_high(PIN_A0)
#define Led_Off()       output_low(PIN_A0)
 
/* RC5 driver include */
#include <RC5_driver.h>

/*******************
//PORT C Register = 0x07
//PORT A Register = 0x05
*********************/
#BIT RL1 = 0x07.0
#BIT RL2 = 0x07.1
#BIT RL3 = 0x07.2
#BIT RL4 = 0x07.3
#BIT RL5 = 0x07.4
#BIT RL6 = 0x07.5
#BIT RL7 = 0x05.1
#BIT RL8 = 0x05.2

/******************************************************************************
 Key Codes
 *****************************************************************************/
#define RC5_NUMERIC_KEY_0 0x00 /* NUMERIC KEY 0 */
#define RC5_NUMERIC_KEY_1 0x01 /* NUMERIC KEY 1 */
#define RC5_NUMERIC_KEY_2 0x02 /* NUMERIC KEY 2 */
#define RC5_NUMERIC_KEY_3 0x03 /* NUMERIC KEY 3 */
#define RC5_NUMERIC_KEY_4 0x04 /* NUMERIC KEY 4 */
#define RC5_NUMERIC_KEY_5 0x05 /* NUMERIC KEY 5 */
#define RC5_NUMERIC_KEY_6 0x06 /* NUMERIC KEY 6 */
#define RC5_NUMERIC_KEY_7 0x07 /* NUMERIC KEY 7 */
#define RC5_NUMERIC_KEY_8 0x08 /* NUMERIC KEY 8 */
#define RC5_NUMERIC_KEY_9 0x09 /* NUMERIC KEY 9 */

/*****************************************************************************/
/* Interrupt_RA                                                              */
/*                                                                           */
/* Port A change interrupt service routine.                                  */
/* Used to decode RC5 IR signal.                                             */
/*****************************************************************************/
#INT_RA
void Interrupt_RA(void)
{
   Led_On();
   RC5_InterruptHandler();
   Led_Off();
   
   clear_interrupt(INT_RA4);
}

/*****************************************************************************/
/* main                                                                      */
/*                                                                           */
/* Configures timer0 as time base for RC5 decoding and enables port A        */
/* interrupt on change.                                                      */
/* Main loop checks if a RC5 code has been received and processes the code   */
/* in the switch-case statement                                              */
/*****************************************************************************/
/* NOTE: Currently it only works if PIC is reset after programming? */
 void main()
{
   unsigned int16 rc5code;
   unsigned int rc5cmd;

   setup_comparator(NC_NC);      // All Ports Digital

    /* FOSC/4 is timer source */
    
    /* Original Configuration:
    /* FOSC = 20MHz => 5MHz, i.e. timer increment every t = 1/(FOSC/4) = 200ns */
    /* with prescaler of 128 timer will increment every 25.6us */
    
    // 16F676 Running at Internal 4 Mhz Configuration:
    // Timer0 increments every 1 us (4 Mhz/4 = 1 Mhz)
    setup_timer_0(RTCC_INTERNAL | RTCC_DIV_32);
    
    
    // TRIS Ports set by #use fixed_io in RC5 IR Remote.h
    /* read port A to clear mismatch condition */
    input_a();                      
    
    /* configure port A interrupt on change */
    clear_interrupt(INT_RA);        /* clear port A interrupt flag */
    enable_interrupts(INT_RA);     /* enable RA4 interrupt on change */
      
    /* global interrupt enable */
    enable_interrupts(GLOBAL);
    
    // Main Loop
    while (1)
    {
      /* check if new data is available */
        if (RC5_CodeReady())
        {
            /* get code */
            rc5code = RC5_GetCode();
            
            // Extract command from RC5 code
            rc5cmd = RC5_GetCmd(rc5code);
            
           switch (rc5cmd)
           {
               case RC5_NUMERIC_KEY_0:
                    RL1 = ~RL1;
                    break;
                 
                 case RC5_NUMERIC_KEY_1:
                    RL2 = ~RL2;
                    break;
                 
                 case RC5_NUMERIC_KEY_2:
                    RL3 = ~RL3;
                    break;
                 
                 case RC5_NUMERIC_KEY_3:
                    RL4 = ~RL4;
                    break;
                 
                 case RC5_NUMERIC_KEY_4:
                    RL5 = ~RL5;
                    break;
                    
                 case RC5_NUMERIC_KEY_5:
                    RL6 = ~RL6;
                    break;
                    
                 case RC5_NUMERIC_KEY_6:
                    RL7 = ~RL7;
                    break;
                    
                 case RC5_NUMERIC_KEY_7:
                    RL8 = ~RL8;
                    break;
                    
                 case RC5_NUMERIC_KEY_8:
                    RL1 = 1;
                    RL2 = 1;
                    RL3 = 1;
                    RL4 = 1;
                    RL5 = 1;
                    RL6 = 1;
                    RL7 = 1;
                    RL8 = 1;
                    break;
                     
                 case RC5_NUMERIC_KEY_9:
                    RL1 = 0;
                    RL2 = 0;
                    RL3 = 0;
                    RL4 = 0;
                    RL5 = 0;
                    RL6 = 0;
                    RL7 = 0;
                    RL8 = 0;
                    break;
                    
         }   
        }
        
        /* this function increments the RC5 timeout timer */
        /* NOTE: Decoding will also work without calling this function, but */
        /*       it could happen that RC5 codes are sometimes not getting */
        /*       recognized because of decoding state machine stucks due */
        /*       to erroneous RC5 signal. */
        RC5_TimeoutIncrement();    
   }
    
}
pl guied me
 

Re: rc5 code yager

HI..i have a problem.. i have a project with name " designing an application (in MPLAB IDE) for interfacing with a remote IR signal (display key codes pressed)". Accessories: PIC18F4520, TSOP 1136, remote control PHILIPS (RC5 STANDARD ), MPLAB C18 COMPILER..;are 14 bits, start bit 1, start bit 2, toggle bit ,5 address,6 command ..How to read .. and how to display bits?pls if u can help me with the C CODE( main.c). an example please..waiting for your answer ..thanks
 

If you have a debugger, try a few break points to see whats going on.

I have the same Explorer board, I will try your code later today when I can and report back.

hey btbass, i got a code from this forum and is tat writen by u?
 

Attachments

  • rc5_interrupt.zip
    2 KB · Views: 59

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top