//When the IR receive pin goes low and interrupt is generated
// IR is collected by starting timer 2 in the first falling edge of the in
// then on every other falling edge, the timer value is saved and the timer restarted .
// the captured time is then used to get the IR data
// a "start of data" is 13.5Msec,a "1" is 2.25Msec,a "0" is 1.12 msec and a "repeat" is 11.25msec.
// the counter increments at 1.085 Usec
// I allow a fairly large tolerance to time jitter but there are no false triggers seen.
switch(IRstate)
{
case IR_idle:
TL1=0;
TH1=0;
TR1=1;
IRstate=IR_waitstart;
IRtimer=26;
break;
case IR_waitstart: //P2_4=!P2_4;
TR1=0;
time=TH1;
time =(time <<8)+TL1;;
TL1=0;
TH1=0;
TR1=1;
if ((time > msec_12p5)&&(time < msec_15)) // greater than 12.5Msec & less than 15 msec = start code
{
IRaddr=0;
_IRaddr=0;
IRdata=0;
_IRdata=0;
bits=1;
IRstate=IR_getaddr;
}
else if ((time > msec_9)&&(time < msec_12p5))// less than 12.5Msec and greater than 9 msec =Repeat code
{
IR_repeat=2;
IRstate=IR_idle;
}
else
{ // to short, bad data just go to idle
IRstate=IR_idle;
}
break;
case IR_getaddr: // P2_4=!P2_4;
TR1=0;
time=TH1;
time =(time <<8)+TL1;;
TL1=0;
TH1=0;
TR1=1;
if ((time>msec_2p5)||(time<msec_0p9))// if > 2.5msec or shorter than .9Msec bad data , go to idle
{
IRstate=IR_idle;
break;
}
if (time>msec_1p68)// greater than 1.68Msec is a 1
{
IRaddr|= bits;
}
bits=bits<<1;
if (!bits)
{
IRstate=IR_getaddrinv;
bits=1;
}
break;
case IR_getaddrinv: //P2_4=!P2_4;
TR1=0;
time=TH1;
time =(time <<8)+TL1;;
TL1=0;
TH1=0;
TR1=1;
if ((time>msec_2p5)||(time<msec_0p9))// if > 2.5msec or shorter than .9Msec bad data , go to idle
{
IRstate=IR_idle;
break;
}
if (time>msec_1p68)// greater than 1.68Msec is a 1
{
_IRaddr|= bits;
}
bits=bits<<1;
if (!bits)
{
IRstate=IR_getdata;;
bits=1;
}
break;
case IR_getdata:
TR1=0;
time=TH1;
time =(time <<8)+TL1;;
TL1=0;
TH1=0;
TR1=1;
if ((time>msec_2p5)||(time<msec_0p9))// if > 2.5msec or shorter than .9Msec bad data , go to idle
{
IRstate=IR_idle;
break;
}
if (time>msec_1p68)// greater than 1.68Msec is a 1
{
IRdata|= bits;
}
bits=bits<<1;
if (!bits)
{
IRstate=IR_getdatainv;
bits=1;
}
break;
case IR_getdatainv:
TR1=0;
time=TH1;
time =(time <<8)+TL1;;
TL1=0;
TH1=0;
TR1=1;
if ((time>msec_2p5)||(time<msec_0p9)) // if > 2.5msec or shorter than .9Msec bad data , go to idle
{
IRstate=IR_idle;
break;
}
if (time>msec_1p68)// greater than 1.68Msec is a 1
{
_IRdata|= bits;
}
bits=bits<<1;
if (!bits) // we have it all , now we make sure it is a NEC code from the CHS IR transmitter
{ // make sure address,~address are correct , data ,~data are correct and address is 0.
IR_ready=((IRaddr^_IRaddr)==0xff)&&((IRdata^_IRdata)==0xff)&&(IRaddr==0);
if(IR_ready)
{
IRstate=IR_idle;
}
}
break;
default:
IRstate=IR_idle;
break;
}
} 作者: 51hei電控2112312 時間: 2014-12-21 04:35
很好,正需要作者: bhjyqjs 時間: 2015-6-2 10:03
真不錯的東東,頂一個