《51单片机红外遥控格力空调程序》由会员分享,可在线阅读,更多相关《51单片机红外遥控格力空调程序(17页珍藏版)》请在金锄头文库上搜索。
1、51 单片机红外遥控格力空调程序#include #define uchar unsigned char#define uint unsigned intsbit keyl=P3人4;/按键控制开机sbit key2=P3人5;/按键控制关机sbit key3=P3人6;/按键控制温度+sbit key4=P3人7;/按键控制温度-sbit out=Pl人5;/发送 10 口uchar wd115=0x00,0x08,0x04,0x0c,0x02,0x0a,0x06,0x0e,0x01,0x09,0x05,0x0d,0x03,0x0b,0x07 ;uchar wd215=0x0a,0x06,0
2、x0e,0x01,0x09, 0x05,0x0d,0x03,0x0b,0x07,0x0f,0x00,0x08,0x04,0x0c ;uchar x=12; 开机 28 度/*晶振 11.0592MHz*void delay(uint xms)uint i,j;for(i=xms;i0;i-)/i=xms 即延时约 xms 毫秒for(j=112;j0;j-);void delay560us(void) /560us 延迟函数uint j; for(j=63;j0;j-);void delay4500us(void) /4.5ms 延迟函数uint j; for(j=516;j0;j-);voi
3、d khz_2(uint num)/38KHZ脉冲占空比 1:2for(;num0;num-)out=out;void send0_a(void) /发送 0khz_2(42) ;/khz_3(21) ;out=1;delay560us(); void send1_a(void)/发送 1khz_2(42) ;out=1;delay560us();delay560us();delay560us(); void leadcode_a(void) /发送引导码khz_2(690) ;out=1;delay4500us();void close( uchar a,uchar b,uchar c,uc
4、har d) uint i;leadcode_a();send1_a(); for(i=0;i7;i+) send0_a();if(a)send1_a();else send0_a();if(b)send1_a();else send0_a();if(c) send1_a();elsesend0_a();if(d)send1_a();elsesend0_a();send0_a();send0_a();send0_a();send0_a();for(i=0;i5;i+)send0_a();send1_a(); for(i=0;i6;i+) send0_a();send1_a();send0_a(
5、);send1_a();send0_a();send0_a();send1_a();send0_a();khz_2(42) ;out=1;delay(20);void close1(uchar e,uchar f,uchar g,uchar h ) uchar i;for(i=0;i13;i+)send0_a();send1_a();send0_a();send0_a();for(i=0;i12;i+)send0_a();if(e)send1_a();elsesend0_a();if(f)send1_a();elsesend0_a();if(g)send1_a();elsesend0_a();
6、if(h)send0_a();elsesend1_a();khz_2(42) ;out=1;delay(1000);void open(uchar a,uchar b,uchar c,uchar d ) uint i;leadcode_a();send1_a();send0_a();send0_a();send1_a(); for(i=0;i4;i+) send0_a();if(a)send1_a();elsesend0_a();if(b)send1_a();else send0_a();if(c) send1_a();elsesend0_a();if(d)send1_a();elsesend
7、0_a();send0_a();send0_a();send0_a();send0_a() ; for(i=0;i5;i+) send0_a(); send1_a(); for(i=0;i6;i+) send0_a(); send1_a(); send0_a(); send1_a(); send0_a();send0_a();send1_a();send0_a();khz_2(42) ;out=1;delay(20);void open1(uchar e,uchar f,uchar g,uchar h) uchar i; for(i=0;i13;i+) send0_a();send1_a();
8、send0_a();send0_a(); for(i=0;i12;i+)send0_a();if(e)send1_a();elsesend0_a();if(f)send1_a();elsesend0_a();if(g)send1_a();elsesend0_a();if(h)send1_a();elsesend0_a();khz_2(42) ;out=1;delay(1000);void keyscan()uchar a,b,c,d,e,f,g,h;if(key1=0)delay(10);if(key1=0)while(!key1);if(wd1x & 0x08)a=1;elsea=0;if(
9、wd1x & 0x04)b=1;elseb=0;if(wd1x & 0x02)c=1;elsec=0;if(wd1x & 0x01)d=1;elsed=0;if(wd2x & 0x08)e=1;elsee=0;if(wd2x & 0x04)f=1;elsef=0;if(wd2x & 0x02)g=1;elseg=0;if(wd2x & 0x01)h=1;elseh=0;open(a,b,c,d);open1(e,f,g,h);if(key2=0)delay(10);if(key2=0)while(!key2);if(wd1x & 0x08) a=1;elsea=0;if(wd1x & 0x04
10、)b=1;elseb=0;if(wd1x & 0x02)c=1;elsec=0;if(wd1x & 0x01)d=1;elsed=0;if(wd2x & 0x08)e=1;elsee=0;if(wd2x & 0x04)f=1;elsef=0;if(wd2x & 0x02)g=1;elseg=0;if(wd2x & 0x01)h=1;elseh=0;close(a,b,c,d);close1(e,f,g,h);if(key3=0)delay(10);if(key3=0)while(!key1);x+;if(wd1x & 0x08)a=1;elsea=0;if(wd1x & 0x04)b=1;el
11、seb=0;if(wd1x & 0x02)c=1;elsec=0;if(wd1x & 0x01)d=1;elsed=0;if(wd2x & 0x08)e=1;elsee=0;if(wd2x & 0x04)f=1;elsef=0;if(wd2x & 0x02)g=1;elseg=0;if(wd2x & 0x01)h=1;elseh=0;open(a,b,c,d);open1(e,f,g,h);if(key4=0)delay(10);if(key4=0)while(!key1);x-;if(wd1x & 0x08)a=1;elsea=0;if(wd1x & 0x04)b=1;elseb=0;if(wd1x & 0x02)c=1;elsec=0;if(wd1x & 0x01)d=1;elsed=0;if(wd2x & 0x08)e=1;elsee=0;if(wd2x & 0x04)f=1;elsef=0;if(wd2x & 0x02)g=1;elseg=0;if(wd2x & 0x01)h=1;elseh=0;open(a,b,c,d);open1(e,f,g,h);void init()key1=1;key2=1;key3=1;key4=1;out=1;void main()init();while(1) keyscan();