/*******************************************************************************
//CCP_TIMER1 捕获引脚 -> PB4    Uart_Tx -> PB0，波特率 = 9600
********************************************************************************/

#include "AD18F06A.h"
#include <stdio.h>
#include "Periph.h"
#include "usart.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_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_ON_4L       & _EEDIS_OFF_4L       & _FLDIS_OFF_4L      & _FCMEN_OFF_4L  & _IESO_OFF_4L&_DBGS_IOSEL_1_4L);


//捕获引脚 -> PB4    Uart_Tx -> PB0，波特率 = 9600
volatile unsigned int Value_temp;
bit ccp_flag;

void main (void)
{
    Value_temp = 0;
    ccp_flag = 0;
	GPIO_Init();    		//IO初始化
	TRISD0 = 0;				//PD0输出模式
    TIMER1_init();
	CCP_Init();             //捕获通道选择PB4
	usart0_init();
    INTER_init();
	printf("AD18F06_CCP_TIMER1-RST\r\n\r\n");

	while(1)
	{
        if(ccp_flag  == 1)
        {
            ccp_flag = 0;

            Value_temp = (CCPR1H << 8)+CCPR1L;
            GIE = 0;
            printf("Value_temp = %d us \r\n",Value_temp);
            GIE = 1;
            Value_temp = 0;
        }
	}
}

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


void interrupt ISR_h(void)
{
//进入中断后系统自动关闭中断
	if((CCPIF == 1) && (CCPIE == 1))
	{
		CCPIF = 0;
		PD0 =~ PD0;
        ccp_flag = 1;
	}
}

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