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

相关文章

Python实现快速扫描目标主机的开放端口和服务

《Python实现快速扫描目标主机的开放端口和服务》这篇文章主要为大家详细介绍了如何使用Python编写一个功能强大的端口扫描器脚本,实现快速扫描目标主机的开放端口和服务,感兴趣的小伙伴可以了解下... 目录功能介绍场景应用1. 网络安全审计2. 系统管理维护3. 网络故障排查4. 合规性检查报错处理1.

Python中4大日志记录库比较的终极PK

《Python中4大日志记录库比较的终极PK》日志记录框架是一种工具,可帮助您标准化应用程序中的日志记录过程,:本文主要介绍Python中4大日志记录库比较的相关资料,文中通过代码介绍的非常详细,... 目录一、logging库1、优点2、缺点二、LogAid库三、Loguru库四、Structlogphp

Springboot3统一返回类设计全过程(从问题到实现)

《Springboot3统一返回类设计全过程(从问题到实现)》文章介绍了如何在SpringBoot3中设计一个统一返回类,以实现前后端接口返回格式的一致性,该类包含状态码、描述信息、业务数据和时间戳,... 目录Spring Boot 3 统一返回类设计:从问题到实现一、核心需求:统一返回类要解决什么问题?

maven异常Invalid bound statement(not found)的问题解决

《maven异常Invalidboundstatement(notfound)的问题解决》本文详细介绍了Maven项目中常见的Invalidboundstatement异常及其解决方案,文中通过... 目录Maven异常:Invalid bound statement (not found) 详解问题描述可

idea粘贴空格时显示NBSP的问题及解决方案

《idea粘贴空格时显示NBSP的问题及解决方案》在IDEA中粘贴代码时出现大量空格占位符NBSP,可以通过取消勾选AdvancedSettings中的相应选项来解决... 目录1、背景介绍2、解决办法3、处理完成总结1、背景介绍python在idehttp://www.chinasem.cna粘贴代码,出

SpringBoot整合Kafka启动失败的常见错误问题总结(推荐)

《SpringBoot整合Kafka启动失败的常见错误问题总结(推荐)》本文总结了SpringBoot项目整合Kafka启动失败的常见错误,包括Kafka服务器连接问题、序列化配置错误、依赖配置问题、... 目录一、Kafka服务器连接问题1. Kafka服务器无法连接2. 开发环境与生产环境网络不通二、序

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

nacos服务无法注册到nacos服务中心问题及解决

《nacos服务无法注册到nacos服务中心问题及解决》本文详细描述了在Linux服务器上使用Tomcat启动Java程序时,服务无法注册到Nacos的排查过程,通过一系列排查步骤,发现问题出在Tom... 目录简介依赖异常情况排查断点调试原因解决NacosRegisterOnWar结果总结简介1、程序在

解决java.util.RandomAccessSubList cannot be cast to java.util.ArrayList错误的问题

《解决java.util.RandomAccessSubListcannotbecasttojava.util.ArrayList错误的问题》当你尝试将RandomAccessSubList... 目录Java.util.RandomAccessSubList cannot be cast to java.

Apache服务器IP自动跳转域名的问题及解决方案

《Apache服务器IP自动跳转域名的问题及解决方案》本教程将详细介绍如何通过Apache虚拟主机配置实现这一功能,并解决常见问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录​​问题背景​​解决方案​​方法 1:修改 httpd-vhosts.conf(推荐)​​步骤