⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remote.c

📁 用C写的51遥控测码程序,结果在LCM1602上显示
💻 C
字号:

#include "reg51.h"
#include "remote.h"
#include "common.h"


bit   ir_enable;
uchar ir_data;
uchar idata ir_data1;
uint  ir_system_code;
uchar ir_data_code;

void  _nop_(void);
bit   Filter13(bit);

bit Filter13(bit le)
{
	bit r;
	r=INT0;
	if(le==1)
	{
		_nop_();
		_nop_();
		_nop_();
		r&=INT0;
		_nop_();
		_nop_();
		_nop_();
		r&=INT0;
	}
	else
	{
		_nop_();
		_nop_();
		_nop_();
		r|=INT0;
		_nop_();
		_nop_();
		_nop_();
		r|=INT0;
	}
	if(r==le)
		return 1;
	else
		return 0;
}
/*遥控接收在此中断程序内完成
  数据放在ir_data内*/
/************************************/
/*  Interrupt for IR subroutine     */
/*  external interrupt source INT0  */
/************************************/
void int1Remote(void) interrupt 0
{
/*    static uWORD IR_HEAD=20000;//	=	(XCLK_MCU*3/4);
	static uWORD OFF_CODE=10000;//  =   (IR_HEAD/2);
	uWORD OFF_REP;//	=	(IR_HEAD/4);
	uWORD REP_CODE;//	=	(IR_HEAD/16);
	uWORD DELTA ;//    = 	300;//250
	uWORD CODE_0_TIME;//=	(IR_HEAD/16);
	uWORD CODE_1_TIME;//=	(CODE_0_TIME*3);*/
	register uchar i;
	uint count,count1;		//计时变量
	uint  temp0,temp1,ir_code=0;
	ir_enable=true;
	TMOD=0x11;             /*timer 0 operate in 16 bit mode*/
	EX0=0;
	TL0=0;
	TH0=0;
	TR0=1;
	/************************************/
	/*   Detect IR Head                 */
	/************************************/
	do                /*Detect IR head 9mS*/
	{
		count=(TL0+TH0*256);   /*count :us unit */
		if(Filter13(1)==1) 
			break;
	}
	while(count<=(IR_HEAD+DELTA));
		TR0=0;			/*停止计时*/
		count=(TL0+TH0*256);   /*count :us unit */

		if(Filter13(0)==1||((count<(IR_HEAD-DELTA*25))))//&&(IR_HEAD!=0xffff)))//||count<(7000))//IR_HEAD!=0xffff)       
		/*for some noise disturb IR*/
		{
			ir_enable=false; /* no IR head or not suitable for Ir head*/
			EX0=1;
			TR0=1;
			return;
		}
	/************************************/
	/*   Detect OFF CODE     4.5mS      */
	/************************************/
	while(Filter13(0)==1);
	TR0=0;
	TL0=0;
	TH0=0;
	TR0=1;
	do                /*Detect IR OFF CODE*/
	{
		count=(TL0+TH0*256);   /*count :us unit */
		if(Filter13(0)==1) break;
	}
	while(count<(OFF_CODE+DELTA));
	count1=count;
 /*  if(IR_HEAD==0xffff)IR_HEAD	=	count*2;//10738//9000//7500
	OFF_CODE  =   IR_HEAD/2;//5369//4500//3750
	OFF_REP	=	(IR_HEAD/4);//2684//2250//	1875
	REP_CODE	=	(IR_HEAD/16);//667//467
	DELTA     = 	300;//250
	CODE_0_TIME=	(IR_HEAD/16);//667//557//467
	CODE_1_TIME=	(CODE_0_TIME*3);//2000//1670//1400*/
	TR0=0;
	count=(TL0+TH0*256);   /*count :us unit */
	if(count<=(OFF_CODE-DELTA*2))
	{
		/*重复码OFF=2.25*/
		if((count>(OFF_REP-DELTA)&&count<(OFF_REP+DELTA)))
		{
			TR0=0;
			TL0=0;
			TH0=0;
			TR0=1;
			do
			{
				count=(TL0+TH0*256);   /*count :us unit */
				if(Filter13(1)==1) break;
			}while(count<=(REP_CODE+DELTA));
			TR0=0;
			count=(TL0+TH0*256);   /*count :us unit */
			if(INT0==1)
			{
				TR0=0;
				if(ir_data==0xdf)
				{
					ir_enable=true;	//注意:ir_data不变
					EX0=0;
				}
				else
				{
					ir_data=0;
					ir_enable=false;
					EX0=1;
					TR0=1;
				}
				return;
			}
		}
		else
		{
			ir_enable=false;   /*not suitable for off code*/
			EX0=1;
			TR0=1;
			return;
		}
	}
	/************************************/
	/*   Detect 16 bit address code     */
	/************************************/
	if(ir_enable==true)
	{
		TMOD=0x11;           /*counter start with INT0 and TR0*/
		ir_code=0;		/*初始化变量*/
		for(i=0;i<16;i++)
		{
			while(INT0==0);
			TL0=0;
			TH0=0;
			TR0=1;
			do
			{
				count=(TL0+TH0*256);   /*count :us unit */
				if(Filter13(0)==1) break;
			}while(count<=(CODE_1_TIME+DELTA));	/*INT0为高电平时计时*/
			TR0=0;
			ir_code<<=1;
			if((count>(CODE_1_TIME-DELTA))&&(count<=(CODE_1_TIME+DELTA)))  /*detect 1 code  2.24mS-0.56mS=1.68mS*/
				ir_code++;
			else if((count>(CODE_0_TIME-DELTA))&&(count<=(CODE_0_TIME+DELTA)));  /*detect 0 code 1.12mS-0.56mS=0.56mS*/
			else
			{
			 	ir_enable=false;
				TR0=1;
				break;           /*not suitable for address code */
			}
		}
		ir_system_code=ir_code;             /*end for*/
	}
	/************************************/
	/*   Detect 16 bit data code        */
	/************************************/
	if(ir_enable==true)//&&ir_code==IR_SYSTEM_CODE)
	{
		TMOD=0x11;           /*counter start with INT0 and TR0*/
		ir_code=0;
		for(i=0;i<16;i++)
		{
			while(INT0==0);
			TL0=0;
			TH0=0;
			TR0=1;
			do
			{
				count=(TL0+TH0*256);
				if(Filter13(0)==1) break;
			}while(count<=(CODE_1_TIME+DELTA));	/*INT0为高电平时计时*/
			TR0=0;
			//count=(TL0+TH0*256);   /*count :us unit */
			ir_code<<=1;
			if((count>(CODE_1_TIME-DELTA))&&(count<=(CODE_1_TIME+DELTA)))  /*detect 1 code 1.68mS*/
				ir_code++;
			else if((count>(CODE_0_TIME-DELTA)&&count<=(CODE_0_TIME+DELTA)));  /*detect 0 code 0.56mS*/
			else
			{
				ir_enable=false;
				TR0=1;
				break;           /*not suitable for address code */
			}
		}                /*end for*/
		if(ir_enable==true)
		{
			temp0=ir_code&0xff00;
			temp0=~temp0;
			temp0>>=8;
			temp1=ir_code&0x00ff;
			if(temp0!=temp1)
				ir_enable=false;
			else
			{
				ir_data=(uchar)(temp1);     /*true code effective*/
				ir_data_code=ir_data;
				ir_enable=true;            /*set ir flag*/
				EX0=0;
//				IR_HEAD	=	count1*2;
				return;
			}
		}
	}
	else
	{	
		ir_enable=false;
	  EX0=1;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -