wdt.c
#include "wdt.h"void wdt_init(void)
{WDT_CONTR = 0x24; // 0010 0100 - 1.1377s
}void wdt_feed(void)
{WDT_CONTR |= 0x10; // 喂狗
}
wdt.h
#ifndef _WDT_H_
#define _WDT_H_#include "stc12c5a60s2.h"// 函数声明
extern void wdt_init(void);
extern void wdt_feed(void);#endif
time.c
#include "time.h"extern unsigned char wdt_flag;void Timer0_Init(void) //1毫秒@11.0592MHz
{TMOD &= 0xF0; //设置定时器模式TMOD |= 0x01; //设置定时器模式TL0 = 0x66; //设置定时初始值TH0 = 0xFC; //设置定时初始值TF0 = 0; //清除TF0标志ET0 = 1;TR0 = 1; //定时器0开始计时
}void timer_isr() interrupt 1
{static unsigned int wdtcnt;TR0 = 0; //定时器0开始计时wdtcnt++;if(wdtcnt >=1000) // 1s钟喂狗一次{wdtcnt = 0 ;wdt_flag = 1;}TL0 = 0x66; //设置定时初始值TH0 = 0xFC; //设置定时初始值TR0 = 1; //定时器0开始计时
}
main.c
#include "stc12c5a60s2.h"
#include "time.h"
#include "wdt.h"unsigned char wdt_flag;void main()
{wdt_init();while(1){if(wdt_flag){wdt_flag = 0;wdt_feed();} }
}