/*******************************************************************************
* Device      : AD18F06A
* Author      : AD Application Team
* Version     : V0.0.1
* Date        : 20220615
* Description :
* Note        : None
********************************************************************************/
#include "AD18F06A.h"
#include <stdio.h>
#include "common.h"
#include "usart.h"

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


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

__CONFIG(1,_FINTOSC_DIV1_1L   & _LVREN_OFF_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_OFF_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_OFF_3L       & _TWDT_011_3L                                            );
__CONFIG(4,_OSCM_HIRC_16M_4L  &_ENVDDL_OFF_4L       & _EEDIS_OFF_4L       & _FLDIS_OFF_4L       & _FCMEN_OFF_4L      & _IESO_OFF_4L    & _DBGS_IOSEL_1_4L );

//PC2翻转，若停止翻转。则进入休眠状态，具体休眠模式可通过屏蔽的宏选择模式
void main(void)
{
	volatile unsigned char tmp=0;
	unsigned char i;

	usart0_init();  //PB2_TX  PB1_RX

	printf("PWOFF TEST!\r\n\r\n");



    CLKCFG1=0X20;   //uart0
    CLKCFG2=0X00;

	PAOD = 0X00;
	PBOD = 0X00;
	PCOD = 0X00;
	PDOD = 0X00;

	PAPU = 0XFF;
	PBPU = 0XFE;     // PB0 上拉
	PCPU = 0XFF;
	PDPU = 0XFF;

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

	TRISA=0X00;
	TRISB=0X01;      // PB0设为输入
	TRISC=0X00;
	TRISD=0X00;

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

	PBINTMASK=0X01;   //PB0唤醒

	PBIP=1;
    PBIF=0;
    PBIE=1;
    IPEN=1;
    PEIE=1;
    GIEH=1;
	while(1)
	{
		for(i=0;i<10;i++)
		{
			PC2 = ~PC2;
            __delay_us(50);
		}
		PC2 = 0;
		tmp = PINB;

		PBIF = 0;
        PBIE = 1;

		//SLEEP_PWIDLE();               //唤醒时间7.38us
		// SLEEP_PWSAVE();               //唤醒时间9.14us
		// SLEEP_DEEPPWSAVE();           //唤醒时间10.56us
		SLEEP_PWOFF();                  //唤醒时间1.17ms

	}
}


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

void interrupt ISR_h (void)
{
//进入中断后系统自动关闭中断

	if((PBIE==1)&&(PBIF==1)&&(PBIP==1))
	{
	   	PBIF=0;
	}
}


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

}


