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
12. Enable Watchdog settings
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
14. Enable ADC Interrupt
Go to System mode > Analog > ADC > NVIC settings > ADC Interrupt enable
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
16. Press below icon for 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:
- STM32CubeIDE
- STM32CubeMx
- Teraterm
Hardware Setup:
- STM32F429IDISCOVERY board
- Mini USB Cable
- 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:
- STM32 HAL Library
- STM32 UM1718 document