/*******************************************************************************
//TIMER1  WDT作为时钟源，T = 777ms
********************************************************************************/

#include "AD18F06A.h"
#include <stdio.h>


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


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

__CONFIG(1,_FINTOSC_DIV1_1L   & _LVREN_ON_1L       & _LVR24V_1L                                                                                         );
__CONFIG(2,_FLASH_CP7_OFF_2L  & _FLASH_CP6_OFF_2L  & _FLASH_CP5_OFF_2L   & _FLASH_CP4_OFF_2L   & _FLASH_CP3_OFF_2L
         & _FLASH_CP2_OFF_2L  & _FLASH_CP1_OFF_2L  & _FLASH_CP0_OFF_2L   & _CRYLEN_OFF_2L      & _CRYHEN_OFF_2L
  	     & _IDIS_ON_2L        & _CKSUM_OFF_2L      & _CBP_OFF_2L         & _RESETE_OFF_2L      & _RSEL_LOW_2L     & _STV_ON_2L              	        );
__CONFIG(3,_DBG_OFF_3L        & _PSUT_8_3L         & _FCPU_4T_3L         & _WDTEN_ON_3L       & _TWDT_000_3L                                            );
__CONFIG(4,_OSCM_HIRC_16M_4L  &_ENVDDL_ON_4L      & _EEDIS_OFF_4L       & _FLDIS_OFF_4L       & _FCMEN_OFF_4L    & _IESO_OFF_4L&  _DBGS_IOSEL_1_4L      );


void GPIO_Init(void)
{
	PAOD = 0X00;
	PBOD = 0X00;
	PCOD = 0X00;
	PDOD = 0X00;

	PAPU = 0XFF;
	PBPU = 0XFF;
	PCPU = 0XFF;
	PDPU = 0XFF;

	PAPD = 0XFF;
	PBPD = 0XFF;
	PCPD = 0XFF;
	PDPD = 0XFF;

	TRISA=0X00;
	TRISB=0X00;
	TRISC=0X00;
	TRISD=0X00;

	PORTA=0X00;
	PORTB=0X00;
	PORTC=0X00;
	PORTD=0X00;

	ANASEL0 = 0X00; //关PA模拟量通道
	ANASEL1 = 0X00; //关PB模拟量通道
	ANASEL2 = 0X00; //关PC模拟量通道
}

void timer1_Init(void)
{
	T1CLKEN = 1;     //开T1时钟

	TMR1H = 0XFF;    //t = 384ms  WDT作为TIMER1计数时钟，从0xFFFD到0x0000有三个数，即128ms*3=384ms
	TMR1L = 0XFD;

	PREDIV2 = 0;
	PREDIV1 = 0;
	PREDIV0 = 1;

	T1CON0 = 0X23;   //WDT  1分频  使能timer1
	T1CON1 = 0X00;   //工作在普通模式

	TMR1IF = 0;      //清中断标志
	TMR1IP = 1;		 //高优先级中断
	TMR1IE = 1;      //开TMR1中断
}




void main (void)
{
	unsigned char i;

	GPIO_Init();      //初始化IO
	TRISD7 = 0;
    timer1_Init();    //timer1初始化

	IPEN = 1;        //允许中断优先级
	GIEH = 1;        //开高优先级中断
	GIEL = 1;        //开低优先级中断


	while(1)
	{
		for ( i = 0; i < 100; i++)
		{
			PB0 = ~PB0;
		}

		// SLEEP_DEEPPWSAVE();
	}
}


//高优先级中断服务函数

void interrupt ISR_h(void)
{
//进入中断后系统自动关闭中断
	if((TMR1IE == 1)&&(TMR1IF == 1))      //当tmr1定时时间到，产生中断时，PD7翻转
	{
		TMR1IF = 0;
		PD7 = ~PD7;
	}
}

//低优先级中断服务函数
void interrupt low_priority ISR_l (void)
{
//进入中断后系统自动关闭中断

}

