定时器外部中断红外通讯问题
我是红外接收波特率1200bps,我定时器是按照这个时间中断读每位的数据吗?我发送应该是38khz,我数据从哪里来呢,定义一个字节,一位一位移动,那校验位要求怎么处理?下面是源代码,请帮忙看看。
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
#ifndef __IR30MS
if (UartIR.FENum != 0)
{
UartIR.FENum--;
IRB = 0xfe;
IRBits = 1; //start bit
IRParity = 0;
CMP01 = 103;
TMHE1 = 1;
TMHE0 = 1;
}
else if (UartIR.DataNum < (UartIR.Buf[9]+12))
{
IRB = UartIR.Buf[UartIR.DataNum++];
IRBits = 1;//start bit
IRParity = 0;
CMP01 = 103;
TMHE1 = 1;
TMHE0 = 1;//start bit
}
else
{
FUartIR_Ting = 0;
FUartIR_RStart = 0;
PIF3 = 0;
PMK3 = 0;//enable P5 interrupt
TMHE1 = 0;//stop TMH1
}
#endif
}
}
/*-----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;
}
}
}
}
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |