|
发表于 2009-8-5 08:59:10
|
显示全部楼层
发一个正常使用的遥控器for Gree
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iom48v.h>
#include <macros.h>
#include "lcd4bit.h"
#define delay_timer1(k) {TIFR1|=BIT(TOV1); TCNT1=65536-(k/2); TCCR1B=0x02; while (!(TIFR1&BIT(TOV1))); TCCR1B=0x0;}
const unsigned char asc[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
static unsigned char txbuf[35]={1,0,0,1,1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,0,1,0};
unsigned char work_mode=1; //"0":heat
//"1":cold
//"2":dehumidify
//"3":fan
//"4":auto
unsigned char power=0; //"0":power off
//"1":power on
unsigned char fan=0; //"0":auto
//"1":low
//"2":mid
//"3":high
unsigned char fanangle=0; //"0":fan angle off
//"1":fan angle on
unsigned char sleep=0; //"0":sleep off
//"1":sleep on
unsigned char temperature=25; //16+n,max 30,min 16
///////////////////////////////////////////////////////////////////////////////
void write_eeprom(unsigned int address,unsigned char dat)
{ while (EECR & (1<<EEWE)); //wait until EEWE to 0
EEAR=address;
EEDR=dat;
EECR|=(1<<EEMWE); //set EEMWE to 1
EECR|=(1<<EEWE); // set EEWE to 1
}
unsigned char read_eeprom(unsigned int address)
{ while (EECR &(1<<EEWE)); //wait until EEWE to 0
EEAR=address;
EECR|=(1<<EERE); // set EERE to 1
return (EEDR);
}
///////////////////////////////////////////////////////////////////////////////
void ir_send(unsigned char *p,unsigned char len)
{ unsigned char k;
TCNT2=0; //send start bit
TCCR2A=0x42; //enable OC2
TCCR2B=0x01; //start timer2,
delay_timer1(9000); //delay 9000us
TCCR2B=0x0; //stop timer2
TCCR2A=0x0; //disable OC2
delay_timer1(4500); //delay 4500us
for (k=0;k<len;k++)
{ if (*p) //send "1"
{ TCNT2=0;
TCCR2A=0x42; //enable OC2
TCCR2B=0x01; //start timer2
delay_timer1(560); //delay 560us
TCCR2B=0x0; //stop timer2
TCCR2A=0x0; //disable OC2
delay_timer1(1680); //delay 1680us
}
else //send "0"
{ TCNT2=0;
TCCR2A=0x42; //enable OC2
TCCR2B=0x01; //start timer2
delay_timer1(560); //delay 560us
TCCR2B=0x0; //stop timer2
TCCR2A=0x0; //disable OC2
delay_timer1(560); //delay 560us
}
p++;
}
TCNT2=0;
TCCR2A=0x42; //start timer2,enable OC2
TCCR2B=0x01;
delay_timer1(560);
TCCR2B=0x0; //stop timer2
TCCR2A=0x0; //disable OC2
delay_timer1(10000);
}
void show_message(void)
{ LCD4_Home();
if (power)
{ if (work_mode==0)
LCD4_PutS("Heat_M ");
else if (work_mode==1)
LCD4_PutS("Cold_M ");
else if (work_mode==2)
LCD4_PutS("Debu_M ");
else if (work_mode==3)
LCD4_PutS("Fan_M ");
else
LCD4_PutS("Auto_M ");
if (fan==1)
LCD4_PutS(" low ");
else if (fan==2)
LCD4_PutS(" mid ");
else if (fan==3)
LCD4_PutS(" high ");
else
LCD4_PutS(" auto ");
LCD4_PutS("fan");
LCD4_Home2();
LCD4_PutC(asc[temperature/10]);
LCD4_PutC(asc[temperature%10]);
LCD4_PutC('\"');
LCD4_PutC('C');
LCD4_PutS(" swing ");
if (fanangle)
LCD4_PutS("on ");
else
LCD4_PutS("off");
}
else
{LCD4_PutS("------ OFF -----");
LCD4_Home2();
LCD4_PutS(" ");
return;
}
}
////////////////////////////////////////////////////////////////////////////////
void main(void)
{ PORTD=0x04;
DDRD=0xF8;
PORTC=0x3F;
DDRC=0x0;
PORTB=0x01;
DDRB=0xFE;
OCR2A=0x35;
MCUCR=0x02; //INT0 falling edge
delay_ms(100);
LCD4_Init();
LCD4_Clear();
LCD4_PutS("GREE Remote for ");
LCD4_Home2();
LCD4_PutS("Air-Condition ");
delay_ms(1000);
show_message();
MCUCR=0x02; // set int0 to failing edge interrupt
while (1)
{ ir_send(txbuf,35);
delay_ms(200);
}
/* { unsigned char temp;
//SMCR=0x04;
//SMCR|=0x01;
//asm("sleep");
delay_ms(10);
temp=(~PINC)&0x3F;
if (temp==0)
continue; //no key pressed
delay_ms(20);
temp=(~PINC)&0x3F;
switch (temp)
{ case 0x1: if (power) //PC0 to power control
power=0;
else
power=1;
txbuf[3]=power;
show_message();
ir_send(txbuf,35);
break;
case 0x2: if (!power)
break;
work_mode++; //PC1 to mode control
if (work_mode>4)
work_mode=0;
txbuf[0]=work_mode&0x1;
txbuf[1]=(work_mode&0x2)>>1;
txbuf[2]=(work_mode&0x4)>>1;
show_message();
ir_send(txbuf,35);
break;
case 0x4: if (!power)
break;
fan++; //PC2 to fan mode
if (fan>3)
fan=0;
txbuf[4]=fan&0x1;
txbuf[5]=(fan&0x2)>>1;
show_message();
ir_send(txbuf,35);
break;
case 0x8: if (!power)
break;
if (fanangle) //PC3 to fanangle control
fanangle=0;
else
fanangle=1;
txbuf[6]=fanangle;
show_message();
ir_send(txbuf,35);
break;
case 0x10: if (!power)
break;
temperature--; //PC4 to increase temperature
if (temperature<16)
temperature=16;
else
txbuf[8]=temperature&0x1;
txbuf[9]=(temperature&0x2)>>1;
txbuf[10]=(temperature&0x4)>>2;
txbuf[11]=(temperature&0x8)>>3;
show_message();
ir_send(txbuf,35);
break;
case 0x20: if (!power)
break;
temperature++; //PC5 to increase temperature
if (temperature>30)
temperature=30;
else
txbuf[8]=temperature&0x1;
txbuf[9]=(temperature&0x2)>>1;
txbuf[10]=(temperature&0x4)>>2;
txbuf[11]=(temperature&0x8)>>3;
show_message();
ir_send(txbuf,35);
break;
default: break; //no key pressed
}
do
temp=(~PINC)&0x3F;
while (temp!=0);
delay_ms(100);
} */
} |
|