校准PH传感器

This commit is contained in:
xiaozhou233 committed 2026-07-16 17:55:43 +08:00
1 parent eef608b0d0
commit 86396545bd
1 file changed
+12 -9
+12 -9
View File
@@ -7,9 +7,8 @@
#include "ph.h" #include "ph.h"
#include <stddef.h> #include <stddef.h>
static const float PH7_MV = 1810.0f; static const float PH_K = -0.005710f;
static const float PH_B = 14.0446f;
static const float PH_SLOPE = 177.43f;
static const float ADC_VREF = 3.3f; static const float ADC_VREF = 3.3f;
static const float ADC_MAX = 4096.0f; static const float ADC_MAX = 4096.0f;
@@ -18,24 +17,28 @@ void PH_Init(void)
{ {
} }
static float ph_offset = -1.2f;
void PH_Calc(uint16_t adc, PH_Data_t *out) void PH_Calc(uint16_t adc, PH_Data_t *out)
{ {
if(out == NULL) return; if(out == NULL) return;
float voltage = adc * (ADC_VREF / ADC_MAX); float voltage = adc * (ADC_VREF / ADC_MAX);
out->voltage = voltage; out->voltage = voltage;
float mv = voltage * 1000.0f; float mv = voltage * 1000.0f;
out->mv = mv; out->mv = mv;
float ph = 7.0f + (PH7_MV - mv) / PH_SLOPE;
ph += ph_offset; float ph = PH_K * mv + PH_B;
if(ph < 0.0f)
ph = 0.0f;
if(ph > 14.0f)
ph = 14.0f;
if(ph < 0.0f) ph = 0.0f;
if(ph > 14.0f) ph = 14.0f;
out->ph = ph; out->ph = ph;
} }