#include <reg52.h>
#include <intrins.h>
sbit InfraredSend = P1^4; //红外发射引脚
sbit SW1 = P3^0; //独立按键定义
sbit SW2 = P3^1;
unsigned char KeyCode[23][4] ={{0x00,0xff,0x06,~0x06},{0x00,0xff,0x0d,~0x0d},{0x00,0xff,0x18,~0x18},{0x00,0xff,0x1a,~0x1a},{0x00,0xff,0x1b,~0x1b}, //Mute Code, Power Code, 1 Code, 2 Code, 3 Code
{0x00,0xff,0x14,~0x14},{0x00,0xff,0x16,~0x16},{0x00,0xff,0x17,~0x17},{0x00,0xff,0x10,~0x10},{0x00,0xff,0x12,~0x12}, //4 Code, 5 Code, 6 Code, 7 Code, 8 Code
{0x00,0xff,0x13,~0x13},{0x00,0xff,0x08,~0x08},{0x00,0xff,0x0c,~0x0c},{0x00,0xff,0x0f,~0x0f},{0x00,0xff,0x0a,~0x0a}, //9 Code, -/-- Code, 0 Code, Cycle Code, Video Code
{0x00,0xff,0xf1,~0xf1},{0x00,0xff,0x02,~0x02},{0x00,0xff,0x04,~0x04},{0x00,0xff,0x00,~0x00},{0x00,0xff,0x07,~0x07}, //Hode Code, Channel+ Code, Volume- Code, Menu Code, Volume+ Code
{0x00,0xff,0x06,~0x06},{0x00,0xff,0xfc,~0xfc},{0x00,0xff,0x0b,~0x0b}}; //Channel- Code, Enter Code, Display Code
void Delay100US(unsigned char x); //参数为1时延时100us
void Delay10US(unsigned char x); //参数为1时延时10us
void main(void)
{
unsigned char i,Bit,Byte,KeyCodeSend;
while(1)
{
SW1 = 1;
SW2 = 1;
if(SW1 == 0) //1键按下,发射一组数据,四个字节
{
InfraredSend = 1;//发射引导码9ms
Delay100US(90);
InfraredSend = 0;
Delay100US(45); //4.5ms.引导码
for(Byte = 0;Byte < 4;Byte++) //开始发射四个字节内容
{
KeyCodeSend = KeyCode[0][Byte];
for(Bit = 0;Bit < 8;Bit++)
{
InfraredSend = 1;//发射0.56ms
Delay10US(56);
// CarrierWave38K(21); //发射0.56ms
if(KeyCodeSend & 0x01) //由数据来判断高1.69ms还是0.56ms
Delay10US(169);
else
Delay10US(56);
KeyCodeSend >>= 1;
}
}
InfraredSend = 1;//发射0.56ms
Delay10US(56);
for(i = 0;i < 10;i++)
Delay100US(180); //结束正常控制在189ms
}
if(SW2 == 0) //2键按下,发射一组数据,四个字节
{
InfraredSend = 1;//发射引导码9ms
Delay100US(90);
InfraredSend = 0;
Delay100US(45); //4.5ms.引导码
for(Byte = 0;Byte < 4;Byte++) //开始发射四个字节内容
{
KeyCodeSend = KeyCode[1][Byte];
for(Bit = 0;Bit < 8;Bit++)
{
InfraredSend = 1;//发射0.56ms
Delay10US(56);
// CarrierWave38K(21); //发射0.56ms
if(KeyCodeSend & 0x01) //由数据来判断高1.69ms还是0.56ms
Delay10US(169);
else
Delay10US(56);
KeyCodeSend >>= 1;
}
}
InfraredSend = 1;//发射0.56ms
Delay10US(56);
for(i = 0;i < 10;i++)
Delay100US(180); //结束正常控制在189ms
}
/* 参考为1时延时100us */
void Delay100US(unsigned char X100us)
{
unsigned char i;
while(X100us--)
{
_nop_();
_nop_();
_nop_();
_nop_();
i = 12;
while(i--);
}
}
/* 参数为1时延时10us */
void Delay10US(unsigned char X10us)
{
while(X10us--)
{
_nop_();
_nop_();
}
}