/* initialize A/D hardware */
void ADC_init()
{
	ADCF=1;		/* set P1.0 as analog input */
	ADCON &= 0xF8;	/* clear A/D channel select */
	ADCON |= MSK_ADCON_ADEN; /* enable ADC */
}

/* execute A/D conversion
    IN: channel 0-7   the A/D channel we want to measure
   OUT: conversion result - 16bit value
 */
int ADC(unsigned char channel)
{
	int result=0;	

	ADCON |= channel; /* set channel to measure */
	ADCON |= MSK_ADCON_ADSST; /* start A/D input */

	/* wait until conversion has finished  - inline assembler is faster */
	_asm
adcloop:
	mov A,_ADCON
	jb ACC.3,adcloop
	_endasm;

	ADCON &= 0xef;

	/* read A/D value */
	result=((ADDH<<2)+ADDL);
	
	return(result);
}

