#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit IR = P2^1;
sbit key1 = P1^1;
sbit key2 = P1^2;
void Delay();
void SendInstructionCode();
void SendCodeZero();
void SendCodeOne();
void UsrCode();
void Key1();
void Key2();
void KeyScan();
void SendKeyCode();
/*******发送引导码函数**********/
void SendInstructionCode()
{
TH0 = 0xDC; //9000us
TL0 = 0xD8;
TR0 = 1;
IR = 0; //拉低IR
while(!(TF0 == 1)); //等待定时器溢出,即使IR持续低电平9000us
IR = 1;
TF0 = 0;
TR0 = 0;
TH0 = 0xEE; //4500us
TL0 = 0x6c;
TR0 = 1;
while(!(TF0 == 1)); //等待定时器溢出,即使IR持续高电平4500us
IR = 0;
TF0 = 0;
TR0 = 0;
}
/*********发送0函数*********/
void SendCodeZero()
{
TH0 = 0xFD; //560us
TL0 = 0xD0;
TR0 = 1;
IR = 0;
while(!(TF0 == 1));
IR = 1;
TF0 = 0;
TR0 = 0;
TH0 = 0xFD; //560us
TL0 = 0xD0;
TR0 = 1;
while(!(TF0 == 1));
IR = 0;
TF0 = 0;
TR0 = 0;
}
/*********发送1函数*********/
void SendCodeOne()
{
TH0 = 0xFD; //560us
TL0 = 0xD0;
TR0 = 1;
IR = 0;
while(!(TF0 == 1));
IR = 1;
TF0 = 0;
TR0 = 0;
TH0 = 0xF9; //1680us
TL0 = 0x70;
TR0 = 1;
while(!(TF0 == 1));
IR = 0;
TF0 = 0;
TR0 = 0;
}
/*********用户码函数*********/
void UsrCode() //发送用户码 11111111 00000000
{
uchar i;
for(i=0;i<8;i++)
{
SendCodeOne();
}
for(i=0;i<8;i++)
{
SendCodeZero();
}
}
/*********键值1函数********/
void Key1() //0x3d 0011 1101 ~0x3d 1100 0010
{
SendCodeZero();
SendCodeZero();
SendCodeOne();
SendCodeOne();
SendCodeOne();
SendCodeOne();
SendCodeZero();
SendCodeOne();
SendCodeOne();
SendCodeOne();
SendCodeZero();
SendCodeZero();
SendCodeZero();
SendCodeZero();
SendCodeOne();
SendCodeZero();
}
/*********键值2函数********/
void Key2() //0xe3 1110 0011 ; ~0xe3 0001 1100
{
SendCodeOne();
SendCodeOne();
SendCodeOne();
SendCodeZero();
SendCodeZero();
SendCodeZero();
SendCodeOne();
SendCodeOne();
SendCodeZero();
SendCodeZero();
SendCodeZero();
SendCodeOne();
SendCodeOne();
SendCodeOne();
SendCodeZero();
SendCodeZero();
}
/*********按键扫描函数********/
void KeyScan() //返回值是按下按键的按键值,没按下返回100,即不发送
{
uchar key = 100;
uchar keybuf [2]={ 1,1};
static uchar backup[2]= { 1,1};
keybuf [0]= key1;
if(keybuf[0] != backup[0])
{
Delay(100);
if(keybuf[0] == key1)
{
if(backup[0] == 0)
key = 1;
}
}
if(keybuf[1] != backup[1])
{
Delay(100);
if(keybuf[1] == key2)
{
if(backup[1] == 0)
key = 2;
}
}
return key;
}
/*********发送函数********/
void SendKeyCode(uchar key)
{
if(key == 1)
{
SendInstructionCode();
UsrCode();
Key1();
}
if(key == 2)
{
SendInstructionCode();
UsrCode();
Key2();
}
}
void Delay(uint z)
{
uint t1,y;
for(t1=z; t1>0; t1--)
for(y=110; y>0; y--);
}
void main()
{
uchar key;
TMOD = 0x01;
while(1)
{
key = KeyScan();
SendKeyCode(key);
}
}
|