#include <Windows.h>
class t_off_typ//延时关类结构
{
private:
LARGE_INTEGER start,fre;
bool oldstatus;
bool flag;//程序功能是否打开标志
public:
t_off_typ()
{
oldstatus=false;
Q=false;
flag=false;
ET=0;
}
bool I;//输入电平信号,高true,低false,要想延时关,要给I一个下降沿信号
bool Q;//输出电平信号,同上
long ET;//要延时的时间,单位:毫秒
friend void t_off(t_off_typ * t);
};
void t_off(t_off_typ * t)//延时关函数
{
LARGE_INTEGER end;
long count1;
if(t->ET>64000||t->ET<=0)
{
return;
}
if(t->oldstatus==true&&t->I==false)//如果该结构获取了一个下降沿信号
{
QueryPerformanceFrequency(&t->fre);//得到当前时钟的频率,即1秒钟能记录的脉冲数
QueryPerformanceCounter(&t->start);//开始计时
t->flag=true;//延时功能开启
}
else if(t->I==true)
{
t->Q=true;
t->flag=false;
}
t->oldstatus=t->I;
if(t->flag==true)
{
QueryPerformanceCounter(&end);
count1=(long)((double)(end.QuadPart-(t->start.QuadPart))/(t->fre.QuadPart)*1000);
if(count1>t->ET)//如果时间已经超过需延时的时间
{
t->Q=false;
t->flag=false;//延时功能关闭
}
}
return;
}
class t_on_typ//延时开类结构
{
private:
LARGE_INTEGER start,fre;
bool oldstatus;
bool flag;//程序功能是否打开标志
public:
t_on_typ()
{
oldstatus=true;
Q=true;
flag=false;
ET=0;
}
bool I;//输入电平信号,高true,低false,要想延时开,要给I一个上升沿信号
bool Q;//输出电平信号,同上
long ET;//要延时的时间,单位:毫秒
friend void t_on(t_on_typ * t);
};
void t_on(t_on_typ * t)//延时开函数
{
LARGE_INTEGER end;
long count1;
if(t->ET>64000||t->ET<=0)
{
return;
}
if(t->oldstatus==false&&t->I==true)//如果该结构获取了一个上升沿信号
{
QueryPerformanceFrequency(&t->fre);//得到当前时钟的频率,即1秒钟能记录的脉冲数
QueryPerformanceCounter(&t->start);//开始计时
t->flag=true;
}
else if(t->I==false)
{
t->Q=false;
t->flag=false;
}
t->oldstatus=t->I;
if(t->flag==true)
{
QueryPerformanceCounter(&end);
count1=(long)((double)(end.QuadPart-(t->start.QuadPart))/(t->fre.QuadPart)*1000);
if(count1>t->ET)//如果时间已经超过需延时的时间
{
t->Q=true;
t->flag=false;
}
}
return;
}