极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 23887|回复: 10

贴一段网上找到可以用的格力空调遥控YB0F2控制代码,可惜是AVR的

[复制链接]
发表于 2013-5-23 15:51:24 | 显示全部楼层 |阅读模式
看到这么多人研究控制格力空调,网上找到几种成功控制的代码,觉得这个写得还清晰,贴出来大家研究一下
  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V2.05.0 Professional
  4. Automatic Program Generator
  5. ?Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
  6. http://www.hpinfotech.com

  7. Project :  greesend
  8. Version :  2 (YB0F2)
  9. Date    : 2012/10/26
  10. Author  : axiu
  11. Company :
  12. Comments: 这个程序发送制冷模式下27摄氏度时空调的开关控制波形,当按下openb,空调开,closeb空调关,
  13.           按下inc,温度上加,直到30摄氏度,按下dec,温度下减,直到16摄氏度。从而实现空调的控制
  14.          
  15. Chip type               : ATmega16
  16. Program type            : Application
  17. AVR Core Clock frequency: 12.000000 MHz
  18. Memory model            : Small
  19. External RAM size       : 0
  20. Data Stack size         : 256
  21. *****************************************************/

  22. #include <mega16.h>

  23. // Alphanumeric LCD Module functions
  24. #include <alcd.h>
  25. #include <delay.h>
  26. #include <stdio.h>

  27. #define hongwai PORTB.3
  28. #define openb  PINC.0
  29. #define closeb PINC.1
  30. #define inc   PINA.1        //空调温度加
  31. #define dec   PINA.2        //空调温度减

  32. // Declare your global variables here
  33. unsigned char m=0,timeout=0,openflag=0;      //8位数据计数器,gree空调编码一共有9个数据位,4*8+1*3+4*8
  34. unsigned char ir_data[9];
  35. unsigned char open[9]={0x09,0x0b,0xa0,0x50,0x02,0x00,0x20,0x00,0x00}; //27摄氏度开
  36. unsigned char close[9]={0x01,0x0b,0xa0,0x50,0x02,0x00,0x20,0x00,0x80};//27摄氏度关
  37. unsigned char tempcount=0x1b;     //温度计数器,初始值为27摄氏度
  38. char dis[3];

  39. void send_data(unsigned char ircode)     
  40. {
  41. unsigned char i,j;
  42. if(m==4) i=3;
  43.     else  i=8;   
  44. for(j=0;j<i;j++)  
  45. {
  46.   //发送0.684ms低电平,38KHZ方波调制
  47.   OCR1AH=0x04;
  48.   OCR1AL=0x02;
  49.   TCNT1H=0x00;
  50.   TCNT1L=0X00;  
  51.   TCCR0|=0x10;   //T0 OC0选择比较匹配时,取反,从PORTB.3输出   
  52.   TCNT0=0x00;
  53.   TIMSK=0x10;   //T1开OCR1A匹配中断
  54.   while(timeout==0) ;
  55.   timeout=0;
  56.   hongwai=1;

  57.   //数据位是1,发送1.606ms高电平;是0发送0.526ms高电平
  58.   if(ircode&0x01)     
  59.     {
  60.     OCR1AH=0x09; OCR1AL=0x60;// OCR1AH=0x09; OCR1AL=0x69;
  61.     }  
  62.     else  {OCR1AH=0x03; OCR1AL=0x15;}
  63.   TCNT1H=0x00;
  64.   TCNT1L=0X00;
  65.   TIMSK=0x10;   //T1开OCR1A匹配中断
  66.   while(timeout==0);
  67.   timeout=0;
  68. // PORTB.3=0;
  69.   ircode=ircode>>1;
  70.   }
  71. }

  72. //T1匹配中断 中断服务程序
  73. interrupt[TIM1_COMPA]void time0_comp_isr(void)
  74. {
  75.   TCCR0&=0xEF;   //T0 OC0输出关闭
  76.   TIMSK=0x00;    //T1 中断关闭
  77.   timeout=1;
  78. }  

  79. void main(void)
  80. {
  81. // Declare your local variables here
  82. unsigned char i;

  83. // Input/Output Ports initialization
  84. // Port A initialization
  85. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  86. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  87. PORTA=0x00;
  88. DDRA=0x00;

  89. // Port B initialization
  90. // Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
  91. // State7=T State6=T State5=T State4=T State3=1 State2=T State1=T State0=T
  92. PORTB=0x08;
  93. DDRB=0x08;

  94. // Port C initialization
  95. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  96. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  97. PORTC=0x0f;
  98. DDRC=0x00;

  99. // Port D initialization
  100. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  101. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  102. PORTD=0x00;
  103. DDRD=0x00;

  104. // Timer/Counter 0 initialization
  105. // Clock source: System Clock 8分频
  106. // Clock value: Timer 0 Stopped
  107. // Mode: CTC top=OCR0
  108. // OC0 output:disconnect
  109. TCCR0=0x0A;
  110. TCNT0=0x00;
  111. OCR0=0x13;

  112. // Timer/Counter 1 initialization
  113. // Clock source: System Clock 8分频
  114. // Clock value: Timer1 Stopped
  115. // Mode: CTC mode
  116. // OC1A output: Discon.
  117. // OC1B output: Discon.
  118. // Noise Canceler: Off
  119. // Input Capture on Falling Edge
  120. // Timer1 Overflow Interrupt: Off
  121. // Input Capture Interrupt: Off
  122. // Compare A Match Interrupt: Off
  123. // Compare B Match Interrupt: Off
  124. TCCR1A=0x00;
  125. TCCR1B=0x0A;  
  126. TCNT1H=0x00;
  127. TCNT1L=0x00;
  128. ICR1H=0x00;
  129. ICR1L=0x00;
  130. OCR1AH=0xff;
  131. OCR1AL=0xff;
  132. OCR1BH=0x00;
  133. OCR1BL=0x00;

  134. // Timer/Counter 2 initialization
  135. // Clock source: System Clock
  136. // Clock value: Timer2 Stopped
  137. // Mode: Normal top=0xFF
  138. // OC2 output: Disconnected
  139. ASSR=0x00;
  140. TCCR2=0x00;
  141. TCNT2=0x00;
  142. OCR2=0x00;

  143. // External Interrupt(s) initialization
  144. // INT0: Off
  145. // INT1: Off
  146. // INT2: Off
  147. MCUCR=0x00;
  148. MCUCSR=0x00;

  149. // Timer(s)/Counter(s) Interrupt(s) initialization
  150. TIMSK=0x00;

  151. // USART initialization
  152. // USART disabled
  153. UCSRB=0x00;

  154. // Analog Comparator initialization
  155. // Analog Comparator: Off
  156. // Analog Comparator Input Capture by Timer/Counter 1: Off
  157. ACSR=0x80;
  158. SFIOR=0x00;

  159. // ADC initialization
  160. // ADC disabled
  161. ADCSRA=0x00;

  162. // SPI initialization
  163. // SPI disabled
  164. SPCR=0x00;

  165. // TWI initialization
  166. // TWI disabled
  167. TWCR=0x00;

  168. // Alphanumeric LCD initialization
  169. // Connections specified in the
  170. // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
  171. // RS - PORTD Bit 4
  172. // RD - PORTD Bit 5
  173. // EN - PORTD Bit 6
  174. // D4 - PORTB Bit 4
  175. // D5 - PORTB Bit 5
  176. // D6 - PORTB Bit 6
  177. // D7 - PORTB Bit 7
  178. // Characters/line: 16
  179. lcd_init(16);
  180. #asm("sei")

  181. while (1)
  182.       {
  183.       while((openb&closeb&inc&dec)==1);
  184.       delay_ms(15);        
  185.       if(!openb)
  186.         {
  187.          openflag=1;               //空调打开,标志位置为 1
  188.          for(i=0;i<9;i++)      
  189.             {
  190.              ir_data[i]=open[i];
  191.             }
  192.          while(!openb);
  193.          lcd_gotoxy(3,0);
  194.          lcd_putsf("open  ");  
  195.         }
  196.         else if(!closeb)   
  197.                {
  198.                 openflag=0;        //空调关闭,标志位置为 0
  199.                 for(i=0;i<9;i++)      
  200.                    {
  201.                     ir_data[i]=close[i];
  202.                    }  
  203.                 while(!closeb);
  204.                 lcd_gotoxy(3,0);
  205.                 lcd_putsf("close ");
  206.                }   

  207.        if(openflag==1)        
  208.          {
  209.             if(!inc)               //如果温度加 1,open和close缓冲器值发生变化,然后发送至PB3控制红外发送头的发送
  210.               {
  211.                open[1]+=0x01;
  212.                open[8]+=0x10;      
  213.                close[1]+=0x01;
  214.                close[8]+=0x10;
  215.                tempcount=tempcount+1;
  216.                if(close[1]>=14)  //加到30摄氏度后所有值不变  
  217.                  {
  218.                   tempcount=30;   
  219.                   open[1]=0x0e;
  220.                   open[8]=0x30;
  221.                   close[1]=0x0e;
  222.                   close[8]=0xb0;
  223.                  }
  224.                for(i=0;i<9;i++)
  225.                   {
  226.                    ir_data[i]=open[i];
  227.                   }
  228.                while(!inc);
  229.                lcd_gotoxy(3,0);
  230.                lcd_putsf("inc 1 ");
  231.               }
  232.              else if(!dec)         //如果温度减 1,open和close缓冲器值发生相应变化,然后发送
  233.                     {
  234.                      tempcount=tempcount-1;
  235.                      open[1]-=0x01;
  236.                      open[8]-=0x10;
  237.                      close[1]-=0x01;
  238.                      close[8]-=0x10;
  239.                      if(open[1]==255)      //减到16摄氏度后所有值将不变
  240.                        {
  241.                         tempcount=tempcount+1;
  242.                         open[1]+=0x01;
  243.                         open[8]+=0x10;
  244.                         close[1]+=0x01;
  245.                         close[8]+=0x10;
  246.                        }
  247.                     for(i=0;i<9;i++)  
  248.                        {
  249.                         ir_data[i]=open[i];
  250.                        }
  251.                     while(!inc);
  252.                     lcd_gotoxy(3,0);
  253.                     lcd_putsf("dec 1 ");            
  254.                    }     
  255.          }


  256.      if(TIFR&0x10)   TIFR|=0x10;
  257.     //  lcd_clear();
  258.       // Place your code here
  259.       // 发送9ms低电平,38KHZ调试     
  260.       OCR1AH=0x34;
  261.       OCR1AL=0xbc;
  262.       TCNT1H=0x00;
  263.       TCNT1L=0X00;  
  264.       TCCR0|=0x10;   //T0 OC0选择比较匹配时,取反,从PORTB.3输出   
  265.       TCNT0=0x00;
  266.       TIMSK=0x10;   //T1开OCR1A匹配中断
  267.       timeout=0;
  268.       while(timeout==0) ;
  269.       timeout=0;
  270.       hongwai=1;

  271.       // 发送4.422ms高电平   
  272.       OCR1AH=0x19;
  273.       OCR1AL=0xe9;
  274.       TCNT1H=0x00;
  275.       TCNT1L=0X00;  
  276.       TIMSK=0x10;   //T1开OCR1A匹配中断
  277.       while(timeout==0) ;
  278.       timeout=0;
  279.       
  280.      
  281.       // 发送前5个数据
  282.       for(m=0;m<5;m++)
  283.          {
  284.           send_data(ir_data[m]);   
  285.          }
  286.          
  287.       // 发送0.684ms低电平,38KHZ调制
  288.       OCR1AH=0x04;
  289.       OCR1AL=0x02;
  290.       TCNT1H=0x00;
  291.       TCNT1L=0X00;  
  292.       TCCR0|=0x10;   //T0 OC0选择比较匹配时,取反,从PORTB.3输出     
  293.       TCNT0=0x00;
  294.       TIMSK=0x10;   //T1开OCR1A匹配中断
  295.       while(timeout==0) ;
  296.       timeout=0;
  297.       hongwai=1;
  298.       
  299.       // 发送19.926ms高电平
  300.       OCR1AH=0x74;
  301.       OCR1AL=0xc1;
  302.       TCNT1H=0x00;
  303.       TCNT1L=0X00;  
  304.       TIMSK=0x10;   //T1开OCR1A匹配中断
  305.       while(timeout==0) ;
  306.       timeout=0;
  307.       
  308.       // 发送后4个数据   
  309.       for(m=5;m<9;m++)  
  310.          {
  311.           send_data(ir_data[m]);
  312.          }
  313.          
  314.       // 发送0.684ms低电平,38KHZ调制
  315.       OCR1AH=0x04;
  316.       OCR1AL=0x02;
  317.       TCNT1H=0x00;
  318.       TCNT1L=0X00;  
  319.       TCCR0|=0x10;   //T0 OC0选择比较匹配时,取反,从PORTB.3输出
  320.       TCNT0=0x00;
  321.       TIMSK=0x10;   //T1开OCR1A匹配中断
  322.       while(timeout==0) ;
  323.       timeout=0;
  324.       hongwai=1;
  325.       
  326.       lcd_gotoxy(0,0);
  327.       lcd_putsf("OK ");    //发送完毕  
  328.       sprintf(dis,"%d",tempcount);
  329.       lcd_gotoxy(9,0);  
  330.       lcd_puts(dis);
  331.       lcd_putchar('C');   
  332.       }
  333. }
