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

📄 main.c

📁 红外遥控协议的发送程序
💻 C
字号:
#include <reg51.h>

typedef unsigned char uchar;
typedef unsigned short ushort;

sbit dKeyPort1=P1^7;
sbit dKeyPort2=P1^5;
sbit dKeyPort3=P1^4;
sbit dIrPort=P1^6;

void SysInit(void)
{
	TMOD=0x10;
	TH1=0xf8;
	TL1=0x30;
	ET1=1;
	EA=1;
	TR1=1;
}

void Delay100Us(uchar tInputCnt)
{
	uchar i;
	uchar j;

	for(i=0;i<tInputCnt;i++)
	{
		for(j=96;j;j--);
	}
}

#define dSendDelay		\
dIrPort=0;				\
Delay100Us(7);			\
dIrPort=1;

void SendChar(uchar tInputVal)
{
	const uchar pTempBitMap[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
	uchar i,j;

	for(i=0;i<8;i++)
	{
		dSendDelay;
		if( pTempBitMap[i]&tInputVal )
			Delay100Us(17);
		else
		{
			Delay100Us(6);
			for(j=50;j;j--);
		}
	}
}

void SendData(uchar tInputKeyVal)
{
#define dIrCustHi	0x89
#define dIrCustLo	0xd3

	dIrPort=0;
	Delay100Us(90);
	dIrPort=1;
	Delay100Us(45);
	SendChar(dIrCustHi);	//Send Ir Customer Code.
	SendChar(dIrCustLo);
	SendChar(tInputKeyVal);		//Send Key Val.
	SendChar(~tInputKeyVal);	//Send Key Val's Inv .
	dSendDelay;
}


uchar tUpKeyPressedCnt=0,tDownKeyPressedCnt=0,tSndKeyPressedCnt=0;
bit bUpKeyPressedFlag=0,bDownKeyPressedFlag=0,bSndKeyPressedFlag=0;

void main(void)
{
	uchar tTempVal=1;// The Speed.

	SysInit();

	while(1)
	{
		if( bUpKeyPressedFlag )
		{
			if( tTempVal<20 )
				tTempVal++;
			bUpKeyPressedFlag=0;
		}
		if( bDownKeyPressedFlag )
		{
			if( tTempVal>1 )
				tTempVal--;
			bDownKeyPressedFlag=0;
		}
		if( bSndKeyPressedFlag )
		{	// Send Speed, In case Value Lost, I Send Twice. 
			// The Interval about 20 ms.
			SendData(0xa0+tTempVal);
			Delay100Us(200);
			SendData(0xa0+tTempVal);

			bSndKeyPressedFlag=0;
		}
	}
}

//1ms Timer Interrupt.	Mainly capture Key Pressed.
void Timer1()interrupt 3
{	
	TH1=0XF8;
	if( dKeyPort1==0 )
	{
		if( ++tUpKeyPressedCnt==4 )
			bUpKeyPressedFlag=1;	
	}else 
		tUpKeyPressedCnt=0;
	if( dKeyPort2==0 )
	{
		if( ++tDownKeyPressedCnt==4 )
			bDownKeyPressedFlag=1;	
	}else 
		tDownKeyPressedCnt=0;
	if( dKeyPort3==0 )
	{
		if( ++tSndKeyPressedCnt==4 )
			bSndKeyPressedFlag=1;	
	}else 
		tSndKeyPressedCnt=0;



//	dIrPort=~dIrPort;
}

⌨️ 快捷键说明

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