你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 ADC 模拟看门狗及其应用

[复制链接]
STMCU小助手 发布时间:2022-2-23 19:05
前言
模拟看门狗特性允许应用程序检测输入电压是否超出用户定义的高低阈值,用户可以预先设定个模拟看门狗的上下限电压值,一旦采集到的电压超出该上下限,将会触发模拟看门狗中断。模拟看门狗一般用于检测单个的常规或注入转换通道,或同时检测所有的常规和注入通道。
模块框图

PQL7CUA})XMG1WU}O`IIZSX.png
模拟看门狗可以预先设置 ADC 转换的高低阈值,ADC_HTR 寄存器来配置 ADC 转换的上限阈值,ADC_LTR 寄存器用来配置ADC 转换的下限阈值。

应用示例
ADC 配置代码
  1. /**

  2. * @brief ADC configuration
  3. * @param None
  4. * @retval None
  5.   */
  6.   static void ADC_Config(void)
  7.   {
  8.   ADC_ChannelConfTypeDef sConfig;
  9.   ADC_AnalogWDGConfTypeDef AnalogWDGConfig;

  10. /* Configuration of ADCx init structure: ADC parameters and regular group */
  11. AdcHandle.Instance = ADCx;
  12. AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; /* Asynchronous
  13. clock mode, input ADC clock not divided */
  14. AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit
  15. resolution for converted data */
  16. AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Rightalignment for converted data */
  17. AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer
  18. disabled (ADC conversion on only 1 channel: channel set on rank 1) */
  19. AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag
  20. picked-up to indicate conversion end */
  21. AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed
  22. conversion feature disabled */
  23. AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous
  24. mode disabled to have only 1 conversion at each conversion trig */
  25. AdcHandle.Init.NbrOfConversion = 1; /* Parameter
  26. discarded because sequencer is disabled */
  27. AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter
  28. discarded because sequencer is disabled */
  29. AdcHandle.Init.NbrOfDiscConversion = 1; /* Parameter
  30. discarded because sequencer is disabled */
  31. AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO; /* Timer 3
  32. external event triggering the conversion */
  33. AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; /* Parameter
  34. discarded because software trigger chosen */
  35. AdcHandle.Init.DMAContinuousRequests = ENABLE; /* DMA circular
  36. mode selected */
  37. AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is
  38. overwritten with the last conversion result in case of overrun */
  39. AdcHandle.Init.OversamplingMode = DISABLE; /* No
  40. oversampling */
  41. if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
  42. {
  43. /* ADC initialization error */
  44. Error_Handler();
  45. }
  46. /* Configuration of channel on ADCx regular group on sequencer rank 1 */
  47. /* Note: Considering IT occurring after each ADC conversion if ADC */
  48. /* conversion is out of the analog watchdog window selected (ADC IT */
  49. /* enabled), select sampling time and ADC clock with sufficient */
  50. /* duration to not create an overhead situation in IRQHandler. */
  51. sConfig.Channel = ADC_CHANNEL_5; /* Sampled channel number */
  52. sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number
  53. ADCx_CHANNEL */
  54. sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock
  55. cycles unit) */
  56. sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
  57. sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
  58. sConfig.Offset = 0; /* Parameter discarded because
  59. offset correction is disabled */

  60. if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
  61. {
  62. /* Channel Configuration Error */
  63. Error_Handler();
  64. }

  65. /* Set analog watchdog thresholds in order to be between steps of DAC */
  66. /* voltage. */
  67. /* - High threshold: between DAC steps 1/2 and 3/4 of full range: */
  68. /* 5/8 of full range (4095  Vdda=3.3V): 2559 2.06V */
  69. /* - Low threshold: between DAC steps 0 and 1/4 of full range: */
  70. /* 1/8 of full range (4095  Vdda=3.3V): 512  0.41V */
  71. /* Analog watchdog 1 configuration */
  72. AnalogWDGConfig.WatchdogNumber = ADC_ANALOGWATCHDOG_1;
  73. AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_ALL_REG;
  74. AnalogWDGConfig.Channel = ADCx_CHANNELa;
  75. AnalogWDGConfig.ITMode = ENABLE;
  76. AnalogWDGConfig.HighThreshold = (RANGE_12BITS * 5/8);
  77. AnalogWDGConfig.LowThreshold = (RANGE_12BITS * 1/8);
  78. if (HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig) != HAL_OK)
  79. {
  80. /* Channel Configuration Error */
  81. Error_Handler();
  82. }

  83. }
复制代码

如上图所示,AnalogWDGConfig 结构体中分别使能了模拟看门狗及中断,设置了电压的上下阈值 HighThreshold 和LowThreshold。模拟参考电压为 3.3V,以上代码设置的下限电压阈值为 3.3*1/8=0.41V,上限电压阈值为 3.3*5/8=2.06V。

模拟看门狗中断服务程序
  1. /**

  2. * @brief Analog watchdog callback in non blocking mode.
  3. * @param hadc: ADC handle
  4. * @retval None
  5.   */
  6.   void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
  7.   {
  8.   /* Set variable to report analog watchdog out of window status to main */
  9.   /* program. */
  10.   ubAnalogWatchdogStatus = SET;
  11.   }
复制代码

  当模拟看门狗检测到电压高于上限或者低于下限时将会产生看门狗中断,中断服务程序触发后,可以做出一些应对措施。这里置了 ubAnalogWatchdogStatus 标志,然后由主程序去根据标志的值去执行相应处理程序。
  1.   /* Infinite loop */
  2.   while (1)
  3.   {
  4.   /* Turn-on/off LED1 in function of ADC conversion result */
  5.   /* - Turn-off if voltage is into AWD window */
  6.   /* - Turn-on if voltage is out of AWD window */
  7.   /* Variable of analog watchdog status is set into analog watchdog */
  8.   /* interrupt callback */
  9.   if (ubAnalogWatchdogStatus == RESET)
  10.   {
  11.   BSP_LED_Off(LED1);
  12.   }
  13.   else
  14.   {
  15.   BSP_LED_On(LED1);

  16. /* Reset analog watchdog status for next loop iteration */
  17. ubAnalogWatchdogStatus = RESET;
  18. }
  19. }
复制代码

启动 ADC 转换代码
  1. #define ADCCONVERTEDVALUES_BUFFER_SIZE 256 /* Size of array
  2. aADCxConvertedValues[] */
  3. /* Variable containing ADC conversions results */
  4. __IO uint16_t aADCxConvertedValues[ADCCONVERTEDVALUES_BUFFER_SIZE];
  5. /* Start ADC conversion on regular group with transfer by DMA */
  6. if (HAL_ADC_Start_DMA(&AdcHandle,
  7. (uint32_t *)aADCxConvertedValues,
  8. ADCCONVERTEDVALUES_BUFFER_SIZE
  9. ) != HAL_OK)
  10. {
  11. /* Start Error */
  12. Error_Handler();
  13. }
复制代码

使用循环模式 DMA 启动 ADC 转换,DMA 可以降低 CPU 负载。

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

收藏 评论0 发布时间:2022-2-23 19:05

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版