📄 infrayreceive.c
字号:
//***************************************************************//
//红外信号接收处理程序
//***************************************************************//
#include "SPCE061V004.h"
static unsigned int GetIOA_0(void);//读取IOA口数值的函数
static unsigned int GetCode_NBit(int N);//读取N位的红外信号的函数
static unsigned int CodeEsti(unsigned int m);//数据验证函数
unsigned int InfRayReceive(void)//红外信号接收程序
{
unsigned int TimerADat,InfRayAdress0=0,InfRayAdress1=0,InfRayCode=0,Code;
if(GetIOA_0())
goto _error;
*P_TimerA_Ctrl = 0x0032;//TimerA时钟源:32768Hz
*P_TimerA_Data = 0x0000;//启动定时计数器
while(1)//等待头脉冲低电平结束
{
TimerADat=*P_TimerA_Data;
if(TimerADat>350)//如果超时,退出
goto _error;
if(GetIOA_0())
break;
}
//if(TimerADat<200)//如果头脉冲时间长度错误,退出
//goto _error;
*P_TimerA_Data = 0x0000;//启动定时计数器
while(1)//等待头脉冲高电平结束
{
TimerADat=*P_TimerA_Data;
if(TimerADat>160)//如果超时,退出
goto _error;
if(!GetIOA_0())
break;
}
//if(TimerADat<100)//如果头脉冲时间长度错误,退出
//goto _error;
/*if(TimerADat<110)//如过头脉冲长度表明它是重复码,则做相应处理
{
InfRayCode=0x01fe;
goto _recend;
}*/
/*InfRayAdress0=GetCode_NBit(10);//接收26位的系统码
if(InfRayAdress0==0)
goto _error;*/
InfRayAdress1=GetCode_NBit(16);
if(InfRayAdress1==0)
goto _error;
InfRayCode=GetCode_NBit(16);//接收16位的资料码及资料反码
_recend:
*P_TimerA_Data = 0x0000;//启动定时计数器
while(1)//等待结束位结束
{
TimerADat=*P_TimerA_Data;
if(TimerADat>20)//如果超时,退出
goto _error;
if(GetIOA_0())
break;
}
Code=CodeEsti(InfRayCode);//判断数据是否错误
return Code;
_error://如果接收过程中发现错误,则该程序返回0xff
return 0xff;
}
unsigned int GetIOA_0(void)//读取IOA口数值的函数
{
unsigned int i=*P_IOA_Data;//读取IOA的数据
i&=0x0001;
return i;
}
unsigned int GetCode_NBit(int N)//读取16位的红外信号的函数
{
unsigned int n,TimerADat,Code16Bit=0;
for(n=N;n!=0;n--)
{
*P_TimerA_Data = 0x0000;//启动定时计数器
while(1)//等待脉冲低电平结束
{
TimerADat=*P_TimerA_Data;
if(TimerADat>21)//如果超时,退出
goto _recerror;
if(GetIOA_0())
break;
}
while(1)//等待脉冲高电平结束
{
TimerADat=*P_TimerA_Data;
if(TimerADat>75)//如果超时,退出
goto _recerror;
if(!GetIOA_0())
break;
}
TimerADat=*P_TimerA_Data;
//if(TimerADat<20)//如果脉冲时间长度错误,退出
//goto _recerror;
if(TimerADat>55)//如果脉冲时间长度大于55,则该位为1
Code16Bit|=0x8000;
if(n!=1)
Code16Bit>>=1;
}
return Code16Bit;
_recerror://如果接收过程中发生错误,返回0
return 0x0000;
}
unsigned int CodeEsti(unsigned int m)//数据验证函数
{
unsigned int n;
n=~m;
m%=0x0100;
n/=0x0100;
if(n!=m)
return 0xff;
return m;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -