uchar ADC_read_data(uchar CH)
{
uchar i,test,adval;
adval = 0x00;
test = 0x00;
CLK = 0; //初始化
DATI = 1;
_nop_();
CS = 0;
_nop_();
CLK = 1;
_nop_();
if ( CH == 0x00 ) //通道选择
{
CLK = 0;
DATI = 1; //通道0的第一位
_nop_();
CLK = 1;
_nop_();
CLK = 0;
DATI = 0; //通道0的第二位
_nop_();
CLK = 1;
_nop_();
}
else
{
CLK = 0;
DATI = 1; //通道1的第一位
_nop_();
CLK = 1;
_nop_();
CLK = 0;
DATI = 1; //通道1的第二位
_nop_();
CLK = 1;
_nop_();
}
CLK = 0;
DATI = 1;
for( i = 0;i < 8;i++ ) //读取前8位的值
{
_nop_();
adval <<= 1;
CLK = 1;
_nop_();
CLK = 0;
if (DATO)
adval |= 0x01;
else
adval |= 0x00;
}
for (i = 0; i < 8; i++) //读取后8位的值
{
test >>= 1;
if (DATO)
test |= 0x80;
else
test |= 0x00;
_nop_();
CLK = 1;
_nop_();
CLK = 0;
}
// if (adval == test) //比较前8位与后8位的值,如果不相同舍去。若一直出现显示为零,请将该行去掉
dat = test;
nop_();
CS = 1; //释放ADC0832
DATO = 1;
CLK = 1;
return dat;
}
void main()
{
uchar adc = 0;
uint voltage = 0;
uchar LED_buffer1[16] = {" ADC: "};//ADC显示缓冲
uchar LED_buffer2[16] = {"voltage: V "};//电压显示缓冲
LCD_Init(); // 初始化LCD
while(1){
adc = ADC_read_data(0x00);//读取adc值
voltage = adc * 500.0 / 255;//将adc值换算成电压
LED_buffer1[9] = adc / 100 + '0'; //取adc百位
LED_buffer1[10] = adc % 100 / 10 + '0'; //取adc十位
LED_buffer1[11] = adc % 10 + '0'; //取adc个位
LED_buffer2[9] = voltage / 100 + '0'; //取电压个位
LED_buffer2[10] = '.';//小数点
LED_buffer2[11] = voltage / 10 % 10 + '0'; //取电压十分位
LED_buffer2[12] = voltage % 10 + '0'; //取电压百分位
LCD_ShowString(1, 1, &LED_buffer1[0]); //显示字符串
LCD_ShowString(2, 1, &LED_buffer2[0]); //显示字符串
}
}
|