原来的电风扇就两个档,不能摇头,一直对着人吹,对健康不利。所以进行改造。
功能与结构:
两个LED是指示灯,闪烁是为了省电。
控制器交流电用的是TLP3503,是光耦可控硅。
按键可以选择弱风、中风、强风和关机,关机后,单片机是掉电模式。关机模式时,此按键可以把单片机唤醒。
不管什么情况,只要不按键,半个小时候,关机,处于掉电模式
电路图:
用万用板焊接的线路板以及和风扇的结合。
程序:
/
void wind ();
uchar read_key(void);
void keycount();
void DelayMs(unsigned int msec);
void Init()
{
TRIS=~0x3F; //GP3输入,其它输出
OPTION = 0x84; //分频器给定时器 32 4MHz
TMR0=0x64; //5ms x=257-tJ/4F
Fan_Open=1;
LED1_F=1;
LED2_F=1;
Delay_Time=1800;
}
void main()
{
Init();
while(1)
{
// if((GPWUF==1)&&(KEY1==0)) //引脚引起的中断唤醒
// {
// Init();
// }
if(Fan_Open==0)
{
FAN1=OFF;
FAN2=OFF;
LED1=OFF;
LED2=OFF;
LED2_F=0;
LED1_F=0;
DelayMs(2000);//延时一段时间,避免按键没有起来就唤醒单片机了。
OPTION =0x24; //引脚中断唤醒使能 弱上拉使能
// KEY_F2=KEY2;
KEY_F1=KEY1; //读出休眠前的按键状态。
SLEEP();
}
if(TMR0==0xFF)
{
TMR0=0x63; //
keycount();
count++;
if((count==100)||(count==200))
{
if(LED1_F==1)
{
LED1=~LED1;
}
if(LED2_F==1)
{
LED2=~LED2;
}
}
if(count==200)//1s
{
count=0;
if(Fan_Open==1)
{
wind ();
}
Delay_Time--;
if(Delay_Time==0)//30分钟后全关闭,要重新按键开机才行
{
Fan_Open=0;
}
}
}
}
}
void wind ()
{
static unsigned char KeyTask1=0,KeyTime1;
{
switch(KeyTask1)
{
case 0:
FAN1=ON;
FAN2=OFF;
KeyTime1=speed1;
KeyTask1=1;
break;
case 1:
KeyTime1--;
if(KeyTime1==0) KeyTask1=2;
break;
case 2:
FAN1=OFF;
FAN2=OFF;
KeyTime1=speed2;
KeyTask1=3;
break;
case 3:
KeyTime1--;
if(KeyTime1==0) KeyTask1=4;
break;
case 4:
FAN1=OFF;
FAN2=ON;
KeyTime1=speed3;
KeyTask1=5;
break;
case 5:
KeyTime1--;
if(KeyTime1==0) KeyTask1=6;
break;
case 6:
FAN1=OFF;
FAN2=OFF;
KeyTime1=speed4;
KeyTask1=7;
break;
case 7:
KeyTime1--;
if(KeyTime1==0) KeyTask1=0;
break;
default:
break;
}
}
}
uchar read_key(void)
{
static unsigned char KeyTask=0,KeyTime;
unsigned char KeyVal=0;
switch(KeyTask)
{
case 0:
if (KEY1==0)
{
KeyTime=4;
KeyTask++;
}
break;
case 1: KeyTime--;
if(KeyTime==0) KeyTask++;
break;
case 2:
if (KEY1==0)
{
if(KEY1==0) {KeyVal=1;}
KeyTask++;
}
else KeyTask=0;
break;
case 3:
if(KEY1==1)
{
KeyTime=4;
KeyTask++;
}
break;
case 4:
KeyTime--;
if(KeyTime==0)KeyTask++;
break;
case 5:
if(KEY1==1)
{
KeyTask=0;
}
else
{
KeyTask=3;
}
break;
default:
break;
}
return KeyVal;
}
void keycount()
{
uchar Key=0;
Key = read_key();
if(Key!=0)
{
Delay_Time=1800;
if((Key==1)&&(Fan_Open==1)) //关闭的时候不响应
{
if(++model==5)
{
model=0;
}
switch(model)
{
case 1: //弱风
speed1=1;
speed2=5;
speed3=3;
speed4=5;
LED2=OFF;
LED1=ON;
LED1_F=1;
LED2_F=0;
break;
case 2: //中风
speed1=5;
speed2=5;
speed3=5;
speed4=5;
LED1=OFF;
LED2=ON;
LED2_F=1;
LED1_F=0;
break;
case 3: //强风
speed1=5;
speed2=3;
speed3=5;
speed4=3;
LED1=ON;
LED2=ON;
LED1_F=1;
LED2_F=1;
break;
case 4:
Fan_Open=0;
break;
default:
break;
}
}
}
Key=0;
}
void DelayMs(unsigned int msec)
{
unsigned int x,y;
for(x=0; x<=msec;x++)
{
for(y=0;y<=110;y++);
}
}
/
void wind ();
uchar read_key(void);
void keycount();
void DelayMs(unsigned int msec);
void Init()
{
TRIS=~0x3F; //GP3输入,其它输出
OPTION = 0x84; //分频器给定时器 32 4MHz
TMR0=0x64; //5ms x=257-tJ/4F
Fan_Open=1;
LED1_F=1;
LED2_F=1;
Delay_Time=1800;
}
void main()
{
Init();
while(1)
{
// if((GPWUF==1)&&(KEY1==0)) //引脚引起的中断唤醒
// {
// Init();
// }
if(Fan_Open==0)
{
FAN1=OFF;
FAN2=OFF;
LED1=OFF;
LED2=OFF;
LED2_F=0;
LED1_F=0;
DelayMs(2000);//延时一段时间,避免按键没有起来就唤醒单片机了。
OPTION =0x24; //引脚中断唤醒使能 弱上拉使能
// KEY_F2=KEY2;
KEY_F1=KEY1; //读出休眠前的按键状态。
SLEEP();
}
if(TMR0==0xFF)
{
TMR0=0x63; //
keycount();
count++;
if((count==100)||(count==200))
{
if(LED1_F==1)
{
LED1=~LED1;
}
if(LED2_F==1)
{
LED2=~LED2;
}
}
if(count==200)//1s
{
count=0;
if(Fan_Open==1)
{
wind ();
}
Delay_Time--;
if(Delay_Time==0)//30分钟后全关闭,要重新按键开机才行
{
Fan_Open=0;
}
}
}
}
}
void wind ()
{
static unsigned char KeyTask1=0,KeyTime1;
{
switch(KeyTask1)
{
case 0:
FAN1=ON;
FAN2=OFF;
KeyTime1=speed1;
KeyTask1=1;
break;
case 1:
KeyTime1--;
if(KeyTime1==0) KeyTask1=2;
break;
case 2:
FAN1=OFF;
FAN2=OFF;
KeyTime1=speed2;
KeyTask1=3;
break;
case 3:
KeyTime1--;
if(KeyTime1==0) KeyTask1=4;
break;
case 4:
FAN1=OFF;
FAN2=ON;
KeyTime1=speed3;
KeyTask1=5;
break;
case 5:
KeyTime1--;
if(KeyTime1==0) KeyTask1=6;
break;
case 6:
FAN1=OFF;
FAN2=OFF;
KeyTime1=speed4;
KeyTask1=7;
break;
case 7:
KeyTime1--;
if(KeyTime1==0) KeyTask1=0;
break;
default:
break;
}
}
}
uchar read_key(void)
{
static unsigned char KeyTask=0,KeyTime;
unsigned char KeyVal=0;
switch(KeyTask)
{
case 0:
if (KEY1==0)
{
KeyTime=4;
KeyTask++;
}
break;
case 1: KeyTime--;
if(KeyTime==0) KeyTask++;
break;
case 2:
if (KEY1==0)
{
if(KEY1==0) {KeyVal=1;}
KeyTask++;
}
else KeyTask=0;
break;
case 3:
if(KEY1==1)
{
KeyTime=4;
KeyTask++;
}
break;
case 4:
KeyTime--;
if(KeyTime==0)KeyTask++;
break;
case 5:
if(KEY1==1)
{
KeyTask=0;
}
else
{
KeyTask=3;
}
break;
default:
break;
}
return KeyVal;
}
void keycount()
{
uchar Key=0;
Key = read_key();
if(Key!=0)
{
Delay_Time=1800;
if((Key==1)&&(Fan_Open==1)) //关闭的时候不响应
{
if(++model==5)
{
model=0;
}
switch(model)
{
case 1: //弱风
speed1=1;
speed2=5;
speed3=3;
speed4=5;
LED2=OFF;
LED1=ON;
LED1_F=1;
LED2_F=0;
break;
case 2: //中风
speed1=5;
speed2=5;
speed3=5;
speed4=5;
LED1=OFF;
LED2=ON;
LED2_F=1;
LED1_F=0;
break;
case 3: //强风
speed1=5;
speed2=3;
speed3=5;
speed4=3;
LED1=ON;
LED2=ON;
LED1_F=1;
LED2_F=1;
break;
case 4:
Fan_Open=0;
break;
default:
break;
}
}
}
Key=0;
}
void DelayMs(unsigned int msec)
{
unsigned int x,y;
for(x=0; x<=msec;x++)
{
for(y=0;y<=110;y++);
}
}