51单片机红外遥控格力空调程序.docx
《51单片机红外遥控格力空调程序.docx》由会员分享,可在线阅读,更多相关《51单片机红外遥控格力空调程序.docx(14页珍藏版)》请在冰豆网上搜索。
51单片机红外遥控格力空调程序
51单片机红外遥控格力空调程序
51单片机红外遥控格力空调程序
#include
#defineucharunsignedchar
#defineuintunsignedint
sbitkey1=P3^4;//按键控制开机
sbitkey2=P3^5;//按键控制关机
sbitkey3=P3^6;//按键控制温度+
sbitkey4=P3^7;//按键控制温度-
sbitout=P1^5;//发送IO口
ucharwd1[15]={0x00,0x08,0x04,0x0c,0x02,
0x0a,0x06,0x0e,0x01,0x09,
0x05,0x0d,0x03,0x0b,0x07
};
ucharwd2[15]={0x0a,0x06,0x0e,0x01,0x09,
0x05,0x0d,0x03,0x0b,0x07,
0x0f,0x00,0x08,0x04,0x0c
};
ucharx=12;//开机28度
/************晶振11.0592MHz**************/
voiddelay(uintxms)
{
uinti,j;
for(i=xms;i>0;i--)//i=xms即延时约xms毫秒
for(j=112;j>0;j--);
}
voiddelay560us(void)//560us延迟函数
{
uintj;
for(j=63;j>0;j--);
}
voiddelay4500us(void)//4.5ms延迟函数
{
uintj;
for(j=516;j>0;j--);
}
voidkhz_2(uintnum)//38KHZ脉冲占空比1:
2
{
for(;num>0;num--)
{
out=~out;
}
}
voidsend0_a(void)//发送0
voidclose(uchara,ucharb,ucharc,uchard)
{
uinti;
leadcode_a();
send1_a();
for(i=0;i<7;i++)
send0_a();
if(a)
send1_a();
else
send0_a();
if(b)
send1_a();
else
send0_a();
if(c)
send1_a();
else
send0_a();
if(d)
send1_a();
else
send0_a();
send0_a();
send0_a();
send0_a();
send0_a();
for(i=0;i<5;i++)
send0_a();
send1_a();
for(i=0;i<6;i++)
send0_a();
send1_a();
send0_a();
send1_a();
send0_a();
send0_a();
send1_a();
send0_a();
khz_2(42);
out=1;
delay(20);
}
voidclose1(uchare,ucharf,ucharg,ucharh)
{
uchari;
for(i=0;i<13;i++)
send0_a();
send1_a();
send0_a();
send0_a();
for(i=0;i<12;i++)
send0_a();
if(e)
send1_a();
else
send0_a();
if(f)
send1_a();
else
send0_a();
if(g)
send1_a();
else
send0_a();
if(h)
send0_a();
else
send1_a();
khz_2(42);
out=1;
delay(1000);
/*******************************************/
/****************开机************************/
}
voidopen(uchara,ucharb,ucharc,uchard)
{
uinti;
leadcode_a();
send1_a();
send0_a();
send0_a();
send1_a();
for(i=0;i<4;i++)
send0_a();
if(a)
send1_a();
else
send0_a();
if(b)
send1_a();
else
send0_a();
if(c)
send1_a();
else
send0_a();
if(d)
send1_a();
else
send0_a();
send0_a();
send0_a();
send0_a();
send0_a();
for(i=0;i<5;i++)
send0_a();
send1_a();
for(i=0;i<6;i++)
send0_a();
send1_a();
send0_a();
send1_a();
send0_a();
send0_a();
send1_a();
send0_a();
khz_2(42);
out=1;
delay(20);
}
voidopen1(uchare,ucharf,ucharg,ucharh)
{
uchari;
for(i=0;i<13;i++)
send0_a();
send1_a();
send0_a();
send0_a();
for(i=0;i<12;i++)
send0_a();
if(e)
send1_a();
else
send0_a();
if(f)
send1_a();
else
send0_a();
if(g)
send1_a();
else
send0_a();
if(h)
send1_a();
else
send0_a();
khz_2(42);
out=1;
delay(1000);
}
voidkeyscan()
{
uchara,b,c,d,e,f,g,h;
if(key1==0)
{
delay(10);
if(key1==0)
{
while(!
key1);
if(wd1[x]&0x08)
a=1;
else
a=0;
if(wd1[x]&0x04)
b=1;
else
b=0;
if(wd1[x]&0x02)
c=1;
else
c=0;
if(wd1[x]&0x01)
d=1;
else
d=0;
if(wd2[x]&0x08)
e=1;
else
e=0;
if(wd2[x]&0x04)
f=1;
else
f=0;
if(wd2[x]&0x02)
g=1;
else
g=0;
if(wd2[x]&0x01)
h=1;
else
h=0;
open(a,b,c,d);
open1(e,f,g,h);
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
while(!
key2);
if((wd1[x]&0x08))
a=1;
else
a=0;
if((wd1[x]&0x04))
b=1;
else
b=0;
if((wd1[x]&0x02))
c=1;
else
c=0;
if((wd1[x]&0x01))
d=1;
else
d=0;
if((wd2[x]&0x08))
e=1;
else
e=0;
if((wd2[x]&0x04))
f=1;
else
f=0;
if((wd2[x]&0x02))
g=1;
else
g=0;
if((wd2[x]&0x01))
h=1;
else
h=0;
close(a,b,c,d);
close1(e,f,g,h);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
while(!
key1);
x++;
if((wd1[x]&0x08))
a=1;
else
a=0;
if((wd1[x]&0x04))
b=1;
else
b=0;
if((wd1[x]&0x02))
c=1;
else
c=0;
if((wd1[x]&0x01))
d=1;
else
d=0;
if((wd2[x]&0x08))
e=1;
else
e=0;
if((wd2[x]&0x04))
f=1;
else
f=0;
if((wd2[x]&0x02))
g=1;
else
g=0;
if((wd2[x]&0x01))
h=1;
else
h=0;
open(a,b,c,d);
open1(e,f,g,h);
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
while(!
key1);
x--;
if((wd1[x]&0x08))
a=1;
else
a=0;
if((wd1[x]&0x04))
b=1;
else
b=0;
if((wd1[x]&0x02))
c=1;
else
c=0;
if((wd1[x]&0x01))
d=1;
else
d=0;
if((wd2[x]&0x08))
e=1;
else
e=0;
if((wd2[x]&0x04))
f=1;
else
f=0;
if((wd2[x]&0x02))
g=1;
else
g=0;
if((wd2[x]&0x01))
h=1;
else
h=0;
open(a,b,c,d);
open1(e,f,g,h);
}
}
}
voidinit()
{
key1=1;
key2=1;
key3=1;
key4=1;
out=1;
}
voidmain()
{
init();
while
(1)
{
keyscan();
}
}