From e1a0264dba60d905aec3cd51e4efcd8f4c6977bf Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 19 Aug 2025 22:01:41 +0800 Subject: [PATCH] =?utf8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E8=B7=AF=E6=AF=94?= =?utf8?q?=E4=BE=8B=E9=98=80=E9=A9=B1=E5=8A=A8=E7=94=B5=E6=B5=81ADC?= =?utf8?q?=E9=87=87=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Core/Src/adc.c | 94 ++++++++++++++++++++++++++---------------------- tools_v2/test.py | 1 + tools_v2/test.ui | 2 +- user/user.h | 1 + user/user_adc.c | 58 +++++++++++++++++------------- user/user_app.c | 2 ++ 6 files changed, 90 insertions(+), 68 deletions(-) diff --git a/Core/Src/adc.c b/Core/Src/adc.c index 3d7d146..8790e0e 100644 --- a/Core/Src/adc.c +++ b/Core/Src/adc.c @@ -39,7 +39,6 @@ void MX_ADC1_Init(void) /* USER CODE END ADC1_Init 0 */ - ADC_MultiModeTypeDef multimode = {0}; ADC_ChannelConfTypeDef sConfig = {0}; /* USER CODE BEGIN ADC1_Init 1 */ @@ -51,13 +50,13 @@ void MX_ADC1_Init(void) hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4; hadc1.Init.Resolution = ADC_RESOLUTION_16B; - hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc1.Init.LowPowerAutoWait = DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; - hadc1.Init.NbrOfConversion = 2; + hadc1.Init.NbrOfConversion = 1; hadc1.Init.DiscontinuousConvMode = DISABLE; - hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO; + hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT; hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED; @@ -72,19 +71,11 @@ void MX_ADC1_Init(void) Error_Handler(); } - /** Configure the ADC multi-mode - */ - multimode.Mode = ADC_MODE_INDEPENDENT; - if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) - { - Error_Handler(); - } - /** Configure Regular Channel */ - sConfig.Channel = ADC_CHANNEL_5; + sConfig.Channel = ADC_CHANNEL_18; sConfig.Rank = ADC_REGULAR_RANK_1; - sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; + sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; @@ -94,14 +85,6 @@ void MX_ADC1_Init(void) Error_Handler(); } - /** Configure Regular Channel - */ - sConfig.Channel = ADC_CHANNEL_9; - sConfig.Rank = ADC_REGULAR_RANK_2; - if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) - { - Error_Handler(); - } /* USER CODE BEGIN ADC1_Init 2 */ /* USER CODE END ADC1_Init 2 */ @@ -115,6 +98,7 @@ void MX_ADC2_Init(void) /* USER CODE END ADC2_Init 0 */ + ADC_MultiModeTypeDef multimode = {0}; ADC_ChannelConfTypeDef sConfig = {0}; /* USER CODE BEGIN ADC2_Init 1 */ @@ -126,13 +110,13 @@ void MX_ADC2_Init(void) hadc2.Instance = ADC2; hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4; hadc2.Init.Resolution = ADC_RESOLUTION_16B; - hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc2.Init.LowPowerAutoWait = DISABLE; hadc2.Init.ContinuousConvMode = DISABLE; - hadc2.Init.NbrOfConversion = 1; + hadc2.Init.NbrOfConversion = 3; hadc2.Init.DiscontinuousConvMode = DISABLE; - hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T2_TRGO; + hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO; hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_ONESHOT; hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED; @@ -147,9 +131,18 @@ void MX_ADC2_Init(void) Error_Handler(); } + + /** Configure the ADC multi-mode + */ + multimode.Mode = ADC_MODE_INDEPENDENT; + if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK) + { + Error_Handler(); + } + /** Configure Regular Channel */ - sConfig.Channel = ADC_CHANNEL_18; + sConfig.Channel = ADC_CHANNEL_5; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; @@ -160,6 +153,24 @@ void MX_ADC2_Init(void) { Error_Handler(); } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_9; + sConfig.Rank = ADC_REGULAR_RANK_2; + if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) + { + Error_Handler(); + } + + /** Configure Regular Channel + */ + sConfig.Channel = ADC_CHANNEL_2; + sConfig.Rank = ADC_REGULAR_RANK_3; + if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK) + { + Error_Handler(); + } /* USER CODE BEGIN ADC2_Init 2 */ /* USER CODE END ADC2_Init 2 */ @@ -256,15 +267,14 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) __HAL_RCC_ADC12_CLK_ENABLE(); } - __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); /**ADC1 GPIO Configuration - PB0 ------> ADC1_INP9 - PB1 ------> ADC1_INP5 + PA4 ------> ADC1_INP18 */ - GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; + GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* ADC1 DMA Init */ /* ADC1 Init */ @@ -299,17 +309,17 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) if(HAL_RCC_ADC12_CLK_ENABLED==1){ __HAL_RCC_ADC12_CLK_ENABLE(); } - - __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); /**ADC2 GPIO Configuration - PA4 ------> ADC2_INP18 + PB0 ------> ADC2_INP9 + PB1 ------> ADC2_INP5 PF13 ------> ADC2_INP2 */ - GPIO_InitStruct.Pin = GPIO_PIN_4; + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; @@ -403,10 +413,9 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) } /**ADC1 GPIO Configuration - PB0 ------> ADC1_INP9 - PB1 ------> ADC1_INP5 + PA4 ------> ADC2_INP18 */ - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4); /* ADC1 DMA DeInit */ HAL_DMA_DeInit(adcHandle->DMA_Handle); @@ -426,11 +435,12 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) } /**ADC2 GPIO Configuration - PA4 ------> ADC2_INP18 + PB0 ------> ADC1_INP9 + PB1 ------> ADC1_INP5 PF13 ------> ADC2_INP2 - */ - HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4); + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1); HAL_GPIO_DeInit(GPIOF, GPIO_PIN_13); /* ADC2 DMA DeInit */ diff --git a/tools_v2/test.py b/tools_v2/test.py index 1f1e325..46669ba 100644 --- a/tools_v2/test.py +++ b/tools_v2/test.py @@ -682,6 +682,7 @@ class App( QDialog, ui_welding.Ui_Dialog ): self.tableWidget_devinfo.item(0,6).setText( "%d"%(method)) self.tableWidget_devinfo.item(0,7).setText( "%d"%(factor)) #self.tableWidget_devinfo.item(0,8).setText( "%0.1f"%(tem_x10/10.0)) + self.tableWidget_devinfo.item(0,9).setText( "%d"%(res)) if idx == 0: self.flow_x=[] self.flow_y=[] diff --git a/tools_v2/test.ui b/tools_v2/test.ui index 93e3702..61435f5 100644 --- a/tools_v2/test.ui +++ b/tools_v2/test.ui @@ -304,7 +304,7 @@ - 电流校准 + 读取控制电流 diff --git a/user/user.h b/user/user.h index 8eac30d..9af39f8 100644 --- a/user/user.h +++ b/user/user.h @@ -237,6 +237,7 @@ typedef struct { //输出控制 float target_flowspeed; //目标流量 float valve_curr_out; //当前输出电流,对应dac输出 1.16~1.73V --- 60~165mA --- 0L/min <60mA + float valve_curr_read; //当前通过ADC读取到的比例实际阀控制电流 //状态 int32_t state; //设备状态 <非0:故障码, 0:正常 diff --git a/user/user_adc.c b/user/user_adc.c index e7835fe..b861626 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -7,26 +7,26 @@ void user_adc_task( void* unused ); //ADC2 INP18 电流, 100微秒采集一次,500毫秒触发DMA中断,计算电流。 -#define ADC1_RAW_NRS (10*2) -#define ADC2_RAW_NRS 5000 +#define ADC1_RAW_NRS 5000 +#define ADC2_RAW_NRS (10*3) #define ADC3_RAW_NRS (100*2) //DMA采集buffer 地址32字节对齐,大小为32字节倍数,以适配cache刷新操作 -ALIGN_32BYTES (static uint16_t adc1_raw[(ADC1_RAW_NRS+15)&~15] ); //2路采集10次, 0.1秒 -ALIGN_32BYTES (static uint16_t adc2_raw[(ADC2_RAW_NRS+15)&~15] ); //采集5000次,电流采样率10Khz 0.5秒 +ALIGN_32BYTES (static uint16_t adc2_raw[(ADC2_RAW_NRS+15)&~15] ); //3路采集10次, 0.1秒 +ALIGN_32BYTES (static uint16_t adc1_raw[(ADC1_RAW_NRS+15)&~15] ); //采集5000次,电流采样率10Khz 0.5秒 ALIGN_32BYTES (static uint16_t adc3_raw[(ADC3_RAW_NRS+15)&~15] ); //采集100次 1秒 #define SECTION_DMA_RAM #define ARRAY_SIZE( arr ) ( sizeof( arr )/sizeof( arr[0] ) ) -static uint16_t adc1_raw_copy[ADC1_RAW_NRS]; +static uint16_t adc2_raw_copy[ADC2_RAW_NRS]; //static uint16_t adc3_raw_copy[ADC3_RAW_NRS]; //__attribute__((at(0x20005000))) static uint16_t adc2_raw_copy[ADC2_RAW_NRS]; //__attribute__((at(0x20000000))) static float adc2_curr[ADC2_RAW_NRS]; -__attribute__((section(".DTCM"),zero_init)) static uint16_t adc2_raw_copy[ADC2_RAW_NRS]; -__attribute__((section(".DTCM"),zero_init)) static float adc2_curr[ADC2_RAW_NRS]; +__attribute__((section(".DTCM"),zero_init)) static uint16_t adc1_raw_copy[ADC1_RAW_NRS]; +__attribute__((section(".DTCM"),zero_init)) static float adc1_curr[ADC1_RAW_NRS]; __attribute__((section(".DTCM"),zero_init)) static uint16_t adc3_raw_copy[ADC3_RAW_NRS]; -__attribute__((section(".DTCM"),zero_init)) static float adc3_curr[ADC3_RAW_NRS]; +//__attribute__((section(".DTCM"),zero_init)) static float adc3_curr[ADC3_RAW_NRS]; extern dev_info_t g_dev; void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) @@ -301,6 +301,7 @@ void user_adc_task( void* unused ) uint32_t flowrate_sum = 0; uint32_t temp_sum = 0; uint32_t vol_sum = 0; + uint32_t valve_curr_sum = 0; EventBits_t uxBits; @@ -329,33 +330,37 @@ void user_adc_task( void* unused ) pdFALSE, /* Don't wait for all bits, either bit will do. */ xTicksToWait );/* Wait a maximum of xxx ms for either bit to be set. */ - if( uxBits & EVENT_ADC1_UPDATE ){ - // ADC1 压力计 和 流量计 + if( uxBits & EVENT_ADC2_UPDATE ){ + // ADC2 压力计 和 流量计 和 比例阀驱动电流 volatile static uint32_t t0, t1; t1 = HAL_GetTick() - t0; t0 = HAL_GetTick(); - if( HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], ADC1_RAW_NRS ) != HAL_OK){ + if( HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], ADC2_RAW_NRS ) != HAL_OK){ DBG_PRINTF( "start DMA fail\r\n" ); } pressure_sum = 0; flowrate_sum = 0; + valve_curr_sum = 0; - for( int i=0; i 10 ){ @@ -370,10 +375,13 @@ void user_adc_task( void* unused ) g_dev.state &= ~PRESSURE_SENSOR_ERR; } } + + //计算比例阀驱动的实际电流,AD采集R35(4.7Ω)上的电压,将电压除以4.7就得到电流 + g_dev.valve_curr_read = (valve_curr_sum/(ADC2_RAW_NRS/3) *VREF/0xffff )/4.7; } - if( uxBits & EVENT_ADC2_UPDATE ){ - //电流互感器 ADC2 - HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], ADC2_RAW_NRS ); + if( uxBits & EVENT_ADC1_UPDATE ){ + //电流互感器 ADC1 + HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], ADC1_RAW_NRS ); #if 0 if( g_dev.hw_ver == HW_V2 ){ //HW_V2 互感器输出 400A -- 2.5V +/-0.625V @@ -389,17 +397,17 @@ void user_adc_task( void* unused ) } #else - for( int32_t i=0; i< ADC2_RAW_NRS; i++ ) - adc2_curr[i] = vol_to_curr( adc2_raw_copy[i] * VREF*2/0xffff ); + for( int32_t i=0; i< ADC1_RAW_NRS; i++ ) + adc1_curr[i] = vol_to_curr( adc1_raw_copy[i] * VREF*2/0xffff ); #endif if( g_dev.debug_report == 2 ){ //发送调试数据到pc, extern uint32_t dbg_report_raw_current; extern uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len); - static uint8_t dbg_curr_raw[ADC2_RAW_NRS*2+8] = {0x5A, 0xA5, (sizeof(dbg_curr_raw)>>8)&0xff, sizeof(dbg_curr_raw)&0xff, 0x00, 0x24, }; + static uint8_t dbg_curr_raw[ADC1_RAW_NRS*2+8] = {0x5A, 0xA5, (sizeof(dbg_curr_raw)>>8)&0xff, sizeof(dbg_curr_raw)&0xff, 0x00, 0x24, }; - for( int i=0; i < ADC2_RAW_NRS; i++ ){ - int16_t curr = adc2_curr[i]; + for( int i=0; i < ADC1_RAW_NRS; i++ ){ + int16_t curr = adc1_curr[i]; dbg_curr_raw[6+i*2+1] = curr&0xff; dbg_curr_raw[6+i*2] = (curr>>8)&0xff; } @@ -415,7 +423,7 @@ void user_adc_task( void* unused ) }else{ // static uint32_t t10, t11; // t10 = HAL_GetTick(); - g_dev.welding_current = calc_welding_current( adc2_curr, ADC2_RAW_NRS); + g_dev.welding_current = calc_welding_current( adc1_curr, ADC1_RAW_NRS); // t11 = HAL_GetTick() - t10; } diff --git a/user/user_app.c b/user/user_app.c index 52b0d26..3786471 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -608,6 +608,8 @@ void welding_data_store( uint32_t index ) item.whelding_method = g_dev.cfg.welding_method; item.reserved = 0; + item.reserved = (uint16_t)(g_dev.valve_curr_read*1000); + #if 0 if( g_dev.welding_start ){ if( index==0 ){ -- 2.34.1