已在板子上正常运行读取了
adc.c
- #include "adc.h"
- #include "math.h"
- __IO uint16_t ADC1ConvertedValue[4]={0,0,0,0};//定义的采集数据寄存数组
- float AD_Vaule[4] = {0}; // AD转换值
- void ADC1_DMA_Config(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- //打开ADC1,DMA2,GPIOA时钟,DMA1不行,没有ADC的通道,只能用DMA2
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
- //通道选择,ADC1为0和4,ADC2为2和3,ADC3为0和1,
- DMA_InitStructure.DMA_Channel = DMA_Channel_0;
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
- 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;
- // 外设基址为:ADC 数据寄存器地址
- DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADCx->DR;//ADCx一律宏定义成了ADC1
-
- // 存储器地址
- DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC1ConvertedValue;
-
- // 缓冲区大小,应该等于数据目的地的大小
- DMA_InitStructure.DMA_BufferSize = 4;//几个通道就写几
-
- // 外设寄存器只有一个,地址不用递增
- DMA_Init(DMA2_Stream0, &DMA_InitStructure);
- DMA_Cmd(DMA2_Stream0, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//A4/A5/A6/A7
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
- 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_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
- ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
-
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfConversion=4;//
- ADC_Init(ADCx, &ADC_InitStructure);
- //一共四个通道的设置
- ADC_RegularChannelConfig(ADCx, ADC_Channel_4, 1, ADC_SampleTime_3Cycles);
- ADC_RegularChannelConfig(ADCx, ADC_Channel_5, 2, ADC_SampleTime_3Cycles);
- ADC_RegularChannelConfig(ADCx, ADC_Channel_6, 3, ADC_SampleTime_3Cycles);
- ADC_RegularChannelConfig(ADCx, ADC_Channel_7, 4, ADC_SampleTime_3Cycles);
- ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE);
- ADC_DMACmd(ADCx, ENABLE);
- ADC_Cmd(ADCx, ENABLE);
- }
- void ADC1_GetVaule(void)
- {
- u8 i = 0;
-
- u32 AD[4] = {0};
-
- for(i=0;i<30;i++) // 进行30次转换
- {
- AD[0] =AD[0] + ADC1ConvertedValue[0]; // 求和
- AD[1] =AD[1] + ADC1ConvertedValue[1]; // 求和
- AD[2] =AD[2] + ADC1ConvertedValue[2]; // 求和
- AD[3] =AD[3] + ADC1ConvertedValue[3]; // 求和
- } // 启动转换
- AD[0] =AD[0]/30;//平均值更稳定
- AD[1] =AD[1]/30;
- AD[2] =AD[2]/30;
- AD[3] =AD[3]/30;
- // while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )); // 等待转换完毕
- AD_Vaule[0] = (float)AD[0]/4096*3.3; // //转换成电压值
- AD_Vaule[1] = (float)AD[1]/4096*3.3; // //转换成电压值
- AD_Vaule[2] = (float)AD[2]/4096*3.3; // //转换成电压值
- AD_Vaule[3] = (float)AD[3]/4096*3.3; // //转换成电压值
-
- // AD_Vaule[0] = AD_Vaule[0] / 30; // 取平均值
- // AD_Vaule[1] = AD_Vaule[1] / 30; // 取平均值
- // AD_Vaule[2] = AD_Vaule[2] / 30; // 取平均值
- // AD_Vaule[3] = AD_Vaule[3] / 30; // 取平均值
- //
- }
复制代码
adc.h
- #ifndef __ADC_H
- #define __ADC_H
- #include "stm32f4xx.h"
- #define ADCx ADC1
- /*------------------------------- AD引脚 --------------------------------*/
- //#define ADC1_CH6_PIN GPIO_Pin_6 // ADC1 CH6 引脚
- //#define ADC1_CH6_PORT GPIOA // ADC1 CH6 端口
- //#define ADC1_CH6_CLK RCC_AHB1Periph_GPIOA // ADC1 CH6 时钟
- /*------------------------------ 函数声明 --------------------------------*/
-
- void ADC1_DMA_Config(void); // 初始化ADC1
- void ADC1_GetVaule(void); // 进行AD转换,采集电压
- #endif // __ADC_H
复制代码
|