123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*
- 型号:SSCMANN025MG2A3
- 表压传感器,测的是相对压力。
- 量程:0-25mba
- 压力值:0-16383
- 压力转换公式:压力(mba)=[(压力值-0)*(25-0)] / (16383-0) + 0
- 1 mba = 100 pa
- */
- #include "SSCMAN.h"
- #include "iic.h"
- #include "usart.h"
- #include <stdio.h> // sprintf格式化输出
- #include <string.h> // strlen
- #include "main.h"
- #define P_MIN 0.0f // 最小压力,单位:mbar
- #define P_MAX 25.0f // 最大压力,单位:mbar
- #define RAW_MIN 0 // 原始数据的最小值
- #define RAW_MAX 16383 // 原始数据的最大值(14位)
- #define MBAR_TO_PA 100.0f // 1 mbar = 100 Pa
- uint16_t zero_offset = 0; // 用于存储零点偏移值
- uint16_t pressure_raw = 0;
- extern uint8_t data_P01[]; // 用于存储传感器数据
- // 压力转换函数:将原始的14位压力值转换为实际压力值(单位:帕斯卡)
- float Convert_Pressure_To_Pa(uint16_t raw_pressure)
- {
- // 计算公式:P(Pa) = [(Raw - RAW_MIN) / (RAW_MAX - RAW_MIN)] * (P_MAX - P_MIN) + P_MIN
- float pressure_mbar = ((float)(raw_pressure - RAW_MIN) / (RAW_MAX - RAW_MIN)) * (P_MAX - P_MIN) + P_MIN;
- return pressure_mbar * MBAR_TO_PA; // 转换为帕斯卡
- }
- // 读取压力数据
- uint8_t SSC_ReadData(uint16_t* pressure)
- {
- uint8_t data[4] = {0}; // 存储传感器返回的4字节数据
- uint8_t status = 0; // 存储传感器的状态
- // 读取4字节数据
- Soft_I2C_ReadMulti(SSC_I2C_ADDRESS, 0x00, data, 4);
- // 读取状态位
- status = (data[0] & 0xC0) >> 6;
- // 读取压力数据(14位)
- uint16_t raw_pressure = ((data[0] & 0x3F) << 8) | data[1];
- *pressure = raw_pressure; // 原始压力值
- return status; // 返回状态码,0表示正常
- }
- void Print_SensorData(void)
- {
- uint8_t status = 0;
- char buffer[50];
- // 读取传感器数据
- status = SSC_ReadData(&pressure_raw);
-
- // 将原始压力值转换为帕斯卡
- float pressure_pa = Convert_Pressure_To_Pa(pressure_raw);
- uint16_t pressure_pa_int = (uint16_t)pressure_pa; // 转换为整数
- // 将压力数据存储到data_P01的前两个字节
- data_P01[0] = (pressure_pa_int >> 8) & 0xFF; // 高字节
- data_P01[1] = pressure_pa_int & 0xFF; // 低字节
-
- if (status == 0)
- {
- // 输出读取到的压力数据
- sprintf(buffer, "Pressure (Pa): %u\r\n", pressure_pa_int);
- HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
- }
- else
- {
- // 输出错误信息
- sprintf(buffer, "Sensor read failed, status: %u\r\n", status);
- HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
- }
- }
- // 零点校准函数
- void Zero_Point_Calibration(void)
- {
- uint16_t raw_pressure = 1791; // 1791 16383
-
- // 读取压力传感器的初始值,并获取其原始压力值
- // SSC_ReadData(&raw_pressure); // 获取传感器的原始压力值
-
- // 将初始压力值保存为零点偏移量
- zero_offset = raw_pressure;
-
- printf("Zero Point Calibration Completed. Zero Offset: %d (raw)\r\n", zero_offset);
- }
- // 读取并校准压力数据
- void Read_PressureData(void)
- {
- uint16_t raw_pressure = 0;
- int16_t calibrated_pressure = 0;
- // char buffer[50];
- //
- // 读取传感器原始压力数据
- SSC_ReadData(&raw_pressure);
- printf("SSC_ReadData %d \n",raw_pressure);
-
- // 将原始压力值转换为帕斯卡(单位:Pa)
- float pressure_pa = Convert_Pressure_To_Pa(raw_pressure);
- uint16_t pressure_pa_int = (uint16_t)pressure_pa; // 转换为整数
- printf("pressure_pa_int %d \n",pressure_pa_int);
- // 计算校准后的压力值(减去零点偏移量)
- calibrated_pressure = pressure_pa_int - Convert_Pressure_To_Pa(zero_offset); // 使用校准偏差
- printf("calibrated_pressure %d \n",calibrated_pressure);
- if (calibrated_pressure < 0)
- {
- calibrated_pressure = 0;
- }
- if(calibrated_pressure > flash_data.second_threshold)
- {
- data_P01[0] = 2;
- Save_parameter_To_Flash( 0x0064, 0x01 );
- }else
- {
- data_P01[0] = 0;
- Save_parameter_To_Flash( 0x0064, 0x78 );
- }
- // 将压力数据存储到data_P01的前两个字节
- data_P01[1] = (calibrated_pressure >> 8) & 0xFF; // 高字节
- data_P01[2] = calibrated_pressure & 0xFF; // 低字节
-
- printf("data_P01: ");
- for (int i = 0; i < 8; i++) {
- printf("%02X ", data_P01[i]);
- }
- printf("\n");
- // // 输出校准后的压力值
- // sprintf(buffer, "Calibrated Pressure (Pa): %d\r\n", calibrated_pressure);
- // HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), 1000);
- }
|