|
看到这么多人研究控制格力空调,网上找到几种成功控制的代码,觉得这个写得还清晰,贴出来大家研究一下 - /*****************************************************
- This program was produced by the
- CodeWizardAVR V2.05.0 Professional
- Automatic Program Generator
- ?Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
- http://www.hpinfotech.com
- Project : greesend
- Version : 2 (YB0F2)
- Date : 2012/10/26
- Author : axiu
- Company :
- Comments: 这个程序发送制冷模式下27摄氏度时空调的开关控制波形,当按下openb,空调开,closeb空调关,
- 按下inc,温度上加,直到30摄氏度,按下dec,温度下减,直到16摄氏度。从而实现空调的控制
-
- Chip type : ATmega16
- Program type : Application
- AVR Core Clock frequency: 12.000000 MHz
- Memory model : Small
- External RAM size : 0
- Data Stack size : 256
- *****************************************************/
- #include <mega16.h>
- // Alphanumeric LCD Module functions
- #include <alcd.h>
- #include <delay.h>
- #include <stdio.h>
- #define hongwai PORTB.3
- #define openb PINC.0
- #define closeb PINC.1
- #define inc PINA.1 //空调温度加
- #define dec PINA.2 //空调温度减
- // Declare your global variables here
- unsigned char m=0,timeout=0,openflag=0; //8位数据计数器,gree空调编码一共有9个数据位,4*8+1*3+4*8
- unsigned char ir_data[9];
- unsigned char open[9]={0x09,0x0b,0xa0,0x50,0x02,0x00,0x20,0x00,0x00}; //27摄氏度开
- unsigned char close[9]={0x01,0x0b,0xa0,0x50,0x02,0x00,0x20,0x00,0x80};//27摄氏度关
- unsigned char tempcount=0x1b; //温度计数器,初始值为27摄氏度
- char dis[3];
- void send_data(unsigned char ircode)
- {
- unsigned char i,j;
- if(m==4) i=3;
- else i=8;
- for(j=0;j<i;j++)
- {
- //发送0.684ms低电平,38KHZ方波调制
- OCR1AH=0x04;
- OCR1AL=0x02;
- TCNT1H=0x00;
- TCNT1L=0X00;
- TCCR0|=0x10; //T0 OC0选择比较匹配时,取反,从PORTB.3输出
- TCNT0=0x00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- while(timeout==0) ;
- timeout=0;
- hongwai=1;
-
- //数据位是1,发送1.606ms高电平;是0发送0.526ms高电平
- if(ircode&0x01)
- {
- OCR1AH=0x09; OCR1AL=0x60;// OCR1AH=0x09; OCR1AL=0x69;
- }
- else {OCR1AH=0x03; OCR1AL=0x15;}
- TCNT1H=0x00;
- TCNT1L=0X00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- while(timeout==0);
- timeout=0;
- // PORTB.3=0;
- ircode=ircode>>1;
- }
- }
- //T1匹配中断 中断服务程序
- interrupt[TIM1_COMPA]void time0_comp_isr(void)
- {
- TCCR0&=0xEF; //T0 OC0输出关闭
- TIMSK=0x00; //T1 中断关闭
- timeout=1;
- }
-
- void main(void)
- {
- // Declare your local variables here
- unsigned char i;
- // Input/Output Ports initialization
- // Port A initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTA=0x00;
- DDRA=0x00;
- // Port B initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=1 State2=T State1=T State0=T
- PORTB=0x08;
- DDRB=0x08;
- // Port C initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTC=0x0f;
- DDRC=0x00;
- // Port D initialization
- // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
- // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
- PORTD=0x00;
- DDRD=0x00;
- // Timer/Counter 0 initialization
- // Clock source: System Clock 8分频
- // Clock value: Timer 0 Stopped
- // Mode: CTC top=OCR0
- // OC0 output:disconnect
- TCCR0=0x0A;
- TCNT0=0x00;
- OCR0=0x13;
- // Timer/Counter 1 initialization
- // Clock source: System Clock 8分频
- // Clock value: Timer1 Stopped
- // Mode: CTC mode
- // OC1A output: Discon.
- // OC1B output: Discon.
- // Noise Canceler: Off
- // Input Capture on Falling Edge
- // Timer1 Overflow Interrupt: Off
- // Input Capture Interrupt: Off
- // Compare A Match Interrupt: Off
- // Compare B Match Interrupt: Off
- TCCR1A=0x00;
- TCCR1B=0x0A;
- TCNT1H=0x00;
- TCNT1L=0x00;
- ICR1H=0x00;
- ICR1L=0x00;
- OCR1AH=0xff;
- OCR1AL=0xff;
- OCR1BH=0x00;
- OCR1BL=0x00;
- // Timer/Counter 2 initialization
- // Clock source: System Clock
- // Clock value: Timer2 Stopped
- // Mode: Normal top=0xFF
- // OC2 output: Disconnected
- ASSR=0x00;
- TCCR2=0x00;
- TCNT2=0x00;
- OCR2=0x00;
- // External Interrupt(s) initialization
- // INT0: Off
- // INT1: Off
- // INT2: Off
- MCUCR=0x00;
- MCUCSR=0x00;
- // Timer(s)/Counter(s) Interrupt(s) initialization
- TIMSK=0x00;
- // USART initialization
- // USART disabled
- UCSRB=0x00;
- // Analog Comparator initialization
- // Analog Comparator: Off
- // Analog Comparator Input Capture by Timer/Counter 1: Off
- ACSR=0x80;
- SFIOR=0x00;
- // ADC initialization
- // ADC disabled
- ADCSRA=0x00;
- // SPI initialization
- // SPI disabled
- SPCR=0x00;
- // TWI initialization
- // TWI disabled
- TWCR=0x00;
- // Alphanumeric LCD initialization
- // Connections specified in the
- // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
- // RS - PORTD Bit 4
- // RD - PORTD Bit 5
- // EN - PORTD Bit 6
- // D4 - PORTB Bit 4
- // D5 - PORTB Bit 5
- // D6 - PORTB Bit 6
- // D7 - PORTB Bit 7
- // Characters/line: 16
- lcd_init(16);
- #asm("sei")
- while (1)
- {
- while((openb&closeb&inc&dec)==1);
- delay_ms(15);
- if(!openb)
- {
- openflag=1; //空调打开,标志位置为 1
- for(i=0;i<9;i++)
- {
- ir_data[i]=open[i];
- }
- while(!openb);
- lcd_gotoxy(3,0);
- lcd_putsf("open ");
- }
- else if(!closeb)
- {
- openflag=0; //空调关闭,标志位置为 0
- for(i=0;i<9;i++)
- {
- ir_data[i]=close[i];
- }
- while(!closeb);
- lcd_gotoxy(3,0);
- lcd_putsf("close ");
- }
- if(openflag==1)
- {
- if(!inc) //如果温度加 1,open和close缓冲器值发生变化,然后发送至PB3控制红外发送头的发送
- {
- open[1]+=0x01;
- open[8]+=0x10;
- close[1]+=0x01;
- close[8]+=0x10;
- tempcount=tempcount+1;
- if(close[1]>=14) //加到30摄氏度后所有值不变
- {
- tempcount=30;
- open[1]=0x0e;
- open[8]=0x30;
- close[1]=0x0e;
- close[8]=0xb0;
- }
- for(i=0;i<9;i++)
- {
- ir_data[i]=open[i];
- }
- while(!inc);
- lcd_gotoxy(3,0);
- lcd_putsf("inc 1 ");
- }
- else if(!dec) //如果温度减 1,open和close缓冲器值发生相应变化,然后发送
- {
- tempcount=tempcount-1;
- open[1]-=0x01;
- open[8]-=0x10;
- close[1]-=0x01;
- close[8]-=0x10;
- if(open[1]==255) //减到16摄氏度后所有值将不变
- {
- tempcount=tempcount+1;
- open[1]+=0x01;
- open[8]+=0x10;
- close[1]+=0x01;
- close[8]+=0x10;
- }
- for(i=0;i<9;i++)
- {
- ir_data[i]=open[i];
- }
- while(!inc);
- lcd_gotoxy(3,0);
- lcd_putsf("dec 1 ");
- }
- }
- if(TIFR&0x10) TIFR|=0x10;
- // lcd_clear();
- // Place your code here
- // 发送9ms低电平,38KHZ调试
- OCR1AH=0x34;
- OCR1AL=0xbc;
- TCNT1H=0x00;
- TCNT1L=0X00;
- TCCR0|=0x10; //T0 OC0选择比较匹配时,取反,从PORTB.3输出
- TCNT0=0x00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- timeout=0;
- while(timeout==0) ;
- timeout=0;
- hongwai=1;
-
- // 发送4.422ms高电平
- OCR1AH=0x19;
- OCR1AL=0xe9;
- TCNT1H=0x00;
- TCNT1L=0X00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- while(timeout==0) ;
- timeout=0;
-
-
- // 发送前5个数据
- for(m=0;m<5;m++)
- {
- send_data(ir_data[m]);
- }
-
- // 发送0.684ms低电平,38KHZ调制
- OCR1AH=0x04;
- OCR1AL=0x02;
- TCNT1H=0x00;
- TCNT1L=0X00;
- TCCR0|=0x10; //T0 OC0选择比较匹配时,取反,从PORTB.3输出
- TCNT0=0x00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- while(timeout==0) ;
- timeout=0;
- hongwai=1;
-
- // 发送19.926ms高电平
- OCR1AH=0x74;
- OCR1AL=0xc1;
- TCNT1H=0x00;
- TCNT1L=0X00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- while(timeout==0) ;
- timeout=0;
-
- // 发送后4个数据
- for(m=5;m<9;m++)
- {
- send_data(ir_data[m]);
- }
-
- // 发送0.684ms低电平,38KHZ调制
- OCR1AH=0x04;
- OCR1AL=0x02;
- TCNT1H=0x00;
- TCNT1L=0X00;
- TCCR0|=0x10; //T0 OC0选择比较匹配时,取反,从PORTB.3输出
- TCNT0=0x00;
- TIMSK=0x10; //T1开OCR1A匹配中断
- while(timeout==0) ;
- timeout=0;
- hongwai=1;
-
- lcd_gotoxy(0,0);
- lcd_putsf("OK "); //发送完毕
- sprintf(dis,"%d",tempcount);
- lcd_gotoxy(9,0);
- lcd_puts(dis);
- lcd_putchar('C');
- }
- }
复制代码 |
|