diff --git a/Power_Pico/BSP/GATE/gate.h b/Power_Pico/BSP/GATE/gate.h index 3f61a61..42f545d 100644 --- a/Power_Pico/BSP/GATE/gate.h +++ b/Power_Pico/BSP/GATE/gate.h @@ -16,9 +16,9 @@ extern "C" { #define EN2_PIN GPIO_PIN_15 // -#define HIGH_CUR 0 +#define HIGH_CUR 2 #define MID_CUR 1 -#define LOW_CUR 2 +#define LOW_CUR 0 void Gate_Port_Init(void); void flow_route_selection(uint8_t selection); diff --git a/Power_Pico/Core/Inc/adc.h b/Power_Pico/Core/Inc/adc.h index a89c33e..0957737 100644 --- a/Power_Pico/Core/Inc/adc.h +++ b/Power_Pico/Core/Inc/adc.h @@ -29,7 +29,10 @@ extern "C" { #include "main.h" /* USER CODE BEGIN Includes */ - +#define ADC_TIMES 10 +#define ADC_CHANELS 4 +// cur 5mr, cur 500mr, cur 50r, val +extern uint16_t adc_buf[ADC_TIMES][ADC_CHANELS]; /* USER CODE END Includes */ extern ADC_HandleTypeDef hadc1; diff --git a/Power_Pico/Core/Src/adc.c b/Power_Pico/Core/Src/adc.c index 042fe30..c0d49b2 100644 --- a/Power_Pico/Core/Src/adc.c +++ b/Power_Pico/Core/Src/adc.c @@ -22,6 +22,9 @@ /* USER CODE BEGIN 0 */ +// cur 5mr, cur 500mr, cur 50r, voltage +uint16_t adc_buf[ADC_TIMES][ADC_CHANELS]; + /* USER CODE END 0 */ ADC_HandleTypeDef hadc1; diff --git a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c index 9eef352..1c4c2ec 100644 --- a/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c +++ b/Power_Pico/User/Tasks/Src/user_HardwareInitTask.c @@ -5,6 +5,7 @@ #include "usart.h" #include "tim.h" #include "stm32f4xx_it.h" +#include "adc.h" // user #include "user_TasksInit.h" @@ -13,6 +14,7 @@ #include "key.h" #include "lcd.h" #include "lcd_init.h" +#include "gate.h" // ui #include "lvgl.h" @@ -25,6 +27,16 @@ /* Private variables ---------------------------------------------------------*/ +/* protocol frame ++ 0x55 0xAA header ++ 1byte direct & unit, 0x0? for positive, 0x1? for negative, + 0x?0 for uA (0-500.00uA), 0x?1 for mA (0.50-500.00mA), 0x?2 for A (0.50-5.00A) ++ 2byte current (100 times) ++ 2byte voltage (100 times) ++ 0xAA 0x55 footer +*/ +uint8_t protocol_frame[] = {0x55, 0xAA, 0x02, 0x01, 0x23, 0x01, 0xf4, 0xAA, 0x55}; + /* Private function prototypes -----------------------------------------------*/ @@ -39,7 +51,22 @@ void HardwareInitTask(void *argument) { vTaskSuspendAll(); - // usart start + // ADC + HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buf, ADC_TIMES * ADC_CHANELS); /*启动ADC的DMA传输,配合定时器触发ADC转换 12位的ADC对应0-4095 */ + HAL_TIM_Base_Start(&htim2); /*开启定时器,用溢出时间来触发ADC*/ + + // uart start + HAL_UART_Receive_DMA(&huart6, (uint8_t*)uart_receive_str, USART_RX_BUFFER_SIZE); + __HAL_UART_ENABLE_IT(&huart6, UART_IT_IDLE); + // + UART6_TX_Send((uint8_t *)"power-pico\r\n", 13); + // + // UART6_TX_Send(protocol_frame, sizeof(protocol_frame)); + // + + // gate, for current flow route selection, high current by default + Gate_Port_Init(); + flow_route_selection(HIGH_CUR); // PWM Start for LCD backlight HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3); @@ -49,10 +76,6 @@ void HardwareInitTask(void *argument) // lcd // done in lvgl disp init - LCD_Init(); - LCD_Fill(0,0, LCD_W, LCD_H, BLACK); - LCD_Set_Light(50); - LCD_ShowString(72,LCD_H/2,(uint8_t*)"Welcome!", WHITE, BLACK, 24, 0); // ui // LVGL and disp init diff --git a/Power_Pico/User/Tasks/Src/user_SendTask.c b/Power_Pico/User/Tasks/Src/user_SendTask.c index a436b01..2f288fa 100644 --- a/Power_Pico/User/Tasks/Src/user_SendTask.c +++ b/Power_Pico/User/Tasks/Src/user_SendTask.c @@ -2,7 +2,8 @@ //includes #include "user_TasksInit.h" #include "main.h" - +#include "adc.h" +#include "gate.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ @@ -20,11 +21,37 @@ void UartSendTask(void *argument) { uint8_t keystr = 0; + uint16_t cur_bias = 4096 / 2; while(1) { if(osMessageQueueGet(Key_MessageQueue, &keystr, NULL, 0)==osOK) { - UART6_TX_Send((uint8_t *)"key pressed\r\n", 13); + // UART6_TX_Send((uint8_t *)"key pressed\r\n", 13); + } + // send protocol frame + for(int i = 0 ; i < ADC_TIMES ; i++) + { + // direct & unit + uint8_t dir_unit = 0x00; // positive, uA + uint8_t unit = Gate_get_status(); // 0 - low current, 1 - mid current, 2 - high current + dir_unit |= (unit & 0x0F); // unit + // current + uint32_t cur; + // voltage *100times + uint32_t volt = adc_buf[i][3] * 3 * 100 / 4096; // 12bit ADC, 0-4095 + // cur direction and value + if(adc_buf[i][2-unit] > cur_bias) { + dir_unit |= 0x10; // negative + // cur *100times + cur = (adc_buf[i][2-unit] - cur_bias) * 3 * 100 / 4096; // 3.0 Vref, 100 times, 12bit ADC + } + else { + // cur *100times + cur = (cur_bias - adc_buf[i][2-unit]) * 3 * 100 / 4096; // 3.0 Vref, 100 times, 12bit ADC + } + // protocol frame + uint8_t protocol_frame[9] = {0x55, 0xAA, dir_unit, (cur<<8 & 0xff), (cur & 0xff), (volt<<8 & 0xff), (volt & 0xff), 0xAA, 0x55}; + UART6_TX_Send(protocol_frame, sizeof(protocol_frame)); } osDelay(1); }