发射部分代码:
#include
#define uchar unsigned char
#define uint unsigned int
sbit P20=P2^0; //发射引脚
sbit P10=P1^0;
sbit P11=P1^1;
uchar k;
void delay()
{
uchar j,i;
for(i=0;i<255l;i++)
for(j=0;j<255;j++)
;
}
void qindongyindaoma()
{
TH0=0XFD;
TL0=0XA7;//600US
k=0;
P20=1;
TR0=1;//开启T0
}
void yindaoma()
{
TF0=0;//溢出清零
TH0=0XFD;
TL0=0XA7;//600US
P20=~P20;
k++;
TR0=1;
}
void send_1 ()
{
TH0=0XFD;
TL0=0X11;//750US
P20=1; //高电平
TF0=0;//溢出清零
TR0=1;//开启T0
while(TF0==0); //等待溢出
TH0=0XFf;
TL0=0X05;//250US
P20=0; //低电平
TF0=0;//溢出清零
TR0=1;//开启T0
while(TF0==0);
}
void send_0 ()
{
TH0=0XFf;
TL0=0X05;//250US
P20=1; //高电平
TF0=0;//溢出清零
TR0=1;//开启T0
while(TF0==0); //等待溢出
TH0=0XFd;
TL0=0X11;//750US
P20=0; //低电平
TF0=0;//溢出清零
TR0=1;//开启T0
while(TF0==0);
}
void main()
{
TMOD=0X01; //定时器0,工作方式1,十六位
EA=1;//允许中断
ET0=1; //定时器T0中断允许
TR0=0;// 关闭T0
P20=0;
k=0;
while(1) //先发送高位
{
if(P10==0)
{
delay();//消震
qindongyindaoma();
while(k<12) ; //等待引导码发射结束
while(TF0==0); //等待溢出
send_0();
send_0();
P20=1;
delay();
P20=0; //默认低电平
}
else if(P11==0)
{
delay();//消震
qindongyindaoma();
while(k<12) ; //等待引导码发射结束
while(TF0==0); //等待溢出
send_0();
send_1();
P20=1;
delay();
P20=0; //默认低电平
}
else ;
}
}
void time_0 () interrupt 1
{
TR0=0;//关闭定时器0
if(k<12)
{
yindaoma();
}
else
{
;
}
}
*************************************
接收部分代码:
#include
#define uchar unsigned char
#define uint unsigned int
sbit P32=P3^2; //将IR位定义为P3.2引脚
sbit P10=P1^0;
sbit P11=P1^1;
uchar lowtime,count;
void delay()
{
uchar i,j;
for(i=0;i<255;i++)
for(j=0;j<255;j++)
;
}
void main()
{
TMOD=0X01;
EA=1;
ET0=1;
TH0=0X00;
TL0=0X00;
count=0;
IT0=1;
EX0=1;
while(1)
{
;
}
}
void int_0 () interrupt 0
{
P10=~P10;
EX0=0;
TR0=1;
while(P32==0)
;
TR0=0;
lowtime=TH0*256+TL0;
if(count==6)
{
P11=0;
count++;
//delay();
//count=0;
}
if((lowtime>120)&&(lowtime<300)&&(count<6))//不能大于120,小于280!!
{
count++;
}
else if((lowtime>10)&&(lowtime<300)&&(count>5))
{
P11=1;
count=0;
}
TH0=0X00;
TL0=0X00;
EX0=1;
}
测试用,不能开闭空调电视等。
完整资料打包: