mirror of
https://github.com/xiaozhou233/RiverWaterQualityMonitoring.git
synced 2026-12-05 13:05:53 +08:00
添加PH和浊度传感器驱动,更新主程序以支持PH和浊度计算
This commit is contained in:
1 parent
891f9a0479
commit
20c44ac853
6 files changed
+164
-7
No files matched your search
+11
-1
@@ -59,6 +59,9 @@
|
|||||||
</tool>
|
</tool>
|
||||||
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.513854929" name="MCU/MPU GCC Linker" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker">
|
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.513854929" name="MCU/MPU GCC Linker" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker">
|
||||||
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script.472359014" name="Linker Script (-T)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script" value="${workspace_loc:/${ProjName}/STM32F103C8TX_FLASH.ld}" valueType="string"/>
|
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script.472359014" name="Linker Script (-T)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.script" value="${workspace_loc:/${ProjName}/STM32F103C8TX_FLASH.ld}" valueType="string"/>
|
||||||
|
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.otherflags.1956224985" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.option.otherflags" valueType="stringList">
|
||||||
|
<listOptionValue builtIn="false" value="-u _printf_float"/>
|
||||||
|
</option>
|
||||||
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.input.1632244449" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.input">
|
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.input.1632244449" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.linker.input">
|
||||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||||
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
||||||
@@ -173,5 +176,12 @@
|
|||||||
</scannerConfigBuildInfo>
|
</scannerConfigBuildInfo>
|
||||||
</storageModule>
|
</storageModule>
|
||||||
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
|
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
|
||||||
<storageModule moduleId="refreshScope"/>
|
<storageModule moduleId="refreshScope" versionNumber="2">
|
||||||
|
<configuration configurationName="Debug">
|
||||||
|
<resource resourceType="PROJECT" workspacePath="/stm32f103c8t6"/>
|
||||||
|
</configuration>
|
||||||
|
<configuration configurationName="Release">
|
||||||
|
<resource resourceType="PROJECT" workspacePath="/stm32f103c8t6"/>
|
||||||
|
</configuration>
|
||||||
|
</storageModule>
|
||||||
</cproject>
|
</cproject>
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* ph.c
|
||||||
|
*
|
||||||
|
* Created on: 2026年7月3日
|
||||||
|
* Author: xiaozhou233
|
||||||
|
*/
|
||||||
|
#include "ph.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* ph.h
|
||||||
|
*
|
||||||
|
* Created on: 2026年7月3日
|
||||||
|
* Author: xiaozhou233
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PH_H
|
||||||
|
#define PH_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#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
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* turbidity.c
|
||||||
|
*
|
||||||
|
* Created on: 2026年7月3日
|
||||||
|
* Author: xiaozhou233
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* turbidity.c
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "turbidity.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* turbidity.h
|
||||||
|
*
|
||||||
|
* Created on: 2026年7月3日
|
||||||
|
* Author: xiaozhou233
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef DRIVERS_TURBIDITY_TURBIDITY_H_
|
||||||
|
#define DRIVERS_TURBIDITY_TURBIDITY_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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_ */
|
||||||
@@ -25,12 +25,16 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "ADC/adc_driver.h"
|
#include "ADC/adc_driver.h"
|
||||||
#include "TDS/tds.h"
|
#include "TDS/tds.h"
|
||||||
|
#include "Turbidity/turbidity.h"
|
||||||
|
#include "PH/ph.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};
|
ADC_RawData_t raw = {0};
|
||||||
|
TDS_Data_t tds = {0};
|
||||||
|
TURBIDITY_Data_t tur = {0};
|
||||||
|
PH_Data_t ph = {0};
|
||||||
/* USER CODE END PTD */
|
/* USER CODE END PTD */
|
||||||
|
|
||||||
/* Private define ------------------------------------------------------------*/
|
/* Private define ------------------------------------------------------------*/
|
||||||
@@ -109,6 +113,7 @@ int main(void)
|
|||||||
MX_ADC1_Init();
|
MX_ADC1_Init();
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
ADC_Driver_Init();
|
ADC_Driver_Init();
|
||||||
|
PH_Init();
|
||||||
|
|
||||||
char uart_buf[80];
|
char uart_buf[80];
|
||||||
/* USER CODE END 2 */
|
/* USER CODE END 2 */
|
||||||
@@ -120,12 +125,14 @@ int main(void)
|
|||||||
ADC_GetRaw(&raw);
|
ADC_GetRaw(&raw);
|
||||||
|
|
||||||
TDS_Calc(raw.tds, &tds);
|
TDS_Calc(raw.tds, &tds);
|
||||||
|
TURBIDITY_Calc(raw.turbidity, &tur);
|
||||||
|
PH_Calc(raw.ph, &ph);
|
||||||
|
|
||||||
sprintf(uart_buf,
|
sprintf(uart_buf,
|
||||||
"PH=%d;TDS=%d;TUR=%d\r\n",
|
"PH=%.2f;TDS=%d;TUR=%d\r\n",
|
||||||
raw.ph,
|
ph.ph, // ✔ 改这里
|
||||||
(int)tds.tds,
|
(int)tds.tds,
|
||||||
raw.turbidity);
|
(int)tur.ntu);
|
||||||
|
|
||||||
HAL_UART_Transmit(&huart1,
|
HAL_UART_Transmit(&huart1,
|
||||||
(uint8_t*)uart_buf,
|
(uint8_t*)uart_buf,
|
||||||
|
|||||||
Reference in new issue
Block a user