uint16 read_AD(uint8 channel)
{
uint8 AD_finished=0;
uint16 h_val;
uint16 l_val;
ADC_RES = 0;
ADC_RESL = 0;
channel &= 0x01;
ADC_CONTR = AD_SPEED; //转换速度90个时钟一次
delay_us(1);
ADC_CONTR |= channel; //选通道、
delay_us(1);
ADC_CONTR |= 0x80; //开ADC电源
delay_us(1);
ADC_CONTR |= 0x08; //启动AD转换
AD_finished = 0;
while ( AD_finished ==0 )
{
AD_finished = (ADC_CONTR & 0x10); //查询标志位
}
ADC_CONTR &= 0xE7;//标志手动清0
h_val = ADC_RES;//AD寄存器高8位。
h_val = h_val<<2;
l_val = ADC_RESL & 0X03; //AD寄存器低8位,2位有效。
h_val = h_val | l_val;
h_val=(h_val*5/1024)*1000; //换算成电压放大1000倍
return (h_val);
}