{
P_SW2 |= 0x80;
ADCTIM = 0x3f; // Set ADC internal timing
P_SW2 &= 0x7f;
ADCCFG = 0x2f; //Set the ADC clock to the system clock/2/16
ADC_CONTR = 0x8f; //Enable ADC module, and select channel 15
}
int ADCRead()
{
int res;
ADC_CONTR |= 0x40; //Start AD conversion
_nop_();
_nop_();
while (!(ADC_CONTR & 0x20)); //Query ADC completion flag
ADC_CONTR &= ~0x20; //Clear completion flag
res = (ADC_RES << 8) | ADC_RESL; //Read ADC results
return res;
}
void main()
{
int res;
int vcc;
int i;
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
BGV = (int idata *)0xef;
ADCInit(); //ADC initialization
UartInit(); //UART initialization
ES = 1;
EA = 1;
// ADCRead();
// ADCRead(); //Discard the first two data
res = 0;
for (i=0; i<8; i++)
{
res += ADCRead(); //Read data 8 times
}
res >>= 3; //take the average