STM32 LL库下ADC + DMA多通道连续扫描采集通道错乱问题记录

本文主要是介绍STM32 LL库下ADC + DMA多通道连续扫描采集通道错乱问题记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

         cubemx配置ADC+DMA转换后,代码在 adc.c 中将ADC_REG_InitStruct.DMATransfer 属性设置为:

        LL_ADC_REG_DMA_TRANSFER_UNLIMITED 或者

        LL_ADC_REG_DMA_TRANSFER_LIMITED(在MX中配置时只有这两选项)

,都会在初始化ADC时同时使能DMA。

/* ADC init function */
void MX_ADC_Init(void)
{/* USER CODE BEGIN ADC_Init 0 *//* USER CODE END ADC_Init 0 */LL_ADC_InitTypeDef ADC_InitStruct = {0};LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};LL_GPIO_InitTypeDef GPIO_InitStruct = {0};/* Peripheral clock enable */LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);/**ADC GPIO ConfigurationPA0   ------> ADC_IN0PA5   ------> ADC_IN5PA6   ------> ADC_IN6PA7   ------> ADC_IN7PB0   ------> ADC_IN8PB1   ------> ADC_IN9*/GPIO_InitStruct.Pin = OTP_Pin;GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(OTP_GPIO_Port, &GPIO_InitStruct);GPIO_InitStruct.Pin = VSENSE_Pin;GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(VSENSE_GPIO_Port, &GPIO_InitStruct);GPIO_InitStruct.Pin = OVP1_Pin;GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(OVP1_GPIO_Port, &GPIO_InitStruct);GPIO_InitStruct.Pin = OVP2_Pin;GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(OVP2_GPIO_Port, &GPIO_InitStruct);GPIO_InitStruct.Pin = SIGNAL1_Pin;GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(SIGNAL1_GPIO_Port, &GPIO_InitStruct);GPIO_InitStruct.Pin = SIGNAL2_Pin;GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;LL_GPIO_Init(SIGNAL2_GPIO_Port, &GPIO_InitStruct);/* ADC DMA Init *//* ADC Init */LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_LOW);LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);/* USER CODE BEGIN ADC_Init 1 *//* USER CODE END ADC_Init 1 *//** Configure Regular Channel*/LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_0);/** Configure Regular Channel*/LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_5);/** Configure Regular Channel*/LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_6);/** Configure Regular Channel*/LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_7);/** Configure Regular Channel*/LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_8);/** Configure Regular Channel*/LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_9);/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)*/ADC_InitStruct.Clock = LL_ADC_CLOCK_ASYNC;ADC_InitStruct.Resolution = LL_ADC_RESOLUTION_12B;ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;ADC_InitStruct.LowPowerMode = LL_ADC_LP_MODE_NONE;LL_ADC_Init(ADC1, &ADC_InitStruct);ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED;ADC_REG_InitStruct.Overrun = LL_ADC_REG_OVR_DATA_PRESERVED;LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);LL_ADC_REG_SetSequencerScanDirection(ADC1, LL_ADC_REG_SEQ_SCAN_DIR_FORWARD);LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_239CYCLES_5);/* USER CODE BEGIN ADC_Init 2 *//* USER CODE END ADC_Init 2 */}

开启通道转换前,如果调用了 LL_ADC_StartCalibration 开启ADC校准,ADC的DMA转换通道顺序会错乱(比如原本通道0的数据会跑掉其他通道去),在该函数的表述中就有 

        In case of usage of ADC with DMA transfer:
  *         On this STM32 serie, ADC DMA transfer request should be disabled
  *         during calibration:  * @note   In case of usage of ADC with DMA transfer:
  *         On this STM32 serie, ADC DMA transfer request should be disabled
  *         during calibration:
  *         Calibration factor is available in data register
  *         and also transfered by DMA.
  *         To not insert ADC calibration factor among ADC conversion data
  *         in array variable, DMA transfer must be disabled during
  *         calibration.
  *         (DMA transfer setting backup and disable before calibration,
  *         DMA transfer setting restore after calibration.
  *         Refer to functions @ref LL_ADC_REG_GetDMATransfer(),
  *         @ref LL_ADC_REG_SetDMATransfer() ).

