번역 편집
I2C Code with STM32F10x(Sensor ID:0x58)
Sensor_0x58.H
/**
******************************************************************************
* @file Sensor_0x58.h
* @brief Generic driver for sensors with I2C address 0x58
* @version V2.0
* @date 2025-07-19
* @note Supports all Sencoch sensors with I2C address 0x58
* Based on STM32F10x Standard Peripheral Library
******************************************************************************
*/
#ifndef __SENSOR_0X58_H
#define __SENSOR_0X58_H
#포함 "myiic.h"
#포함 "main.h"
/*============================================================================*/
/* Sensor I2C Configuration */
/*============================================================================*/
#define SENSOR_I2C_ADDR0x58 /* Sensor I2C device address */
#define SENSOR_REG_DATA0x04 /* Data register start address */
#define SENSOR_REG_EOF0x20 /* Temperature offset register */
#define SENSOR_REG_SHIFT0x21 /* Temperature gain register */
/*============================================================================*/
/* Sensor Parameters */
/*============================================================================*/
/*
* 노트: Modify PMIN and PMAX according to your specific sensor model
*
* Common Ranges for I2C ADD 0x58 Sensors (unit: Pa):
* 0 ~ 0.5kPa: PMIN = 0.0f, PMAX = 500.0f
* 0 ~ 5kPa: PMIN = 0.0f, PMAX = 5000.0f
* 0 ~ 1000kPa: PMIN = 0.0f, PMAX = 1000000.0f
* -1 ~ 1kPa: PMIN = -1000.0f, PMAX = 1000.0f
* -5 ~ 5kPa: PMIN = -5000.0f, PMAX = 5000.0f
* -100 ~ 100kPa: PMIN = -100000.0f, PMAX = 100000.0f
*/
#define PMIN-100000.0f /* Lower limit: -100 kPa */
#define PMAX100000.0f /* Upper limit: 100 kPa */
/*============================================================================*/
/* Data Structure */
/*============================================================================*/
typedef struct {
uint8_t pressure_data[5]; /* Raw I2C data buffer */
int32_t pressure_AD; /* 24-bit pressure ADC value */
int16_t temperature_AD; /* 16-bit temperature ADC value */
uint8_t temp_calib_offset_code; /* Register 0x20 value */
uint8_t temp_calib_gain_exp; /* Register 0x21 value */
int16_t eof_out; /* Calculated EOFOut */
뜨다 shift_n; /* Calculated shiftN */
뜨다 압력; /* Pressure in Pa */
뜨다 온도; /* Temperature in ℃ */
} SensorData;
/*============================================================================*/
/* Function Prototypes */
/*============================================================================*/
무효의 Sensor_Init(무효의);
uint8_t Sensor_ReadData(SensorData*sensor_data);
#endif /* __SENSOR_0X58_H */
Sensor_0x58.C
/**
******************************************************************************
* @file Sensor_0x58.c
* @brief Generic driver implementation for sensors with I2C address 0x58
* @version V2.0
* @date 2025-07-19
* @note Supports all Sencoch sensors with I2C address 0x58
* Based on STM32F10x Standard Peripheral Library
******************************************************************************
*/
#포함 "Sensor_0x58.h"
#포함 "usart.h"
#포함 <math.h>
/*============================================================================*/
/* Private Functions */
/*============================================================================*/
/**
* @brief Read temperature calibration coefficients from sensor
* @param sensor_data: Pointer to SensorData structure
* @retval SUCCESS or FAILED
*/
static uint8_t Sensor_ReadCalibration(SensorData*sensor_data)
{
sensor_data->temp_calib_offset_code= i2c_read(SENSOR_I2C_ADDR, SENSOR_REG_EOF);
sensor_data->temp_calib_gain_exp= i2c_read(SENSOR_I2C_ADDR, SENSOR_REG_SHIFT);
만약 (sensor_data->temp_calib_offset_code== FAILED||
sensor_data->temp_calib_gain_exp== FAILED) {
반품 FAILED;
}
switch (sensor_data->temp_calib_offset_code) {
case 0x0C: sensor_data->eof_out= 4096; break;
case 0x8C: sensor_data->eof_out= -4096; break;
case 0x0D: sensor_data->eof_out= 8192; break;
case 0x8D: sensor_data->eof_out= -8192; break;
case 0x0E: sensor_data->eof_out= 16384; break;
case 0x8E: sensor_data->eof_out= -16384; break;
default: sensor_data->eof_out= 0; break;
}
sensor_data->shift_n= pow(2, sensor_data->temp_calib_gain_exp/ 10.0f);
반품 SUCCESS;
}
/**
* @brief Convert raw 24-bit pressure data to physical value
* @param raw: 24-bit raw pressure data (sign-extended to int32_t)
* @retval Pressure value in Pa
*/
static 뜨다 Sensor_ConvertPressure(int32_t raw)
{
const int32_t TWO_POW_21= 2097152;
반품 ((뜨다)raw/ TWO_POW_21) * (PMAX- PMIN);
}
/**
* @brief Convert raw 16-bit temperature data to physical value
* @param raw: 16-bit raw temperature data
* @param eof_out: EOFOut calibration coefficient
* @param shift_n: shiftN calibration coefficient
* @retval Temperature value in ℃
*/
static 뜨다 Sensor_ConvertTemperature(int16_t raw, int16_t eof_out, 뜨다 shift_n)
{
반품 ((뜨다)(raw- eof_out) / shift_n) + 25.0f;
}
/*============================================================================*/
/* Public Functions */
/*============================================================================*/
무효의 Sensor_Init(무효의)
{
/* I2C pins are initialized by IIC_Init() in main */
}
/**
* @brief Read pressure and temperature data from sensor
* @param sensor_data: Pointer to SensorData structure
* @retval SUCCESS or FAILED
*/
uint8_t Sensor_ReadData(SensorData*sensor_data)
{
int32_t pressure_raw;
int16_t temp_raw;
uint8_t result;
result= IIC_Read_nByte(SENSOR_I2C_ADDR, SENSOR_REG_DATA, 5, sensor_data->pressure_data);
만약 (result!= SUCCESS) {
반품 FAILED;
}
/* Combine 24-bit pressure data with sign extension */
pressure_raw= ((int32_t)sensor_data->pressure_data[0] << 16) |
((int32_t)sensor_data->pressure_data[1] << 8) |
(int32_t)sensor_data->pressure_data[2];
/* Sign extension: if bit 23 (0x800000) is set, extend to 32-bit */
만약 (pressure_raw& 0x800000) {
pressure_raw|= 0xFF000000;
}
/* Combine 16-bit temperature data (signed) */
temp_raw= ((int16_t)sensor_data->pressure_data[3] << 8) |
(int16_t)sensor_data->pressure_data[4];
sensor_data->pressure_AD= pressure_raw;
sensor_data->temperature_AD= temp_raw;
result= Sensor_ReadCalibration(sensor_data);
만약 (result!= SUCCESS) {
반품 FAILED;
}
sensor_data->압력= Sensor_ConvertPressure(pressure_raw);
sensor_data->온도= Sensor_ConvertTemperature(temp_raw,
sensor_data->eof_out,
sensor_data->shift_n);
#만약 DEBUG
printf("\r\n--- Sensor Data ---\r\n");
printf("Raw bytes: %02X %02X %02X %02X %02X\r\n",
sensor_data->pressure_data[0],
sensor_data->pressure_data[1],
sensor_data->pressure_data[2],
sensor_data->pressure_data[3],
sensor_data->pressure_data[4]);
printf("Pressure_AD: %ld (0엑스%08lX)\r\n", pressure_raw, pressure_raw);
printf("Temperature_AD: %디 (0엑스%04X)\r\n", temp_raw, temp_raw);
printf("EOFOut: %디, shiftN: %.2f\r\n", sensor_data->eof_out, sensor_data->shift_n);
printf("Pressure: %.2f Pa, 온도: %.2f 씨\r\n",
sensor_data->압력, sensor_data->온도);
#endif
반품 SUCCESS;
}