SSCMAN.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. 型号:SSCMANN025MG2A3
  3. 表压传感器,测的是相对压力。
  4. 量程:0-25mba
  5. 压力值:0-16383
  6. 压力转换公式:压力(mba)=[(压力值-0)*(25-0)] / (16383-0) + 0
  7. 1 mba = 100 pa
  8. */
  9. #include "SSCMAN.h"
  10. #include "iic.h"
  11. #include "usart.h"
  12. #include <stdio.h> // sprintf格式化输出
  13. #include <string.h> // strlen
  14. #include "main.h"
  15. #define P_MIN 0.0f // 最小压力,单位:mbar
  16. #define P_MAX 25.0f // 最大压力,单位:mbar
  17. #define RAW_MIN 0 // 原始数据的最小值
  18. #define RAW_MAX 16383 // 原始数据的最大值(14位)
  19. #define MBAR_TO_PA 100.0f // 1 mbar = 100 Pa
  20. uint16_t zero_offset = 0; // 用于存储零点偏移值
  21. uint16_t pressure_raw = 0;
  22. extern uint8_t data_P01[]; // 用于存储传感器数据
  23. // 压力转换函数:将原始的14位压力值转换为实际压力值(单位:帕斯卡)
  24. float Convert_Pressure_To_Pa(uint16_t raw_pressure)
  25. {
  26. // 计算公式:P(Pa) = [(Raw - RAW_MIN) / (RAW_MAX - RAW_MIN)] * (P_MAX - P_MIN) + P_MIN
  27. float pressure_mbar = ((float)(raw_pressure - RAW_MIN) / (RAW_MAX - RAW_MIN)) * (P_MAX - P_MIN) + P_MIN;
  28. return pressure_mbar * MBAR_TO_PA; // 转换为帕斯卡
  29. }
  30. // 读取压力数据
  31. uint8_t SSC_ReadData(uint16_t* pressure)
  32. {
  33. uint8_t data[4] = {0}; // 存储传感器返回的4字节数据
  34. uint8_t status = 0; // 存储传感器的状态
  35. // 读取4字节数据
  36. Soft_I2C_ReadMulti(SSC_I2C_ADDRESS, 0x00, data, 4);
  37. // 读取状态位
  38. status = (data[0] & 0xC0) >> 6;
  39. // 读取压力数据(14位)
  40. uint16_t raw_pressure = ((data[0] & 0x3F) << 8) | data[1];
  41. *pressure = raw_pressure; // 原始压力值
  42. return status; // 返回状态码,0表示正常
  43. }
  44. void Print_SensorData(void)
  45. {
  46. uint8_t status = 0;
  47. char buffer[50];
  48. // 读取传感器数据
  49. status = SSC_ReadData(&pressure_raw);
  50. // 将原始压力值转换为帕斯卡
  51. float pressure_pa = Convert_Pressure_To_Pa(pressure_raw);
  52. uint16_t pressure_pa_int = (uint16_t)pressure_pa; // 转换为整数
  53. // 将压力数据存储到data_P01的前两个字节
  54. data_P01[0] = (pressure_pa_int >> 8) & 0xFF; // 高字节
  55. data_P01[1] = pressure_pa_int & 0xFF; // 低字节
  56. if (status == 0)
  57. {
  58. // 输出读取到的压力数据
  59. sprintf(buffer, "Pressure (Pa): %u\r\n", pressure_pa_int);
  60. HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
  61. }
  62. else
  63. {
  64. // 输出错误信息
  65. sprintf(buffer, "Sensor read failed, status: %u\r\n", status);
  66. HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
  67. }
  68. }
  69. // 零点校准函数
  70. void Zero_Point_Calibration(void)
  71. {
  72. uint16_t raw_pressure = 1791; // 1791 16383
  73. // 读取压力传感器的初始值,并获取其原始压力值
  74. // SSC_ReadData(&raw_pressure); // 获取传感器的原始压力值
  75. // 将初始压力值保存为零点偏移量
  76. zero_offset = raw_pressure;
  77. printf("Zero Point Calibration Completed. Zero Offset: %d (raw)\r\n", zero_offset);
  78. }
  79. // 读取并校准压力数据
  80. void Read_PressureData(void)
  81. {
  82. uint16_t raw_pressure = 0;
  83. int16_t calibrated_pressure = 0;
  84. // char buffer[50];
  85. //
  86. // 读取传感器原始压力数据
  87. SSC_ReadData(&raw_pressure);
  88. printf("SSC_ReadData %d \n",raw_pressure);
  89. // 将原始压力值转换为帕斯卡(单位:Pa)
  90. float pressure_pa = Convert_Pressure_To_Pa(raw_pressure);
  91. uint16_t pressure_pa_int = (uint16_t)pressure_pa; // 转换为整数
  92. printf("pressure_pa_int %d \n",pressure_pa_int);
  93. // 计算校准后的压力值(减去零点偏移量)
  94. calibrated_pressure = pressure_pa_int - Convert_Pressure_To_Pa(zero_offset); // 使用校准偏差
  95. printf("calibrated_pressure %d \n",calibrated_pressure);
  96. if (calibrated_pressure < 0)
  97. {
  98. calibrated_pressure = 0;
  99. }
  100. if(calibrated_pressure > flash_data.second_threshold)
  101. {
  102. data_P01[0] = 2;
  103. Save_parameter_To_Flash( 0x0064, 0x01 );
  104. }else
  105. {
  106. data_P01[0] = 0;
  107. Save_parameter_To_Flash( 0x0064, 0x78 );
  108. }
  109. // 将压力数据存储到data_P01的前两个字节
  110. data_P01[1] = (calibrated_pressure >> 8) & 0xFF; // 高字节
  111. data_P01[2] = calibrated_pressure & 0xFF; // 低字节
  112. printf("data_P01: ");
  113. for (int i = 0; i < 8; i++) {
  114. printf("%02X ", data_P01[i]);
  115. }
  116. printf("\n");
  117. // // 输出校准后的压力值
  118. // sprintf(buffer, "Calibrated Pressure (Pa): %d\r\n", calibrated_pressure);
  119. // HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), 1000);
  120. }