#include "main.h" #include "cmsis_os.h" #include "filament.h" extern TIM_HandleTypeDef htim1; extern ADC_HandleTypeDef hadc1; extern DMA_HandleTypeDef hdma_adc1; uint16_t gausAdcBuf[ADC_CHNL_NUM]; static uint16_t gusDuty = 0; void ifilaAdust(void) { static uint32_t ulLastTick = 0; uint32_t ulTick = osKernelGetTickCount(); if (ulTick - ulLastTick < 5000) { return; } ulLastTick = ulTick; if (gusDuty == 0) { gusDuty = 300; } else if (gusDuty == 300) { gusDuty = 600; } else if (gusDuty == 600) { gusDuty = 300; } __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, gusDuty); } uint16_t adc2voltage(uint16_t adcVal) { return adcVal * 3000 / 65535; } uint16_t adc2Iflla(uint16_t adcVal) { return adc2voltage(adcVal) * 1000 / 20 / 20; } uint16_t adc2Vflla(uint16_t adcVal) { return adc2voltage(adcVal) * 3; } static void filamentThread(void *arg) { int iCnt = 0; HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t *)gausAdcBuf, ADC_CHNL_NUM); debug("ADC_CHNL_NUM = %d\n", ADC_CHNL_NUM); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1); ifilaAdust(); while (1) { osDelay(10); // ifilaAdust(); iCnt++; if (iCnt < 100) { continue; } iCnt = 0; SCB_InvalidateDCache_by_Addr(gausAdcBuf, ADC_CHNL_NUM * sizeof(gausAdcBuf[0])); printf("ADC: %d,%d,%d\n", gausAdcBuf[0], gausAdcBuf[1], gausAdcBuf[2]); printf("duty:%d%%,I:%.2fA,V:%.2fV\n", gusDuty*100/2400, adc2Iflla(gausAdcBuf[1]) / 1000.0, adc2Vflla(gausAdcBuf[2])/1000.0); } } void filamentInit(void) { // 创建任务 const osThreadAttr_t filament_task_attributes = { .name = "filament_task", .stack_size = 4096 * 2, .priority = (osPriority_t) osPriorityNormal, }; if (osThreadNew(filamentThread, NULL, &filament_task_attributes) == NULL) { printf("failed to create filament_task\n"); } else { printf("create filament_task success\n"); } }