芯启元 发表于 2026-6-29 13:15:07

09-看门狗复位测试程序,STC32G144K246实验箱演示程序

09-看门狗复位测试程序,STC32G144K246实验箱演示程序
主程序main.C代码如下(汇编版本在附件中):



/*---------------------------------------------------------------------*/
/* --- Web: www.STCAI.com ---------------------------------------------*/
/* --- BBS: www.STCAIMCU.com-----------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序      */
/*---------------------------------------------------------------------*/

/*************功能说明    **************

本例程基于STC32G144K246为主控芯片的实验箱进行编写测试。

使用Keil C251编译器,Memory Model推荐设置XSmall模式,默认定义变量在edata,单时钟存取访问速度快。

edata建议保留1K给堆栈使用,空间不够时可将大数组、不常用变量加xdata关键字定义到xdata空间。

用STC的MCU的IO方式驱动8位数码管。

显示效果为: 显示秒计数, 5秒后不喂狗, 等复位.

下载时, 选择时钟 48MHz (用户可自行修改频率).

******************************************/

#include "..\..\comm\STC32G144K246.h"

typedef         unsigned char      u8;
typedef         unsigned int      u16;
typedef         unsigned long      u32;

/****************用户定义宏****************/

#define MAIN_Fosc         48000000UL
#define Timer0_Reload       (65536UL -(MAIN_Fosc / 1000))       //Timer 0 中断频率, 1000次/秒

#define D_WDT_FLAG          (1<<7)
#define D_EN_WDT            (1<<5)
#define D_CLR_WDT         (1<<4)//auto clear
#define D_IDLE_WDT          (1<<3)//WDT counter when Idle
#define D_WDT_SCALE_2       0
#define D_WDT_SCALE_4       1
#define D_WDT_SCALE_8       2       //T=393216*N/fo
#define D_WDT_SCALE_16      3
#define D_WDT_SCALE_32      4
#define D_WDT_SCALE_64      5
#define D_WDT_SCALE_128   6
#define D_WDT_SCALE_256   7

#define DIS_DOT   0x20
#define DIS_BLACK   0x10
#define DIS_      0x11

/****************本地常量声明**************/
u8 code t_display[]={                     //标准字库
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black-   H    J    K    L    N    o   P    U   t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码

/****************本地变量声明**************/

u8LED8;      //显示缓冲
u8display_index;//显示位索引

u16 ms_cnt;
u8tes_cnt;    //测试用的计数变量

/****************本地函数声明**************/

void delay_ms(u16 ms);
void DisplayScan(void);

/***************** 主函数 *********************/
void main(void)
{
    u8i;
   
    EAXFR = 1; //扩展寄存器(XFR)访问使能
    CKCON = 0; //提高访问XRAM速度

    P0M1 = 0x00;   P0M0 = 0xff;   //设置为推挽输出
    PAM1 = 0x00;   PAM0 = 0xff;   //设置为推挽输出

    RSTFLAG |= 0x04;   //设置看门狗复位需要检测P3.2的状态,否则看门狗复位后进入USB下载模式

    display_index = 0;
    for(i=0; i<8; i++)LED8 = DIS_BLACK;    //全部消隐
   
    tes_cnt = 0;
    ms_cnt = 0;
    LED8 = ms_cnt;

    while(1)
    {
      delay_ms(1);    //延时1ms
      DisplayScan();
      if(tes_cnt <= 5)    //5秒后不喂狗, 将复位,
            WDT_CONTR = (D_EN_WDT + D_CLR_WDT + D_WDT_SCALE_16);    // 喂狗

      if(++ms_cnt >= 1000)
      {
            ms_cnt = 0;
            tes_cnt++;
            LED8 = tes_cnt;
      }
    }
}

//========================================================================
// 函数: void delay_ms(unsigned int ms)
// 描述: 延时函数。
// 参数: ms,要延时的ms数.
// 返回: none.
// 版本: VER1.0
// 日期: 2025-11-01
// 备注: 由于芯片使能了Cache功能,软件延时时间可能不太准确
//========================================================================
void delay_ms(u16 ms)
{
    u16 i;
    do{
      i = MAIN_Fosc / 6000;
      while(--i);
    }while(--ms);
}

/********************** 显示扫描函数 ************************/
void DisplayScan(void)
{
    PAOUT = ~T_COM;
    P0 = ~t_display];
    if(++display_index >= 8)    display_index = 0;//8位结束回0
}



页: [1]
查看完整版本: 09-看门狗复位测试程序,STC32G144K246实验箱演示程序