[技术问答] mini 54 22m内部晶振 看门狗 怎么设置

[复制链接]
1600|16
 楼主| coldfish522 发表于 2017-8-31 19:37 | 显示全部楼层 |阅读模式
mini 54  22m内部晶振  看门狗 怎么设置??
xinpian101 发表于 2017-8-31 21:22 | 显示全部楼层
  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: 3 $
  5. * $Date: 14/01/28 11:45a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series WDT Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"

  13. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  14. #define PLL_CLOCK           50000000


  15. /*---------------------------------------------------------------------------------------------------------*/
  16. /* Global Interface Variables Declarations                                                                 */
  17. /*---------------------------------------------------------------------------------------------------------*/
  18. extern int IsDebugFifoEmpty(void);
  19. volatile uint8_t g_u8IsWDTTimeoutINT = 0;


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

  35.         g_u8IsWDTTimeoutINT = 1;

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

  39. void SYS_Init(void)
  40. {
  41.     /*---------------------------------------------------------------------------------------------------------*/
  42.     /* Init System Clock                                                                                       */
  43.     /*---------------------------------------------------------------------------------------------------------*/
  44.     /* Enable IRC22M clock */
  45.     CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;

  46.     /* Waiting for IRC22M clock ready */
  47.     CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

  48.     /* Switch HCLK clock source to HIRC */
  49.     CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC;

  50.     /* Set PLL to Power-down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
  51.     CLK->PLLCON |= CLK_PLLCON_PD_Msk;

  52.     /* Enable external 12 MHz XTAL, IRC 10 kHz */
  53.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk;

  54.     /* Enable PLL and Set PLL frequency */
  55.     CLK->PLLCON = PLLCON_SETTING;

  56.     /* Waiting for clock ready */
  57.     CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_IRC10K_STB_Msk);

  58.     /* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
  59.     CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL;

  60.     /* Enable peripheral clock */
  61.     CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk | CLK_APBCLK_WDT_EN_Msk;

  62.     /* Peripheral clock source */
  63.     CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL | CLK_CLKSEL1_WDT_S_LIRC;

  64.     /* Update System Core Clock */
  65.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  66.     SystemCoreClockUpdate();

  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init I/O Multi-function                                                                                 */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Set P3 multi-function pins for UART0 RXD, TXD */
  71.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
  72. }

  73. void UART0_Init(void)
  74. {
  75.     /*---------------------------------------------------------------------------------------------------------*/
  76.     /* Init UART                                                                                               */
  77.     /*---------------------------------------------------------------------------------------------------------*/
  78.     /* Reset IP */
  79.     SYS_ResetModule(UART0_RST);

  80.     /* Configure UART0 and set UART0 Baudrate */
  81.     UART_Open(UART0, 115200);
  82. }

  83. /*---------------------------------------------------------------------------------------------------------*/
  84. /*  MAIN function                                                                                          */
  85. /*---------------------------------------------------------------------------------------------------------*/
  86. int main(void)
  87. {
  88.     /* Unlock protected registers */
  89.     SYS_UnlockReg();

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

  92.     /* Lock protected registers */
  93.     SYS_LockReg();

  94.     /* Init UART0 for printf */
  95.     UART0_Init();

  96.     if(WDT_GET_RESET_FLAG() == 1)
  97.     {
  98.         /* Use P0.0 to check time-out period time */
  99.         GPIO_SetMode(P0, 0, GPIO_PMD_OUTPUT);
  100.         P00 = 1;
  101.         P00 = 0;

  102.         WDT_CLEAR_RESET_FLAG();
  103.         printf("\n*** WDT time-out reset occurred ***\n");
  104.         while(1);
  105.     }

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

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

  114.     /* Use P0.0 to check time-out period time */
  115.     GPIO_SetMode(P0, 0, GPIO_PMD_OUTPUT);
  116.     P00 = 1;
  117.     P00 = 0;

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

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

  124.     /* Enable WDT interrupt function */
  125.     WDT_EnableInt();

  126.     /* Enable WDT NVIC */
  127.     NVIC_EnableIRQ(WDT_IRQn);

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

  130.     P00 = 1;

  131.     while(1);
  132. }

  133. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
