STM32CUBEIDE-F407(5)-DMA-ADC采样(2)

概述

本章通过STM32CUBEIDE配置STM32F407的DMA-ADC采样

IDE操作

本章实现的为ADC2的通道1.8.9.10.11.12.13.14.15,共9个通道的DMA采样,每次采8个点取平均值。

 

在main.c文件里进行相关操作

  //ADC-DMA
  HAL_ADC_Start_DMA(&hadc2,(uint32_t*)&ADC2_Value,72);
  HAL_ADC_Start_DMA(&hadc3,(uint32_t*)&ADC3_Value,56);
  //ADC-DMA-END

我通常会把采样函数写在中断里

              ADC2_1=ADC2_Value[0]+ADC2_Value[9]+ADC2_Value[18]+ADC2_Value[27]+ADC2_Value[36]+ ADC2_Value[45]+ADC2_Value[54]+ADC2_Value[63];

UC_Sample_A=ADC2_1/8;

给1通道注入1.65V电压,对应分辨率12位,数字量应为2048;

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于STM32CubeIDEADC采集电压代码示例,可以参考: 1. 配置ADC ``` /* ADC1 init function */ static void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_0; // ADC采集通道为0 sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; // ADC采样时间为1.5个周期 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } } ``` 2. 启动ADC ``` /* Start ADC conversion */ if (HAL_ADC_Start(&hadc1) != HAL_OK) { /* Start Conversation Error */ Error_Handler(); } ``` 3. 读取ADC值 ``` /* Wait for conversion to complete */ if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK) { /* Error */ Error_Handler(); } /* Read ADC value */ uint16_t adc_value = HAL_ADC_GetValue(&hadc1); ``` 完整代码示例: ``` #include "main.h" #include "stm32f1xx_hal.h" ADC_HandleTypeDef hadc1; void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_ADC1_Init(void); int main(void) { /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); while (1) { /* Start ADC conversion */ if (HAL_ADC_Start(&hadc1) != HAL_OK) { /* Start Conversation Error */ Error_Handler(); } /* Wait for conversion to complete */ if (HAL_ADC_PollForConversion(&hadc1, 100) != HAL_OK) { /* Error */ Error_Handler(); } /* Read ADC value */ uint16_t adc_value = HAL_ADC_GetValue(&hadc1); /* Convert ADC value to voltage */ float voltage = (float)adc_value / 4096.0 * 3.3; /* Do something with voltage value */ } } /** * @brief ADC1 Initialization Function * @param None * @retval None */ static void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_0; // ADC采集通道为0 sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; // ADC采样时间为1.5个周期 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); } /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* User can add his own implementation to report the HAL error return state */ while (1) { } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值