STM32-Peripheral’s-ADC: Watchdog Mode

STM32-Peripheral’s-ADC: Watchdog Mode

Analog watchdog:

The AWD analog watchdog status bit is set if the analog voltage converted by the ADC is
below a lower threshold or above a higher threshold. These thresholds are programmed in
the 12 least significant bits of the ADC_HTR and ADC_LTR 16-bit registers. An interrupt can
be enabled by using the AWDIE bit in the ADC_CR1 register.
The threshold value is independent of the alignment selected by the ALIGN bit in the
ADC_CR2 register. The analog voltage is compared to the lower and higher thresholds
before alignment.

For this blog we are going to use ADC in trigger mode (Single Channel) , for demonstration purpose we will configure one DAC, output of DAC will be Input of ADC channel & Timer 2 is used to trigger adc conversion.

Creating STM32 executable projects steps are available on this link , please follow steps 1 to 10 as per blog, here we will start from step 11. PA9 & PA10 Pin used for debugging purpose as UART Tx & UART Rx respectively. to learn more about ADC in scan mode click here.

11. Enable ADC Functionality

Go to System mode > Analog > ADC > Enable IN0

Figure 1: Enable ADC

12. Enable Watchdog settings

Figure 2: Enable Watchdog

13. Enable external trigger conversion mode

Go to System mode > Analog > ADC > Parameter settings > External Trigger Conversion Source > Timer 2 Trigger Out Event

Go to System mode > Analog > ADC > Parameter settings > External Trigger Conversion Edge > Trigger detection on the rising edge

Figure 3: Enable external conversion mode

14. Enable ADC Interrupt

Go to System mode > Analog > ADC > NVIC settings > ADC Interrupt enable

Figure 4: Enable ADC Interrupt

15. Enable timer 2 for internal ADC trigger

Go to System mode > Timers > Tim2 > Clock Source > Enable Internal Clock

Go to System Mode > timers > Tim2 > Parameter Settings > Set timer according to your requirement

Go to System Mode > timers > Tim2 > Parameter Settings > Trigger Event Selection > select Update Event

Figure 5: Enable Timer 2 for internal ADC Interrupt

16. Press below icon for code generation

Figure 6: Cube IDE Code Generation

17. Sample Code

static volatile U8  adc_wd_flag = 0;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  if (hadc->Instance->SR & ADC_SR_AWD)
  {
    adc_wd_flag = 1;
  }
}

  HAL_ADC_Start_IT(&hadc1);	//Enable ADC in interrupt mode 
  HAL_TIM_Base_Start(&htim2);//Enable timer 2 for adc internal trigger.

while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	HAL_Delay(1000);	//1000 ms delay
	RTC_CalendarShow(aShowTime, aShowDate);
	uart1_printf_debug(YEL"time = %s\n",aShowTime);
	HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_data);
	HAL_Delay(500);	//500 ms delay
	uart1_printf_debug(GRN"DAC Value = %d  === ",dac_data);

	if (adc_wd_flag == 1)
	{
      uart1_printf_debug(RED"Watchdag Alarm\n");
      adc_wd_flag = 0;
	}

	dac_data++;
	if (dac_data > 2100)
		dac_data = 2048;
  }

17. Debug Output

Software Tools:
  1. STM32CubeIDE
  2. STM32CubeMx
  3. Teraterm
Hardware Setup:
  1. STM32F429IDISCOVERY board
  2. Mini USB Cable
  3. Jumper wire
Conclusion:

Successfully demonstrated ADC functionality in Watchdog mode (Single Channel), for testing purpose we have used DAC to connect its output to ADC & UART for debug.

References:
  1. STM32 HAL Library
  2. STM32 UM1718 document
Similar topics:
  1. https://kalapiinfotech.in/stm32-cube-ide-freertos-code-generation-using-cubemx/
  2. STM32 Peripherals: GPIO
  3. STM32 Peripherals: DAC
  4. STM32 Peripherals: ADC using Polling Mode

Leave a Reply

Your email address will not be published. Required fields are marked *