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

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

#include "AD18F06A.h"
#include <stdio.h>
#include "common.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_2T_3L        & _WDTEN_OFF_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 );

#define RX0_PIN TRISB1
#define TX0_PIN TRISB2
#define BAUD0 9600
#define FSYS 16000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define DIVIDER0 ((int)(FSYS/(16UL * BAUD0)))
//#define DIVIDER  1
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

void usart0_init()
{
    UART0CLKEN = 1;
	RX0_PIN    = 1;
	TX0_PIN    = 0;

	PORTB|=0X06;
	PBPU&=~(1<<1);
	TX0_MAP=(1<<3)|0X02;	//PB2_TX
	RX0_MAP=(1<<3)|0X01;	//PB1_RX
	SPBRG     = DIVIDER0;
	RCSTA     = 0x90;
	TXSTA     = 0x26;//24
}


unsigned char  ADC_INT_Flag = 0;
unsigned int  VOLT;
/*
//TIMER1定时器与CCP配置，使用比较模式，定时启动ADC采集功能 ADC->PB0 UART->PB2 BPS->9600
*/
void main (void)
{
	GPIO_Init();    		//IO初始化
	usart0_init();

	ADCCLKEN=1;
	TRISB0 = 1;PB0PD = 1;PB0PU = 1;PB0OD = 0;PB0AEN = 1;	//PB0为模拟输入口
	ADRESH0=0x00;
	ADCON0=0x01;
	ADCON1=0xBE;
	ADCON3=0X00;

	timer1_CCP_Pwm_Init();  //timer1初始化为比较模式，周期10ms，采集ADC

	CCPIP = 1;	//CCP中断高优先级
	CCPIF = 0;	//CCP中断标志清0
	CCPIE = 1;	//使能CCP中断

	IPEN  = 1;	//高优先级中断使能位
	GIEL  = 0; 	//禁止所有低优先级的外设中断
	GIEH  = 1; //允许所有高优先级中断

	printf("18F06A TIMER1 CCP AD DEMO\r\n");

	while(1)
	{
		if(ADC_INT_Flag == 1)
		{
				ADC_INT_Flag   = 0;                 //清除标志，为下一次进入中断做准备
				GIEH   = 0;                          //暂时关闭中断，防止打印出错
				VOLT = ((ADRESH0& 0xf)<<8)|ADRESL0;  //读AD值
				printf("VOLT = %d \r\n",VOLT);      //打印AD转换数据
				GIEH  = 1;                           //开启中断
		}
	}
}



void putch( char byte)
{
	while(!TXIF)		continue;
	TXREG = byte;
}


unsigned char getch()
{
	/* retrieve one byte */
	while(!RCIF)	/* set when register is not empty */
		continue;
	return RCREG;
}



unsigned char getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}


//高优先级中断服务函数
void interrupt ISR_h(void)
{
//进入中断后系统自动关闭中断
	//进入中断后系统自动关闭中断
	if(CCPIE == 1 && CCPIF == 1)
	{
		GIEH  = 0;
		CCPIF = 0;
		ADC_INT_Flag = 1;
		//PB0 = ~PB0;
	}
}

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