原創蚊帳空調無極調溫
#include<stc8g.h>
#include "intrins.h"
bit g=0,f=0, e=0,ms3f2=0,h=1,j=0;
unsigned int a,c=7,Alarm1,Alarm,i=0;
unsigned long ms3f1=0;
typedef unsigned char u8;
typedef unsigned int u16;
#define ADCTIM (*(unsigned char volatile xdata *)0xfea8)
u16 Get(u8 channel); //channel = 0~15
void Timer_Init(void);
void Key_Check(void);
void fs();
void ysj();
void gr();
void delay1us(unsigned int us)
{
unsigned int i, j;
for(i = us; i > 0; i--)
for(j =700; j > 0; j--);
}
void Key_Check(void)//蒸發器風扇調速
{
static unsigned int press_time=0;
if(P30==0)
{ //掃描按鍵1
if(++press_time<=0) --press_time; //計算按鍵時間,避免數據溢出
if(press_time==1000)
{ //按鍵長按2s時要做的事情
c--;
if(c<1)
{ c=10;
}}}
else
{
if((20<=press_time)&&(press_time<1000))
{ //大于10ms小于2s,短按
c++;
}
press_time=0;
if(c>10)
{ c=1;
}
if(c==10)
{ Alarm1=1000;
Alarm=50; }
if(c==1)
{
Alarm1=300;
Alarm=3000; }
if(c==2)
{ Alarm1=500;
Alarm=3000; }
if(c==3)
{
Alarm1=800;
Alarm=3000;
}
if(c==4)
{ Alarm1=1200;
Alarm=3000; }
if(c==5)
{
Alarm1=1600;
Alarm=3000;}
if(c==6)
{
Alarm1=2000;
Alarm=3000; }
if(c==7)
{ Alarm1=3000;
Alarm=2000; }
if(c==8)
{Alarm1=3000;
Alarm=1000;}
if(c==9)
{Alarm1=0;
Alarm=50;}
}}
void Timer1() interrupt 3
{
TH1=0xfc; //給定時器1賦初值,定時1ms
TL1=0x18;
Key_Check();//按鍵掃描
if( P55==0)
{ms3f1++; }
if(ms3f1>=170000&&g==1)//壓縮機約三分鐘延時保護
{ms3f2=1;}
a++;
if( a>1)
{a=0;}
if( a==1)
{P33=1;}//濕敏電阻約460赫方波
if( a==0)
{P33=0;}
if( P31==0)
{ms3f2=1;
j=1;
}
if( h==1 &&P55==1)
{j=1;
}
if( j==1)
{i++;}
if( i>=40000)//壓縮機啟動后最少運行約40秒
{ j=0;
h=0;
i=0;
}
if( P55==0)
{h=1;}
}
void Timer_Init(void)
{
TMOD|=0x10; //打開定時器01,工作方式1。
TH1=0xfc; //給定時器0賦初值,定時1ms
TL1=0x18;
ET1=1; //打開定時器0中斷允許
TR1=1; //打開定時器
EA=1; //打開總中斷
}
void fs1() //蒸發器風扇1
{
if(P55==1&&e==0 )
{
delay1us(1);
c=5;
e=1;
f=1;
}
if(P55==0&&f==1 )
{ delay1us(5000);
c=9;
f=0;
e=0;
} }
void main()
{
P_SW2 |= 0x80; //擴展寄存器(XFR)訪問使能
P3M0 = (P3M0 & ~0x07) | 0x08; P3M1 = (P3M1 & ~0x08) |
0x07;
//p33推挽其它高阻
P5M0 |= 0x30; P5M1 &= ~0x30;
ADCTIM = 0x3f; //設置 ADC 內部時序,ADC采樣時間建議設
ADCCFG = 0x2f; //設置 ADC 時鐘為系統時鐘/2/16
ADC_CONTR = 0x80; //使能 ADC 模塊
P55=0 ;
Timer_Init();
while (1)
{ fs() ;
gr();
ysj();
}}
void gr() //蒸發器過冷
{
if(Get(2)<700&&Get(1)>945&&ms3f1>150000)
{ c=3 ;
}}
void ysj() //壓縮機
{
if(Get(2)<700&&Get(1)<960&&ms3f2==1||j==1)//950xiao
{ delay1us(3);
if(Get(2)<700&&Get(1)<960&&ms3f2==1||j==1)
P55=1 ;
ms3f1=0;
}
else{ P55=0 ;
ms3f2=0;
}}
void fs(void) //蒸發器風扇
{ if(Get(0)>800)
{ g=1;
fs1();
P54=1;
delay1us(Alarm1);
P54=0;
delay1us(Alarm); }
else
{g=0;
}}
u16 Get(u8 channel) //channel = 0~15
{
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xF0) | 0x40 | channel; //啟動 AD 轉換
_nop_();
_nop_();
_nop_();
_nop_();
delay1us(1);
while((ADC_CONTR & 0x20) == 0) ; //wait for ADC finish
ADC_CONTR &= ~0x20; //清除ADC結束標志
return (((u16)ADC_RES << 8) | ADC_RESL);
} |