方法一:用SDADC的规则通道一个一个去读,配置SDADC如下
读入代码比较简单,依次读取SDADC1通道6、通道8和SDADC3通道0,代码如下
/* USER CODE BEGIN Header_StartDefaultTask */ volatile uint32_t SDADC_Value[10]; /** * @brief Function implementing the defaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { //read SDADC1 Channel6 HAL_SDADC_ConfigChannel(&hsdadc1,SDADC_CHANNEL_6,SDADC_CONTINUOUS_CONV_OFF);// set channel HAL_SDADC_Start(&hsdadc1);//start sdadc if(HAL_SDADC_PollForConversion(&hsdadc1,HAL_MAX_DELAY)==HAL_OK)//waiting for convert complete { SDADC_Value[0] = HAL_SDADC_GetValue(&hsdadc1);// read the value of sdadc HAL_SDADC_Stop(&hsdadc1); } //read SDADC1 Channel8 HAL_SDADC_ConfigChannel(&hsdadc1,SDADC_CHANNEL_8,SDADC_CONTINUOUS_CONV_OFF); HAL_SDADC_Start(&hsdadc1); if(HAL_SDADC_PollForConversion(&hsdadc1,HAL_MAX_DELAY)==HAL_OK) { SDADC_Value[1] = HAL_SDADC_GetValue(&hsdadc1); HAL_SDADC_Stop(&hsdadc1); } //read SDADC3 Channel0 HAL_SDADC_ConfigChannel(&hsdadc3,SDADC_CHANNEL_0,SDADC_CONTINUOUS_CONV_OFF); HAL_SDADC_Start(&hsdadc3); if(HAL_SDADC_PollForConversion(&hsdadc3,HAL_MAX_DELAY)==HAL_OK) { SDADC_Value[2] = HAL_SDADC_GetValue(&hsdadc3); HAL_SDADC_Stop(&hsdadc3); } osDelay(1); } /* USER CODE END StartDefaultTask */ }
方法二:用注入通道一次读取多个转换值
配置SDADC
配置DMA
在stm32f3xx_it.c文件中修改
/** * @brief This function handles DMA2 channel3 global interrupt. */ void DMA2_Channel3_IRQHandler(void) { /* USER CODE BEGIN DMA2_Channel3_IRQn 0 */ /* USER CODE END DMA2_Channel3_IRQn 0 */ HAL_DMA_IRQHandler(&hdma_sdadc1); /* USER CODE BEGIN DMA2_Channel3_IRQn 1 */ IsFinishDMA2_Channel3 = 1;//DMA 转换完毕 /* USER CODE END DMA2_Channel3_IRQn 1 */ } /** * @brief This function handles DMA2 channel5 global interrupt. */ void DMA2_Channel5_IRQHandler(void) { /* USER CODE BEGIN DMA2_Channel5_IRQn 0 */ /* USER CODE END DMA2_Channel5_IRQn 0 */ HAL_DMA_IRQHandler(&hdma_sdadc3); /* USER CODE BEGIN DMA2_Channel5_IRQn 1 */ IsFinishDMA2_Channel5 = 1;//DMA 转换完毕 /* USER CODE END DMA2_Channel5_IRQn 1 */ }
/* USER CODE BEGIN Header_StartDefaultTask */ static uint32_t SDADC1_DegitalValue[5]; static uint32_t SDADC3_DegitalValue[5]; /** * @brief Function implementing the defaultTask thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void const * argument) { /* USER CODE BEGIN StartDefaultTask */ /* Infinite loop */ for(;;) { if(HAL_SDADC_InjectedStart_DMA(&hsdadc3,SDADC3_DegitalValue,5) == HAL_OK) { while(IsFinishDMA2_Channel5 == 0) osDelay(1);//等待DMA转换完成 IsFinishDMA2_Channel5 = 0; HAL_SDADC_InjectedStop_DMA(&hsdadc3); } if(HAL_SDADC_InjectedStart_DMA(&hsdadc1,SDADC1_DegitalValue,5) == HAL_OK) { while(IsFinishDMA2_Channel3 == 0) osDelay(1);//等待DMA转换完成 IsFinishDMA2_Channel3 = 0; HAL_SDADC_InjectedStop_DMA(&hsdadc1); } osDelay(1); } /* USER CODE END StartDefaultTask */ }
转换结果如下:
源码下载地址:https://github.com/lizhiqiang0204/STM32_SDADC 本例程是用STM32CubeIDE软件编写的