2013年5月6日 星期一

STM8 I2C溝通 MASTER MODE READ



void I2C_Config(void)
{
I2C_DeInit();
  // I2C_Init(400000, 0x92,I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_10BIT, (uint8_t)16 );  //flow sensor
   I2C_Init(400000,0x92,I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, (uint8_t)16 ); //pressure sensor
}




void I2C_Read_Honeywell(u8* pBuffer,u8 ReadAddr,u8 NumByteToRead)
{
I2C_Cmd(ENABLE);

I2C_GenerateSTART(ENABLE);
    /* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
{
}
/* Send address for read */
I2C_Send7bitAddress(ReadAddr,I2C_DIRECTION_RX);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
{
}
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
I2C_AcknowledgeConfig(I2C_ACK_NONE);
I2C_GenerateSTOP(ENABLE);
}
if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData();
pBuffer++;
}
NumByteToRead--;
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C_ACK_CURR);
}

沒有留言:

張貼留言