2018-11-24 05:51 AM
hello,
I'm trying to interface the temperature and humidity sensor (HTU21D RH/T) which is based on the I2C interface. As referring to the given datasheet of the following sensor ( https://cdn-shop.adafruit.com/datasheets/1899_HTU21D.pdf ), in order to read the temperature or humidity's value I need to send an "ack bit" after reception of each 8bits data. I've tried to do this on my own but I'm not sure whether I'm arranging the commands (functions, flag, etc) in a proper sequence.
note* please refer the image file which is being attached along below.
thanking with regards
Aatif Shaikh
/****controller=stm32f205zgt6****/
/****clock source= 16Mhz internal******/
/****I2C INIT*****/
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* RCC Configuration */
/*I2C Peripheral clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
/*SDA GPIO clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
/*SCL GPIO clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
/* Connect PXx to I2C_SCL */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource0, GPIO_AF_I2C2);
/* Connect PXx to I2C_SDA */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource1, GPIO_AF_I2C2);
/* GPIO Configuration */
/*Configure I2C SCL pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/*Configure I2C SDA pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOF, &GPIO_InitStructure);
I2C_DeInit(I2C2);
/*!< I2C Struct Initialize */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_16_9;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_OwnAddress1 = 0xA0;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
/* I2C Initialization */
I2C_Init(I2C2, &I2C_InitStructure);
/* I2C ENABLE */
I2C_Cmd(I2C2, ENABLE) ;
/*****I2C write function*******/
uint8_t slaveAddress1= 0x40 ;
uint8_t dataBuffer = 0xE3 ;
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY))
;
/* Enable acknowledge */
I2C_AcknowledgeConfig(I2C2, ENABLE);
/* Generate the Start condition */
I2C_GenerateSTART(I2C2, ENABLE);
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)))
;
I2C_Send7bitAddress(I2C2,slaveAddress , I2C_Direction_Transmitter );
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)))
;
I2C_SendData(I2C2,dataBuffer);
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)))
;
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)))
;
/* Send I2Cx STOP Condition after the last byte has been transmitted */
I2C_GenerateSTOP(I2C2, ENABLE);
/*****I2C read function*******/
uint8_t rcvbuff[4];
uint8_t slaveAddress1 = 0x41 ;
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY))
;
/* Enable acknowledge */
I2C_AcknowledgeConfig(I2C2, ENABLE);
/* Generate the Start condition */
I2C_GenerateSTART(I2C2, ENABLE);
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)))
;
I2C_Send7bitAddress(I2C2,slaveAddress1 , I2C_Direction_Transmitter );
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)))
;
for(uint8_t ucloop=0;ucloop < 1; ucloop++)
{
rcvbuff[ucloop]= I2C_ReceiveData(I2C2);
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)))
;
while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)))
;
}
/* Send I2Cx STOP Condition after last byte has been transmitted */
I2C_GenerateSTOP(I2C2, ENABLE);
2018-11-24 01:23 PM
Seems like a relatively normal expectation for an I2C device..
Might want to use 0x80 for the address and not 0x40, ST passes the top 7-bits
The Slave should ACK if it recognizes the address presented, if it fails to respond to that you have the address wrong.