From: kyz Date: Mon, 15 Apr 2024 08:34:49 +0000 (+0800) Subject: 增加流量计MF4008驱动 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=6ca491ca0cf29b823de7164c03093cb9f93fcf4d;p=yn%2Fweldingcontroller.git 增加流量计MF4008驱动 --- diff --git a/user/user.h b/user/user.h index d7516e0..4d6ab68 100644 --- a/user/user.h +++ b/user/user.h @@ -25,6 +25,11 @@ //#include "fmc.h" //-------------------- + +//使用MF4008 流量计 +#define FLOWMETER_MF4008 0 //MF4008-50-R-BV-A, B1T09240 + + #define APP_VER_MAJOR 2 #define APP_VER_MINOR 5 diff --git a/user/user_adc.c b/user/user_adc.c index 050ecd4..41007bf 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -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 ) diff --git a/user/user_flowmeter.c b/user/user_flowmeter.c index aa018c0..6b6f1cc 100644 --- a/user/user_flowmeter.c +++ b/user/user_flowmeter.c @@ -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 } diff --git a/user/user_sdmmc.c b/user/user_sdmmc.c index d9c9f9b..72f63fd 100644 --- a/user/user_sdmmc.c +++ b/user/user_sdmmc.c @@ -138,5 +138,4 @@ void user_sdmmc_test( void ) while(1){ osDelay(1000); } - return; }