STM32F407ZGT6实现两路1Mhz连续采样连续存储库函数程序,ADC选择ADC1的APB0和APB1IO口
时间: 2024-03-05 15:54:50 浏览: 55
STM32F407ZGT6标准库学习-1-工程模板
以下是一个简单的库函数程序,可以实现两路1MHz连续采样和连续存储,同时使用了ADC1的APB2和APB1 IO口。
```c
#include "stm32f4xx.h"
#define ADC_BUFFER_SIZE 1024 // ADC缓冲区大小
#define ADC_SAMPLE_RATE 1000000 // ADC采样率
volatile uint16_t adc_buffer[ADC_BUFFER_SIZE]; // ADC缓冲区
void ADC_DMA_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
// 使能GPIOA、GPIOB、DMA2和ADC1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_ADC1, ENABLE);
// 配置PA0和PB1为模拟输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// ADC参数配置
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // 连续转换模式
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; // 外部触发由定时器1触发
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; // 上升沿触发
ADC_InitStructure.ADC_NbrOfConversion = 2; // 要转换的通道数
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // 12位精度
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // 自动扫描模式
ADC_Init(ADC1, &ADC_InitStructure);
// 配置DMA参数
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_buffer;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = ADC_BUFFER_SIZE * 2; // 要传输的数据量是ADC_BUFFER_SIZE * 2(每次转换两个通道)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
// 配置ADC DMA请求
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
// 配置ADC通道1和通道2
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_3Cycles);
// 使能DMA和ADC
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_Cmd(ADC1, ENABLE);
// 配置定时器1为1000000 Hz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / ADC_SAMPLE_RATE - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
// 配置定时器1触发ADC
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
// 启动定时器1
TIM_Cmd(TIM1, ENABLE);
}
```
这个程序使用了DMA和定时器来实现1MHz的采样率。在DMA的帮助下,ADC每次转换将两个通道的值存储在一个循环缓冲区中。定时器用于触发ADC,以确保在每个采样周期内进行转换。其中,ADC1的APB2时钟用于ADC外设本身的工作,而ADC1的APB1时钟用于ADC通道的IO口。
阅读全文