View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
Decode RC6 protocol using simple routine |
Posted: Thu Nov 25, 2010 5:52 am |
|
|
Hi guys i am sharing the code which i have developed for decoding RC6 protocol,hope it will be helpful for you
Code: |
///////////////////////////////////////////////////////////////////////////////
//This code decode the RC6 signal.The signal is read on INT2 pin,whenever an //
//EXT interrupt comes, value of '1/2T' is loaded in timer0,whenever timer0 //
//overflows it will load the value of 'T' in the timer0 and read the status //
//of the pin,this is done because it will be easy to check the status of the //
//pin in middle.if pin is low then 0 will be store in array then increament //
//array position if pin is high then 1 will be store in array then increament//
//array position.when array position becomes 58, it means complete stream has//
//arrived, so the timer0 will be disable and shows the result on //
//hyperterminal. //
//whenever the EXT interrupt comes the above procedure will be repeated. //
///////////////////////////////////////////////////////////////////////////////
#include <18f252.h>
#fuses HS,NOWDT,NOLVP,PUT,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(xmit=PIN_C6, rcv=PIN_C7, baud=9600)
int1 a[58]={}; //array for storing the complete stream
int flag=0,state,i,number;
#int_TIMER0
void timer0_interrupt(void)
{
clear_interrupt(int_TIMER0);
enable_interrupts(int_TIMER0);
set_timer0(65258);//value of T i-e 444 us
if(!input(PIN_B2))
{
a[state]=0;//store 0 in array
state++; //increament the postion
}
else if(input(PIN_B2))
{
a[state]=1;//store 1 in array
state++; //increament the postion
}
if(state>=58)//if stream is complete then disable the timer 0
{
disable_interrupts(INT_TIMER0);
state=0;
flag=1;
}
}
#int_EXT2
void isr_ext2(void)
{
flag=0;
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
set_timer0(65397);//value of 1/2T i-e 222 us
}
void main()
{
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_8);//timer is in 16-bit mode,prescaler is 8
ext_int_edge(2,H_TO_L);//when interrupt goes high to low (falling edge)
enable_interrupts(INT_EXT2);//enable int2
enable_interrupts(Global); //enable global interrupts
while(1)
{
if(flag==1)
{
printf("\r\n\n");
for(i=0;i<58;i++)
{
printf(" %d ",a[i]);//print complete stream on pc
}
flag=2;
}
}
}
|
you have captured the stream, now you can seprately store the start bit,toggle bits,address bits,data bits etc form the array. |
|
|
arocholl
Joined: 14 Dec 2008 Posts: 21
|
|
Posted: Thu Nov 25, 2010 8:47 am |
|
|
Quick suggestion: your enable_interrupts(INT_TIMER0); should be at the end of the ISR, not at the beginning. Otherwise you increase risk of reentrancy. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Thu Nov 25, 2010 11:14 am |
|
|
Actually,
You don't need the Clear, Disable, Enable calls inside the Timer0 ISR.
That's dangerous and unneeded.
They need to be removed.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Nov 25, 2010 10:58 pm |
|
|
Quote: | That's dangerous and unneeded |
bkamen plz give me detail on "dangerous", would it be the same as arocholl said
Quote: | you increase risk of reentrancy |
or there will be some more issues. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Fri Nov 26, 2010 1:06 am |
|
|
hayee wrote: | Quote: | That's dangerous and unneeded |
bkamen plz give me detail on "dangerous", would it be the same as arocholl said |
I *think* he was talking about the INT_EXT2 ISR. (maybe not) I was talking about the timer0 ISR.
Unless you are somewhere defining the global interrupt handler, clearing, disabling,re-enabling is unneeded.
The CCS interrupt handler already clears the interrupt flag for you.. If I remember right - typically at the END of the ISR. You are clearing it in the beginning. This could cause the interrupt to fire again while it's being serviced.
You don't need to disable the interrupt while it's being serviced - thus, you don't need to re-enable it when exiting out.
Just remove it. It's unneeded and other people on this forum have described their projects not working when they do some combination if not exactly what you're doing. _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Fri Nov 26, 2010 1:35 am |
|
|
Thanks bkamen,
Is it same for all types of interrupt or only for timer interrupts. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat Nov 27, 2010 1:02 am |
|
|
hayee wrote: | Thanks bkamen,
Is it same for all types of interrupt or only for timer interrupts. |
All interrupts have the "global interrupt handler" included whenever you define an ISR.
As soon as you say
Code: | #INT_xxx
void some_usr (void) {
}
|
CCS includes the global interrupt handler UNLESS you specifically include the a replacement for the global interrupt handler.
You can see it in ROM at the ISR Vector location when you view ROM after a compile.
This handler takes care of everything like clearing the interrupt bits that generated the interrupt.
Most of the time all the user's code ever needs to really do is define the interrupt and then enable/disable it -- BUT NOT IN THE INTERRUPT itself (unless you have a VERY specific reason to.. like use the INT_EXT to wake up the PIC after sleep -- but then disable the interrupt... and even that could be done after the sleep command when the PIC comes out of wakeup)
Before putting any interrupt manipulating commands in an ISR, it needs to be considered carefully as to "what will happen". _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Sat Nov 27, 2010 3:38 am |
|
|
as you said there is no need for enable/disable interrupt in the interrupt service routine, so i exactly do that
Code: |
///////////////////////////////////////////////////////////////////////////////
//This code decode the RC6 signal.The signal is read on INT2 pin,whenever an //
//EXT interrupt comes, value of '1/2T' is loaded in timer0,whenever timer0 //
//overflows it will load the value of 'T' in the timer0 and read the status //
//of the pin,this is done because it will be easy to check the status of the //
//pin in middle.if pin is low then 0 will be store in array then increament //
//array position if pin is high then 1 will be store in array then increament//
//array position.when array position becomes 58, it means complete stream has//
//arrived, so the timer0 will be disable and shows the result on //
//hyperterminal. //
//whenever the EXT interrupt comes the above procedure will be repeated. //
///////////////////////////////////////////////////////////////////////////////
#include <18f252.h>
#fuses HS,NOWDT,NOLVP,PUT,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(xmit=PIN_C6, rcv=PIN_C7, baud=9600)
int1 a[58]={}; //array for storing the complete stream
int flag=0,state,i,number;
#int_TIMER0
void timer0_interrupt(void)
{
set_timer0(65258);//value of T i-e 444 us
if(!input(PIN_B2))
{
a[state]=0;//store 0 in array
state++; //increament the postion
}
else if(input(PIN_B2))
{
a[state]=1;//store 1 in array
state++; //increament the postion
}
if(state>=58)//if stream is complete then disable the timer 0
{
state=0;
flag=1;
}
}
#int_EXT2
void isr_ext2(void)
{
flag=0;
set_timer0(65397);//value of 1/2T i-e 222 us
enable_interrupts(INT_TIMER0);
}
void main()
{
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_8);//timer is in 16-bit mode,prescaler is 8
ext_int_edge(2,H_TO_L);//when interrupt goes high to low (falling edge)
enable_interrupts(INT_EXT2);//enable int2
enable_interrupts(Global); //enable global interrupts
while(1)
{
if(flag==1)
{
disable_interrupts(INT_TIMER0);
printf("\r\n\n");
for(i=0;i<58;i++)
{
printf(" %d ",a[i]);//print complete stream on pc
}
flag=2;
}
}
}
|
Is that right? |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sat Nov 27, 2010 9:24 am |
|
|
Excellent. Does that routine as written still work for you? (I'm assuming it does).
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Sun Nov 28, 2010 10:36 pm |
|
|
Yes the routine is working well |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Nov 29, 2010 12:24 pm |
|
|
Fabulous! _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
Re: Decode RC6 protocol using simple routine |
Posted: Sun Oct 16, 2011 1:23 pm |
|
|
hayee wrote: | Hi guys i am sharing the code which i have developed for decoding RC6 protocol,hope it will be helpful for you
Code: |
///////////////////////////////////////////////////////////////////////////////
//This code decode the RC6 signal.The signal is read on INT2
}
}
|
you have captured the stream, now you can seprately store the start bit,toggle bits,address bits,data bits etc form the array. |
what need change for rc5 ? pl guide me here ... _________________ sahu |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Oct 18, 2011 7:56 am |
|
|
man... i swear CCS users have been reading my mind this past week... ethier asking the questions i have or developing similar code...
im developing a little widget to decode/clone ANY ir protocol...main interest is cloning....
i finished it monday night... as in: its working... but im still in the cleanup/optimize part that comes after working code....
already tried LG, Panasonic, Sony, and cable box remote... no problem.
its not as nice as this one though.... good job! _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
|
Posted: Wed Oct 19, 2011 11:03 am |
|
|
wow very - very god job . im also think as just u think _________________ sahu |
|
|
sahu77
Joined: 08 Sep 2011 Posts: 202
|
|
Posted: Sun Feb 03, 2013 9:57 am |
|
|
what need change for rc5 ? pl guide me here ...
!
!
! _________________ sahu |
|
|
|