add tmp112a code

This commit is contained in:
2025-08-18 00:18:21 +08:00
parent 443e9bf8f2
commit b6485c5c39
7 changed files with 135 additions and 52 deletions

View File

@@ -6,8 +6,8 @@
#include "tmp112.h"
/* Private function prototypes */
static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value);
static i2c_status_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value);
static i2c_result_t tmp112a_write_register(uint8_t reg_addr, uint16_t value);
static i2c_result_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value);
static float tmp112a_raw_to_celsius(uint16_t raw_data);
static uint16_t tmp112a_celsius_to_raw(float temperature);
@@ -18,11 +18,11 @@ static uint16_t tmp112a_celsius_to_raw(float temperature);
\retval tmp112a_status_t
*/
tmp112a_status_t tmp112a_init(void) {
i2c_status_t i2c_status;
i2c_result_t i2c_status;
/* 配置传感器为默认设置 */
i2c_status = tmp112a_config(TMP112A_CONFIG_DEFAULT);
if (i2c_status != I2C_STATUS_SUCCESS) {
if (i2c_status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -39,8 +39,8 @@ tmp112a_status_t tmp112a_init(void) {
\retval tmp112a_status_t
*/
tmp112a_status_t tmp112a_config(uint16_t config) {
i2c_status_t status = tmp112a_write_register(TMP112A_CONFIG_REG, config);
return (status == I2C_STATUS_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
i2c_result_t status = tmp112a_write_register(TMP112A_CONFIG_REG, config);
return (status == I2C_RESULT_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
}
/*!
@@ -51,7 +51,7 @@ tmp112a_status_t tmp112a_config(uint16_t config) {
*/
tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
uint16_t raw_data;
i2c_status_t status;
i2c_result_t status;
if (result == NULL) {
return TMP112A_STATUS_INVALID_PARAM;
@@ -59,7 +59,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
/* 读取温度寄存器 */
status = tmp112a_read_register(TMP112A_TEMP_REG, &raw_data);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -76,7 +76,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
/* 检查报警标志 */
uint16_t config_reg;
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status == I2C_STATUS_SUCCESS) {
if (status == I2C_RESULT_SUCCESS) {
result->alert_flag = (config_reg & TMP112A_CONFIG_AL) ? true : false;
} else {
result->alert_flag = false;
@@ -85,6 +85,11 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
return TMP112A_STATUS_SUCCESS;
}
void tmp112a_get_raw_temperature_value(uint8_t *value) {
i2c_read_16bits(TMP112A_ADDR, TMP112A_TEMP_REG, value);
return;
}
/*!
\brief 设置温度阈值
\param[in] low_temp: 低温阈值 (°C)
@@ -94,7 +99,7 @@ tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result) {
*/
tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
uint16_t low_raw, high_raw;
i2c_status_t status;
i2c_result_t status;
/* 参数验证 */
if (low_temp < TMP112A_TEMP_MIN || low_temp > TMP112A_TEMP_MAX ||
@@ -109,13 +114,13 @@ tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
/* 写入低温阈值 */
status = tmp112a_write_register(TMP112A_TLOW_REG, low_raw);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
/* 写入高温阈值 */
status = tmp112a_write_register(TMP112A_THIGH_REG, high_raw);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -130,11 +135,11 @@ tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp) {
*/
tmp112a_status_t tmp112a_shutdown(void) {
uint16_t config_reg;
i2c_status_t status;
i2c_result_t status;
/* 读取当前配置 */
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -143,7 +148,7 @@ tmp112a_status_t tmp112a_shutdown(void) {
/* 写回配置 */
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
return (status == I2C_STATUS_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
return (status == I2C_RESULT_SUCCESS) ? TMP112A_STATUS_SUCCESS : TMP112A_STATUS_ERROR;
}
/*!
@@ -154,11 +159,11 @@ tmp112a_status_t tmp112a_shutdown(void) {
*/
tmp112a_status_t tmp112a_wakeup(void) {
uint16_t config_reg;
i2c_status_t status;
i2c_result_t status;
/* 读取当前配置 */
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -167,7 +172,7 @@ tmp112a_status_t tmp112a_wakeup(void) {
/* 写回配置 */
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -185,7 +190,7 @@ tmp112a_status_t tmp112a_wakeup(void) {
*/
tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
uint16_t config_reg;
i2c_status_t status;
i2c_result_t status;
uint8_t timeout = 100; // 100ms超时
if (result == NULL) {
@@ -194,14 +199,14 @@ tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
/* 读取当前配置 */
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
/* 启动单次转换 */
config_reg |= TMP112A_CONFIG_OS;
status = tmp112a_write_register(TMP112A_CONFIG_REG, config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
@@ -209,7 +214,7 @@ tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result) {
do {
delay_ms(1);
status = tmp112a_read_register(TMP112A_CONFIG_REG, &config_reg);
if (status != I2C_STATUS_SUCCESS) {
if (status != I2C_RESULT_SUCCESS) {
return TMP112A_STATUS_ERROR;
}
timeout--;
@@ -253,9 +258,9 @@ const char* tmp112a_get_status_string(tmp112a_status_t status) {
\param[in] reg_addr: 寄存器地址
\param[in] value: 写入值
\param[out] none
\retval i2c_status_t
\retval i2c_result_t
*/
static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
static i2c_result_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
uint8_t data[2];
data[0] = (value >> 8) & 0xFF;
data[1] = value & 0xFF;
@@ -267,18 +272,18 @@ static i2c_status_t tmp112a_write_register(uint8_t reg_addr, uint16_t value) {
\brief 读取寄存器
\param[in] reg_addr: 寄存器地址
\param[out] value: 读取值指针
\retval i2c_status_t
\retval i2c_result_t
*/
static i2c_status_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value) {
static i2c_result_t tmp112a_read_register(uint8_t reg_addr, uint16_t *value) {
uint8_t data[2];
i2c_status_t status;
i2c_result_t status;
if (value == NULL) {
return I2C_STATUS_INVALID_PARAM;
return I2C_RESULT_INVALID_PARAM;
}
status = i2c_read_16bits(TMP112A_ADDR, reg_addr, data);
if (status == I2C_STATUS_SUCCESS) {
if (status == I2C_RESULT_SUCCESS) {
*value = ((uint16_t)data[0] << 8) | data[1];
}