cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure and use the STM32’s ADC Analog Watchdog feature

ST AME Support NF
ST Employee

How to configure and use the STM32’s ADC Analog Watchdog feature

1. Introduction

In most of the STM32’s ADC (Analog Digital Converter), there is a feature called the Analog Watchdog. In this article we will learn how to configure it using the STM32CubeIDE tool and how to use it in an application.
 

2. Prerequisites

  • Hardware
    • Micro USB cable used to power the Nucleo board from a host machine and to load the code into the STM32.
    • Nucleo-L496
1745.png
  • Software: STM32CubeIDE
 

3. Theory

In this article we will use an STM32L496 Microcontroller. If you are using another STM32, please refer to the reference manual to check if there are any differences with what is presented in this article.
First, let’s review the Analog Watchdog feature of the ADC. The analog watchdog monitors whether some channels remain within a configured voltage range (window).
1746.png
You can define a low threshold and a high threshold as shown above which will define your “window” or guarded area. In this article we will use the AWD1 (Analog Watchdog 1) and enable it on a specific analog channel. We will use PA4 as the analog channel that will enable AED1. We will define a low and high threshold. A full range conversion for a 12-bit ADC bit is 4096.
For this article we will use the following thresholds:
High Threshold: 3000
Low Threshold: 1000
The AWD1 analog watchdog status bit is set if the analog voltage converted by the ADC is below the  low threshold or above the high threshold. A potentiometer will be connected and used on PA4 to test the detection of the analog watchdog. We will enable the interrupt on the AWD1 to generate an interrupt if the converted value is outside the Analog Watchdog Window. If the converted value on the channel is outside the window, we will turn on the Nucleo board’s LEDs LD2 and LD3.

 

4. Steps 

  1. Open STM32CubeIDE

  2. Create a new project using the NUCLEO-L496ZG board


1747.png
 
  1. Give a name to the project


1748.png
 
  1. Initialize all peripherals with their default mode settings by clicking on Yes

1749.png
 
  1. Configure ADC1

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”.
1750.png

  1. Enable ADC1 Channel 9 which is an alternate function for PA4

1751.png

  1. Configure ADC1 Parameters settings

We will enable the continuous conversion mode of the ADC1 so that the channel is continuously converted. We also want to overwrite the data so that no overrun situation occurs because we do not want to have to read the converted value after every conversion. We will keep the rest of the configuration by default.
1752.png
 
  1. Change the sampling Time of channel 9

1753.png
 
  1. Enable Analog Watchdog 1

1754.png
 
  1. Configure the Analog Watchdog 1

For this configuration we will select a single regular channel watchdog mode on channel 9 that we have previously enabled. As described before we will use the following threshold for the analog watchdog window:
High Threshold: 3000
Low Threshold: 1000
We will also enable the Interrupt mode so that an interrupt is generated when the converted channel is outside the defined window.
1755.png
  1. Enable ADC1 Interrupt

We are going to use the call back function of the ADC analog watchdog so interrupts need to be enabled for ADC1.
1756.png
 
  1. Generate Code

Save the project, that will also generate the code.
 
  1. Add code

We will now add code to turn on an LED when the converted channel is outside the analog watchdog window.
In main.c, add the declaration of these variables:
/* 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 */

In USER CODE SECTION 4 add the call back function for the Analog WatchDog mode of the ADC:
 
/* 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 */
 
  1. Prepare your HW setup

Connect a potentiometer to PA4 of your Nucleo board so that you can change the voltage on the pin from VSS to VDD.
 
  1. Build the project, enter debug mode and run the code

1757.png
After the project is built, enter a debug session and then click on the Resume icon to run the code.
1758.png
1759.png
Now if you change the potentiometer position you will see that the LEDs will turn on and off depending on whether the converted channel is outside the analog watchdog window or not.
 

5. Links

STM32CubeIDE - Integrated Development Environment for STM32 - STMicroelectronics
NUCLEO-L496ZG - STM32 Nucleo-144 development board with STM32L496ZG MCU, supports Arduino, ST Zio and morpho connectivity - STMicroelectronics
STM32L47xxx, STM32L48xxx, STM32L49xxx and STM32L4Axxx advanced Arm®-based 32-bit MCUs - Reference manual
Datasheet - STM32L496xx - Ultra-low-power Arm® Cortex®-M4 32-bit MCU+FPU, 100 DMIPS, up to 1 MB Flash, 320 KB SRAM, USB OTG FS, audio, external SMPS
 
Comments
Tobe
Senior III

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:

https://community.st.com/s/question/0D53W00002EjYk8SAF/what-is-the-accuracy-of-the-adc-watchdog-stm32f303

Any ideas?

NMoha.2
Associate

!!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);

}

Version history
Last update:
‎2021-12-01 09:35 PM
Updated by: