#include
#define uchar unsigned char
sbit key0=P0^0;
sbit key1=P0^1;
sbit key2=P0^2;
sbit key3=P0^3;
sbit TX=P3^1;
//led=P2
uchar m;
void v0(); void v1(); void v2(); void v3();
void delay_315();
void Delay_ms(unsigned int ms) //nms延时
{
unsigned int i,j;
for(i=0;i
}
void main()
{
uchar i;
bit dong=0; //开定时器0中断
TMOD=0X01;
TL0 = 0xAE; //??????
TH0 = 0xFB;
EA=1;
ET0=1;
TR0=1;
TX=0;
while(1)
{
P2=0xff; //led
if(key0==0) //发送指令0
{
delay_315();
while(key0==0)
{
v3();v2();v0();v0();v0();m=0;while(m<40);
P2=0x7f;
}
dong=1;
}
if(key1==0) //指令1
{
delay_315();
while(key1==0)
{
v3();v2();v0();v0();v1();m=0;while(m<40);
P2=0xbf;
}
dong=1;
}
if(key2==0) //指令2
{
delay_315();
while(key2==0)
{
v3();v2();v0();v1();v0();m=0;while(m<40);
P2=0xdf;
}
dong=1;
}
if(key3==0) //指令3
{
delay_315();
while(key3==0)
{
v3();v2();v0();v1();v1();m=0;while(m<40);
P2=0xef;
}
dong=1;
}
if(dong==1)
{
dong=0;
for(i=0;i<5;i++)
{
v3();v2();v1();v0();v0();m=0;while(m<40);
}
}
}
}
void timer0() interrupt 1
{
TL0 = 0xAE;
TH0 = 0xFB;
m++;
TF0=0;
}
void v0() // 1MS高电平 500us低电平
{
m=0;
while(m<10)
TX=1;
m=0;
while(m<5)
TX=0;
}
void v1() // 2MS高电平 500us低电平
{
m=0;
while(m<20)
TX=1;
m=0;
while(m<5)
TX=0;
}
void v2() // 4MS高 4ms低
{
m=0;
while(m<40)
TX=1;
m=0;
while(m<40)
TX=0;
}
void v3() // 1MS高 500us低
{
m=0;
while(m<10)
TX=1;
m=0;
while(m<5)
TX=0;
}
void delay_315()
{
uchar i,j;
for(i=0;i<10;i++)
for(j=0;j<110;j++);
}
接收解码部分1(us级)
#include"reg52.h"
#define uchar unsigned char
sbit RX=P3^3;
uchar m; bit n;
void Delay_ms(unsigned int ms) //nms延时
{
unsigned int i,j;
for(i=0;i
}
void main()
{
uchar a,b,c,d,e,f,g,h,p,x,y; //自定义存取的数据
TMOD = 0x01;
TL0 = 0xAE; //??????
TH0 = 0xFB;
EA=1;
ET0=1;
TR0=1;
EX1=1;
IT1=1;
p=0;
x=0;
P2=0x00;
while(1)
{
if(n==1)
{
while(RX==0); //读取协议下降沿
m=0;
EX1=0;
while(RX==1);
if(m>25&&m<35) //读取协议起始位高电平
{
m=0;
while(RX==0);
if(m>25&&m<35) //读取协议位低电平
{
m=0;
while(RX==1); //读取数据1
if(m>5&&m<15)a=0;
if(m>15&&m<25)a=8;
while(RX==0);
m=0;
while(RX==1); //读取数据2
if(m>5&&m<15)b=0;
if(m>15&&m<25)b=7;
while(RX==0);
m=0;
while(RX==1); //读取数据3
if(m>5&&m<15)c=0;
if(m>15&&m<25)c=6;
while(RX==0);
m=0;
while(RX==1); //读取数据4
if(m>5&&m<15)d=0;
if(m>15&&m<25)d=5;
while(RX==0);
m=0;
while(RX==1); //读取数据5
if(m>5&&m<15)e=0;
if(m>15&&m<25)e=4;
while(RX==0);
m=0;
while(RX==1); //读取数据6
if(m>5&&m<15)f=0;
if(m>15&&m<25)f=3;
while(RX==0);
m=0;
while(RX==1); //读取数据7
if(m>5&&m<15)g=0;
if(m>15&&m<25)g=2;
while(RX==0);
m=0;
while(RX==1); //读取数据8
if(m>5&&m<15)h=0;
if(m>15&&m<25)h=1;
while(RX==0);
m=0;
p=a+b+c+d+e+f+g+h;
x++;
if(x==1)y=p;
if(x==2) //连续收到2次数据才进入匹配
{
x=0;
if(y==p)
{
switch(p)
{
case 0:P2=0x80;
break;
case 1:P2=0x40;
break;
case 2:P2=0x20;
break;
case 3 :P2=0x10; //5
break;
case 4:P2=0x08;
break;
case 5:P2=0x04;
break;
case 6:P2=0x02;
break;
case 7:P2=0x01;
break;
case 8:P2=0x80;
break;
case 9:P2=0x40;
break;
case 10:P2=0x20;
break;
case 11:P2=0x10;
break;
case 12:P2=0x08;
break;
case 13:P2=0x04;
break;
case 14:P2=0x02;
break;
case 15:P2=0x01;
break;
}
}
}
}
}
}
}
}
void INT_1() interrupt 2
{
n=1;
}
void timer0() interrupt 1
{
TL0 = 0x91;
TH0 = 0xff;
m++;
TF0=0;
}