/************************************************/
/* 深宝小耀移动空调 控制程序 */
/* 设计人:LJP 2006-12-18 */
/* 目标MCU:MEGA8 内/外晶振:4MHZ */
/************************************************/
#include "iom8.h"
#include <inavr.h>
#include <ina90.h>
#define BIT(x) (1 << (x))
#define Low_ON PORTD|=BIT(1)
#define Low_OFF PORTD&=~BIT(1)
#define DAT_ON PORTD|=BIT(2)
#define DAT_OFF PORTD&=~BIT(2)
#define CLK_ON PORTD|=BIT(3)
#define CLK_OFF PORTD&=~BIT(3)
#define Hig_ON PORTD|=BIT(4)
#define Hig_OFF PORTD&=~BIT(4)
#define LED_ON PORTD|=BIT(5)
#define LED_OFF PORTD&=~BIT(5)
#define YSJ_ON PORTC|=BIT(1)
#define YSJ_OFF PORTC&=~BIT(1)
#define FAN_ON PORTB|=BIT(3)
#define FAN_OFF PORTB&=~BIT(3)
#define STF_ON PORTC|=BIT(2)
#define STF_OFF PORTC&=~BIT(2)
#define LFAN_ON PORTB|=BIT(5)
#define LFAN_OFF PORTB&=~BIT(5)
#define HFAN_ON PORTC|=BIT(0)
#define HFAN_OFF PORTC&=~BIT(0)
#define FLF_ON PORTB|=BIT(2)
#define FLF_OFF PORTB&=~BIT(2)
#define TSB_ON PORTB|=BIT(4)
#define TSB_OFF PORTB&=~BIT(4)
#define RED_IN() (PINB&BIT(0))
unsigned char DIS,SHI,GER;
unsigned char adc_buf[3],Max,count,pd;
unsigned int adc_count[6],dis_time;
unsigned char redram[11];
unsigned char remoh,remol;
unsigned char renum,reloop;
unsigned char sym_adc,redcode;
unsigned char symbol,mode,swcount=0,swerror=0;
unsigned char Ints,ts,BZ_flag,BZ_flag,v,aut,csec;
unsigned char Hot_f,Hot_s,qs_dis,qs_over_flag;
unsigned char p1,p2,p3,p4,p5,p6,settemp; /* p1:开关机 p2:温度+ p3:定时 p4:温度- p5:模式 p6:风速 */
unsigned char set_temp,old_temp,set_speed,set_time,Err,Er;
unsigned char Power_on,type,sy_flag,time_on,time_off;
unsigned char time_ints,Sec,Min,Hour,st_time,st_change;
unsigned char OFF_h,ss,bz,qs_time,dis_change;
unsigned char t30,t41,t90,t145,sleep_h,drv_flag;
unsigned char set_flag,H_flag,mode_flag,xp,st_flag;
unsigned char Check_flag,start_flag,auto_flag,sleep_flag,qs_flag;
unsigned char ys,ysj,prt_over,Old_temp,qsover;
unsigned char prt1,prt2,prt,drv_ptr,dis_set,adc_1;
unsigned char Old_mode,Old_speed,Old_sleep,Old_Power;
unsigned char key1,key2,key3,key4,key5,key6;
unsigned char key11,key12,key13,key14,key15,key16;
unsigned char key_flag,key_check,key_time,sleep_1,sleep_2;
unsigned int ysj_on_s,ysj_off_s,run_h,stop_h;
unsigned int ysj_m,qs_m,Over_m,BZ_count,drv_on;
unsigned char mode_change,mode_time,mode_flag;
__flash unsigned char LED_tab[10]={0x77,0x14,0xB3,0xB5,0xD4,0xE5,0xE7,0x34,0xF7,0xF5};
__flash unsigned int temp_tab[]={0xC7,0xCE,0xD6,0xDE,0xE6,
0xEE,0xF6,0xFE,0x107,0x110,0x119,0x122,0x12B,0x134,0x13D,
0x147,0x150,0x15A,0x163,0x16D,0x177,0x181,0x18A,0x194,0x19E,
0x1A8,0x1B2,0x1BC,0x1C6,0x1D0,0x1D9,0x1E3,0x1ED,0x1F7,0x201,
0x20A,0x214,0x21D,0x227,0x230,0x239,0x242,0x24C,0x255,0x25D,
0x266,0x26F,0x277,0x280,0x288,0x290,0x298,0x2A0,0x2A8,0x2B0,
0x2B7,0x2BF,0x2C6,0x2CD,0x2D4,0x2DB,0x2E2,0x2E9,0x2EF,0x2F6,
0x2FC,0x302,0x308,0x30E,0x314,0x319,0x31F,0x325,0x32A,0x330};
//*****************
void ysjon()
{
YSJ_ON;
ysj_off_s=0;
ysj=1;
}
void ysjoff(void)
{
YSJ_OFF;
ysj_on_s=0;
ysj=0;
}
//*********************
union adres
{int x;
unsigned char adre[2];
}adresult;
void inits(void)
{unsigned char i;
__disable_interrupt();
DDRB=0xfe;
DDRD=0x3e;
DDRC=0xc3;
for(i=0;i<10;i++) _NOP();
if(PINC & 0x04) type=0;
else type=1;
DDRC=0xc7;
PORTD=0x3e;
SFIOR=0x03; /* 复位定时器/计数器 */
TCNT0 = 0x3d;
TCCR0 = 0x05; /* T/C0 时钟源 clk */
TCNT1L = 0x00;
TCNT1H = 0x00;
TCCR1A = 0x80; /* 比较匹配时OC1A输出低电平 */
TCCR1B = 0xC9; /* 输入捕捉噪声抑制使能 比较匹配时清零定时器 */
OCR1A = 499;
ACSR = 0x80;
MCUCR = 0x00;
GICR = 0x00;
TIFR = 0x00;
TIMSK = (1<<TOIE0)|(1<<TICIE1)|(1<<OCIE1A);
__enable_interrupt();
}
void Status_Inits(void)
{
drv_flag=0;
OFF_h=0;
sleep_h=0;
sleep_1=0;
sleep_2=0;
sleep_flag=0;
set_temp=settemp;
t30=0;
xp=0;
t41=0;
t90=0;
t145=0;
qs_flag=0; //hs flage
Over_m=0;
qs_m=0; //hs 9 min
qs_time=0; //hs 2MIN
ysj_m=0;
ysjoff();
//YSJ_OFF;
FAN_OFF;
LFAN_OFF;
HFAN_OFF;
FLF_OFF;
TSB_OFF;
//ysj=0;
//ysj_on_s=0;
//ysj_off_s=0;
}
void delay(unsigned int n)
{unsigned int i,j;
for(i=0;i<n;i++)
{__watchdog_reset();
for(j=0;j<71;j++) _NOP();
}
}
void Check(void)
{__watchdog_reset();
switch(csec)
{
case 1:{HFAN_ON;LFAN_OFF;STF_OFF;ysjoff();FAN_OFF;TSB_OFF;FLF_OFF;}break;
case 2:{HFAN_OFF;LFAN_ON;STF_OFF;FAN_OFF;TSB_OFF;FLF_OFF;}break;
case 3:{HFAN_OFF;LFAN_OFF;STF_ON;FAN_OFF;TSB_OFF;FLF_OFF;}break;
case 4:{HFAN_OFF;LFAN_OFF;STF_OFF;ysjon();FAN_OFF;TSB_OFF;FLF_OFF;}break;
case 5:{HFAN_OFF;LFAN_OFF;STF_OFF;ysjoff();FAN_ON;TSB_OFF;FLF_OFF;}break;
case 6:{HFAN_OFF;LFAN_OFF;STF_OFF;FAN_OFF;TSB_ON;FLF_OFF;}break;
case 7:{HFAN_OFF;LFAN_OFF;STF_OFF;FAN_OFF;TSB_OFF;FLF_ON;}break;
case 8:
{
HFAN_OFF;
LFAN_OFF;
STF_OFF;
ysjoff();
FAN_OFF;
TSB_OFF;
FLF_OFF;
}break;
case 10:{TCCR1A=0x40;delay(1000);TCCR1A=0x80;Check_flag=0;ss=1;Sec=0;}
default:break;
}
}
unsigned int read_adc(unsigned char chanel)
{
ADCSR &= ~(1<<ADEN);
ADMUX=chanel;
ADCSR = (1<<ADEN)|(1<<ADSC)|(1<<ADPS2)|(1<<ADPS1); /* 单次转换,分频因子32 */
while (!(ADCSR & (1<<ADIF))) ;
ADCSR=1<<ADIF;
adresult.adre[0]=ADCL;
adresult.adre[1]=ADCH;
return(adresult.x&0x3ff);
}
void AD_CON(void)
{unsigned char i;
unsigned int temp;
if(dis_time==0)
{
adc_count[0]=read_adc(3);
adc_count[2]=read_adc(4);
adc_count[4]=read_adc(5);
}
if(dis_time==10)
{
adc_count[1]=read_adc(3);
adc_count[3]=read_adc(4);
adc_count[5]=read_adc(5);
temp=0;
temp=(adc_count[0]+adc_count[1])/2;
if((temp>=25)&&(temp<238)) adc_buf[0]=5;
else if((temp>816)&&(temp<=1000)) adc_buf[0]=75;
else if((temp>=238)&&(temp<=816))
{
for(i=0;i<75;i++) {if(temp<=temp_tab[i]) break;}
adc_buf[0]=i-5;
}
else if((temp<25)||(temp>1000)) {if(!Er) Er=2,bz=1;}
if(qs_dis==0) count=adc_buf[0];
temp=(adc_count[2]+adc_count[3])/2;
if((temp>=25)&&(temp<199)) adc_buf[1]=0;
else if((temp>816)&&(temp<=1000)) adc_buf[1]=75;
else if((temp>=199)&&(temp<=816))
{
for(i=0;i<75;i++) {if(temp<=temp_tab[i]) break;}
adc_buf[1]=i;
}
else if((temp<25)||(temp>1000)) {if(!Er) Er=1,bz=1;}
if(adc_buf[1]>60) Max=61;
else if(Max<adc_buf[1]) Max=adc_buf[1];
temp=(adc_count[4]+adc_count[5])/2;
if((temp>=25)&&(temp<199)) adc_buf[2]=0;
else if((temp>816)&&(temp<=1000)) adc_buf[2]=75;
else if((temp>=199)&&(temp<=816))
{
for(i=0;i<75;i++) {if(temp<=temp_tab[i]) break;}
adc_buf[2]=i;
}
else if((temp<25)||(temp>1000)) Err=1;
}
}
void autofan_col(void)
{
if(set_temp<=(adc_buf[0]-4)) set_speed=1;
else if(set_temp>=(adc_buf[0]-1)) set_speed=0;
if(set_speed==1) {LFAN_OFF;HFAN_ON;}
else {HFAN_OFF;LFAN_ON;}
}
void autofan_hot(void)
{
if(set_temp>=(adc_buf[0]+5)) set_speed=1;
else if(set_temp<=(adc_buf[0]-1)) set_speed=0;
if(set_speed==1) {LFAN_OFF;HFAN_ON;}
else {HFAN_OFF;LFAN_ON;}
}
void coolstfoff(void)
{
if((ysj_off_s>=120)||(ysj==1)) STF_OFF;
}
void Make_col(void)
{
__watchdog_reset();
coolstfoff(); //if((ysj_off_s>=120)||(ysj==1)) STF_OFF;
if(ysj_on_s>=300)
{xp=1;
if(t30>=120)
{
ysjoff();
//YSJ_OFF;
FAN_OFF;
//ysj=0;
//ysj_on_s=0;
prt1=1;
}
}
if((ysj_off_s>=180)&&(adc_buf[1]>=12)) {prt1=0;t30=0;xp=0;}
if(sleep_flag==1){HFAN_OFF;LFAN_ON;}
else
{
if(aut==1) autofan_col();
else
{
if(se
评论0