|
#include "AD7705.h"
/************************************************
功能:延時(shí)
參數(shù):循環(huán)次數(shù)
返回:無
************************************************/
void adc_delay(unsigned char NUM)
{
for(;NUM>0;NUM--);
}
/************************************************
功能:寫入AD芯片一個(gè)字節(jié)
參數(shù):要寫入的命令或參數(shù)
返回:無
************************************************/
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芯片讀取一個(gè)字(16位)
參數(shù):無
返回:讀取的字(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芯片復(fù)位
參數(shù):無
返回:無
************************************************/
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芯片初始化
參數(shù):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轉(zhuǎn)換碼
參數(shù):通道號(hào) 如:1、2
返回:讀取的AD轉(zhuǎn)換結(jié)果(如果等于零表示正忙)
************************************************/
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;
}
}
|
|