• 大小: 6KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: 其他
  • 标签: stm32  adc  

资源简介

基于stm32f407zgt6的四通道adc采样,使用DMA,自己写的,可使用。

资源截图

代码片段和文件信息

#include “include.h“
__IO uint16_t AD_Value[N][M]={0}; //用来存放 ADC 转换结果,也是 DMA 的目标地址
float final[M]={0};//多次取平均之后的值
float temp[M]={0};//代入公式后的值
float all[M]={0};
float all_begin[M]={0};
int adc_flag=0;
int receive_flag=0;

void adc_Init(void){
  GPIO_InitTypeDef    GPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef       ADC_InitStructure;
DMA_InitTypeDef  DMA_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOAENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2ENABLE);/*DMA2的时钟使能*/

RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1ENABLE); //ADC1 复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1DISABLE); //复位结束

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA&GPIO_InitStructure);

DMA_DeInit(DMA2_Stream0);
while(DMA_GetCmdStatus(DMA2_Stream0)!=DISABLE);/*等待DMA可以配置*/
DMA_InitStructure.DMA_Channel=DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralbaseAddr= (uint32_t)ADC1_base+0x4C;/*外设地址*/
DMA_InitStructure.DMA_Memory0baseAddr=(u32)AD_Value;
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize=N*M;//数据传输的数量
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=DISABLE;
DMA_InitStructure.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst=DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0&DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0ENABLE);


ADC_DeInit();//将外设 ADC1 的全部寄存器重设为缺省值
ADC_CommonInitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler=ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode=ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);

ADC_InitStructure.ADC_Resolution=ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode=ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_NbrOfConversion=M;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;

ADC_Init(ADC1&ADC_InitStructure);

ADC_RegularChannelConfig(ADC1ADC_Channel_11ADC_SampleTime_480Cycles);//PA1  
ADC_RegularChannelConfig(ADC1ADC_Channel_22ADC_SampleTime_480Cycles);//PA2  

评论

共有 条评论