46 lines
1.0 KiB
C
46 lines
1.0 KiB
C
|
#include "tmp117.h"
|
|||
|
#include "i2c.h"
|
|||
|
|
|||
|
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ģʽ<C4A3><CABD>64<36><34>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>
|
|||
|
HAL_StatusTypeDef TMP117_Init(void)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ģʽ + AVG=64 (0x00A0)
|
|||
|
uint8_t config_data[2] = {0x00, 0xA0}; // <20><><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>ǰ
|
|||
|
return HAL_I2C_Mem_Write(&hi2c1, TMP117_ADDR << 1, TMP117_CONFIG_REG,
|
|||
|
I2C_MEMADD_SIZE_8BIT, config_data, 2, 100);
|
|||
|
}
|
|||
|
|
|||
|
// <20>ӼĴ<D3BC><C4B4><EFBFBD><EFBFBD><EFBFBD>ȡ˫<C8A1>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
HAL_StatusTypeDef TMP117_Read(uint8_t reg, uint8_t *buffer) {
|
|||
|
return HAL_I2C_Mem_Read(&hi2c1, TMP117_ADDR << 1, reg,
|
|||
|
I2C_MEMADD_SIZE_8BIT, buffer, 2, 100);
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD>¶<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>棩
|
|||
|
HAL_StatusTypeDef TMP117_Get_Temp(float *temp)
|
|||
|
{
|
|||
|
uint8_t raw_data[2] = {0};
|
|||
|
int16_t temp_raw;
|
|||
|
|
|||
|
if (HAL_OK == TMP117_Read(TMP117_TEMP_REG, raw_data)) {
|
|||
|
temp_raw = (raw_data[0] << 8) | raw_data[1];
|
|||
|
*temp = temp_raw * 0.0078125f;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
*temp = 0;
|
|||
|
return HAL_ERROR;
|
|||
|
}
|
|||
|
if(*temp <= -60)
|
|||
|
{
|
|||
|
*temp = -60;
|
|||
|
}
|
|||
|
if(*temp >= 150)
|
|||
|
{
|
|||
|
*temp = 150;
|
|||
|
}
|
|||
|
return HAL_OK;
|
|||
|
}
|
|||
|
|