/************************************************************************************************ ** Function name : ** Description : ** ** Input : ** Output : ** Return : ** Others : ** ************************************************************************************************/ void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; //GPIOC Periph clock enable RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //ADC1 Periph clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Configure ADC Channel1 as analog input GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); //ADC1 Configuration //ADCs DeInit ADC_DeInit(ADC1); //Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); //转换ADC1通道,周期采样时间 ADC_ChannelConfig(ADC1, ADC_Channel_1 , ADC_SampleTime_28_5Cycles); //ADC Calibration-ADC校准 ADC_GetCalibrationFactor(ADC1); //Enable the auto delay feature-启用自动延时功能 ADC_WaitModeCmd(ADC1, ENABLE); //使能ADC1 ADC_AutoPowerOffCmd(ADC1, ENABLE); //Enable ADCperipheral[PerIdx]-启用ADC外设[PerIdx] ADC_Cmd(ADC1, ENABLE); //Wait the ADCEN falg-等待ADC1校准完成 while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //ADC1 regular Software Start Conv ADC_StartOfConversion(ADC1); }
//============================================================================== // ADC 初始化 //============================================================================== void F_InitialADC(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // 聲明定時器初始化結構體 TIM_OCInitTypeDef TIM_OCInitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(P_VR2_GPIO_CLK, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* TIM3 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Configure ADC Channel0 as analog input */ GPIO_InitStructure.GPIO_Pin = P_VR2_PIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(P_VR2_GPIO_PORT, &GPIO_InitStructure); /* TIM3 Configuration *******************************************************/ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM3 TRGO selection */ TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); /* ADC2 Configuration *******************************************************/ /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolutuion equal to 8 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 0 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, P_VR2_Channel , ADC_SampleTime_28_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the auto delay feature */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* TIM2 enable counter */ TIM_Cmd(TIM3, ENABLE); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief ADC1 configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOC Peripheral clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Peripheral clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel11(for STM320518 EVAL) and Chanenl10(for STM32072B EVAL) as analog input */ #ifdef USE_STM320518_EVAL GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; #else GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; #endif /* USE_STM320518_EVAL */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolution equal to 12 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 11 and channel10 with 28.5 Cycles as sampling time */ #ifdef USE_STM320518_EVAL ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles); #else ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_28_5Cycles); #endif /* USE_STM320518_EVAL */ /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable OVR interupt */ ADC_ITConfig(ADC1, ADC_IT_OVR, ENABLE); /* Configure and enable ADC1 interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); #if defined (ADC_LOWPOWER) /* Enable the wait conversion mode */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); #endif /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
/** * @brief ADC1 channel11 configuration * @param None * @retval None */ void ADC1_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Configure ADC Channel11 as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* TIM3 Configuration *******************************************************/ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM3 TRGO selection */ TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); /* ADC1 Configuration *******************************************************/ /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 11 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); #if defined (ADC_LOWPOWER) /* Enable the wait conversion mode */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); #endif /* Enable OVR interupts */ ADC_ITConfig(ADC1, ADC_IT_OVR, ENABLE); /* Configure and enable ADC1 interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* TIM2 enable counter */ TIM_Cmd(TIM3, ENABLE); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }