Reputation: 1
I am trying to set up my STM32 microcontroller with HAL libraries to read from a TMP175 temperature sensor using i2c.
However, I am able to read the value from the single sensor but I wrote the code for 4 TMP175 temperature sensor.
I am getting only single current sensor value.
I referred the Datasheet of TMP175: http://www.ti.com/lit/ds/symlink/tmp75.pdf
I am using Keil IDE version 5 for programming.
Here is how I have done it so far using HAL libraries:
void StartDefaultTask(void const * argument)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x90 , (uint8_t) CONFIG_REG , (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t)0x90 , (uint8_t) thigh , (uint8_t) 1, THigh, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x90 , (uint8_t) tlow , (uint8_t) 1, TLow, 1, 1000);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x91, (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) 0x91, (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) 0x91, (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) 0x91, (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);
}
/* StartTask02 function */
void StartTask02(void const * argument)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9a , (uint8_t) CONFIG_REG , (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t)0x9a , (uint8_t) thigh , (uint8_t) 1, THigh, 2, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9a , (uint8_t) tlow , (uint8_t) 1, TLow, 2, 1000);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x01, (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) 0x02, (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) 0x03, (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) 0x00, (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);
}
/* StartTask03 function */
void StartTask03(void const * argument)
{
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9c , (uint8_t) CONFIG_REG , (uint8_t) 1,Config, 1, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t)0x9c , (uint8_t) thigh , (uint8_t) 1, THigh, 2, 1000);
HAL_I2C_Mem_Write(&hi2c1, (uint8_t) 0x9c , (uint8_t) tlow , (uint8_t) 1, TLow, 2, 1000);
HAL_I2C_Mem_Read(&hi2c1, (uint8_t) 0x01, (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) 0x02, (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) 0x03, (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) 0x00, (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);
}
Upvotes: 0
Views: 1460
Reputation: 7057
You are using the wrong slave/device address in the calls to HAL_I2C_Mem_Read()
. For example, in StartTask02()
you call HAL_I2C_Mem_Write()
with device address 0x9a but then you call HAL_I2C_Mem_Read()
with device address 0x01, 0x02, 0x03, and 0x00. Why did you do this differently than what you did in StartDefaultTask()
?
I would suggest you use some #defines, like this:
#define TMP175_1_I2C_ADDRESS 0x90
#define TMP175_2_I2C_ADDRESS 0x9a
#define TMP175_3_I2C_ADDRESS 0x9c
HAL_I2C_Mem_Write(&hi2c1, TMP175_2_I2C_ADDRESS , (uint8_t) tlow , (uint8_t) 1, TLow, 2, 1000);
HAL_I2C_Mem_Read(&hi2c1, TMP175_2_I2C_ADDRESS, (uint8_t)CONFIG_REG , (uint8_t) 1, &ConfigReg[0], 1, 1000);
I believe you can pass the same raw address to HAL_I2C_Mem_Write()
and HAL_I2C_Mem_Read()
because the HAL functions will set the least significant R/W bit appropriately.
Upvotes: 1