应用示例
ADC 配置代码
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] ADC configuration
- * @param None
- * @retval None
- */
- static void ADC_Config(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- ADC_AnalogWDGConfTypeDef AnalogWDGConfig;
- /* Configuration of ADCx init structure: ADC parameters and regular group */
- AdcHandle.Instance = ADCx;
- AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; /* Asynchronous
- clock mode, input ADC clock not divided */
- AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit
- resolution for converted data */
- AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Rightalignment for converted data */
- AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer
- disabled (ADC conversion on only 1 channel: channel set on rank 1) */
- AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag
- picked-up to indicate conversion end */
- AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed
- conversion feature disabled */
- AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous
- mode disabled to have only 1 conversion at each conversion trig */
- AdcHandle.Init.NbrOfConversion = 1; /* Parameter
- discarded because sequencer is disabled */
- AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter
- discarded because sequencer is disabled */
- AdcHandle.Init.NbrOfDiscConversion = 1; /* Parameter
- discarded because sequencer is disabled */
- AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO; /* Timer 3
- external event triggering the conversion */
- AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; /* Parameter
- discarded because software trigger chosen */
- AdcHandle.Init.DMAContinuousRequests = ENABLE; /* DMA circular
- mode selected */
- AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is
- overwritten with the last conversion result in case of overrun */
- AdcHandle.Init.OversamplingMode = DISABLE; /* No
- oversampling */
- if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
- {
- /* ADC initialization error */
- Error_Handler();
- }
- /* Configuration of channel on ADCx regular group on sequencer rank 1 */
- /* Note: Considering IT occurring after each ADC conversion if ADC */
- /* conversion is out of the analog watchdog window selected (ADC IT */
- /* enabled), select sampling time and ADC clock with sufficient */
- /* duration to not create an overhead situation in IRQHandler. */
- sConfig.Channel = ADC_CHANNEL_5; /* Sampled channel number */
- sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number
- ADCx_CHANNEL */
- sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock
- cycles unit) */
- sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
- sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
- sConfig.Offset = 0; /* Parameter discarded because
- offset correction is disabled */
- if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
- {
- /* Channel Configuration Error */
- Error_Handler();
- }
- /* Set analog watchdog thresholds in order to be between steps of DAC */
- /* voltage. */
- /* - High threshold: between DAC steps 1/2 and 3/4 of full range: */
- /* 5/8 of full range (4095 <=> Vdda=3.3V): 2559<=> 2.06V */
- /* - Low threshold: between DAC steps 0 and 1/4 of full range: */
- /* 1/8 of full range (4095 <=> Vdda=3.3V): 512 <=> 0.41V */
- /* Analog watchdog 1 configuration */
- AnalogWDGConfig.WatchdogNumber = ADC_ANALOGWATCHDOG_1;
- AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_ALL_REG;
- AnalogWDGConfig.Channel = ADCx_CHANNELa;
- AnalogWDGConfig.ITMode = ENABLE;
- AnalogWDGConfig.HighThreshold = (RANGE_12BITS * 5/8);
- AnalogWDGConfig.LowThreshold = (RANGE_12BITS * 1/8);
- if (HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig) != HAL_OK)
- {
- /* Channel Configuration Error */
- Error_Handler();
- }
- }
如上图所示, AnalogWDGConfig 结构体中分别使能了模拟看门狗及中断,设置了电压的上下阈值 HighThreshold 和
LowThreshold。模拟参考电压为 3.3V, 以上代码设置的下限电压阈值为 3.3*1/8=0.41V,上限电压阈值为 3.3*5/8=2.06V。
|