工程代码如下:
/*检测proximity measurement是否可读*/
do { // wait on prox dataready bit
txData[0]=REGISTER_COMMAND; //80h
SetupXferRecAndExecute(I2C_ADDR_7BIT,txData,txSize, rxData, rxSize);// read command register
delay(delay_time);
} while (!(rxData[0] &COMMAND_MASK_PROX_DATA_READY)); // prox data readyCOMMAND_MASK_PROX_DATA_READY= 0010 0000
/*读取proximity measurement值*/
txSize = 1;
rxSize = 2;
txData[0]=REGISTER_PROX_VALUE; //87h
SetupXferRecAndExecute(I2C_ADDR_7BIT,txData,txSize, rxData, rxSize); // read prox value
delay(delay_time);
/*转换数据*/
MSB = rxData[0];
LSB = rxData[1];
last_Data = ((MSB <<8) + LSB);
SummeProxiValue +=last_Data; // Summary of all measured prox values
}
AverageProxiValue = SummeProxiValue/times;// calculate average //计算平均值
// AverageProxiValue =AverageProxiValue -2400;
/* 判断感应距离 输出状态 */
if(AverageProxiValue >3500)
{
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT,0, ledBits[0]); // 离开指示
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT,0, ledBits[1]); // 靠近指示
Chip_GPIO_SetPinToggle(LPC_GPIO_PORT,0, ledBits[2]); // 靠近闪烁
}
else
{
Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT,0, ledBits[0]);
Chip_GPIO_SetPinOutHigh(LPC_GPIO_PORT,0, ledBits[1]);
}
。
实现的视频如下:
http://v.youku.com/v_show/id_XMjYxNjE3NjkzNg==.html
。
最后再次感谢朋友们的帮助和支持!
`