我文化不多,理论套路都不懂,经常想到什么写什么,方法不固定,可能方法很笨,欢迎拍砖。
下面是其中一种:
//端口函数,每4mS扫描一次端口并处理
void FunPortIn(void) //这函数我放主循环里,完成端口扫描和去抖动。端口状态在In.Port中,键值的转换不在该程序。
{
static U8 Time1mS;
static U8 TimeS;
#ifdef DebugPortIn
U8 DbIndex;
#endif
if (Time1mS == Time.T1mS) return;
Time1mS = Time.T1mS;
#ifdef DebugPortIn
if (TimeS++ < 250) return; //调试时改为每250mS扫描一次,方便打印数据观察
#else
if (TimeS++ < 4) return; //每4mS扫描一次
#endif
TimeS = 0;
PortScan();
PortFilter();
#ifdef DebugPortIn
printf("\r\nInput.Port[0]=%x ;",Input.Port[0]);
printf("In.Port[0]=%x ;",In.Port[0]);
printf("Input.Filter=");
for (DbIndex=0; DbIndex<MaxInPort; DbIndex++)
{
printf("%d; ",Input.Filter[DbIndex]);
}
#endif
}
//读入端口数据,如果该端口禁止则固定为0
void PortScan(void)
{
if ((InPort0 == 0) && (CheckInPort(In0) == Enable)) //In0
{
SetBit(Input.Port[In0/8],In0); //Input.Port保存临时端口状态,每Bit对应一位
}
else
{
ClrBit(Input.Port[In0/8],In0);
}
if ((InPort1 == 0) && (CheckInPort(In1) == Enable)) //In1
{
SetBit(Input.Port[In1/8],In1);
}
else
{
ClrBit(Input.Port[In1/8],In1);
}
//还有端口就往这加
}
//端口防抖动,采用的方法是:0的机会多就按0处理,1的机会多就按1处理
void PortFilter(void)
{
U8 Index;
for (Index=0; Index<MaxInPort; Index++)
{
if (Input.Port[(Index/8)] & (0x01<<(Index%8)))
{
if (++Input.Filter[Index] >= Input.SetFilter[Index]) //Input.SetFilter保存每位需要过滤的次数
{
SetBit(In.Port[Index/8],Index); //输出1 //In.Port保存过滤后的端口状态
Input.Filter[Index] = 0;
}
}
else
{
if (-(--Input.Filter[Index]) >= Input.SetFilter[Index])
{
ClrBit(In.Port[Index/8],Index); //输出0
Input.Filter[Index] = 0;
}
}
}
}
|