gas_sensor.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "gas_sensor.h"
  2. #include "math_tools.h"
  3. uint16_t GasSensor_OffSet = 0;
  4. #define ZeroOffSetData 100
  5. //指拨零点值初始化
  6. void GasSensorOffSet_Init(uint16_t* OffSet, uint16_t AdcData)
  7. {
  8. *OffSet = AdcData + ZeroOffSetData;
  9. }
  10. //指拨零点值更新
  11. void GasSensorOffSetData_Update(uint16_t* PresentData, uint16_t AdcData, MC_ErrorCode_Struct_t* p_MC_ErrorCode)
  12. {
  13. static uint16_t Array[50];
  14. static TrueOrFalse_Flag_Struct_t IsArrayFullFalg = FALSE;
  15. uint8_t i = 0;
  16. uint16_t TempData;
  17. static uint32_t ErrorDelayTimeCnt = 0;
  18. //刚开机时,数组未存满
  19. if(IsArrayFullFalg == FALSE)
  20. {
  21. Array[i++] = AdcData;
  22. if(i >= 50)
  23. {
  24. IsArrayFullFalg = TRUE;
  25. i = 0;
  26. }
  27. }
  28. //存满后取滑动均值
  29. else
  30. {
  31. TempData = MovingAverageFilter(AdcData, Array, sizeof(Array) / 2) + ZeroOffSetData;
  32. if(TempData < *PresentData)
  33. {
  34. //更新零点值
  35. *PresentData = TempData;
  36. }
  37. }
  38. //判断零点是否超出范围
  39. if(*PresentData <= 1500)
  40. {
  41. ErrorDelayTimeCnt = HAL_GetTick();
  42. p_MC_ErrorCode->ERROR_Bit.Fault_GasSensor = 0;
  43. }
  44. if((HAL_GetTick() - ErrorDelayTimeCnt) > 10000)
  45. {
  46. p_MC_ErrorCode->ERROR_Bit.Fault_GasSensor = 1;
  47. }
  48. }