74-按键喂狗
/*----------------------------------------------- 名称:看门狗溢出实验 按键不停喂狗 论坛:www.doflye.net 编写:shifang 日期:2009.5 修改:无 内容:通过按键喂狗防止溢出复位 看门狗演示程序 在16383个机器周期内必须至少喂狗一次 标准AT89s52单片机试验通过。 ------------------------------------------------*/ #includesfr WDTRST = 0xA6; sbit K1 = P3^0; sbit K2 = P3^1; sbit LED1=P1^1; sbit LED2=P1^2; void DelayUs2x(unsigned char t);//us级延时函数声明 void DelayMs(unsigned char t); //ms级延时 /*------------------------------------------------ 主函数 ------------------------------------------------*/ main() { LED1=0; DelayMs(100); LED1=1; DelayMs(100); TMOD=0x01; TH0=0xc6; //定时16ms TL0=0x66; EA=1; ET0=1; WDTRST=0x1e; //在程序初始化中激活看门狗。 WDTRST=0xe1; //先送1E,后送E1 if(K1==0) { TR0=1; } while(1) { if(K2==0) { TR0=0; } LED2=1; LED1=1; DelayMs(100); LED2=0; DelayMs(100); } } /*------------------------------------------------ 定时器中断函数 ------------------------------------------------*/ void Time0(void) interrupt 1 { TH0=0xc6; //定时16ms TL0=0x66; WDTRST=0x1e; //喂狗指令 WDTRST=0xe1; } /*------------------------------------------------ uS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时 长度如下 T=tx2+5 uS ------------------------------------------------*/ void DelayUs2x(unsigned char t) { while(--t); } /*------------------------------------------------ mS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编 ------------------------------------------------*/ void DelayMs(unsigned char t) { while(t--) { //大致延时1mS DelayUs2x(245); DelayUs2x(245); } }