cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 i2c sensor reading

er3481
Senior

Hi,

I am trying to read HDC1010 temperature sensor with code below, when i click run button while debugging, code does not working, can not continue. But when i debug the function row by row, it woks clearly. Any advise ?

void Receive(u8 Address, u8 Register)
{   
   
    /*re-enable ACK bit incase it was disabled last call*/
    I2C_AcknowledgeConfig(I2C1, ENABLE);
    /* Test on BUSY Flag */
    while (I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
    /* Enable the I2C peripheral */
/*======================================================*/
    I2C_GenerateSTART(I2C1, ENABLE);
    /* Test on start flag */
    while (!I2C_GetFlagStatus(I2C1,I2C_FLAG_SB));
    /* Send device address for write */
    I2C_Send7bitAddress(I2C1, Address, I2C_Direction_Transmitter);
    /* Test on master Flag */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    /* Send the device's internal address to write to */
    I2C_SendData(I2C1,Register);
    /* Test on TXE FLag (data sent) */
    while (!I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE));
/*=====================================================*/
      /* Send START condition a second time (Re-Start) */
    I2C_GenerateSTART(I2C1, ENABLE);
    /* Test start flag */
    while (!I2C_GetFlagStatus(I2C1,I2C_FLAG_SB));
    /* Send address for read */
    I2C_Send7bitAddress(I2C1, Address|0x01, I2C_Direction_Receiver);
    /* Test Receive mode Flag */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    /* load in all 6 registers */
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
    data[0] = I2C_ReceiveData(I2C1);
    while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
    data[1] = I2C_ReceiveData(I2C1);
    /*enable NACK bit */
    I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Current);
    I2C_AcknowledgeConfig(I2C1, DISABLE);
    /* Send STOP Condition */
    I2C_GenerateSTOP(I2C1, ENABLE);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)); // stop bit flag
 
}

3 REPLIES 3
raptorhal2
Lead

Timing problem?

Sensor conversion time is about 6.5 msec. Do you check the data ready pin before reading?

Cheers, Hal

Hi,

Yes i check data ready pin, but i have noticed that there is a problem with stdh peripheral i2c library at startup. Sometimes i get error from while((I2C_GetFlagStatus(I2C_DEV_EEPROM, I2C_FLAG_BUSY) != 0)) and it returns error (but this doesn't occur everytime). So when i try to communicate with HAL library there is no problem. I don't know what is problem with stdh peripheral library.Any advise ?

raptorhal2
Lead

You have apparently isolated the problem to SPL/HDC1010 timing. Perhaps another viewer may have an answer from experience.

Since you seem to prefer the SPL, options to consider:

  1. Trap the error to the specific status call that fails
  2. Compare the SPL/HAL code for differences.
  3. Insert your own fixed timing delay for library calls for status
  4. Modify the SPL to match HAL timing & status reads.

Cheers, Hal