junpeng324 发表于 2017-8-31 22:30 | 显示全部楼层
#include "printf.h"
#include "systick.h"
#include "iwdg.h"
int main()
{       
        printf_init(); //printf初始化
        iwdg_init();   //独立看门狗初始化配置
        while(1)
        {       
                IWDG_ReloadCounter();           //喂狗的时间是1.28s
                printf("进入喂狗模式\r\n");       
                delay_ms(1000);        //只要在喂狗时间内喂狗就不会让系统以为死机进入复位状态
        }                       
}
#include "iwdg.h"

/*******************************************************************************
* 函 数 名         : iwdg_init
* 函数功能                   : 独立看门狗模式配置初始化          
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void iwdg_init()        //独立看门狗初始化配置
{
        IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//使能寄存器,写功能
        IWDG_SetPrescaler(IWDG_Prescaler_64);//设置IWDG预分频值//40K/64=1.6ms
        IWDG_SetReload(800);//设置IWDG重装载值  12位的(0~4095)//800*1.6ms=1.28s
        IWDG_ReloadCounter();//按照IWDG重装载寄存器的值重装载IWDG计数器
        IWDG_Enable();//使能IWDG
}


zhuotuzi 发表于 2017-9-6 20:50 | 显示全部楼层
新手吗?不知道官方提供了丰富的例子吗
wahahaheihei 发表于 2017-9-6 21:46 | 显示全部楼层
楼主要启用看门狗定时器啊
wahahaheihei 发表于 2017-9-6 21:47 | 显示全部楼层
要看你用寄存器操作还是用库函数操作了,官方都提供了例子。去下载BSP。。
heisexingqisi 发表于 2017-9-6 22:29 | 显示全部楼层
oid WDT_IRQHandler(void)
{
    if(WDT_GET_TIMEOUT_INT_FLAG() == 1)
    {
        /* Clear WDT time-out interrupt flag */
        WDT_CLEAR_TIMEOUT_INT_FLAG();

        g_u8IsWDTTimeoutINT = 1;
    }
}
heisexingqisi 发表于 2017-9-6 22:29 | 显示全部楼层
/* Enable peripheral clock */
    CLK_EnableModuleClock(UART0_MODULE);
    CLK_EnableModuleClock(WDT_MODULE);

    /* Peripheral clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
    CLK_SetModuleClock(WDT_MODULE, CLK_CLKSEL1_WDT_S_LIRC, 0);
heisexingqisi 发表于 2017-9-6 22:30 | 显示全部楼层
一般是这样,要配置看门狗的时钟模块,然后配置时钟源,然后设置中断函数。
heisexingqisi 发表于 2017-9-6 22:30 | 显示全部楼层
你下载BSP开发包没,里面有4个关于看门狗的例子可以参考
捉虫天师 发表于 2017-9-10 12:24 来自手机 | 显示全部楼层
用官方例子看看就很容易懂了
yiy 发表于 2017-9-11 21:57 | 显示全部楼层
跟各位大侠学习学习技术
yiy 发表于 2017-9-11 21:58 | 显示全部楼层
其实每个系列最好都出个中文教程比较好。
毕竟例程上都是英文注释,也看不懂。
wanduzi 发表于 2017-9-12 11:23 | 显示全部楼层
一般例程都是使用内部晶振操作,那个不叫晶振,叫RC振荡器。。好像是这样。
wanduzi 发表于 2017-9-12 11:23 | 显示全部楼层
或者说是压控振荡器。不知道具体是什么,没研究过,只是会用。
598330983 发表于 2017-9-12 20:31 | 显示全部楼层
   /* Enable IRC22M clock */
    CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;

    /* Waiting for IRC22M clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);

    /* Switch HCLK clock source to HIRC */
    CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC;
598330983 发表于 2017-9-12 20:32 | 显示全部楼层
一般都是这样做的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

7

帖子

0

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