#include "wdt.h"

/*WDT_TIME 	= Twdtclk * (wdttime+1) * 4096
						= (wdttime+1) * 0.128s
wdttime :0 ~ 63
*/
void WDT_Init( uint8_t wdttime)
{
	WDTCON = wdttime << 2; 		//WDT复位时间
	WDTCON |= 0x02;			//WDT使能
	//reload
	WDTRLD = 0x5A;		//清空WDT计数器
	WDTRLD = 0x3C;
	
}

void WDT_RLD(void)
{	
	//reload
	 WDTRLD = 0x5A;
	 WDTRLD = 0x3C;
}

/*WDT_TIME 	= Twdtclk * (wdttime*4+3)
						= (wdttime+1) * 0.000125s
wdttime :0 ~ 256
*/
void WDTTimer_Init( uint8_t wdttimer)
{
		WDTWK = wdttimer;		//看门狗唤醒寄存器 计数唤醒值
		CLKEN1 |= 0x01;  //wdt时钟使能
		INTCTRL |= 0x40;		//INT0选择WDT匹配信号
//		INTCTRL |= 0x01;		//INT0下降沿产生中断
		IEN0 |= 0x01;    //使能WDT唤醒中断
		EA = 1; 	
		WDTCON |= 0xFC;	//设置WDTTIME为最大值，防止唤醒后继续计数触发复位
		WDTCON |= 0x02;		//使能看门狗		WDTCON必须在看门狗启动前配置
		//计数器重载
		WDT_RLD();
}

