2025-02-21 06:49:15 +00:00
|
|
|
|
#include "tmp117.h"
|
|
|
|
|
#include "i2c.h"
|
2025-03-07 09:09:42 +00:00
|
|
|
|
#include "cmsis_os.h"
|
2025-02-21 06:49:15 +00:00
|
|
|
|
|
|
|
|
|
|
2025-02-26 02:42:18 +00:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD>
|
2025-02-21 06:49:15 +00:00
|
|
|
|
HAL_StatusTypeDef TMP117_Init(void)
|
|
|
|
|
{
|
2025-02-26 02:42:18 +00:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>
|
|
|
|
|
uint8_t config_data[2] = {0x0C, 0x00}; // <20><><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>ǰ,<2C><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>1100 0000 0000<30><30>12.5ms
|
2025-02-21 06:49:15 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-07 09:09:42 +00:00
|
|
|
|
static float calculateAverage(float arr[], int avgLength) {
|
|
|
|
|
float sum = 0;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飨<EFBFBD><E9A3A8><EFBFBD><EFBFBD>10<31><30>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>ֱֵ<D6B5><D6B1><EFBFBD>ﵽָ<EFB5BD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
|
sum += arr[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|
|
|
|
float average = sum / avgLength;
|
|
|
|
|
return average;
|
|
|
|
|
}
|
2025-02-21 06:49:15 +00:00
|
|
|
|
// <20><>ȡ<EFBFBD>¶<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>棩
|
2025-03-07 09:09:42 +00:00
|
|
|
|
#define COLLECT_DATA_NUM 10
|
2025-02-21 06:49:15 +00:00
|
|
|
|
HAL_StatusTypeDef TMP117_Get_Temp(float *temp)
|
|
|
|
|
{
|
|
|
|
|
uint8_t raw_data[2] = {0};
|
|
|
|
|
int16_t temp_raw;
|
2025-03-07 09:09:42 +00:00
|
|
|
|
uint8_t ret_falt = 0;
|
|
|
|
|
float ret_temp;
|
|
|
|
|
float temp_temp[COLLECT_DATA_NUM] = {0};
|
2025-02-21 06:49:15 +00:00
|
|
|
|
|
2025-03-07 09:09:42 +00:00
|
|
|
|
for(int i = 0; i<COLLECT_DATA_NUM; i++)
|
|
|
|
|
{
|
|
|
|
|
TMP117_Init();
|
|
|
|
|
osDelay(20);
|
|
|
|
|
if(TMP117_Read(TMP117_TEMP_REG, raw_data) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
ret_falt++;
|
|
|
|
|
temp_temp[i] = 0;
|
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
temp_raw = (raw_data[0] << 8) | raw_data[1];
|
|
|
|
|
temp_temp[i] = temp_raw * 0.0078125f;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 06:49:15 +00:00
|
|
|
|
}
|
2025-03-07 09:09:42 +00:00
|
|
|
|
if(ret_falt >= COLLECT_DATA_NUM)
|
2025-02-21 06:49:15 +00:00
|
|
|
|
{
|
|
|
|
|
*temp = 0;
|
|
|
|
|
return HAL_ERROR;
|
|
|
|
|
}
|
2025-03-07 09:09:42 +00:00
|
|
|
|
|
|
|
|
|
ret_temp = calculateAverage(temp_temp, COLLECT_DATA_NUM - ret_falt);
|
|
|
|
|
|
|
|
|
|
if(ret_temp <= -60)
|
2025-02-21 06:49:15 +00:00
|
|
|
|
{
|
|
|
|
|
*temp = -60;
|
2025-03-07 09:09:42 +00:00
|
|
|
|
}else if(ret_temp >= 150)
|
2025-02-21 06:49:15 +00:00
|
|
|
|
{
|
|
|
|
|
*temp = 150;
|
2025-03-07 09:09:42 +00:00
|
|
|
|
}else
|
|
|
|
|
{
|
|
|
|
|
*temp = ret_temp;
|
2025-02-21 06:49:15 +00:00
|
|
|
|
}
|
|
|
|
|
return HAL_OK;
|
|
|
|
|
}
|
|
|
|
|
|