[DemoCode下载] 看门狗定时器(WDT)超时复位应用

[复制链接]
978|3
 楼主| xixi2017 发表于 2024-2-25 13:00 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * $Revision: 4 $
  5. * $Date: 15/09/14 4:06p $
  6. * @brief
  7. *           Show how to generate time-out reset system event while WDT time-out reset delay period expired.
  8. *
  9. * @note
  10. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M058S.h"

  14. #define PLL_CLOCK           50000000


  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Global Interface Variables Declarations                                                                 */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. volatile uint8_t g_u8IsWDTTimeoutINT = 0;


  19. /**
  20. * [url=home.php?mod=space&uid=247401]@brief[/url]       IRQ Handler for WDT and WWDT Interrupt
  21. *
  22. * @param       None
  23. *
  24. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  25. *
  26. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The WDT_IRQHandler is default IRQ of WDT and WWDT, declared in startup_M058S.s.
  27. */
  28. void WDT_IRQHandler(void)
  29. {
  30.     if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
  31.     {
  32.         /* Clear WDT time-out interrupt flag */
  33.         WDT_CLEAR_TIMEOUT_INT_FLAG();

  34.         g_u8IsWDTTimeoutINT = 1;

  35.         printf("WDT time-out interrupt occurred.\n");
  36.     }
  37. }

  38. void SYS_Init(void)
  39. {
  40.     /*---------------------------------------------------------------------------------------------------------*/
  41.     /* Init System Clock                                                                                       */
  42.     /*---------------------------------------------------------------------------------------------------------*/
  43.     /* Enable HIRC 22.1184MHz clock */
  44.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  45.     /* Waiting for HIRC clock ready */
  46.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  47.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  48.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  49.     /* Enable external XTAL 12MHz and LIRC 10KHz clock */
  50.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk);

  51.     /* Waiting for external XTAL and LIRC clock ready */
  52.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC10K_STB_Msk);

  53.     /* Set core clock as PLL_CLOCK from PLL */
  54.     CLK_SetCoreClock(PLL_CLOCK);

  55.     /* Enable peripheral clock */
  56.     CLK_EnableModuleClock(UART0_MODULE);
  57.     CLK_EnableModuleClock(WDT_MODULE);

  58.     /* Peripheral clock source */
  59.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
  60.     CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LIRC, 0);

  61.     /*---------------------------------------------------------------------------------------------------------*/
  62.     /* Init I/O Multi-function                                                                                 */
  63.     /*---------------------------------------------------------------------------------------------------------*/
  64.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  65.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  66.     SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD);
  67. }

  68. void UART0_Init(void)
  69. {
  70.     /*---------------------------------------------------------------------------------------------------------*/
  71.     /* Init UART                                                                                               */
  72.     /*---------------------------------------------------------------------------------------------------------*/
  73.     /* Reset IP */
  74.     SYS_ResetModule(UART0_RST);

  75.     /* Configure UART0 and set UART0 Baudrate */
  76.     UART_Open(UART0, 115200);
  77. }

  78. /*---------------------------------------------------------------------------------------------------------*/
  79. /*  MAIN function                                                                                          */
  80. /*---------------------------------------------------------------------------------------------------------*/
  81. int main(void)
  82. {
  83.     /* Unlock protected registers */
  84.     SYS_UnlockReg();

  85.     /* Init System, peripheral clock and multi-function I/O */
  86.     SYS_Init();

  87.     /* Init UART0 for printf */
  88.     UART0_Init();

  89.     if(WDT_GET_RESET_FLAG() == 1)
  90.     {
  91.         /* Use P0.0 to check time-out period time */
  92.         GPIO_SetMode(P0, BIT0, GPIO_PMD_OUTPUT);
  93.         P00 = 1;
  94.         P00 = 0;

  95.         WDT_CLEAR_RESET_FLAG();
  96.         printf("\n*** WDT time-out reset occurred ***\n");
  97.         while(1);
  98.     }

  99.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  100.     printf("+--------------------------------------+\n");
  101.     printf("|    WDT Time-out Reset Sample Code    |\n");
  102.     printf("+--------------------------------------+\n\n");

  103.     printf("# WDT Settings:\n");
  104.     printf("  Clock source is 10 kHz; Enable interrupt; Time-out interval is 2^14 * WDT clock.\n");
  105.     printf("# When WDT start counting, system will generate a WDT time-out interrupt after 1.6384 ~ 1.7408 s.\n");
  106.     printf("  Measure P0.0 low period to check time-out interval and do not reload WDT counter.\n\n");

  107.     /* Use P0.0 to check time-out period time */
  108.     GPIO_SetMode(P0, BIT0, GPIO_PMD_OUTPUT);
  109.     P00 = 1;
  110.     P00 = 0;

  111.     /* Because of all bits can be written in WDT Control Register are write-protected;
  112.        To program it needs to disable register protection first. */
  113.     SYS_UnlockReg();

  114.     /* Enable WDT time-out reset function and select time-out interval to 2^14 * WDT clock then start WDT counting */
  115.     g_u8IsWDTTimeoutINT = 0;
  116.     WDT_Open(WDT_TIMEOUT_2POW14, 0, TRUE, FALSE);

  117.     /* Enable WDT interrupt function */
  118.     WDT_EnableInt();

  119.     /* Enable WDT NVIC */
  120.     NVIC_EnableIRQ(WDT_IRQn);

  121.     /* Check if WDT time-out interrupt occurred */
  122.     while(g_u8IsWDTTimeoutINT == 0);

  123.     P00 = 1;

  124.     while(1);
  125. }

  126. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| xixi2017 发表于 2024-2-25 13:01 | 显示全部楼层
这段单片机程序是一个演示如何在看门狗定时器(WDT)超时重置延迟期间生成超时重置系统事件的示例代码。以下是程序的分析总结:

初始化设置:

使用了50MHz的PLL时钟。
启用了外部12MHz晶体振荡器和内部10kHz低速振荡器。
将核心时钟设置为PLL_CLOCK。
启用了UART0模块和WDT模块的时钟,并设置了它们的时钟源和分频器。
配置了UART0的引脚,并初始化了UART0。
WDT中断处理函数:

定义了一个WDT的中断处理函数WDT_IRQHandler,当WDT定时器超时时调用。在该函数中,首先清除了WDT定时器的超时中断标志位,然后将一个全局变量g_u8IsWDTTimeoutINT设置为1,表示WDT定时器超时中断发生了。
主函数(main):

初始化系统、外设时钟和多功能IO。
初始化UART0以便进行打印输出。
如果检测到WDT的复位标志,则输出相应的信息并停止在一个无限循环中。
输出CPU时钟频率和一些提示信息,然后配置WDT,使其以10kHz的时钟源计数,设置超时时间为2^14个WDT时钟,并启用中断。
启用WDT的中断,并启用WDT相关的NVIC。
在一个无限循环中等待WDT定时器超时中断发生,一旦发生,将P0.0置高。
程序注释:

对程序的各个部分进行了注释说明,包括WDT设置、时钟源、中断等。
输出信息:

输出了WDT的设置信息,包括时钟源、中断使能等。
输出了WDT定时器超时后的行为,以及相应的时间。
 楼主| xixi2017 发表于 2024-2-25 13:01 | 显示全部楼层
总体来说,这段代码演示了如何配置并使用WDT定时器,并在超时时生成相应的中断事件,以及如何处理这些事件。
598330983 发表于 2024-2-28 22:29 | 显示全部楼层
这个不仅可以作为看门狗使用,还可以作为普通的定时器使用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

145

主题

2034

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部