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

📄 main.c

📁 这是一个成熟的51系列单片机的REMOTE接受程序。
💻 C
字号:
#include <main.h>
#include <stdio.h>                /* prototype declarations for I/O functions */
#include <intrins.h>                                    

#define FALSE   (0)
#define TRUE    (1)

/*------------------------------------------------------------------------------
 System clock:6M    指令周期=机器周期=1 / (6M/12)=1 / 0.5M=2us
------------------------------------------------------------------------------*/
void delay_20us(UINT8 timer)  //10个指令周期*2 = 20us
{
	while(timer)
	{
		_nop_();_nop_();
		_nop_();_nop_();
		//_nop_();_nop_();
		//_nop_();_nop_();
		//_nop_();_nop_();
		timer--;
	}   	
}
/*------------------------------------------------
The main C function.  Program execution starts
here after stack initialization.
------------------------------------------------*/
void main (void) 
{
	UINT8 temp = 0;
	IE    	= 0;   	// disable all interrupt
	TMOD 	= 0x11;  // Timer1 Mode 1, Timer0 Mode 1;
	//TMOD 	= 0x01; // Timer0 Mode 1;
  	EX0   	= 0;     // disable external interrupt 0, IE[0]
	EX1		= 0;
  	//ET0 	= 1;   	// enable bTimer 0
  	//IT0   = 1;     // falling endge generate interrupt, TCON[0]
  	INT0 	= 1;
	IT1   	= 1;     // falling endge generate interrupt, TCON[0]
  	INT1 	= 1;
  	while (1) 
  	{
  		//printf ("Hello World\n");   /* Print "Hello World" */
  		//TI = 0;
  		if(temp)
		{
    		TEST_PIN = 1;     		    
			TEST_PIN1 = 1;
			TEST_PIN2 = 1;
			temp = 0;
		}
		else
		{
    		TEST_PIN = 0;     		    
			TEST_PIN1 = 0;
			TEST_PIN2 = 0;
			temp = 1;
		}
		delay_20us(1);
  	}
}

/*遥控接收在此中断程序内完成
  数据放在ir_data内*/
/************************************/
/*  Interrupt for IR subroutine     */
/*  external interrupt source INT0  */
/************************************/
void int1Remote(void) interrupt 2
{
	register BYTE i;
	unsigned int count,count1;		/*计时变量*/
	unsigned int  temp0,temp1,ir_code=0;
	ir_enable=TRUE;
	TMOD=0x11;             /*timer 0 operate in 16 bit mode*/
	EX1=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*/
		EX1=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(INT1==1)
			{
				TR0=0;
				if(ir_data==REMOTE_KEY_VO_DEC||ir_data==REMOTE_KEY_VO_INC||
				ir_data==REMOTE_KEY_DECR||ir_data==REMOTE_KEY_INCR)
				{
				ir_enable=TRUE;	/*注意:ir_data不变*/
				EX1=0;
				}
				else
				{
				ir_data=0;
				ir_enable=FALSE;
				EX1=1;
				TR0=1;
				}
				return;
			}
		}
		else
		{
			ir_enable=FALSE;   /*not suitable for off code*/
			EX1=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(INT1==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 */
			}
		}                /*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(INT1==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=(BYTE)(temp1);     /*true code effective*/
				#ifdef REMOTE_CODE
				ir_data=0xff-ir_data;
				ir_data=((ir_data<<4)&0xf0)|((ir_data>>4)&0x0f);
				ir_data=((ir_data<<2)&0xcc)|((ir_data>>2)&0x33);
				ir_data=((ir_data<<1)&0xaa)|((ir_data>>1)&0x55);
				#endif
				ir_enable=TRUE;            /*set ir flag*/
				EX1=0;
//				IR_HEAD	=	count1*2;
				return;
			}
		}
	}
	else
	{	ir_enable=FALSE;
	EX1=1;}
}

⌨️ 快捷键说明

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