本帖最后由 pzsh 于 2021-10-26 17:54 编辑
PIC12F508单片机是没有中断的,定时器、按键中断只能是查询方式。
编译器用的XC8,编译环境IDE用的是MPLAB X IDE。
下载器是PICKIT3.
原来的电风扇就两个档,不能摇头,一直对着人吹,对健康不利。所以进行改造。
功能与结构:
两个LED是指示灯,闪烁是为了省电。
控制器交流电用的是TLP3503,是光耦可控硅。
按键可以选择弱风、中风、强风和关机,关机后,单片机是掉电模式。关机模式时,此按键可以把单片机唤醒。
不管什么情况,只要不按键,半个小时候,关机,处于掉电模式
//***************************************************
// __________________
// VDD-| 1 8 |-VSS
// GP5-| 2 27 |-GP0/DAT
// GP4-| 3 26 |-GP1/CLK
//GP3/RMCLR--| 4 25 |-GP2
// |________________|
// 12F508
//***************************************************
//按键唤醒
#include
#include
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT Enable )
#pragma config CP = OFF // Code Protection bit (Code protection off)
#pragma config MCLRE = OFF // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is digital input, MCLR internally tied to VDD)
#define uchar unsigned char
#define uint unsigned int
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef ON //风扇开
#define ON 0
#endif
#ifndef OFF //风扇关闭
#define OFF 1
#endif
uchar speed1=5,speed2=3,speed3=5,speed4=3;//
uchar model;
bit Fan_Open;
#define LED1 GP4
#define LED2 GP2
#define KEY1 GP3
#define KEY2 GP4
#define FAN1 GP0
#define FAN2 GP1
bit KEY_F1,KEY_F2; //读出休眠前的按键状态。;
uchar count;
uint Delay_Time;
bit LED2_F;
bit LED1_F;
/***********函数声明***********/
void wind ();
uchar read_key(void);
void keycount();
void DelayMs(unsigned int msec);
/***********初始化***********/
void Init()
{
TRIS=~0x3F; //GP3输入,其它输出
OPTION = 0xC4; //分频器给定时器 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++);
}
}
|