打印
[应用相关]

STM32 ADC 模拟看门狗及其应用

[复制链接]
1082|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
前言
模拟看门狗特性允许应用程序检测输入电压是否超出用户定义的高低阈值, 用户可以预先设定个模拟看门狗的上下限电压值,
一旦采集到的电压超出该上下限, 将会触发模拟看门狗中断。 模拟看门狗一般用于检测单个的常规或注入转换通道, 或同时
检测所有的常规和注入通道。
  

使用特权

评论回复
沙发
黑心单片机|  楼主 | 2021-11-8 10:48 | 只看该作者
模块框图  


模拟看门狗可以预先设置 ADC 转换的高低阈值, ADC_HTR 寄存器来配置 ADC 转换的上限阈值, ADC_LTR 寄存器用来配置
ADC 转换的下限阈值。


使用特权

评论回复
板凳
黑心单片机|  楼主 | 2021-11-8 10:56 | 只看该作者
应用示例
ADC 配置代码  

/**
* [url=home.php?mod=space&uid=247401]@brief[/url] ADC configuration
* @param None
* @retval None
*/
static void ADC_Config(void)
{
ADC_ChannelConfTypeDef sConfig;
ADC_AnalogWDGConfTypeDef AnalogWDGConfig;
/* Configuration of ADCx init structure: ADC parameters and regular group */
AdcHandle.Instance = ADCx;
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; /* Asynchronous
clock mode, input ADC clock not divided */
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit
resolution for converted data */
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Rightalignment for converted data */
AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer
disabled (ADC conversion on only 1 channel: channel set on rank 1) */
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag
picked-up to indicate conversion end */
AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed
conversion feature disabled */
AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous
mode disabled to have only 1 conversion at each conversion trig */
AdcHandle.Init.NbrOfConversion = 1; /* Parameter
discarded because sequencer is disabled */
AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter
discarded because sequencer is disabled */
AdcHandle.Init.NbrOfDiscConversion = 1; /* Parameter
discarded because sequencer is disabled */
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO; /* Timer 3
external event triggering the conversion */
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; /* Parameter
discarded because software trigger chosen */
AdcHandle.Init.DMAContinuousRequests = ENABLE; /* DMA circular
mode selected */
AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is
overwritten with the last conversion result in case of overrun */
AdcHandle.Init.OversamplingMode = DISABLE; /* No
oversampling */
if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
{
/* ADC initialization error */
Error_Handler();
}
/* Configuration of channel on ADCx regular group on sequencer rank 1 */
/* Note: Considering IT occurring after each ADC conversion if ADC */
/* conversion is out of the analog watchdog window selected (ADC IT */
/* enabled), select sampling time and ADC clock with sufficient */
/* duration to not create an overhead situation in IRQHandler. */
sConfig.Channel = ADC_CHANNEL_5; /* Sampled channel number */
sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number
ADCx_CHANNEL */
sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock
cycles unit) */
sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
sConfig.Offset = 0; /* Parameter discarded because
offset correction is disabled */
if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
{
/* Channel Configuration Error */
Error_Handler();
}
/* Set analog watchdog thresholds in order to be between steps of DAC */
/* voltage. */
/* - High threshold: between DAC steps 1/2 and 3/4 of full range: */
/* 5/8 of full range (4095 <=> Vdda=3.3V): 2559<=> 2.06V */
/* - Low threshold: between DAC steps 0 and 1/4 of full range: */
/* 1/8 of full range (4095 <=> Vdda=3.3V): 512 <=> 0.41V */
/* Analog watchdog 1 configuration */
AnalogWDGConfig.WatchdogNumber = ADC_ANALOGWATCHDOG_1;
AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_ALL_REG;
AnalogWDGConfig.Channel = ADCx_CHANNELa;
AnalogWDGConfig.ITMode = ENABLE;
AnalogWDGConfig.HighThreshold = (RANGE_12BITS * 5/8);
AnalogWDGConfig.LowThreshold = (RANGE_12BITS * 1/8);
if (HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig) != HAL_OK)
{
/* Channel Configuration Error */
Error_Handler();
}
}
如上图所示, AnalogWDGConfig 结构体中分别使能了模拟看门狗及中断,设置了电压的上下阈值 HighThreshold 和
LowThreshold。模拟参考电压为 3.3V, 以上代码设置的下限电压阈值为 3.3*1/8=0.41V,上限电压阈值为 3.3*5/8=2.06V。


使用特权

评论回复
地板
黑心单片机|  楼主 | 2021-11-8 11:06 | 只看该作者
模拟看门狗中断服务程序
/**
* @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. */
ubAnalogWatchdogStatus = SET;
}
当模拟看门狗检测到电压高于上限或者低于下限时将会产生看门狗中断,中断服务程序触发后,可以做出一些应对措施。 这
里置了 ubAnalogWatchdogStatus 标志,然后由主程序去根据标志的值去执行相应处理程序。



/* Infinite loop */
while (1)
{
/* Turn-on/off LED1 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 == RESET)
{
BSP_LED_Off(LED1);
}
else
{
BSP_LED_On(LED1);
/* Reset analog watchdog status for next loop iteration */
ubAnalogWatchdogStatus = RESET;
} }

使用特权

评论回复
5
黑心单片机|  楼主 | 2021-11-8 11:06 | 只看该作者
启动 ADC 转换代码
#define ADCCONVERTEDVALUES_BUFFER_SIZE 256 /* Size of array
aADCxConvertedValues[] */
/* Variable containing ADC conversions results */
__IO uint16_t aADCxConvertedValues[ADCCONVERTEDVALUES_BUFFER_SIZE];
/* Start ADC conversion on regular group with transfer by DMA */
if (HAL_ADC_Start_DMA(&AdcHandle,
(uint32_t *)aADCxConvertedValues,
ADCCONVERTEDVALUES_BUFFER_SIZE
) != HAL_OK)
{
/* Start Error */
Error_Handler();
}
使用循环模式 DMA 启动 ADC 转换, DMA 可以降低 CPU 负载。



结论
控制系统中,需要测量严格电压、 压力、温度等范围的信号,使用模拟看门狗能够快速地检测到异常状况, 并做出相应的应
对措施,以确保设备安全。

使用特权

评论回复
6
mutable| | 2021-11-12 10:43 | 只看该作者
本来挺好奇,结果没看懂

使用特权

评论回复
7
单模先声| | 2021-11-12 14:39 | 只看该作者
本来挺好奇,结果没看懂

使用特权

评论回复
8
yljon| | 2021-11-12 14:52 | 只看该作者
DMA靠谱

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

71

主题

497

帖子

0

粉丝