generated from hulk/gd32e23x_template_cmake_vscode
156 lines
5.4 KiB
C
156 lines
5.4 KiB
C
//
|
|
// Created by dell on 24-12-20.
|
|
// TMP112A Temperature Sensor Driver Header
|
|
//
|
|
|
|
#ifndef TMP112_H
|
|
#define TMP112_H
|
|
|
|
#include "gd32e23x_it.h"
|
|
#include "gd32e23x.h"
|
|
#include "systick.h"
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
#include "board_config.h"
|
|
#include "i2c.h"
|
|
|
|
/******************************************************************************/
|
|
/* TMP112A I2C Address */
|
|
#define TMP112A_ADDR (0x48) // 7-bit address (ADD0=GND)
|
|
|
|
/* Register Addresses */
|
|
/******************************************************************************/
|
|
#define TMP112A_TEMP_REG 0x00 // 温度寄存器
|
|
#define TMP112A_CONFIG_REG 0x01 // 配置寄存器
|
|
#define TMP112A_TLOW_REG 0x02 // 低温阈值寄存器
|
|
#define TMP112A_THIGH_REG 0x03 // 高温阈值寄存器
|
|
|
|
/* Configuration Register Bits */
|
|
/******************************************************************************/
|
|
#define TMP112A_CONFIG_OS (1 << 15) // One-shot
|
|
#define TMP112A_CONFIG_R1 (1 << 14) // Converter resolution bit 1
|
|
#define TMP112A_CONFIG_R0 (1 << 13) // Converter resolution bit 0
|
|
#define TMP112A_CONFIG_F1 (1 << 12) // Fault queue bit 1
|
|
#define TMP112A_CONFIG_F0 (1 << 11) // Fault queue bit 0
|
|
#define TMP112A_CONFIG_POL (1 << 10) // Polarity
|
|
#define TMP112A_CONFIG_TM (1 << 9) // Thermostat mode
|
|
#define TMP112A_CONFIG_SD (1 << 8) // Shutdown
|
|
#define TMP112A_CONFIG_CR1 (1 << 7) // Conversion rate bit 1
|
|
#define TMP112A_CONFIG_CR0 (1 << 6) // Conversion rate bit 0
|
|
#define TMP112A_CONFIG_AL (1 << 5) // Alert
|
|
#define TMP112A_CONFIG_EM (1 << 4) // Extended mode
|
|
|
|
/* Resolution Settings */
|
|
/******************************************************************************/
|
|
#define TMP112A_RESOLUTION_9BIT 0x0000 // 9-bit (0.5°C)
|
|
#define TMP112A_RESOLUTION_10BIT 0x2000 // 10-bit (0.25°C)
|
|
#define TMP112A_RESOLUTION_11BIT 0x4000 // 11-bit (0.125°C)
|
|
#define TMP112A_RESOLUTION_12BIT 0x6000 // 12-bit (0.0625°C)
|
|
|
|
/* Conversion Rate Settings */
|
|
/******************************************************************************/
|
|
#define TMP112A_RATE_0_25HZ 0x0000 // 0.25 Hz (4s)
|
|
#define TMP112A_RATE_1HZ 0x0040 // 1 Hz (1s)
|
|
#define TMP112A_RATE_4HZ 0x0080 // 4 Hz (250ms)
|
|
#define TMP112A_RATE_8HZ 0x00C0 // 8 Hz (125ms)
|
|
|
|
/* Default Configuration */
|
|
/******************************************************************************/
|
|
#define TMP112A_CONFIG_DEFAULT (TMP112A_RESOLUTION_12BIT | TMP112A_RATE_4HZ)
|
|
|
|
/* Temperature Conversion Constants */
|
|
/******************************************************************************/
|
|
#define TMP112A_TEMP_RESOLUTION 0.0625f // 12-bit resolution (°C/LSB)
|
|
#define TMP112A_TEMP_MIN -55.0f // 最低温度 (°C)
|
|
#define TMP112A_TEMP_MAX 125.0f // 最高温度 (°C)
|
|
|
|
/* Status Definitions */
|
|
/******************************************************************************/
|
|
typedef enum {
|
|
TMP112A_STATUS_SUCCESS = 0,
|
|
TMP112A_STATUS_ERROR,
|
|
TMP112A_STATUS_TIMEOUT,
|
|
TMP112A_STATUS_INVALID_PARAM,
|
|
TMP112A_STATUS_OUT_OF_RANGE
|
|
} tmp112a_status_t;
|
|
|
|
typedef struct {
|
|
uint16_t raw_data;
|
|
float temperature_c;
|
|
float temperature_f;
|
|
bool alert_flag;
|
|
} tmp112a_result_t;
|
|
|
|
/******************************************************************************/
|
|
/* Function Declarations */
|
|
|
|
/*!
|
|
\brief 初始化TMP112A传感器
|
|
\param[in] none
|
|
\param[out] none
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_init(void);
|
|
|
|
/*!
|
|
\brief 配置TMP112A传感器
|
|
\param[in] config: 配置值
|
|
\param[out] none
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_config(uint16_t config);
|
|
|
|
/*!
|
|
\brief 读取温度
|
|
\param[in] none
|
|
\param[out] result: 结果结构体指针
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_read_temperature(tmp112a_result_t *result);
|
|
|
|
/*!
|
|
\brief 设置温度阈值
|
|
\param[in] low_temp: 低温阈值 (°C)
|
|
\param[in] high_temp: 高温阈值 (°C)
|
|
\param[out] none
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_set_thresholds(float low_temp, float high_temp);
|
|
|
|
/*!
|
|
\brief 进入关机模式
|
|
\param[in] none
|
|
\param[out] none
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_shutdown(void);
|
|
|
|
/*!
|
|
\brief 退出关机模式
|
|
\param[in] none
|
|
\param[out] none
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_wakeup(void);
|
|
|
|
/*!
|
|
\brief 单次转换
|
|
\param[in] none
|
|
\param[out] result: 结果结构体指针
|
|
\retval tmp112a_status_t
|
|
*/
|
|
tmp112a_status_t tmp112a_one_shot(tmp112a_result_t *result);
|
|
|
|
/*!
|
|
\brief 获取状态字符串
|
|
\param[in] status: 状态码
|
|
\param[out] none
|
|
\retval const char* 状态字符串
|
|
*/
|
|
const char* tmp112a_get_status_string(tmp112a_status_t status);
|
|
|
|
#endif //TMP112_H
|