#include "ADC_nofilter.h"
__IO uint16_t ADC_ConvertedValue[120];
void ADC1_GPIOA_Config(void)
{
GPIO_InitTypeDef GPIO_InitTypeStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitTypeStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitTypeStructure);
}
void ADC1_GPIOB_Config(void)
{
GPIO_InitTypeDef GPIO_InitTypeStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitTypeStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitTypeStructure);
}
void ADC1_Mode_Config(void)
{
//结构体变量定义
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_InitTypeDef DMA_InitTypeStructure;
ADC_InitTypeDef ADC_InitTypeStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//volatile的作用就是保证指令不会因为编译器优化而省略
//DMA配置
DMA_DeInit(DMA1_Channel1);
DMA_InitTypeStructure.DMA_BufferSize=AD_DATASize;
DMA_InitTypeStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_InitTypeStructure.DMA_M2M=DMA_M2M_Disable;
DMA_InitTypeStructure.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue;//说是32位的实际上就是用到低16位
DMA_InitTypeStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitTypeStructure.DMA_Mode=DMA_Mode_Circular;
DMA_InitTypeStructure.DMA_PeripheralBaseAddr=(u32)(&(ADC1->DR));
DMA_InitTypeStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
DMA_InitTypeStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitTypeStructure.DMA_Priority=DMA_Priority_High;
DMA_Init(DMA1_Channel1,&DMA_InitTypeStructure);
DMA_Cmd(DMA1_Channel1,DISABLE);//暂时先关闭DMA,为了延时开启AD采样
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
DMA_ClearITPendingBit(DMA1_IT_TC1);
//DMA_NVIC配置
NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStructure);
//ADC配置
ADC_InitTypeStructure.ADC_ContinuousConvMode=ENABLE;
ADC_InitTypeStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitTypeStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitTypeStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitTypeStructure.ADC_NbrOfChannel=3;
ADC_InitTypeStructure.ADC_ScanConvMode=ENABLE;//因为要同时开启两个adc,所以要开启扫描模式了
ADC_Init(ADC1,&ADC_InitTypeStructure);
//设置ADC时钟分频
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
//ADC通道配置
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,3,ADC_SampleTime_239Cycles5);
//ADC_DMA使能
ADC_DMACmd(ADC1,DISABLE);
ADC_Cmd(ADC1,ENABLE);
//校准复位
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));//为何没用==RESET?
//校准启动
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//采用软件启动方式
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
void ADC1_ENABLE(void)
{
DMA_Cmd(DMA1_Channel1,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel1,AD_DATASize);
DMA_Cmd(DMA1_Channel1,ENABLE);
ADC_DMACmd(ADC1,ENABLE);
}
void ADC1_DISABLE(void)
{
DMA_Cmd(DMA1_Channel1,DISABLE);
ADC_DMACmd(ADC1,DISABLE);
}
void ADC1_Config(void)
{
ADC1_GPIOA_Config();
ADC1_GPIOB_Config();
ADC1_Mode_Config();
}
|