diff --git a/Power_Pico/BSP/GATE/gate.c b/Power_Pico/BSP/GATE/gate.c index b52c17d..b529719 100644 --- a/Power_Pico/BSP/GATE/gate.c +++ b/Power_Pico/BSP/GATE/gate.c @@ -4,10 +4,8 @@ #include "math.h" // 量程阈值 -#define ADC_TRUST_MAX 1952 -#define ADC_TRUST_MIN 10 -#define THRESH_HIGH 1800 -#define THRESH_LOW 18 +#define THRESH_HIGH 4000-2048 // 对应550uA 50mA +#define THRESH_LOW 15 // 对应大概450uA 45mA // 次数 #define THRESH_TIMES 8 @@ -79,7 +77,9 @@ void Gate_Swich_and_UART_Send(ADC_Packet adc_packet) { static uint8_t high_times = 0; static uint8_t low_times = 0; - // low mid high + // adc码值 + uint16_t ref_adc = adc_packet.data[0][4]; + uint16_t vol_adc = adc_packet.data[0][0]; uint16_t cur_adc = 0; switch(Gate_get_status()) { case LOW_CUR: @@ -92,60 +92,52 @@ void Gate_Swich_and_UART_Send(ADC_Packet adc_packet) cur_adc = adc_packet.data[0][3]; break; } - // 可信范围内 + // 计算电压电流值 float voltage; float current; - uint16_t diff = abs((int)cur_adc - 2048); - if(diff < ADC_TRUST_MAX && diff > ADC_TRUST_MIN) { - adc_packet.header[3] = Gate_get_status(); - ADC_Packet adc_packet_trans = adc_packet; - HAL_UART_Transmit_DMA(&huart6, (uint8_t*)&adc_packet_trans, sizeof(adc_packet_trans)); - // 计算电压电流值 - voltage = adc_packet.data[0][0] * 3.0f / 4096.0f * 11.0f; // 11 = (100+10)/10 - // 计算电流值 - if(Gate_get_status() == LOW_CUR) { - current = adc_packet.data[0][1]; - current = (current - 2048.0) * SCALE_LOW; - } - else if(Gate_get_status() == MID_CUR) { - current = adc_packet.data[0][2]; - current = (current - 2048.0) * SCALE_MID; - } - else if(Gate_get_status() == HIGH_CUR) { - current = adc_packet.data[0][3]; - current = (current - 2048.0) * SCALE_HIGH; - } - // 入队列 - queue_push(global_voltage_queue, voltage); - queue_push(global_current_queue, current); - // 判断是否需要切换档位 - // ADC码值超过量程 - if(diff > THRESH_HIGH) { - high_times++; - low_times = 0; - if(high_times >= THRESH_TIMES) { - high_times = 0; - flow_route_selection(Gate_get_status()+1); - } - } - else if(diff < THRESH_LOW) { - low_times++; + uint16_t diff = abs((int)cur_adc - ref_adc); + + adc_packet.header[3] = Gate_get_status(); + ADC_Packet adc_packet_trans = adc_packet; + HAL_UART_Transmit_DMA(&huart6, (uint8_t*)&adc_packet_trans, sizeof(adc_packet_trans)); + // 计算电压电流值 + voltage = vol_adc * 3.0f / 4096.0f * 11.0f; // 11 = (100+10)/10 + // 计算电流值 + if(Gate_get_status() == LOW_CUR) { + current = adc_packet.data[0][1]; + current = (current - ref_adc) * SCALE_LOW; + } + else if(Gate_get_status() == MID_CUR) { + current = adc_packet.data[0][2]; + current = (current - ref_adc) * SCALE_MID; + } + else if(Gate_get_status() == HIGH_CUR) { + current = adc_packet.data[0][3]; + current = (current - ref_adc) * SCALE_HIGH; + } + // 入队列 + queue_push(global_voltage_queue, voltage); + queue_push(global_current_queue, current); + // 判断是否需要切换档位 + // ADC码值超过量程 + if(diff > THRESH_HIGH) { + high_times++; + low_times = 0; + if(high_times >= THRESH_TIMES) { high_times = 0; - if(low_times >= THRESH_TIMES) { - low_times = 0; - flow_route_selection(Gate_get_status()-1); - } - } - else { - high_times = 0; - low_times = 0; + flow_route_selection(Gate_get_status()+1); } } - // 不可信范围内 - else { - if(diff >= ADC_TRUST_MAX) // 过大,切换到更大档位 - flow_route_selection(Gate_get_status()+1); - else if(diff <= ADC_TRUST_MIN) // 过小,切换到更小档位 + else if(diff < THRESH_LOW) { + low_times++; + high_times = 0; + if(low_times >= THRESH_TIMES) { + low_times = 0; flow_route_selection(Gate_get_status()-1); + } + } + else { + high_times = 0; + low_times = 0; } } diff --git a/Power_Pico/Core/Inc/adc.h b/Power_Pico/Core/Inc/adc.h index 805d78e..45acdcb 100644 --- a/Power_Pico/Core/Inc/adc.h +++ b/Power_Pico/Core/Inc/adc.h @@ -31,7 +31,7 @@ extern "C" { /* USER CODE BEGIN Includes */ #define ADC_TIMES 1 -#define ADC_CHANELS 4 +#define ADC_CHANELS 5 typedef struct { uint8_t header[4]; //2的倍数 @@ -39,7 +39,7 @@ typedef struct { // 0x0A adc times, 0x05 adc channels (so data lenth is times * channels * 2 bytes) // 0x?0 for LOW, 0x?1 for MID, 0x?2 for HIGH. uint16_t data[ADC_TIMES][ADC_CHANELS]; -} __attribute__((packed)) ADC_Packet; +} ADC_Packet; extern ADC_Packet adc_packet; diff --git a/Power_Pico/Core/Src/adc.c b/Power_Pico/Core/Src/adc.c index 5f36206..10cdcbf 100644 --- a/Power_Pico/Core/Src/adc.c +++ b/Power_Pico/Core/Src/adc.c @@ -54,7 +54,7 @@ void MX_ADC1_Init(void) hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T2_TRGO; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; - hadc1.Init.NbrOfConversion = 4; + hadc1.Init.NbrOfConversion = 5; hadc1.Init.DMAContinuousRequests = ENABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; if (HAL_ADC_Init(&hadc1) != HAL_OK) @@ -98,6 +98,15 @@ void MX_ADC1_Init(void) { Error_Handler(); } + + /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. + */ + sConfig.Channel = ADC_CHANNEL_9; + sConfig.Rank = 5; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } /* USER CODE BEGIN ADC1_Init 2 */ __HAL_DMA_ENABLE_IT(&hdma_adc1, DMA_IT_TC); /*开启DMA传输完成中断*/ /* USER CODE END ADC1_Init 2 */ @@ -123,13 +132,14 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle) PA6 ------> ADC1_IN6 PA7 ------> ADC1_IN7 PB0 ------> ADC1_IN8 + PB1 ------> ADC1_IN9 */ GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - GPIO_InitStruct.Pin = GPIO_PIN_0; + GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); @@ -175,10 +185,11 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle) PA6 ------> ADC1_IN6 PA7 ------> ADC1_IN7 PB0 ------> ADC1_IN8 + PB1 ------> ADC1_IN9 */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); - HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0); + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1); /* ADC1 DMA DeInit */ HAL_DMA_DeInit(adcHandle->DMA_Handle); diff --git a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c index ef7a3d9..a9f3cb0 100644 --- a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c +++ b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c @@ -52,9 +52,9 @@ void HardwareInitTask(void *argument) // HAL_UART_Transmit_DMA(&huart6,(uint8_t *)"power-pico\r\n",13); // queue for voltage and current - global_voltage_queue = queue_create(8); - global_current_queue = queue_create(8); - for(int i = 0; i < 8; i++) { + global_voltage_queue = queue_create(32); + global_current_queue = queue_create(32); + for(int i = 0; i < 32; i++) { queue_push(global_voltage_queue, 0.0); queue_push(global_current_queue, 0.0); }