/** * @brief 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.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = ADC_SCAN_DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */ #if defined ADC_TRIGGER_FROM_TIMER AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */ #else AdcHandle.Init.ContinuousConvMode = ENABLE; /* Continuous mode to have maximum conversion speed (no delay between conversions) */ #endif 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 */ #if defined ADC_TRIGGER_FROM_TIMER AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_Tx_TRGO; /* Trig of conversion start done by external event */ #else AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */ #endif 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 = ADCx_CHANNELa; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_41CYCLES_5; 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.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(); } }
/** * @brief ADC configuration * @param None * @retval None */ static void ADC_Config(void) { ADC_ChannelConfTypeDef sConfig; ADC_AnalogWDGConfTypeDef AnalogWDGConfig; /* Configuration of AdcHandle init structure: ADC parameters and regular group */ AdcHandle.Instance = ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; /* Sequencer will convert the number of channels configured below, successively from the lowest to the highest channel number */ AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; AdcHandle.Init.LowPowerAutoWait = DISABLE; AdcHandle.Init.LowPowerAutoPowerOff = DISABLE; #if defined(ADC_TRIGGER_FROM_TIMER) AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */ #else AdcHandle.Init.ContinuousConvMode = ENABLE; /* Continuous mode to have maximum conversion speed (no delay between conversions) */ #endif AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */ #if defined(ADC_TRIGGER_FROM_TIMER) AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_Tx_TRGO; /* Trig of conversion start done by external event */ AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; #else AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */ AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because trig of conversion by software start (no external event) */ #endif AdcHandle.Init.DMAContinuousRequests = ENABLE; /* ADC-DMA continuous requests to match with DMA configured in circular mode */ AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; AdcHandle.Init.SamplingTimeCommon = ADC_SAMPLETIME_41CYCLES_5; 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 = ADCx_CHANNELa; sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; 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.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(); } }
/* ADC init function */ void MX_ADC_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* Peripheral clock enable */ __ADC1_CLK_ENABLE(); /* DMA controller clock enable */ __DMA1_CLK_ENABLE(); /**ADC GPIO Configuration PA0-WKUP ------> ADC_IN0 */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral DMA init*/ DmaHandle.Instance = DMA1_Channel1; DmaHandle.Init.Request = DMA_REQUEST_0; DmaHandle.Init.Direction = DMA_PERIPH_TO_MEMORY; DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; DmaHandle.Init.MemInc = DMA_MINC_ENABLE; DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; DmaHandle.Init.Mode = DMA_CIRCULAR; DmaHandle.Init.Priority = DMA_PRIORITY_MEDIUM; HAL_DMA_DeInit(&DmaHandle); HAL_DMA_Init(&DmaHandle); __HAL_LINKDMA(&AdcHandle, DMA_Handle, DmaHandle); /* DMA interrupt init */ HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0); HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); ADC_ChannelConfTypeDef sConfig; ADC_AnalogWDGConfTypeDef AnalogWDGConfig; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ AdcHandle.Instance = ADC1; AdcHandle.Init.OversamplingMode = DISABLE; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1; AdcHandle.Init.Resolution = ADC_RESOLUTION8b; AdcHandle.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; AdcHandle.Init.ScanDirection = ADC_SCAN_DIRECTION_UPWARD; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ContinuousConvMode = ENABLE; AdcHandle.Init.DiscontinuousConvMode = DISABLE; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV; AdcHandle.Init.Overrun = OVR_DATA_PRESERVED; AdcHandle.Init.LowPowerAutoWait = DISABLE; AdcHandle.Init.LowPowerFrequencyMode = DISABLE; AdcHandle.Init.LowPowerAutoOff = DISABLE; HAL_ADC_Init(&AdcHandle); HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED); sConfig.Channel = ADC_CHANNEL_0; HAL_ADC_ConfigChannel(&AdcHandle, &sConfig); /**Configure the analog watchdog */ AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG; AnalogWDGConfig.Channel = ADC_CHANNEL_0; AnalogWDGConfig.ITMode = ENABLE; AnalogWDGConfig.HighThreshold = 0; AnalogWDGConfig.LowThreshold = 0; HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig); HAL_ADC_Start_DMA(&AdcHandle, &resultDMA, DMA_BUFFER_SIZE); }
/** * @brief 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_SYNC_PCLK_DIV4; AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */ AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; AdcHandle.Init.LowPowerAutoWait = DISABLE; 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_EXTERNALTRIGCONV_Tx_TRGO; /* Trig of conversion start done by external event */ AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; 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 widow selected (ADC IT */ /* enabled), select sampling time and ADC clock with sufficient */ /* duration to not create an overhead situation in IRQHandler. */ sConfig.Channel = ADCx_CHANNELa; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; 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); HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig); }