添加ADC和TDS驱动,更新主程序以支持TDS计算和UART输出

This commit is contained in:
xiaozhou233 committed 2026-07-03 20:00:49 +08:00
1 parent 5564921696
commit 891f9a0479
6 files changed
+163 -4

No files matched your search

@@ -0,0 +1,40 @@
/*
* adc_driver.c
*
* Created on: 2026年7月3日
* Author: xiaozhou233
*/
#include "adc_driver.h"
#include "stm32f1xx_hal.h"
#include <stddef.h>
#include <stdint.h>
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];
}
@@ -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 <stdint.h>
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_ */
+44
View File
@@ -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;
}
+25
View File
@@ -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 <stdint.h>
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_ */
+25 -2
View File
@@ -21,12 +21,16 @@
/* Private includes ----------------------------------------------------------*/ /* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN Includes */
#include <string.h>
#include <stdio.h>
#include "ADC/adc_driver.h"
#include "TDS/tds.h"
/* USER CODE END Includes */ /* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */ /* USER CODE BEGIN PTD */
TDS_Data_t tds = {0};
ADC_RawData_t raw = {0};
/* USER CODE END PTD */ /* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/
@@ -104,15 +108,34 @@ int main(void)
MX_USART3_UART_Init(); MX_USART3_UART_Init();
MX_ADC1_Init(); MX_ADC1_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
ADC_Driver_Init();
char uart_buf[80];
/* USER CODE END 2 */ /* USER CODE END 2 */
/* Infinite loop */ /* Infinite loop */
/* USER CODE BEGIN WHILE */ /* USER CODE BEGIN WHILE */
while (1) 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 END WHILE */
/* USER CODE BEGIN 3 */ /* USER CODE BEGIN 3 */
} }
/* USER CODE END 3 */ /* USER CODE END 3 */
+2 -2
View File
@@ -15,8 +15,8 @@
## ADC 接线 ## ADC 接线
PA0 - PH PA0 - PH
PA1 = TDS PA1 - TDS
PA4 = Turbidity PA2 - Turbidity
## UART 接线 ## UART 接线
PA9 - USART1_TX PA9 - USART1_TX