STM32 MCU analog watchdog library function settings

The ADC analog watchdog is used to check if the voltage is out of bounds. He has two upper and lower boundaries, which can be set in the registers ADC_HTR and ADC_LTR, respectively. The library function is set using ADC_AnalogWatchdogThresholdsConfig and is very simple whether it is a regular channel or an injection channel.

A watchdog interrupt will be generated when the analog watchdog detects that the voltage is above the upper limit or below the lower limit. To catch this interruption, you can make some countermeasures.

ADC_DR data register

One thing that is special in the data sheet: The analog watchdog says that the comparison data used is independent of the alignment of the data set in the ADC_CR2 register. The watchdog comparison is done before the data is aligned. Compare the watchdog first and then put the data into the ADC_DR data register.

In ST's library, there are only three simple watchdog-related functions:

Void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
Void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold);
Void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);

Use the ADC_AnalogWatchdogThresholdsConfig setting to trigger the upper and lower limits of the watchdog

Configure the channel to use the analog watchdog using ADC_AnalogWatchdogSingleChannelConfig

After the configuration is complete, use ADC_AnalogWatchdogCmd to start the analog watchdog.

The function I write is very simple, so three lines. The analog watchdog is added to CH1 of ADC1. code show as below:

Void ADC_WatchdogConfig(void)
{
ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_Channel_0);
ADC_AnalogWatchdogThresholdsConfig(ADC1,1500,0xFFF);
ADC_AnalogWatchdogCmd(ADC1,ADC_AnalogWatchdog_SingleRegEnable);
}

Initialize analog watchdog in NVIC:

Void NVIC_Config(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set the interrupt priority group NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreempTIonPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_Init(&NVIC_InitStructure);
}

Capture watchdog interrupt:

Void ADC_IRQHandler(void)
{
ADC_ITConfig(ADC1,ADC_IT_AWD,DISABLE);
If(SET == ADC_GetFlagStatus(ADC1,ADC_FLAG_AWD))
{
ADC_ClearFlag(ADC1,ADC_FLAG_AWD);
ADC_ClearITPendingBit(ADC1,ADC_IT_AWD);
Printf("ADC AWD is happened.");
}
ADC_ITConfig(ADC1,ADC_IT_AWD,ENABLE);
}

Of course, don't forget to turn on the ADC interrupts at the end:

ADC_ITConfig(ADC1,ADC_IT_AWD,ENABLE);

Windows Tablet

Windows Tablet

C&Q Technology (Guangzhou) Co.,Ltd. , https://www.gzcqteq.com

This entry was posted in on