做外贸外文网站怎么做好,做图赚钱的网站有哪些,网站关键字可以做几个,公司网站建设的不足文章目录 一. 看门狗1. 独立看门狗#xff08;IWDG#xff09;1.1 原理1.2 相关配置1.3 相关函数 2. 窗口看门狗#xff08;WWDG#xff09;2.1 原理2.2 相关配置2.3 相关函数 一. 看门狗 单片机在日常工作中常常会因为用户配置代码出现BUG#xff0c;而导致芯片无法正常工… 文章目录 一. 看门狗1. 独立看门狗IWDG1.1 原理1.2 相关配置1.3 相关函数 2. 窗口看门狗WWDG2.1 原理2.2 相关配置2.3 相关函数 一. 看门狗 单片机在日常工作中常常会因为用户配置代码出现BUG而导致芯片无法正常工作或者会受到来自外界电磁场的干扰造成程序跑飞 或陷入死循环 如果无法系统复位那么整个系统都会卡死这对产品的使用是灾难性的后果。
出于对单片机运行状态进行实时监测的考虑产生了一种专门用于监测单片机程序运行状态的模块或者芯片俗称看门狗(WatchDog) 。
在STM32中看门狗分为独立看门狗IWDG还有窗口看门狗WWDG 二者的主要区别如下
独立看门狗 IWDG窗口看门狗 WWDG时钟源独立时钟LSI (40KZ低速时钟)PCLK1 时钟中断没有中断超时直接复位超时产生中断可做复位前函数操作或重新喂狗复位条件递减计数到0窗口期外或者递减到0x3F计数器位数12位 最大计数范围4096-06位最大计数范围127-63应用场合防止程序跑飞、死循环检测程序时效防止软件异常
1. 独立看门狗IWDG
1.1 原理
低速时钟 LSI经过预分频器 分频后用作驱动12 位的递减计数器当递减至重装载值时若还没有及时喂狗系统就会产生一个复位信号CPU收到复位信号系统复位重新运行。具体步骤可以参考下面的框图。
独立看门狗IWDG由专用的低速时钟LSI驱动即使主时钟发生故障它仍有效。额外注意的是独立看门狗由 V D D V_{DD} VDD电压域供电即使MCU在停止模式和待机模式下依然工作除非使用RTC唤醒后喂狗否则STM32进入低功耗模式后会因为没有及时喂狗而被唤醒。 1.2 相关配置
HAL库使用独立看门狗非常简单使用STM32CubeMX配置IWDG只有三个参数 · IWDG counter clock prescaler分频系数 · IWDG down-counter reload value重装载值 · IWDG window value 窗口值默认不修改。当计数器的值大于窗口值时如果执行重载操作则会产生复位 IWDG溢出时间计算公式 T o u t N p r e s c a l e r ∗ N r e l o a d f I W D G T_{out} \frac{N_{prescaler}*N_{reload}}{f_{IWDG}} ToutfIWDGNprescaler∗Nreload 其中 N p r e s c a l e r N_{prescaler} Nprescaler是分频系数 N r e l o a d N_{reload} Nreload为重装载值 f I W D G f_{IWDG} fIWDG为低速时钟源的频率。
1.3 相关函数
初始化函数
/*** brief Initialize the IWDG according to the specified parameters in the* IWDG_InitTypeDef and start watchdog. Before exiting function,* watchdog is refreshed in order to have correct time base.* param hiwdg pointer to a IWDG_HandleTypeDef structure that contains* the configuration information for the specified IWDG module.* retval HAL status*/
HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)喂狗函数
/*** brief Refresh the IWDG.* param hiwdg pointer to a IWDG_HandleTypeDef structure that contains* the configuration information for the specified IWDG module.* retval HAL status*/
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)2. 窗口看门狗WWDG
2.1 原理
窗口看门狗的流程框图如下图所示 窗口看门狗的的时钟信号来自PCLK1然后经过看门狗预分频器进行分频处理。
看门狗控制寄存器(WWDG_CR)为8位寄存器其中首位为使能位用于开启看门狗。后面为T[6:0] 的值。寄存器后六位为递减计数器(CNT)由分频后的时钟信号进行驱动。
看门狗配置寄存器(WWDG_CFR)寄存器的值为窗口上限值当看门狗控制寄存器的值W6大于看门狗配置寄存器的值T6则说明已经进入窗口期比较器输出0否则为非窗口期比较器输出1。 图中三个逻辑门电路A、B、C的作用分别如下 A 与门输出1的条件是看门狗被使能的同时B输出为1。 B 或门输出1的条件要么是递减计数器减至窗口下限值T6为0要么是c门输出1。 C 与门输出1的条件是递减计数器减至窗口上限值的同时被喂狗即WWDG_CR被写入。 相比于独立看门狗窗口看门狗WWDG是一个既能产生系统复位信号和提前唤醒中断的六位递减计数器。 1复位的条件 1.当递减计数器值从0x40递减到0x3F时复位即T6位跳变到0 2. 计数器的值大于W[6:0]值时喂狗会复位。 2中断的条件 1.当递减计数器等于0x40时可产生提前唤醒中断 (EWI)。 3喂狗条件 需要在窗口期0x3F窗口期 W[6:0]重装载计数器的值才能防止复位。
2.2 相关配置 使用STM32CubeMX配置WWDG主要有五个参数 WWDG counter clock prescaler分频系数 WWDG window value 窗口上限值即W[6:0]即喂狗的窗口区间在0x3F ~ WWDG window value之间 WWDG free-running downcounter value每次复位后重新装载的值递减到窗口区间内才可以刷新。这个值必须大于0x40和WWDG window value。 Early wakeup interrupt 早期唤醒中断开启后当计数器到达0x40时会产生中断可以在中断中进行喂狗或者函数操作。 Window watchdog interrupt窗口看门狗全局中断。在NVIC Settings中若Early wakeup interrupt为Enable则该中断需要打开。
2.3 相关函数
1. 初始化函数
/*** brief Initialize the WWDG according to the specified.* parameters in the WWDG_InitTypeDef of associated handle.* param hwwdg pointer to a WWDG_HandleTypeDef structure that contains* the configuration information for the specified WWDG module.* retval HAL status*/
HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)2. 喂狗函数
/*** brief Refresh the WWDG.* param hwwdg pointer to a WWDG_HandleTypeDef structure that contains* the configuration information for the specified WWDG module.* retval HAL status*/
HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)3. 看门狗中断服务函数
/*** brief Handle WWDG interrupt request.* note The Early Wakeup Interrupt (EWI) can be used if specific safety operations* or data logging must be performed before the actual reset is generated.* The EWI interrupt is enabled by calling HAL_WWDG_Init function with* EWIMode set to WWDG_EWI_ENABLE.* When the downcounter reaches the value 0x40, and EWI interrupt is* generated and the corresponding Interrupt Service Routine (ISR) can* be used to trigger specific actions (such as communications or data* logging), before resetting the device.* param hwwdg pointer to a WWDG_HandleTypeDef structure that contains* the configuration information for the specified WWDG module.* retval None*/
void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)4. 早期唤醒回调函数
/*** brief WWDG Early Wakeup callback.* param hwwdg pointer to a WWDG_HandleTypeDef structure that contains* the configuration information for the specified WWDG module.* retval None*/
__weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
{/* Prevent unused argument(s) compilation warning */UNUSED(hwwdg);/* NOTE: This function should not be modified, when the callback is needed,the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file*/
}