代码如下:
void filtInDat(unsigned char now, InBufInf *pInBuf, unsigned char *pRslt)
{
int i;
unsigned char tmp1;
unsigned char tmp2;
unsigned char tmp3;
tmp1 = tmp2 = now; //
for (i = 0; i < pInBuf->siz; i++) { // size=3
tmp1 &= pInBuf->pBuf[i]; // AND, get mininum value(include least "1")
tmp2 |= pInBuf->pBuf[i]; // OR, get maximun value(include most "1")
}
tmp3 = tmp1; // save result
tmp1 = ~tmp1;
tmp1 &= tmp2; // get the difference(where "1" in maximun value and "0" in mininum value)
tmp1 &= *pRslt; // compare the difference with *pRslt
*pRslt = tmp1 | tmp3; // get the new result
/*-----------------------------------------------------
update input buffer
-----------------------------------------------------*/
pInBuf->pBuf[pInBuf->idx] = now; //
if (++pInBuf->idx >= pInBuf->siz) pInBuf->idx = 0;
}
InBufInf是一个结构体,定义如下:
typedef struct {
unsigned char *pBuf; //
int siz; //
int idx; //
} InBufInf;
上面的代码不知大家理解了没有,实际上就是对相隔一定时间(如20ms)连续读取的三次IO的值进行处理,如果三次结果都一致,那么通过temp3直接改变结果(*pRslt ),如果三次结果不一样,那么取得三者的差异并与之前的结果进行比较,与之不同的就舍去(通过temp1实现)。我这样描述可能不好理解,大家列举几个值验证一下就明白了。
|