on 2021-12-01 09:35 PM
We will do a continuous conversion mode on channel 9 which is PA4.
The ADC1 can be found in the Pinout & Configuration Tab of the STM32CubeIDE under “Analog”.
/* USER CODE BEGIN PV */ __IO uint16_t uhADCxConvertedValue = 0; /* Variable to report ADC analog watchdog status: */ /* RESET <=> voltage into AWD window */ /* SET <=> voltage out of AWD window */ uint8_t ubAnalogWatchdogStatus = RESET; /* Set into analog watchdog interrupt callback */ /* USER CODE END PV */
/* USER CODE BEGIN 4 */ /** * @brief Analog watchdog callback in non blocking mode. * @param hadc: ADC handle * @retval None */ void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc) { /* Set variable to report analog watchdog out of window status to main */ /* program. */ uhADCxConvertedValue = HAL_ADC_GetValue(&hadc1); ubAnalogWatchdogStatus = SET; } /* USER CODE END 4 */ We need to add code to start the ADC Conversion. /* USER CODE BEGIN 2 */ if (HAL_ADC_Start_IT(&hadc1) != HAL_OK) { Error_Handler(); } /* USER CODE END 2 */ Now we add code on the main function to turn the LEDs LD2 and LD3 on or off depending on the Analog Watchdog event. /* USER CODE BEGIN WHILE */ while (1) { /* Turn-on/off LED2 and LED3 in function of ADC conversion result */ /* - Turn-off if voltage is into AWD window */ /* - Turn-on if voltage is out of AWD window */ /* Variable of analog watchdog status is set into analog watchdog */ /* interrupt callback */ if (ubAnalogWatchdogStatus == SET) { HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET); } ubAnalogWatchdogStatus = RESET; HAL_Delay(1); /* USER CODE END WHILE */
Hi,
i got some accuracy issues with the watchdog. As i understand, i should never get ADC values that are inside of the window. It kind of works, but near the thresholds it does not.
I already made a post in the forum:
Any ideas?
!!Need some urgent help from STM Engineers and community members!!
ADC Analog Watchdog for STM32G4 MCU.
We have 3 analog filters and ADC analog watchdog filtering configuration (FilteringConfig) is applicable for ADC_AWD1 (Analog watchdog filter for watchdog 1).
And STM provides code as below.
Question: Is there any workaround solution that this Analog watchdog filtering can be implemented for watchdog 2 and 3 as well.
We are in middle of our development, so difficult to make HW changes to realign the ADC mappings.
/**
* @brief Set ADC analog watchdog filtering configuration
* @note On this STM32 series, setting of this feature is conditioned to
* ADC state:
* ADC must be disabled or enabled without conversion on going
* on either groups regular or injected.
* @note On this STM32 series, this feature is only available on first
* analog watchdog (AWD1)
* @rmtoll TR1 AWDFILT LL_ADC_SetAWDFilteringConfiguration
* @param ADCx ADC instance
* @param AWDy This parameter can be one of the following values:
* @arg @ref LL_ADC_AWD1
* @param FilteringConfig This parameter can be one of the following values:
* @arg @ref LL_ADC_AWD_FILTERING_NONE
* @arg @ref LL_ADC_AWD_FILTERING_2SAMPLES
* @arg @ref LL_ADC_AWD_FILTERING_3SAMPLES
* @arg @ref LL_ADC_AWD_FILTERING_4SAMPLES
* @arg @ref LL_ADC_AWD_FILTERING_5SAMPLES
* @arg @ref LL_ADC_AWD_FILTERING_6SAMPLES
* @arg @ref LL_ADC_AWD_FILTERING_7SAMPLES
* @arg @ref LL_ADC_AWD_FILTERING_8SAMPLES
* @retval None
*/
__STATIC_INLINE void LL_ADC_SetAWDFilteringConfiguration(ADC_TypeDef *ADCx, uint32_t AWDy, uint32_t FilteringConfig)
{
/* Prevent unused argument(s) compilation warning */
(void)(AWDy);
MODIFY_REG(ADCx->TR1, ADC_TR1_AWDFILT, FilteringConfig);
}