適合各種沒有休眠功能的焊臺,輸出用繼電器控制,原理是在烙鐵手柄內裝滾珠開關,烙鐵長時間不動關閉電源。代碼如下
#include "STC15F104E.H" //單片機頭文件
#define uint unsigned int //宏定義無符號整型數據
#define uchar unsigned char //宏定義無符號字符型數據
sbit jy=P3^4; //烙鐵繼電器
sbit zd=P3^2; //震動開關
uint xs,xms,cs; //中斷計時變量
bit ZT=0,dj=0,zd_sign=0;
void Timer0Init();
void jc()//次數檢測
{
static uint count=0;
if (zd==1&&zd_sign==0)
{
count++;
if(count>=10000)
{
count=0;
if(zd==1)
zd_sign=1;
}
}
if(zd==0&&zd_sign==1)
{
zd_sign=0;
cs++;xs=0;//cs是動作次數
}
if(cs>=5&&xs==60) //一分鐘內動作超過5次
{ cs=0;ZT=0;xs=0;}//次數歸零重新計數
if(cs<=4&&xs==60) //一分鐘內動作低于4次
{ZT=1;cs=0;}
if(ZT==1&&xs==400)//400秒無動作
{jy=0;cs=0;}//關閉電源
if(jy==0&&cs>=2)//拿起手柄
jy=1;//恢復供電
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定時器時鐘1T模式
TMOD &= 0xF0; //設置定時器模式
TL0 = 0xCD; //設置定時初始值
TH0 = 0xD4; //設置定時初始值
TF0 = 0; //清除TF0標志
TR0 = 1; //定時器0開始計時
}
/***************主程序****************/
void main()
{ Timer0Init();
//定時器初始化
EA=1; //開總中斷
ET0=1;
cs=0;
xms=0;
xs=0;
jy=1;
P3M1=0x00;
P3M0=0x30;
while(1)
{
jc();
}
}
void timer0() interrupt 1
{
xms++; //中斷變量Cnt50ms自+1
if(xms>=1000) //1秒
{
xms=0; //中斷變量Cnt50ms清0
xs++; //計數清0
}
}
|