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

📄 contrler.c

📁 遥控器编码的c51程序,包括遥控器编码,I2C操作编程等
💻 C
字号:
//File name: contrler.c 
//Founded by Wang Feng on Oct 19,2004
//Last modified on Oct 25,2004

#include "I2C.h"
bit bKeyUP;   //Whether the key of contrler is up
bit bNewCodeFlag; //If the code just received is a new code
bit bNewKey=False; //A key press action occurs or no
unsigned char ucSysCode,ucData,ucCnt,ucCount;
//The system code,code data,the count of T1 interrupt after received last code,
//the count of the pulses
unsigned char bdata uc_tmpKeyCode;
//Stores the received code temporarily
sbit D_0=uc_tmpKeyCode^0;
sbit D_1=uc_tmpKeyCode^1;
sbit D_2=uc_tmpKeyCode^2;
sbit D_3=uc_tmpKeyCode^3;
sbit D_4=uc_tmpKeyCode^4;
sbit D_5=uc_tmpKeyCode^5;
sbit D_6=uc_tmpKeyCode^6;
sbit D_7=uc_tmpKeyCode^7;
sbit LED1=P1^7;
sbit LED2=P1^7;

void Delay(/*unsigned char*/);

void LightFlash(unsigned char);
/*=====================================================
Function name: void main()
Funciton: 
	Used to decode the signal carried by the infrared light,
	and light the LED according to the data code decoded.
---Founded by Wang Feng on Oct 19,2004
---Modified by Wang Feng on Ocr 20,2004 
=====================================================*/
void main()
{
bit i=0;
bit n=0; 
char  *s ="abcdefghijkl";
char str[10]={0};

i=WriteStr(0xa0,0,s,2, I2C0) ;
n=ReadStr(0xa1,0,str,2, I2C0);
_Nop();
}
void main1()
{
    unsigned char sla, suba, *s, uc_data[16], j, k;
//	unsigned int i;
	bit breturn;
    TMOD=0x10;//Set the timer1 works at model 1(16 bit counter)
	TR1=0;
	IT0=1;    // Triggered by the rising edge
	bKeyUP=True; 
	bNewCodeFlag=False;
	ucCnt=0;
	ucCount=0;
	LightFlash(5); //Flash the LED five times to make sure that the LED is OK
	EX0=1;
	TR1=1;
	EA=1;     //Turn on the interrupt.
    
	for(;;)
	{	
	k = 1;
    s=uc_data;
    sla=0xa0+0x02*k;   
    suba=j*16;                /*子地址*/
	ReadStr(sla,suba,s,16, I2C0);
    WriteStr(sla,suba,s,16, I2C1) ; 
		if(bNewKey) //If there is a new key code,turn off the external interrupt and  then light the LED
		{
			EX0=0;
			bNewKey=False;
			if((ucSysCode&0x82)==0x82)
				{
					//LightFlash(ucData&0x7f);
					if((ucData&0x7f)==1)
					{
						s=uc_data;
	 					breturn=1;
		 				for(k=0;k<4;k++)               /*AT24C16有4个bank,每个bank有各自的器件地址*/
		 				{
		 					sla=0xa0+0x02*k;              /*bank地址*/
		 					for(j=0;j<16;j++)            /*每个bank有16 page,子地址不同*/
		 					{
		 						suba=j*16;                /*子地址*/
					 			if((ReadStr(sla,suba,s,16, I2C0))==0) {breturn=0;break;}    /*从EEPROM0读出一页数据*/
					 			if((WriteStr(sla,suba,s,16, I2C1))==0) {breturn=0;break;}    /*把该页数据写入到EEPROM1*/
						 		Wait(sla, I2C1);                                     /*等待写操作完成*/
							}
					  	}
					if(breturn==1)
				   	LightFlash(3);
					else
					LightFlash(1);

				 	}
				}
				
			EX0=1;
		  }
	_Nop();
	}	
}
/*=====================================================
Function name: INT1_int()
Funciton: 
	Used to decode the signal carried by the infrared light,
	and then stores the codes.
---Founded by Wang Feng on Oct 19,2004
---Modified by Wang Feng on Ocr 20,2004 
=====================================================*/
void INT1_int() interrupt 0 using 1
{
	static bit bHeadSignFlag; //Flag of the synchro pulse
	unsigned int uiPulseWidth; //The space time between two pulses
	bit bData;   //Stores the bit just received.
	static unsigned char ucSysTmp,ucDataTmp;
	//Two temporary variables used to store the system code and data code
	uiPulseWidth=TH1;
	uiPulseWidth<<=8;
	uiPulseWidth+=TL1;   //Load the value of timer1 down to uiPulseWidth.
						//Now the value of uiPulseWidth is the width of pulse.
	TH1=0;
	TL1=0;             //Clear the Timer1 to count width of the next pulse.  
	ucCount++;         
	if(!bHeadSignFlag)
	{
		if((uiPulseWidth>=SYNL)&&(uiPulseWidth<=SYNU))
		{
			bHeadSignFlag=True;  //If find the synchro pulse ,set it.
			if(bKeyUP)
				bNewCodeFlag=True; 
			else
				bNewCodeFlag=False; //Judge if it a repeat code,and then set it.
			bKeyUP=False;
			ucCount=2;
		}
		else
			ucCount=0;
		return;
	 }
	if(bHeadSignFlag)
	{
	/*=====================*/     //Added by Wang Feng on Oct.20,2004
		if((uiPulseWidth>=SYNL)&&(uiPulseWidth<=SYNU))
		{
			ucCount=2;
			return;
		}
	/*=====================*/
		if(ucCount==19)                 
		{    
			if((uiPulseWidth>=ENDL)&&(uiPulseWidth<=ENDU))
			{
				ucSysCode=ucSysTmp|0x80;      //If the synchro pulse have been found,then judge  
				if(bNewCodeFlag)			//whether the pulse an end pulse.It that is true,
				{							//store the code.Clear the flag of synchro pulse.
					ucData=ucDataTmp&0x7f;
					bNewCodeFlag=False;
				}
				else
					ucData=ucDataTmp|0x80;
			   bNewKey=True;
			}
			bHeadSignFlag=False;
			ucCount=0;
			return;
		}
		if((uiPulseWidth>=BIT1L)&&(uiPulseWidth<=BIT1U)) bData=1;
		else if((uiPulseWidth>=BIT0L)&&(uiPulseWidth<=BIT1U)) bData=0;
		else                           //Judge the pulse is 0 or 1,and stored it to bData.
		{
			bHeadSignFlag=False;
			ucCount=0;
			return;
			}
		if(bHeadSignFlag)
		switch(ucCount)
		{
			case 3:D_7=bData;break;
			case 4:D_6=bData;break;    //Load the bit Value from bData to 
			case 5:D_5=bData;break;		//the uc_tmpKeyCode,a BData type variable.
			case 6:D_4=bData;break;
			case 7:D_3=bData;break;
			case 8:D_2=bData;break;
			case 9:D_1=bData;break;
			case 10:D_0=bData;
			       ucSysTmp=uc_tmpKeyCode;break;
			case 11:D_7=bData;break;
			case 12:D_6=bData;break;
			case 13:D_5=bData;break;
			case 14:D_4=bData;break;
			case 15:D_3=bData;break;
			case 16:D_2=bData;break;
			case 17:D_1=bData;break;
			case 18:D_0=bData;
			       ucDataTmp=uc_tmpKeyCode;break;
			default:
				break;
			}
		}
}
/*=====================================================
Function name: void T1_INT()
Funciton: 
	Used to decode the signal carried by the infrared light,
	and light the LED according to the data code decoded.
---Founded by Wang Feng on Oct 19,2004
---Modified by Wang Feng on Ocr 20,2004 
=====================================================*/
void T1_INT() interrupt 3 using 2
{
	ucCnt++;
	if(ucCnt>=2)
	{
		bKeyUP=True;
		ucCnt=0;
	}

}
/*=====================================================
Function name: void LightFlash(unsigned char Times)
Funciton: 
	Light the LED accord the Times.
Parameter:
	Times: times that LED needed to ON and OFF.
---Founded by Wang Feng on Oct 19,2004
---Modified by Wang Feng on Ocr 20,2004 
=====================================================*/

void LightFlash(unsigned char Times)
{	
	unsigned char i;
	for(i=0;i<Times;i++)
	{
		LED1=0;
		Delay();
		LED1=1;
		Delay();
	  }
}
/*=====================================================
Function name: void Delay()
Funciton: 
	Delay the time by a limited circle.
---Founded by Wang Feng on Oct 19,2004
---Modified by Wang Feng on Ocr 20,2004 
=====================================================*/
void Delay()
{
 unsigned int i;
 for(i=0;i<32000;i++);
}

⌨️ 快捷键说明

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