当前位置: 代码迷 >> 单片机 >> 外部中断红外接收发射程序,不懂,请
  详细解决方案

外部中断红外接收发射程序,不懂,请

热度:88   发布时间:2016-04-28 15:46:22.0
外部中断红外接收发射程序,不懂,请高手指点
void UartIRInit(void)
{
/*----set INTP3-----------------------*/
PIF3 = 0;
PM3.1 = 1;
EGP = 0x00; //EGP3=0
EGN = 0x08; //EPN3=1
PMK3 = 0; //enable interrupt 
PPR3 = 1; //low priority
/*---set TMH1-------------------------*/
TMHMD1 = 0;
TMHMD1 = 0x30;//00110000
CMP01 = 103; //8000000/64/104=1201.92
TMMKH1 = 0; //enble interrupt
TMPRH1 = 0; //high priority
/*---set TMH0-------------------------*/
TMHMD0 = 0;
PM3.2 = 0;
P3.2 = 0;

CMP00 = 104;//8000000/(104+1) = 76.190K

TMHMD0 = 0x01;//enable output
TMMKH0 = 1; //disable interrupt
/*--------byte set--------------------*/
//RXIR=0;
//TXIR
UartIR.Monit500ms = 0;
UartIR.Monit5s = 0;
UartIR.Lapse30ms = 0;
FUartIR_RStart = 0;
FUartIR_REnd = 0;
FUartIR_Treat = 0;
FUartIR_Ting = 0;
}

__interrupt void IntP3(void)
{
CMP01 = 51;
TMHE1 = 1;
PMK3 = 1;
IRB = 0;//infrade buf
IRBits = 0;
IRParity = 0;
}

__interrupt void IntTMH1(void)
{
TMHE1 = 0;
CMP01 = 103;
TMHE1 = 1;
if (FUartIR_Ting == 1)
{
if (IRBits < 9) //data bit
{
if ((IRB&0x01) == 0x0)
TMHE0 = 1;
else
{
IRParity++; //even parity
TMHE0 = 0;
P3.2 = 0;
}
IRB >>= 1;
IRBits++;
}
else if (IRBits == 9) //parity bit
{
if (IRParity.0 == 0)
{
TMHE0 = 1;//even 1 bits,even parity is 0
}
else
{
TMHE0 = 0;
P3.2 = 0;
}
IRBits++;
}
else if (IRBits == 10)//stop bit
{
TMHE0 = 0;
P3.2 = 0;
IRBits++;
}
else if (IRBits == 11)
{
TMHE1 = 0;//end the transmit

/*-----receive state------------------------------------------------------*/
else
{
if (IRBits == 0) //the first judge is 0.416ms
{

if (P3.1 == 1)//start bit is error
{
PMK3 = 0;
TMHE1 = 0;//end the receive
}
else 
{
IRBits = 1;//++;
}
}
else if (IRBits < 9)//data bits
{
IRB >>= 1;
if (P3.1 == 1)
{
IRB |= 0x80;
IRParity++;
}
IRBits++;
}
else if (IRBits == 9) //parity bit
{
if (((P3.1==0)&&(IRParity.0==0)) || 
((P3.1==1)&&(IRParity.0==1))) //even check right
{
IRBits++;
}
else
{
PMK3 = 0;
TMHE1 = 0; //stop receive
IRBits = 0;
}
}
else//end bit 
{
PMK3 = 0;//enable receive last byte
TMHE1 = 0;//because priority of TMH1 higher than P5

if (P3.1 == 1)//stop bit
{
UartIR.Monit500ms = 0;
UartIR.Monit5s = 0;

if (FUartIR_RStart == 0)
{
if (IRB == 0x68)
{
FUartCom = 1;
UartIR.Buf[0] = 0x68;
UartIR.DataNum = 1;
FUartIR_RStart = 1;
}
}
else
{
if (UartIR.DataNum < MAXNUM_UART)
{
UartIR.Buf[UartIR.DataNum] = IRB;
if (UartIR.DataNum == 7)
{
if (IRB != 0x68)
FUartIR_RStart = 0;
}
else if (UartIR.DataNum >= (UartIR.Buf[9]+11))
{
if (IRB == 0x16)
{
PMK3 = 1; //end the receive
FUartIR_REnd = 1;
}
FUartIR_RStart = 0;
}
UartIR.DataNum++;
}
else 
FUartIR_RStart = 0;
  相关解决方案