From 891f9a0479767288b652a6a579f12cc484d31d21 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Fri, 3 Jul 2026 20:00:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ADC=E5=92=8CTDS=E9=A9=B1?= =?UTF-8?q?=E5=8A=A8=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=B8=BB=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81TDS=E8=AE=A1=E7=AE=97=E5=92=8CUART?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stm32f103c8t6/Core/Drivers/ADC/adc_driver.c | 40 +++++++++++++++++++ stm32f103c8t6/Core/Drivers/ADC/adc_driver.h | 27 +++++++++++++ stm32f103c8t6/Core/Drivers/TDS/tds.c | 44 +++++++++++++++++++++ stm32f103c8t6/Core/Drivers/TDS/tds.h | 25 ++++++++++++ stm32f103c8t6/Core/Src/main.c | 27 ++++++++++++- stm32f103c8t6/README.md | 4 +- 6 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 stm32f103c8t6/Core/Drivers/ADC/adc_driver.c create mode 100644 stm32f103c8t6/Core/Drivers/ADC/adc_driver.h create mode 100644 stm32f103c8t6/Core/Drivers/TDS/tds.c create mode 100644 stm32f103c8t6/Core/Drivers/TDS/tds.h diff --git a/stm32f103c8t6/Core/Drivers/ADC/adc_driver.c b/stm32f103c8t6/Core/Drivers/ADC/adc_driver.c new file mode 100644 index 0000000..c5021bd --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/ADC/adc_driver.c @@ -0,0 +1,40 @@ +/* + * adc_driver.c + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +#include "adc_driver.h" +#include "stm32f1xx_hal.h" +#include +#include + +extern ADC_HandleTypeDef hadc1; + +// DMA缓存 +static uint16_t adc_buf[ADC_CHANNEL_NUM]; + +/** + * @brief 启动ADC + DMA + */ +void ADC_Driver_Init(void) +{ + HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buf, ADC_CHANNEL_NUM); +} + +/** + * @brief 获取最新ADC原始数据 + */ +void ADC_GetRaw(ADC_RawData_t *data) +{ + if (data == NULL) return; + + // PA0 -> PH + // PA1 -> TDS + // PA2 -> TURBIDITY + + data->ph = adc_buf[0]; + data->tds = adc_buf[1]; + data->turbidity = adc_buf[2]; +} diff --git a/stm32f103c8t6/Core/Drivers/ADC/adc_driver.h b/stm32f103c8t6/Core/Drivers/ADC/adc_driver.h new file mode 100644 index 0000000..52799e9 --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/ADC/adc_driver.h @@ -0,0 +1,27 @@ +/* + * adc_driver.h + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +#ifndef DRIVERS_ADC_DRIVER_H_ +#define DRIVERS_ADC_DRIVER_H_ + +#define ADC_CHANNEL_NUM 3 + +#include + +typedef struct +{ + uint16_t ph; + uint16_t tds; + uint16_t turbidity; +} ADC_RawData_t; + +void ADC_Driver_Init(void); + +void ADC_GetRaw(ADC_RawData_t *data); + + +#endif /* DRIVERS_ADC_DRIVER_H_ */ diff --git a/stm32f103c8t6/Core/Drivers/TDS/tds.c b/stm32f103c8t6/Core/Drivers/TDS/tds.c new file mode 100644 index 0000000..7bede87 --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/TDS/tds.c @@ -0,0 +1,44 @@ +/* + * tds.c + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +#include "tds.h" + +void TDS_Init(void) +{ + +} + +/** + * @brief TDS + EC 计算核心 + */ +void TDS_Calc(uint16_t adc, TDS_Data_t *out) +{ + if (out == NULL) return; + + // ADC -> V + float voltage = adc * (3.3f / 4096.0f); + out->voltage = voltage; + + // V -> ec + float ec = + 24.9f * voltage * voltage * voltage + -132.3f * voltage * voltage + +268.5f * voltage + +15.0f; + + if (ec < 0) ec = 0; + out->ec = ec; + + const float k = 0.5f; + + + // ec -> TDS + float tds = ec * k; + + if (tds < 0) tds = 0; + out->tds = tds; +} diff --git a/stm32f103c8t6/Core/Drivers/TDS/tds.h b/stm32f103c8t6/Core/Drivers/TDS/tds.h new file mode 100644 index 0000000..7c6108a --- /dev/null +++ b/stm32f103c8t6/Core/Drivers/TDS/tds.h @@ -0,0 +1,25 @@ +/* + * tds.h + * + * Created on: 2026年7月3日 + * Author: xiaozhou233 + */ + +#ifndef DRIVERS_TDS_TDS_H_ +#define DRIVERS_TDS_TDS_H_ + +#include "main.h" +#include + +typedef struct +{ + float voltage; // 电压 + float ec; // 电导率 (uS/cm) + float tds; // TDS (ppm) +} TDS_Data_t; + +void TDS_Init(void); + +void TDS_Calc(uint16_t adc, TDS_Data_t *out); + +#endif /* DRIVERS_TDS_TDS_H_ */ diff --git a/stm32f103c8t6/Core/Src/main.c b/stm32f103c8t6/Core/Src/main.c index 9526cd6..5b7195a 100644 --- a/stm32f103c8t6/Core/Src/main.c +++ b/stm32f103c8t6/Core/Src/main.c @@ -21,12 +21,16 @@ /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ - +#include +#include +#include "ADC/adc_driver.h" +#include "TDS/tds.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ - +TDS_Data_t tds = {0}; +ADC_RawData_t raw = {0}; /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ @@ -104,15 +108,34 @@ int main(void) MX_USART3_UART_Init(); MX_ADC1_Init(); /* USER CODE BEGIN 2 */ + ADC_Driver_Init(); + char uart_buf[80]; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { + ADC_GetRaw(&raw); + + TDS_Calc(raw.tds, &tds); + + sprintf(uart_buf, + "PH=%d;TDS=%d;TUR=%d\r\n", + raw.ph, + (int)tds.tds, + raw.turbidity); + + HAL_UART_Transmit(&huart1, + (uint8_t*)uart_buf, + strlen(uart_buf), + 100); + + HAL_Delay(500); /* USER CODE END WHILE */ + /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ diff --git a/stm32f103c8t6/README.md b/stm32f103c8t6/README.md index d6a11f4..6b476b4 100644 --- a/stm32f103c8t6/README.md +++ b/stm32f103c8t6/README.md @@ -15,8 +15,8 @@ ## ADC 接线 PA0 - PH -PA1 = TDS -PA4 = Turbidity +PA1 - TDS +PA2 - Turbidity ## UART 接线 PA9 - USART1_TX