//毁握定义与ADC有关的特殊功能寄存器
sfr ADC_CONTR =0xC5;
sfr ADC_DATA =0xC6;
sfr ADC_LOW2 =0xBE;
sfr P1M0 =0x91;
sfr P1M1 =0x92;
sbit p20 =P2^0;
void STC90CxxAD_delay(int timer) //延时函数
{
while(timer--);
}
int Ad_Change(char channel) //AD转换
{
int AD_Result_Temp = 0 ;
P1 = 0xff; //将P1口置高,为A/D转换作准备
ADC_CONTR = ADC_CONTR|0x80; //1000,0000打开A/D转换电源
P1M0 = 0x03; //0000,0011用于A/D转换的P1.x口,先设为开漏
P1M1 = 0x03; //0000,0011P1.0--P1.1先设为开漏。断开内部上拉纤山庆电阻
STC90CxxAD_delay(20); //20
ADC_CONTR = ADC_CONTR&0xE0; //1110,0000 清ADC_FLAG,ADC_START位和低3位
ADC_CONTR = ADC_CONTR|(channel); //设置当前通道号
STC90CxxAD_delay(1); //延时使输唯念入电压达到稳定
ADC_DATA = 0; //清A/D转换结果寄存器
ADC_LOW2 = 0;
ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,启动转换
do { ; }
while((ADC_CONTR & 0x10)==0); //0001,0000等待A/D转换结束
ADC_CONTR = ADC_CONTR&0xE7; //1110,0111清ADC_FLAG位,停止A/D转换
AD_Result_Temp = ((AD_Result_Temp|ADC_DATA)<<2)|(ADC_LOW2&0x03);
//保存返回AD转换的 结果
STC90CxxAD_delay(1); //
return AD_Result_Temp;
}
已调试通过,望对你有用