我正在尝试使用HAL设置我的STM32微控制器 使用i2c从TMP175温度传感器读取的库。
但是,我能够从单个传感器读取值,但我编写了4个TMP175温度传感器的代码。
我只获得单个电流传感器值。
我参考了TMP175的数据表:http://www.ti.com/lit/ds/symlink/tmp75.pdf
我正在使用Keil IDE第5版进行编程。
以下是我到目前为止使用HAL库的方法:
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);
}
答案 0 :(得分:1)
您在HAL_I2C_Mem_Read()
的来电中使用了错误的从站/设备地址。例如,在StartTask02()
中,您使用设备地址0x9a调用HAL_I2C_Mem_Write()
,然后使用设备地址0x01,0x02,0x03和0x00调用HAL_I2C_Mem_Read()
。为什么这样做的方式与StartDefaultTask()
中的方式不同?
我建议你使用一些#defines,如下所示:
#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);
我相信你可以将相同的原始地址传递给HAL_I2C_Mem_Write()
和HAL_I2C_Mem_Read()
,因为HAL函数会适当地设置最低有效R / W位。