16Khz音频定时触发采样DMA存储过程

本文介绍STM32 ADC配置流程,包括校准、通道设置、触发源配置及DMA使用方法。同时,讲解了DMA通道配置步骤,以及TIM15作为外部触发源的应用。

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

一、AD Setting

1、Calibration (ADCAL)


2、设定ADC Chennel与SCANDIR等,在低功耗应用方案,选择PCLK/4,并设置SMP(tCONV = Sampling time + 12.5 x ADC clock cycles)

3、AD设置为Single conversion mode (CONT=0)

In Single conversion mode, the ADC performs a single sequence of conversions, converting
all the channels once. This mode is selected when CONT=0 in the ADC_CFGR1 register.
Conversion is started by either:
● Setting the ADSTART bit in the ADC_CR register
● Hardware trigger event
Inside the sequence, after each conversion is complete:
● The converted data are stored in the 16-bit ADC_DR register
● The EOC (end of conversion) flag is set
● An interrupt is generated if the EOCIE bit is set
After the sequence of conversions is complete:
● The EOSEQ (end of sequence) flag is set
● An interrupt is generated if the EOSEQIE bit is set
Then the ADC stops until a new external trigger event occurs or the ADSTART bit is set
again.

4、Starting conversions (ADSTART)

Software starts ADC conversions by setting ADSTART=1.
When ADSTART is set, the conversion:
● Starts immediately if EXTEN = 0x0 (software trigger)
● At the next active edge of the selected hardware trigger if EXTEN  0x0
The ADSTART bit is also used to indicate whether an ADC operation is currently ongoing. It
is possible to re-configure the ADC while ADSTART=0, indicating that the ADC is idle.
The ADSTART bit is cleared by hardware:
● In single mode with software trigger (CONT=0, EXTSEL=0x0)
– At any end of conversion sequence (EOSEQ=1)
● In all cases (CONT=x, EXTSEL=x)
– After execution of the ADSTP procedure invoked by software (see
Section 12.4.11: Stopping an ongoing conversion (ADSTP) on page 179.
Note: 1 In continuous mode (CONT=1), the ADSTART bit is not cleared by hardware when the
EOSEQ flag is set because the sequence is automatically relaunched.
2 When hardware trigger is selected in single mode (CONT=0 and EXTSEL  0x00),
ADSTART is not cleared by hardware when the EOSEQ flag is set. This avoids the need for
software having to set the ADSTART bit again and ensures the next trigger event is not
missed.

5、Conversion on external trigger and trigger polarity (EXTSEL,EXTEN)

EXTEN[1:0] = 01 -> rising edge

EXTSEL[2:0] = 100 ->TIM15_TRGO

6、RES设置解析度12bit与数据右对齐方式

7、Managing converted data using the DMA ==> DMAEN=1,DMA circular mode (DMACFG=1)

Since all converted channel values are stored in a single data register, it is efficient to use
DMA when converting more than one channel. This avoids losing the conversion data
results stored in the ADC_DR register.
When DMA mode is enabled (DMAEN bit set to 1 in the ADC_CFGR1 register), a DMA
request is generated after the conversion of each channel. This allows the transfer of the

converted data from the ADC_DR register to the destination location selected by the
software.


二、DMA Setting

The following sequence should be followed to configure a DMA channelx (where x is the
channel number).
1. Set the peripheral register address in the DMA_CPARx register. The data will be
moved from/ to this address to/ from the memory after the peripheral event.
2. Set the memory address in the DMA_CMARx register. The data will be written to or
read from this memory after the peripheral event.
3. Configure the total number of data to be transferred in the DMA_CNDTRx register.
After each peripheral event, this value will be decremented.
4. Configure the channel priority using the PL[1:0] bits in the DMA_CCRx register
5. Configure data transfer direction, circular mode, peripheral & memory incremented
mode, peripheral & memory data size, and interrupt after half and/or full transfer in the
DMA_CCRx register
6. Activate the channel by setting the ENABLE bit in the DMA_CCRx register.
As soon as the channel is enabled, it can serve any DMA request from the peripheral
connected on the channel.


三、TIM SETTING

1、设置TIM15为16Khz触发update中断;

2、设置MMS[2:0]=010

Update - The update event is selected as trigger output (TRGO).

3、使能中断


注:

1、相应Clk需Enable;

2、DMA的buf满了之后会触发中断,从中获取数据;



### STM32 定时触发 ADC 采样并使用 DMA 进行数据存储 #### 配置定时器用于触发 ADC 采样 对于 STM32G0 系列单片机,在配置定时器以实现每毫秒一次的触发来启动 ADC 转换的过程中,需要设定 TIM3 的参数。具体来说: - 设置自动重装载预分频系数使得计数频率达到期望值。 - 启用更新事件以便能够周期性地发出触发信号给 ADC。 ```c // 初始化TIM3为1KHz频率输出 static void MX_TIM3_Init(void) { TIM_MasterConfigTypeDef sMasterConfig; htim3.Instance = TIM3; htim3.Init.Prescaler = SystemCoreClock / 1000 - 1; // 假设系统核心时钟为8MHz,则此处应调整Prescaler使其实现1ms间隔 htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 999; htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim3) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) { Error_Handler(); } } ``` 上述代码初始化了 TIM3 并将其配置为每隔一毫秒产生一个更新事件[^1]。 #### 配置 ADC 和 DMA 为了确保当定时触发时可以执行多通道转换并将结果传输到内存中,还需要正确设置 ADC 及其关联的 DMA 请求。这里假设已经定义好了 `adc_handle` 来表示 ADC 设备实例以及相应的 DMA 流对象 `hdma_adc`。 ```c // 初始化ADC及其DMA请求 static void MX_ADC_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; hadc.Instance = ADC1; hadc.Init.Resolution = ADC_RESOLUTION_12B; hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc.Init.ScanConvMode = ENABLE; /* Enable scan to convert multiple channels */ hadc.Init.ContinuousConvMode = DISABLE;/* Single conversion mode */ hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO; hadc.Init.DMAContinuousRequests = ENABLE; hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV; if (HAL_ADC_Init(&hadc) != HAL_OK) { Error_Handler(); } /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_4 | ADC_CHANNEL_5 | ADC_CHANNEL_6; // Example of three-phase current sampling on channels 4, 5, and 6 sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5; if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) { Error_Handler(); } __HAL_LINKDMA(&hadc,DMA_Handle,&hdma_adc); } void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){ // 当DMA完成ADC数据转移后的操作 } ``` 这段代码展示了如何配置 ADC 使用外部触发源(即来自 TIM3 更新事件),并且启用了扫描模式和支持连续 DMA 请求的功能。还指定了要使用的模拟输入通道,并设置了样本时间。 #### 开始 ADC 转换和 DMA 操作 一旦完成了硬件资源的初始化工作之后,则可以通过调用下面的方法开启实际的数据采集过程: ```c if(HAL_ADC_Start_DMA(&hadc,(uint32_t*)aAdcValues,BUFFER_SIZE)!= HAL_OK){ Error_Handler(); } ``` 此函数会启动由定时器控制下的 ADC 多次测量流程,并利用 DMA 自动把获取的结果存入指定缓冲区 `aAdcValues[]` 中直到填满整个数组为止[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值