/*** @brief  Start ADC calibration in the mode single-ended*         or differential (for devices with differential mode available).* @note   On this STM32 serie, a minimum number of ADC clock cycles*         are required between ADC end of calibration and ADC enable.*         Refer to literal @ref LL_ADC_DELAY_CALIB_ENABLE_ADC_CYCLES.* @note   In case of usage of ADC with DMA transfer:*         On this STM32 serie, ADC DMA transfer request should be disabled*         during calibration:*         Calibration factor is available in data register*         and also transfered by DMA.*         To not insert ADC calibration factor among ADC conversion data*         in array variable, DMA transfer must be disabled during*         calibration.*         (DMA transfer setting backup and disable before calibration,*         DMA transfer setting restore after calibration.*         Refer to functions @ref LL_ADC_REG_GetDMATransfer(),*         @ref LL_ADC_REG_SetDMATransfer() ).* @note   On this STM32 serie, setting of this feature is conditioned to*         ADC state:*         ADC must be ADC disabled.* @rmtoll CR       ADCAL          LL_ADC_StartCalibration* @param  ADCx ADC instance* @retval None*/
__STATIC_INLINE void LL_ADC_StartCalibration(ADC_TypeDef *ADCx)
{/* Note: Write register with some additional bits forced to state reset     *//*       instead of modifying only the selected bit for this function,      *//*       to not interfere with bits with HW property "rs".                  */MODIFY_REG(ADCx->CR,ADC_CR_BITS_PROPERTY_RS,ADC_CR_ADCAL);
}

 由于在初始化时开启了DMA使能,会导致ADC在校准时产生的数据也被DMA搬运;应在 LL_ADC_StartCalibration 前 ADC_REG_InitStruct.DMATransfer 设置为 LL_ADC_REG_DMA_TRANSFER_NONE

LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_NONE);

关闭ADCDMA转换使能;

这篇关于STM32 LL库下ADC + DMA多通道连续扫描采集通道错乱问题记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/388785

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

poj2406(连续重复子串)

题意:判断串s是不是str^n,求str的最大长度。 解题思路:kmp可解,后缀数组的倍增算法超时。next[i]表示在第i位匹配失败后,自动跳转到next[i],所以1到next[n]这个串 等于 n-next[n]+1到n这个串。 代码如下; #include<iostream>#include<algorithm>#include<stdio.h>#include<math.

购买磨轮平衡机时应该注意什么问题和技巧

在购买磨轮平衡机时,您应该注意以下几个关键点: 平衡精度 平衡精度是衡量平衡机性能的核心指标,直接影响到不平衡量的检测与校准的准确性,从而决定磨轮的振动和噪声水平。高精度的平衡机能显著减少振动和噪声,提高磨削加工的精度。 转速范围 宽广的转速范围意味着平衡机能够处理更多种类的磨轮,适应不同的工作条件和规格要求。 振动监测能力 振动监测能力是评估平衡机性能的重要因素。通过传感器实时监

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

XTU 1233 n个硬币连续m个正面个数(dp)

题面: Coins Problem Description: Duoxida buys a bottle of MaiDong from a vending machine and the machine give her n coins back. She places them in a line randomly showing head face or tail face o

MOLE 2.5 分析分子通道和孔隙

软件介绍 生物大分子通道和孔隙在生物学中发挥着重要作用,例如在分子识别和酶底物特异性方面。 我们介绍了一种名为 MOLE 2.5 的高级软件工具,该工具旨在分析分子通道和孔隙。 与其他可用软件工具的基准测试表明,MOLE 2.5 相比更快、更强大、功能更丰富。作为一项新功能,MOLE 2.5 可以估算已识别通道的物理化学性质。 软件下载 https://pan.quark.cn/s/57

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

【STM32】SPI通信-软件与硬件读写SPI

SPI通信-软件与硬件读写SPI 软件SPI一、SPI通信协议1、SPI通信2、硬件电路3、移位示意图4、SPI时序基本单元(1)开始通信和结束通信(2)模式0---用的最多(3)模式1(4)模式2(5)模式3 5、SPI时序(1)写使能(2)指定地址写(3)指定地址读 二、W25Q64模块介绍1、W25Q64简介2、硬件电路3、W25Q64框图4、Flash操作注意事项软件SPI读写W2