0
votes

I am trying to set up my STM32 microcontroller with HAL libraries to read from a ADT7420 temperature sensor using i2c. However I am unable to read the correct value from the sensor as I run my code.

Here is how I have done it so far using HAL libraries:

uint8_t I2C_ADDR = 0x48;
uint8_t TEMP_CONFIG = 0x03;

uint8_t data[2];

HAL_I2C_Master_Transmit(&hi2c1, I2C_ADDR, &TEMP_CONFIG , 1, 10000);

HAL_I2C_Master_Receive(&hi2c1, I2C_ADDR, data, 2, 10000);

uint16_t temp_raw = (uint16_t)((data[0]<<8) | data[1]);
int temp_value = calc_celcius(temp_raw); //TODO: convert to Celsius

Using this code, the temperature stays at zero indicating that something is not working correctly. Am I missing some config settings for the i2c setup in order to read the temperature value? Thanks.

2

2 Answers

2
votes

You need to send register address as a first byte of I2C write followed either by data to be written into that register, or I2C repeated start and reading the value. See page 18 and 19 in datasheet, you have linked, for details.

It seems that HAL_I2C_Mem_Write() and HAL_I2C_Mem_Read() functions should take care of this address writing for you. So, this part of your code would look like

HAL_I2C_Mem_Write(&hi2c1, I2C_ADDR, 0x03, I2C_MEMADD_SIZE_8BIT, &TEMP_CONFIG , 1, 10000);
HAL_I2C_Mem_Read(&hi2c1, I2C_ADDR, 0x00, I2C_MEMADD_SIZE_8BIT, data, 2, 10000);

Disclaimer: I have no experience with just HAL library, so my answer is based on quick read through documentation and source code provided only.

Note also that directly after powering up the IC, you need to wait for first conversion to complete in order to get non-zero value. According to the datasheet, first conversion should be done in 6 ms only (with low precision), each other conversion in normal mode takes 240 ms.

0
votes

Here am using temperature sensor (TMP175)

static void temp_Init(void)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) W_ADDR, (uint8_t) CONFIG_REG  , 
          (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) W_ADDR, (uint8_t) thigh  , (uint8_t) 1, 
       THigh, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) W_ADDR, (uint8_t) tlow  , (uint8_t) 1, TLow, 1, 1000);
}

The above command are based on the HAL_Drive ...initially give the I2C address.. then create one task in the main program..then in the created task just call the function then u will get the temperature value.. do use the following code.

 void StartDefaultTask(void const * argument)
{

  HAL_I2C_Mem_Read(&hi2c1, (uint8_t) R_ADDR, (uint8_t)CONFIG_REG , (uint8_t) 1, 
 &ConfigReg[0], 1, 1000);

 printf("   configreg==%x\t \r",ConfigReg[0]);

 HAL_I2C_Mem_Read(&hi2c1, (uint8_t) R_ADDR, (uint8_t) tlow, (uint8_t) 1,TLowReg, 
 2, 1000);
TLow1=(TLowReg[0]<<4);
TLow1|=(TLowReg[1]>>4);
TL=TLow1*0.0625;
printf(" Lower Limit Temperature=%f deg celsius \r ", TL);

HAL_I2C_Mem_Read(&hi2c1, (uint8_t) R_ADDR, (uint8_t) thigh, (uint8_t) 1,THighReg, 2, 1000);
THigh1=(THighReg[0]<<4);
THigh1|=(THighReg[1]>>4);TH=THigh1*0.0625;
printf(" upper Limit Temperature=%f deg celsius \r", TH);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) R_ADDR, (uint8_t) TEMPERATURE, (uint8_t) 1, TempReg, 2, 1000);
Shift= TempReg[0]<<4;
Shift|= TempReg[1]>>4;

Temp=Shift*0.0625;
printf("Temperature=%f deg Celsius \r",Temp);
}