From 83a9a79dcdf74a18bc486b8ab54f9569e38dc9ff Mon Sep 17 00:00:00 2001 From: 95384 <664090429@qq.com> Date: Fri, 21 Feb 2025 17:01:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHP203B=E6=B1=82=E5=B9=B3?= =?UTF-8?q?=E5=9D=87=E5=80=BC=E5=AF=BC=E8=87=B4=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Drivers/HP203B/hp203b.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/Drivers/HP203B/hp203b.c b/Drivers/HP203B/hp203b.c index d1ccb2f..aa8649b 100644 --- a/Drivers/HP203B/hp203b.c +++ b/Drivers/HP203B/hp203b.c @@ -84,6 +84,18 @@ BOOL Hp203bReadTempture(float *press) return TRUE; } +static float calculateAverage(float arr[], int avgLength) { + float sum = 0; + + // 遍历数组(最多10个元素),收集非零值直到达到指定数量 + for (int i = 0; i < 10; ++i) { + sum += arr[i]; + } + + // 计算平均值并限制最大值 + float average = sum / avgLength; + return average; +} /**************************** *名称:get_press_data *功能:获取气压与备用温度数据 @@ -99,12 +111,12 @@ BOOL get_HP203_data(float* tempdata, float* press) uint8_t temp_falt = 0; uint8_t press_falt = 0; // 压强 - U_DataType collect_pressure[COLLECT_HB203_DATA_NUM]={0x00}; + float collect_pressure[COLLECT_HB203_DATA_NUM]={0x00}; for(int i=0; i 1200) + if(tmp_press > 1200) { // return FALSE; goto error_return; } // 温度 - U_DataType collect_tempture[COLLECT_HB203_DATA_NUM]={0x00}; + float collect_tempture[COLLECT_HB203_DATA_NUM]={0x00}; for(int i=0; i 95) + if(tmp_tempture > 95) { // return FALSE; goto error_return; } - *tempdata = tmp_tempture.fValue; - *press = tmp_press.fValue; + *tempdata = tmp_tempture; + *press = tmp_press; return TRUE; error_return: