增加流量计MF4008驱动
authorkyz <kongyuzhong@google.com>
Mon, 15 Apr 2024 08:34:49 +0000 (16:34 +0800)
committerkyz <kongyuzhong@google.com>
Mon, 15 Apr 2024 08:34:49 +0000 (16:34 +0800)
user/user.h
user/user_adc.c
user/user_flowmeter.c
user/user_sdmmc.c

index d7516e089ecbd49aa9013e1d0d71e7071fec817f..4d6ab68e413e254dab61815b64f1e38798cae254 100644 (file)
 //#include "fmc.h"
 //--------------------
 
+
+//使用MF4008 流量计
+#define  FLOWMETER_MF4008  0  //MF4008-50-R-BV-A, B1T09240  
+
+
 #define APP_VER_MAJOR 2
 #define APP_VER_MINOR 5
 
index 050ecd4fea8720f1d442d4ffa90e7b4a689be024..41007bfb4f028fa0e06591a846cd0d75727bf52d 100644 (file)
@@ -153,8 +153,22 @@ float vol_to_kpa( float vol )
 
 float vol_to_speed( float vol )
 {
+#if  FLOWMETER_MF4008
+    //MF4008系列输出电压与瞬时流量的关系为:Flow = (Vout-0.5V)/4000 * 100%FS
+    //0.5V -> 0,  4.5V -> 1FS
+       
+    float speed = 0;
+    if( vol <= 0.5f )
+        speed = 0;
+    else if( vol >= 4.5f )
+        speed = 50;
+    else
+        speed = (vol -0.5f)/0.08f;
+    return speed;
+#else
     //AFM07系列输出电压与瞬时流量的关系为:电压(V) = 1+瞬时流量(L/min)×电压系数。
     //AFM07系列有25L/min和50L/min两个量程,如表3所示,两个量程对应的电压系数分别为0.16和0.08。
+
     float speed = 0;
     if( vol <= 1.0f )
         speed = 0;
@@ -163,6 +177,7 @@ float vol_to_speed( float vol )
     else
         speed = (vol -1.0f)/0.08f; //最小 1V -- 0L/min, 最大 5v - - 50L/min
     return speed;
+#endif
 }
 
 void user_adc_task( void* unused )
index aa018c08d2765eed4ae4cbfbea9c8049e15b9262..6b6f1cc510f78f3417f9d1b22804c19059ff0199 100644 (file)
@@ -29,6 +29,55 @@ AFM07系列作为从机,默认的地址为0x01,默认波特率9600,支持
 0x003B 0~2000 读写 03/06 10 报警下限值 (L/min) 如:设置 5L/min —>0x0032
 */
 
+#if FLOWMETER_MF4008
+/*MF4008系列作为从机,默认的地址为0x01,默认波特率38400,支持修改地址和波特率
+
+寄存器 
+地址  取值范围 读写属性 功能码 倍数 寄存器信息 备注
+0x0002 0~65535 只读 03 10 瞬时流量 高16位
+0x0003 0~65535 只读 03 10 瞬时流量 低16位  组合32位数据 /1000.0f
+0x0004 0~65535 只读 03 10 累计流量(高16位)
+0x0005 0~65535 只读 03 10 累计流量 (低16位)组合32位整数位, 后面再上小数位
+0x0006 0~65535 只读 03 10 累计流量 (16位)  小数位/1000.0
+
+*/
+
+void mf4008_uart_init( void )
+{
+  huart1.Instance = USART1;
+  huart1.Init.BaudRate = 38400;
+  huart1.Init.WordLength = UART_WORDLENGTH_8B;
+  huart1.Init.StopBits = UART_STOPBITS_1;
+  huart1.Init.Parity = UART_PARITY_NONE;
+  huart1.Init.Mode = UART_MODE_TX_RX;
+  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
+  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
+  huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
+  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
+  if (HAL_MultiProcessor_Init(&huart1, 0, UART_WAKEUPMETHOD_IDLELINE) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN USART1_Init 2 */
+
+  /* USER CODE END USART1_Init 2 */
+
+}
+#endif
+
 uint16_t usMBCRC16( uint8_t *data, uint16_t sz )
 {
     unsigned int crc= 0xFFFF;//初值初始化
@@ -126,7 +175,37 @@ void user_flowmeter_task( void* unused )
     uint16_t reg_values[16];
     uint32_t last_tick = xTaskGetTickCount();
     int32_t err = 0;
-
+#if FLOWMETER_MF4008
+               mf4008_uart_init();
+    while(1){
+        //100毫秒扫描一次
+        //vTaskDelayUntil(&last_tick, 100 );
+        osDelay( 100 );
+        if( mb_master_read_regs( 1, 2, reg_values, 5 ) == 0 ){
+            //读取到 流量
+            err = 0;
+                                         g_dev.flowmeter.flow_speed_raw = ((reg_values[0]<<16)|(reg_values[1]))/1000.0f;
+            if( g_dev.flowmeter.flow_speed_raw + g_dev.cfg.cal.flowrate > 0 )
+                g_dev.flowmeter.flow_speed = g_dev.flowmeter.flow_speed_raw + g_dev.cfg.cal.flowrate;
+            else
+                g_dev.flowmeter.flow_speed = 0;
+            g_dev.flowmeter.flow_total = ((reg_values[2]<<16)|reg_values[3]) +reg_values[4]/1000.0f;
+            g_dev.flowmeter.tem    = 0;
+            g_dev.flowmeter.state  = 0;
+            if( g_dev.state & FLOWMETER_ERR ){
+                g_dev.state &= ~(FLOWMETER_ERR);
+            }
+            err = 0;
+        }else{
+            //读取失败, 故障计数到10次报错
+            if( err> 10 ){
+                g_dev.state |= (FLOWMETER_ERR);
+            }else{
+                err++;
+            }
+        }
+         }
+#else
     while(1){
         //100毫秒扫描一次
         //vTaskDelayUntil(&last_tick, 100 );
@@ -156,4 +235,5 @@ void user_flowmeter_task( void* unused )
             }
         }
     }
+#endif
 }
index d9c9f9b2e0b16facce7e16882cd6253d4ea494f5..72f63fd7eb31c9a332220348cbf3e976c7b30c0b 100644 (file)
@@ -138,5 +138,4 @@ void user_sdmmc_test( void )
     while(1){
         osDelay(1000);
     }
-        return;
 }