定时器外部中断红外通讯问题
我是红外接收波特率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;
}
}
}
}
}
回复
有奖活动 | |
---|---|
【有奖活动】智能可穿戴设备AR/VR如何引领科技新潮流! | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
【有奖活动】震撼来袭!这场直播将直击工程师的心灵! | |
“我想要一部加热台”活动,不花钱赢取开发好工具!活动开始了哦 | |
【有奖活动】伙伴们:您的“需求”解决了吗? |
打赏帖 | |
---|---|
老胖子聊电路--分析一个反馈电路被打赏50分 | |
放大↔震荡被打赏50分 | |
【换取手持数字示波器】RTduino驱动ssd1306+sht31温湿度计被打赏40分 | |
【换取手持数字示波器】称植国产开源TobudOS到雅特力AT32F425被打赏40分 | |
【分享评测,赢取加热台】+Telink-泰凌微电子B91点灯篇被打赏20分 | |
【换取手持数字示波器】+精英板驱动LCD屏被打赏50分 | |
【换取手持数字示波器】+DFR0216全彩灯带WS2812示例分享被打赏50分 | |
【换取手持数字示波器】安装雅特力国产IDE-AT32IDE被打赏30分 | |
【换取手持数字示波器】+DFR0654板卡驱动数码管和串口功能分享被打赏50分 | |
【换取手持数字示波器】+DFPlayerMini模块推喇叭示例分享被打赏50分 |