void adc_init_analog_watchdog (void) { NVIC_InitTypeDef NVIC_InitStructure; //Set up interruts ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE); // [..] A typical configuration Analog Watchdog is done following these steps : // (#) the ADC guarded channel(s) is (are) selected using the // ADC_AnalogWatchdogSingleChannelConfig() function. //Setup single channel function for ADC Channel 4, ADC_BATT_I ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_4); // (#) The Analog watchdog lower and higher threshold are configured using the // ADC_AnalogWatchdogThresholdsConfig() function. ADC_AnalogWatchdogThresholdsConfig(ADC1, I_BATT_TO_ADC(0.1), I_BATT_TO_ADC(0) ); // (#) The Analog watchdog is enabled and configured to enable the check, on one // or more channels, using the ADC_AnalogWatchdogCmd() function. ADC_AnalogWatchdogCmd(ADC1, ENABLE); // (#) Enable the analog watchdog on the selected channel using // ADC_AnalogWatchdogSingleChannelCmd() function ADC_AnalogWatchdogSingleChannelCmd(ADC1, ENABLE); /* Enable and set ADC1_COMP Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = ADC1_COMP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/** * @brief ADC1 channel configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Configure ADC Channel11 as analog input */ #ifdef USE_STM320518_EVAL GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ; #elif defined (USE_STM32072B_EVAL) 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); /* ADC1 DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continuous 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_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel11 and channel10 with 239.5 Cycles as sampling time */ #ifdef USE_STM320518_EVAL ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_239_5Cycles); #elif defined (USE_STM32072B_EVAL) ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles); #endif /* USE_STM320518_EVAL */ /* Analog watchdog config ******************************************/ /* Configure the ADC Thresholds between 1.5V and 2.5V (1861, 3102) */ ADC_AnalogWatchdogThresholdsConfig(ADC1, 3102, 1861); /* Enable the ADC1 single channel */ ADC_AnalogWatchdogSingleChannelCmd(ADC1, ENABLE); ADC_OverrunModeCmd(ADC1, ENABLE); /* Enable the ADC1 analog watchdog */ ADC_AnalogWatchdogCmd(ADC1,ENABLE); /* Select a single ADC1 channel 11 */ #ifdef USE_STM320518_EVAL ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_11); #elif defined (USE_STM32072B_EVAL) ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_AnalogWatchdog_Channel_10); #endif /* USE_STM320518_EVAL */ /* Enable AWD interrupt */ ADC_ITConfig(ADC1, ADC_IT_AWD, 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 ADC1 Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the ADC peripheral */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADRDY flag */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }