From 20c44ac853f25606b73b59b9c7f4f3396d08a231 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Fri, 3 Jul 2026 20:55:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0PH=E5=92=8C=E6=B5=8A=E5=BA=A6?= =?UTF-8?q?=E4=BC=A0=E6=84=9F=E5=99=A8=E9=A9=B1=E5=8A=A8=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=B8=BB=E7=A8=8B=E5=BA=8F=E4=BB=A5=E6=94=AF=E6=8C=81?= =?UTF-8?q?PH=E5=92=8C=E6=B5=8A=E5=BA=A6=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stm32f103c8t6/.cproject | 12 +++++- stm32f103c8t6/Core/Drivers/PH/ph.c | 41 ++++++++++++++++++ stm32f103c8t6/Core/Drivers/PH/ph.h | 31 +++++++++++++ .../Core/Drivers/Turbidity/turbidity.c | 43 +++++++++++++++++++ .../Core/Drivers/Turbidity/turbidity.h | 25 +++++++++++ stm32f103c8t6/Core/Src/main.c | 19 +++++--- 6 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 stm32f103c8t6/Core/Drivers/PH/ph.c create mode 100644 stm32f103c8t6/Core/Drivers/PH/ph.h create mode 100644 stm32f103c8t6/Core/Drivers/Turbidity/turbidity.c create mode 100644 stm32f103c8t6/Core/Drivers/Turbidity/turbidity.h diff --git a/stm32f103c8t6/.cproject b/stm32f103c8t6/.cproject index 60abbf4..0ae05ca 100644 --- a/stm32f103c8t6/.cproject +++ b/stm32f103c8t6/.cproject @@ -59,6 +59,9 @@ @@ -173,5 +176,12 @@ - + + + + + + + + \ No newline at end of file diff --git a/stm32f103c8t6/Core/Drivers/PH/ph.c b/stm32f103c8t6/Core/Drivers/PH/ph.c new file mode 100644 index 0000000..2ca5d0e --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/PH/ph.c @@ -0,0 +1,41 @@ +/* + * ph.c + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ +#include "ph.h" +#include + +static const float PH7_MV = 1810.0f; + +static const float PH_SLOPE = 177.43f; + +static const float ADC_VREF = 3.3f; +static const float ADC_MAX = 4096.0f; + +void PH_Init(void) +{ +} + +static float ph_offset = -1.2f; + +void PH_Calc(uint16_t adc, PH_Data_t *out) +{ + if(out == NULL) return; + + float voltage = adc * (ADC_VREF / ADC_MAX); + out->voltage = voltage; + + float mv = voltage * 1000.0f; + out->mv = mv; + + float ph = 7.0f + (PH7_MV - mv) / PH_SLOPE; + + ph += ph_offset; + + if(ph < 0.0f) ph = 0.0f; + if(ph > 14.0f) ph = 14.0f; + + out->ph = ph; +} diff --git a/stm32f103c8t6/Core/Drivers/PH/ph.h b/stm32f103c8t6/Core/Drivers/PH/ph.h new file mode 100644 index 0000000..981e5cb --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/PH/ph.h @@ -0,0 +1,31 @@ +/* + * ph.h + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +#ifndef PH_H +#define PH_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct +{ + float voltage; + float mv; + float ph; +} PH_Data_t; + +void PH_Init(void); +void PH_Calc(uint16_t adc, PH_Data_t *out); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/stm32f103c8t6/Core/Drivers/Turbidity/turbidity.c b/stm32f103c8t6/Core/Drivers/Turbidity/turbidity.c new file mode 100644 index 0000000..00665dc --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/Turbidity/turbidity.c @@ -0,0 +1,43 @@ +/* + * turbidity.c + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +/* + * turbidity.c + */ + +#include "turbidity.h" +#include +#include + +static const float K_VALUE = 3347.19f; + +static const float TURBIDITY_OFFSET = 1140.0f; + +void TURBIDITY_Init(void) +{ + +} + +void TURBIDITY_Calc(uint16_t adc, TURBIDITY_Data_t *out) +{ + if(out == NULL) return; + + float voltage = adc * (3.3f / 4096.0f); + out->voltage = voltage; + + float raw = -865.68f * voltage + K_VALUE; + + if(raw < 0) raw = 0; + if(raw > 3000) raw = 3000; + + float ntu = raw - TURBIDITY_OFFSET; + + if(ntu < 0) ntu = 0; + if(ntu > 3000) ntu = 3000; + + out->ntu = ntu; +} diff --git a/stm32f103c8t6/Core/Drivers/Turbidity/turbidity.h b/stm32f103c8t6/Core/Drivers/Turbidity/turbidity.h new file mode 100644 index 0000000..bb91c11 --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/Turbidity/turbidity.h @@ -0,0 +1,25 @@ +/* + * turbidity.h + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +#ifndef DRIVERS_TURBIDITY_TURBIDITY_H_ +#define DRIVERS_TURBIDITY_TURBIDITY_H_ + +#include + +typedef struct +{ + float voltage; // 电压 + float ntu; // 浊度 +} TURBIDITY_Data_t; + + +void TURBIDITY_Init(void); + +void TURBIDITY_Calc(uint16_t adc, TURBIDITY_Data_t *out); + + +#endif /* DRIVERS_TURBIDITY_TURBIDITY_H_ */ diff --git a/stm32f103c8t6/Core/Src/main.c b/stm32f103c8t6/Core/Src/main.c index 5b7195a..e546855 100644 --- a/stm32f103c8t6/Core/Src/main.c +++ b/stm32f103c8t6/Core/Src/main.c @@ -25,12 +25,16 @@ #include #include "ADC/adc_driver.h" #include "TDS/tds.h" +#include "Turbidity/turbidity.h" +#include "PH/ph.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ -TDS_Data_t tds = {0}; ADC_RawData_t raw = {0}; +TDS_Data_t tds = {0}; +TURBIDITY_Data_t tur = {0}; +PH_Data_t ph = {0}; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ @@ -109,6 +113,7 @@ int main(void) MX_ADC1_Init(); /* USER CODE BEGIN 2 */ ADC_Driver_Init(); + PH_Init(); char uart_buf[80]; /* USER CODE END 2 */ @@ -120,12 +125,14 @@ int main(void) ADC_GetRaw(&raw); TDS_Calc(raw.tds, &tds); + TURBIDITY_Calc(raw.turbidity, &tur); + PH_Calc(raw.ph, &ph); - sprintf(uart_buf, - "PH=%d;TDS=%d;TUR=%d\r\n", - raw.ph, - (int)tds.tds, - raw.turbidity); + sprintf(uart_buf, + "PH=%.2f;TDS=%d;TUR=%d\r\n", + ph.ph, // ✔ 改这里 + (int)tds.tds, + (int)tur.ntu); HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf,