STM32CubeMX-多通道ADC-DMA-circular 传输

在此系列上文基础上添加ADC功能

一,CubeMX生成工程

1,配置ADC引脚

STM32CubeMX-多通道ADC-DMA-circular 传输_第1张图片

2,时钟配置

STM32CubeMX-多通道ADC-DMA-circular 传输_第2张图片

3,ADC配置

STM32CubeMX-多通道ADC-DMA-circular 传输_第3张图片

STM32CubeMX-多通道ADC-DMA-circular 传输_第4张图片

4,打开中断

STM32CubeMX-多通道ADC-DMA-circular 传输_第5张图片

5,DMA配置

STM32CubeMX-多通道ADC-DMA-circular 传输_第6张图片

(这里选择 halfword 应该也可以,程序里面对应存储adc数据的变量也为halfword  u16即可)

6,中断配置

STM32CubeMX-多通道ADC-DMA-circular 传输_第7张图片

红色标注为注意点,不清楚原理,ADC中断和DMZ的关系有待探明。

STM32CubeMX-多通道ADC-DMA-circular 传输_第8张图片

7,生成代码二,添加代码

数字标注处为添加部分

/**
  ******************************************************************************
  * File Name          : main.c
  * Date               : 12/07/2018 09:41:40
  * Description        : Main program body
  ******************************************************************************
  *
  * COPYRIGHT(c) 2018 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
 
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
 
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
uint8_t aRxBuffer;
uint32_t ADC_Value[100];			       //111111111
uint32_t DMA_Transfer_Complete_Count=0;
uint8_t i;
uint32_t ad1,ad2;

/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
int main(void)
{
 
  /* USER CODE BEGIN 1 */
 
  /* USER CODE END 1 */
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
 
  /* USER CODE BEGIN 2 */
	 printf("\n\rBEGIN\n\r");
/*##-1- Start the conversion process and enable interrupt ##################*/  	            
                                                             //2222222222222
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC_Value, 100);										
HAL_UART_Receive_IT(&huart1,&aRxBuffer,1);

  /* USER CODE END 2 */
 
  /* USER CODE BEGIN 3 */
  /* Infinite loop */
  while (1)
  {
  printf("\n\rDUDUDU\n\r");
  HAL_Delay(2000);
  ad1 = ADC_Value[1];
  ad2 = ADC_Value[2];
  printf(" AD1 value = %1.3fV \r\n", ad1*3.3f/4096);                //333333333
  printf(" AD2 value = %1.3fV \r\n", ad2*3.3f/4096);
	  printf("ÒѾ­Íê³ÉADת»»´ÎÊý£º%d\n",DMA_Transfer_Complete_Count);
  }
  /* USER CODE END 3 */
 
}
 
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
 
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;
  HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
 
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV4;
  HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
 
  __HAL_RCC_AFIO_CLK_ENABLE();
 
}
 
/* USER CODE BEGIN 4 */
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/**
  * @brief  Retargets the C library printf function to the USART.
  * @param  None
  * @retval None
  */
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
 
  return ch;
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)                                  
{
	
  HAL_UART_Transmit(&huart1,&aRxBuffer,1,0);
  HAL_UART_Receive_IT(&huart1,&aRxBuffer,1);
}
void  HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)																			 
                                                      //444444444444444
{
  DMA_Transfer_Complete_Count++; 
}
/* USER CODE END 4 */
 
#ifdef USE_FULL_ASSERT
 
/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
 
}
 
#endif
 
/**
  * @}
  */ 
 
/**
  * @}
*/ 
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

最后在adc.c中将

 hadc1.Init.ScanConvMode =ENABLE;

改为:

  hadc1.Init.ScanConvMode =ADC_SCAN_ENABLE;

没有改正此处的时候,多通道输出的值都全是第一通道的值。

找了大半天才找到是这个位置的问题,在CubeMX中根本没有这个选项,怀疑是个bug。

尽快熟悉HAL库函数吧,不能完全依靠工具,自身能力最可靠。

你可能感兴趣的:(——STM32)