adc.c- #include "adc.h"
- #include "delay.h"
- void Adc_Init(void)
- {
- /*define structure variables*/
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStructure;
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- /* Enable ADC1 clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_ADC12,ENABLE);
- /* Configure the ADC clock */
- /*配置ADC时钟为PLL时钟*/
- RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);
- /*配置GPIO为PA2,Pin3 模拟输入*/
- GPIO_InitStructure.GPIO_Pin = ADC1_IN3_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- GPIO_Init(ADC1_IN3_PORT,&GPIO_InitStructure);
- /*默认初始化ADC结构体*/
- ADC_StructInit(&ADC_InitStructure);
- /* Calibration procedure */
- ADC_VoltageRegulatorCmd(ADC1, ENABLE);
- delay_us(10);
- /*ADC_CalibrationMode_Single: to select the calibration for single channel*/
- ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));//waiting finish
- //ADC_CommonInitTypeDef 主要为双重ADC配置,我们只需要配置ADC为独立模式和异步时钟即可
- ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
- ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; //ADC异步时钟模式
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
- ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
- ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
- ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;//连续转换失能
- ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //精度为12位
- //外部触发事件6,即EXTI11中断线
- ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_6;
- //外部事件上升沿触发,也可选择下降沿,需要与EXTI对应
- ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_RisingEdge;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
- ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
- ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
- ADC_InitStructure.ADC_NbrOfRegChannel = 1;//the number of ADC channels that will be converted
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /*配置规则通道参数,设置指定ADC的规则通道,一个序列,采样时间:ADC1通道3,采样时间 1.5个周期*/
- ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_1Cycles5);
- //使能ADC1
- ADC_Cmd(ADC1,ENABLE);
- /* wait for ADRDY */
- while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
- }
|