#include "STC12C5A60S2.h"
//#include <stdio.h>
#include <intrins.h>
/*Define ADC operation const for ADC_CONTR*/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //420 clocks
#define ADC_SPEEDL 0x20 //280 clocks
#define ADC_SPEEDH 0x40 //140 clocks
#define ADC_SPEEDHH 0x60 //70 clocks
void InitADC();
unsigned int GetADCResult(unsigned char x);
static void Delay(unsigned int n);
/*----------------------------
Get ADC result
----------------------------*/
unsigned int GetADCResult(unsigned char x)
{
if(x>7)
return 0;
P1ASF |= 0x01<<x;//sol
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | x | ADC_START;
_nop_(); //Must wait before inquiry
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
//P1ASF &= ~(0x01<<x);//sol
return ( (unsigned int)ADC_RES<<2 )| ADC_RESL; //Return ADC result
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC(void)
{
ADC_RES = ADC_RESL = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDHH;
Delay(2); //ADC power-on and delay
}
/*----------------------------
Software delay function
----------------------------*/
static void Delay(unsigned int n)
{
unsigned int x;
while (n--)
{
x = 5000;
while (x--);
}
}