复制代码
回复

使用道具 举报

发表于 2013-5-23 16:04:52 | 显示全部楼层
格力空调这种垃圾货玩来干嘛
回复 支持 反对

使用道具 举报

发表于 2013-5-23 21:42:36 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2013-5-24 10:12:11 | 显示全部楼层
这样的程序是小白看得懂的吗??改天我写个符合Arduino的格力、海尔、奥克斯、美的、TCL=常规空调控制的代码。
回复 支持 反对

使用道具 举报

发表于 2013-5-25 07:56:27 | 显示全部楼层
pgf017979 发表于 2013-5-24 10:12
这样的程序是小白看得懂的吗??改天我写个符合Arduino的格力、海尔、奥克斯、美的、TCL=常规空调控制的代码 ...

强烈支持阿!!!
回复 支持 反对

使用道具 举报

发表于 2013-5-25 13:49:30 | 显示全部楼层
迷你强 发表于 2013-5-25 07:56
强烈支持阿!!!

现在比较忙,有空一定出教程,顺便做红外发射模块,三极管驱动,发射距离更远。我的毕业设计就是语音遥控器,代码库我都有了,51写的。很容易改为arduino的代码。
回复 支持 反对

使用道具 举报

发表于 2013-11-27 14:51:31 | 显示全部楼层
pgf017979 发表于 2013-5-24 10:12
这样的程序是小白看得懂的吗??改天我写个符合Arduino的格力、海尔、奥克斯、美的、TCL=常规空调控制的代码 ...

快写啊 ,不要只说
回复 支持 反对

使用道具 举报

发表于 2014-5-16 11:08:55 | 显示全部楼层
在arduino中怎么给风速定义;
回复 支持 反对

使用道具 举报

发表于 2014-5-16 11:12:18 | 显示全部楼层
pgf017979 发表于 2013-5-24 10:12
这样的程序是小白看得懂的吗??改天我写个符合Arduino的格力、海尔、奥克斯、美的、TCL=常规空调控制的代码 ...
强烈支持阿!!!
回复 支持 反对

使用道具 举报

发表于 2014-8-13 22:05:28 | 显示全部楼层
pgf017979 发表于 2013-5-25 13:49
现在比较忙,有空一定出教程,顺便做红外发射模块,三极管驱动,发射距离更远。我的毕业设计就是语音遥控 ...

您好,不知道您现在这个事情怎么样了?强烈需要您的指导和代码,谢谢 :)
回复 支持 反对

使用道具 举报

发表于 2014-12-12 16:44:11 | 显示全部楼层
联系我 QQ50852758
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-5 19:20 , Processed in 0.044399 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表