本文主要是介绍STM32G030F6使用CubeMx配置DMA读取多通道ADC实验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 使用 CubeMx 创建 ADC 工程
-
打开 CubeMx 软件,选中我们此次使用的单片机型号 STM32G030F6P6 ,点击 StartProject.
-
先配置一下串口,用来打印相关信息
-
再来配置 ADC
-
配置DMA
PS:DMA 需要要配置成循环模式,否则只填满一次缓存数组后就停止工作,需要重调用启动 DMA 的函数. -
配置时钟
ps:本实验使用内部高速时钟,未使用外部晶振,主频设置为最大的64MHZ.
-
配置工程相关选项
-
配置完成后点击右上角 GENERATE CODE完成工程的创建
2. 编程
2.1 串口相关的代码
- 因打印相关信息需要使用 printf ,需要包含 stdio.h 的头文件,且需要重新设 fputc 的函数
/* Includes ------------------------------------------------------------------*/
#include "main.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;UART_HandleTypeDef huart1;/* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
__IO uint16_t adcCovValueBuff[30][2] = {0}; //存放ADC的值 2通道 每个通道存放30个值,由DMA循环写入
uint16_t adcAverageBuff[2] = {0}; //对每个通道30个ADC值取平均值#ifdef __GNUC_
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 0xFFFF);return ch;
}/* USER CODE END 0 */
2.2 main 函数
/*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 */float votage = 0;uint32_t sum = 0;/* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_ADC1_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */printf("stm32g030f6 adc demo...... \n");HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adcCovValueBuff, 60);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while(1){for(uint8_t i = 0; i < 2; i++){for(uint8_t count = 0; count < 30; count++){sum += adcCovValueBuff[count][i];}adcAverageBuff[i] = sum / 30;sum = 0;}printf("---------------------------------------- \n");printf("ADC Channel01 Value:0x%04X \n", adcAverageBuff[0]);votage = (float) adcAverageBuff[0] / 4096 * (float)3.3;printf("ADC Channel01 Voltage:%f V \n", votage);printf("ADC Channel02 Value:0x%04X \n", adcAverageBuff[1]);votage = (float) adcAverageBuff[1] / 4096 * (float)3.3;printf("ADC Channel02 Voltage:%f V \n", votage);printf("---------------------------------------- \n");HAL_Delay(1000);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
2.3 外设的初始化函数
外设的初始化函数都是由 CubeMx 生成的,因为我们只用 DMA 搬运数据,未使用 DMA 的中断功能,故初始化 DMA 时可将中断配置函数注释。
/*** @brief ADC1 Initialization Function* @param None* @retval None*/
static void MX_ADC1_Init(void)
{/* USER CODE BEGIN ADC1_Init 0 *//* USER CODE END ADC1_Init 0 */ADC_ChannelConfTypeDef sConfig = {0};/* USER CODE BEGIN ADC1_Init 1 *//* USER CODE END ADC1_Init 1 *//** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)*/hadc1.Instance = ADC1;hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;hadc1.Init.Resolution = ADC_RESOLUTION_12B;hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;hadc1.Init.LowPowerAutoWait = DISABLE;hadc1.Init.LowPowerAutoPowerOff = DISABLE;hadc1.Init.ContinuousConvMode = ENABLE;hadc1.Init.NbrOfConversion = 2;hadc1.Init.DiscontinuousConvMode = DISABLE;hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;hadc1.Init.DMAContinuousRequests = ENABLE;hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_12CYCLES_5;hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_12CYCLES_5;hadc1.Init.OversamplingMode = DISABLE;hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;if(HAL_ADC_Init(&hadc1) != HAL_OK){Error_Handler();}/** Configure Regular Channel*/sConfig.Channel = ADC_CHANNEL_7;sConfig.Rank = ADC_REGULAR_RANK_1;sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){Error_Handler();}/** Configure Regular Channel*/sConfig.Channel = ADC_CHANNEL_8;sConfig.Rank = ADC_REGULAR_RANK_2;sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;if(HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK){Error_Handler();}/* USER CODE BEGIN ADC1_Init 2 *//* USER CODE END ADC1_Init 2 */}/*** @brief USART1 Initialization Function* @param None* @retval None*/
static void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if(HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}if(HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK){Error_Handler();}if(HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK){Error_Handler();}if(HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}/*** Enable DMA controller clock*/
static void MX_DMA_Init(void)
{/* DMA controller clock enable */__HAL_RCC_DMA1_CLK_ENABLE();/* DMA interrupt init *//* DMA1_Channel1_IRQn interrupt configuration *///HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); //未使用中断功能,故注释 //HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);}/*** @brief GPIO Initialization Function* @param None* @retval None*/
static void MX_GPIO_Init(void)
{/* GPIO Ports Clock Enable */__HAL_RCC_GPIOB_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();}
3. 实验现象
编译并将程序下载到开发板中,连接串口助手并打开;
实验现象:每秒打印一次当前两个通道的 ADC 值及换算后的电压.
4.完整工程
如果您需要完成的工程,可点击此处下载 STM32G030F6使用CubeMx配置DMA读取多通道ADC实验
如果觉得文章对您有帮助,请您关注点赞,谢谢!
这篇关于STM32G030F6使用CubeMx配置DMA读取多通道ADC实验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!