#include "AD7705.h"
/************************************************
功能:延时
参数:循环次数
返回:无
************************************************/
void adc_delay(unsigned char NUM)
{
for(;NUM>0;NUM--);
}
/************************************************
功能:写入AD芯片一个字节
参数:要写入的命令或参数
返回:无
************************************************/
void write_adc_byte(unsigned char chr)
{
unsigned char i;
for(i=0;i<8;i++)
{
ADC_CLK= 0;
chr=chr<<1;
ADC_DI= CY;
ADC_CLK= 1;
}
ADC_CLK = 1;
}
/************************************************
功能:从AD芯片读取一个字(16位)
参数:无
返回:读取的字(16位)
************************************************/
unsigned int read_adc_word()
{
unsigned char i;
unsigned int coder = 0;
for(i=0;i<16;i++)
{
ADC_CLK = 0;
coder = coder<<1;
if(ADC_DO)coder+=1;
ADC_CLK = 1;
}
ADC_CLK = 1;
return(coder);
}
/************************************************
功能:AD芯片复位
参数:无
返回:无
************************************************/
void adc_rest()
{
unsigned char i,j;
ADC_DI = 1;
ADC_CLK = 1;
ADC1_CS = 1;
// ADC2_CS = 1;
ADC_RESET = 0;
for(i=300;i>0;i--)
for(j=300;j>0;j--);
ADC_RESET = 1;
}
/************************************************
功能:AD芯片初始化
参数:calmode 工作模式选择(如:ADC_SELF ……)
gainsetting 输入增益选择(如:ADC_GAIN_128 ……)
operation 单极性/双极性(如:ADC_BIPOLAR ……)
rate 输出更新率(如:ADC_50 ……)
返回:无
************************************************/
void adc1_init(unsigned char calmode, unsigned char gainsetting, unsigned char operation, unsigned char rate)
{
ADC1_CS = 0;
write_adc_byte(0xFF);
write_adc_byte(0xFF);
write_adc_byte(0xFF);
write_adc_byte(0xFF);
ADC1_CS = 1;
adc_delay(100);
ADC1_CS = 0;
write_adc_byte( 0x20 );//
ADC1_CS = 1;
adc_delay(100);
ADC1_CS = 0;
write_adc_byte( rate );//
ADC1_CS = 1;
adc_delay(100);
ADC1_CS = 0;
write_adc_byte( 0x10 );//
ADC1_CS = 1;
adc_delay(100);
ADC1_CS = 0;
write_adc_byte( calmode|gainsetting|operation);//
ADC1_CS = 1;
adc_delay(100);
}
/************************************************
功能:读取AD转换码
参数:通道号 如:1、2
返回:读取的AD转换结果(如果等于零表示正忙)
************************************************/
unsigned int adc1_read_value(unsigned char ch)
{
unsigned int value;
if(!ADC1_DRDY)
{
ADC1_CS = 0;
if(ch == 1)
write_adc_byte(0x38);//
else if(ch == 2)
write_adc_byte(0x39);//
else {ADC1_CS = 1;return 0;}
ADC1_CS = 1;
adc_delay(1);
ADC1_CS = 0;
value = read_adc_word();
ADC1_CS = 1;
return value;
}
else
{
return 0;
}
}
|