HAL ADC连续转换模式 Continuous Conversion Mode

本文探讨了ContinuousConversionMode在ADC采样中的应用,发现其在ENABLE和DISABLE状态下的行为差异,并重点讲解了周期设置对连续转换的影响,以及如何调整以获得准确的连续采样结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在学习Continuous Conversion Mode时,根据网上的例程和配置,我发现Continuous Conversion Mode ENABLE 和DISABLE输出结果好像没有区别。后来自己验证下来的结果,做个笔记

Continuous Conversion Mode:使能后,相比于单次转换模式,连续转换模式在打开ADC后一直转换。假如你单通道,在完成一次ADC转换后就停止了,而连续转换模式会一直转换,当你是多通道AN1 ,AN2,AN3,单次转换模式在把三个通道转换完一次后停止,而连续转换模式在转换完AN3后,会从新再次去转换。下面用个例程比较

例程一:

        while (1)
        {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
        HAL_ADC_Start(&hadc);
      HAL_ADC_PollForConversion(&hadc,50);//
             if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc), HAL_ADC_STATE_REG_EOC))
             {
                 value_adc=HAL_ADC_GetValue(&hadc);
                 voltage=(float)value_adc/4095*3.3*11;
                
                printf("%f\r\n",voltage);
                printf("%d\r\n",value_adc);
                    HAL_Delay(500);
             }

输出结果

输出结果:

 留意此时ADC的值是在变化的,看着好像在连续的采样,但是你看程序,ADC的开启和等待转换的函数都是放在while()里的,我在网上查好像都是这样的例程。这时候你发现Continuous Conversion Mode选择ENABLE 和DISABLE没有区别。但是当你把  HAL_ADC_Start(&hadc);放到while()上面去,连续转换模式还是DISABLE,你就会发现此时ADC的值不变了,只是进行了一次转换.

其实就是转换了一次,printf函数一直在输出那次的ADC转换而已

此时,你把连续扫描模式ENABLE ,程序不动。你会发现ADC再次连续转换 

 

 

此时的输出结果是连续转换的,但是我发现此时ADC的值偏大,后来研究一下是采用周期太短的问题。之前Continuous Conversion Mode DISABLE,在while里连续转换,它的真正采用周期并不是你设置的ADC采用周期,而是你while()的循环周期,你多久 循环一次就进行一次采用。当你Continuous Conversion Mode DISABLE ENABLE时,就是上面ADC偏大的那种情况,此时你把周期调长,就可以了.(此时 HAL_ADC_Start(&hadc)放在while里和while外都一样)

### STM32F1 HAL ADC Sampling Tutorial For the STM32F1 series microcontroller, using the Hardware Abstraction Layer (HAL) library to perform Analog-to-Digital Converter (ADC) sampling involves several key steps and configurations that ensure reliable data acquisition. The configuration of ADC with Direct Memory Access (DMA) is crucial for efficient operation without CPU intervention during transfers. When configuring ADC in conjunction with DMA within the HAL environment, it's important to set up single conversion mode where each use requires restarting DMA explicitly. This approach helps prevent potential issues related to data transmission errors by ensuring fresh starts for every conversion cycle[^1]. In CubeMX initialization settings, one can configure ADC parameters such as resolution, alignment, external trigger source, continuous conversion state, etc., which are essential for setting up proper ADC functionality before generating code. Afterward, specific adjustments might be necessary through software coding based on application requirements[^2]. Here’s an example demonstrating how to initialize ADC along with enabling DMA support: ```c // Initialize ADC peripheral according to specified parameters values. static void MX_ADC_Init(void) { /* USER CODE BEGIN ADC_Init 0 */ /* USER CODE END ADC_Init 0 */ ADC_ChannelConfTypeDef sConfig = {0}; hadc.Instance = ADC1; hadc.Init.ScanConvMode = DISABLE; // Single channel conversion hadc.Init.ContinuousConvMode = DISABLE; // Disable Continuous Conversion Mode hadc.Init.DiscontinuousConvMode = DISABLE; hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.NbrOfConversion = 1; if (HAL_ADC_Init(&hadc) != HAL_OK) { Error_Handler(); } /** Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } } void Start_DMA_Transfer(void){ uint16_t *pData; pData = buffer_for_dma_transfer; if(HAL_ADC_Start_IT(&hadc)!= HAL_OK){ Error_Handler(); } if(HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED)!= HAL_OK){ Error_Handler(); } if(HAL_ADC_Start_DMA(&hadc,(uint32_t*)pData,BUFFER_SIZE)!= HAL_OK){ Error_Handler(); } } ``` This setup ensures that when starting a new measurement sequence via `Start_DMA_Transfer`, both interrupt-driven start (`HAL_ADC_Start_IT`) and calibration procedures precede initiating DMA transfer (`HAL_ADC_Start_DMA`). Such preparation minimizes risks associated with unexpected behavior seen previously while working under standard libraries. --related questions-- 1. How does changing from Standard Library to HAL affect performance metrics like speed or power consumption? 2. What additional considerations should be taken into account when implementing multi-channel ADC conversions using DMA? 3. Can you provide examples illustrating differences between handling interrupts versus polling methods in ADC operations? 4. Are there any best practices recommended for debugging common problems encountered during ADC implementation?
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值