实验室需要做一个空气质量检测的节点,选用了IAQengine传感器测量室内CO2浓度。
附上代码:
#define IAQ_READ_ADDRESS 0xB5
void IAQengine_Read(u8 *pBuffer, u8 NumByteToRead)
{
/* Wait when the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* Generate start condition */
I2C_GenerateSTART(I2C1, ENABLE);
printf("step 1");
/* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
/* Send IAQ address for read */
I2C_Send7bitAddress(I2C1, IAQ_READ_ADDRESS, I2C_Direction_Receiver);
printf("step 2");
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
printf("step 3");
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Generate stop condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
/* Wait for the byte to be received */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET);
*pBuffer = I2C_ReceiveData(I2C1);
pBuffer++;
NumByteToRead--;
printf("step 4");
}
问题:
程序运行后,“Step 3”死活不出来,也就是主机发送地址后,从机没有应答。不知道原因出在哪?
我看网上好多例子都是首先主机作为发送者,发送从机的片内地址,然后主机再作为接受者开始接受数据。但是这款传感器好像没有片内地址,一共就7个寄存器。所以也就没有主机发送数据的过程。不知道问题是不是出在这里。。。。。
第一次发帖,希望各位高手能够帮帮我,先谢谢了!
|