/*******************************************************************************
* Device      : AD18F06A
* Author      :
* Version     :
* Date        :
* Description :
********************************************************************************/

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


unsigned char  ADC_INT_Flag = 0;
unsigned int  VOLT;
//PWM10 -> PA0 PWM11 ->PA1  TX ->PB2    AD_PIN ->  PB0
//Uart -> PB2  BPS-> 9600
void main (void)
{
	GPIO_Init();      	//初始化IO

	usart0_init();      //UART初始化

	timer3_PWM2();		//timer3初始化为PWM输出，周期4ms，占空比70%
	Config_ADC();		//配置PB0为模拟量输入口


	printf("AD18F06 TIMER3_PWM2_ADC6 DEMO\r\n");

	IPEN = 1;           //允许中断优先级
	GIEL = 1;           //开总中断

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


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

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

	if(ADIE == 1 && ADIF == 1)
	{
		GIE  = 0;
		ADIF = 0;
		ADC_INT_Flag = 1;
	}
}

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

}

