/*_____ I N C L U D E S _________________________________*/
#include "adc.h"

/*_____ F U N C T I O N S _______________________________*/

/*_____ D E F I N I T I O N S ___________________________*/

/*_____ D E C L A R A T I O N S _________________________*/
bit bADC_IRQFlag;

/*_____ M A C R O S _____________________________________*/


/**
 *	@Brief		Initialize GPIO status
 *	@param		None
 *	@retval		None
 */
void GPIO_Initialization(void)
{
	/*	Set whole IOs to output low	*/
	IO_PORT0_OUTPUT_LOW
	IO_PORT1_OUTPUT_LOW
	IO_PORT2_OUTPUT_LOW
	IO_PORT3_OUTPUT_LOW
}

/**
 *	@Brief		Initialize ADC status
 *	@param		None
 *	@retval		None
 */
void ADC_Initialization(uint8_t bChannelSel, uint8_t bConvertingTime)
{
  /*	Configures ADC function register	*/
  ADM = mskADM_ADC_ENALBE | bChannelSel;
	ADR = mskADR_ADC_CLOCK_FHOSC | bConvertingTime;
	ADR |= mskADR_ADC_GLOBALCHS;
  
  /*	Enable ADC interrupt	*/
  IEN2 = mskIEN2_INT_ADC;
}

/**
 *	@Brief		Start to convert ADC
 *	@param		None
 *	@retval		None
 */
void ADC_StartConverting(void)
{
	ADM	|= mskADM_ADC_START;	
  
  bADC_IRQFlag = 0;
}

/**
 *	@Brief		Check ADC convert status
 *	@param		None
 *	@retval		bADC_IRQFlag
 */
bit ADC_CheckStatus(void)
{
	if(((IEN2&mskIEN2_INT_ADC)==0x00) && (IRCON2&mskIRCON2_ADC_IRQ))
  {
    ADC_ClearFlag();
    bADC_IRQFlag = 1;	
  }
    
  return bADC_IRQFlag;
}

/**
 *	@Brief		Clear ADC flag
 *	@param		None
 *	@retval		None
 */
void ADC_ClearFlag(void)
{
  IRCON2 &= ~mskIRCON2_ADC_IRQ;
}

/**
 *	@Brief		Check ADC convert status
 *	@param		None
 *	@retval		dbAdcData
 */
uint16_t ADC_GetAdcValue(void)
{
  uint16_t	dbAdcData;
  /*	Read ADB and ADR register to get ADC value	*/
	dbAdcData = (ADB <<4) | (ADR&0x0F);			
  
  return dbAdcData;
}


/**
 *	@Brief		ISR_Adc
 *						ADC interrupt service routine
 */
void ISR_Adc(void) interrupt ISRAdc
{		
  bADC_IRQFlag = 1;
}