⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ad11.c

📁 AD7705的驱动程序
💻 C
字号:

#include <src51rd.h>
//#include<reg51.h>
#include<absacc.h>
#include<intrins.h>
//#include<math.h>

sbit ADC_CLK  = P1^3;
sbit ADC_DIN  = P1^2;
sbit ADC_DOUT = P1^1;
sbit ADC_DRDY = P1^0;
//=================================================================
//during reading AD7705,prohibit interrupt siginal from disturbing the scheduling
//parameter in eeprom:gain
//=================================================================
//write AD7705

void WriteAD7705(unsigned char aa)	//通过串口给ad7705发送命令字a
{
    unsigned char data i;

    for(i=0; i<8; i++)
    {
        ADC_CLK = 1;
        _nop_();
        ADC_CLK = 0;
        ADC_DIN = ((aa&0x80)==0x80);	//prepare the data for DIN interface
        aa = (aa<<1);
        ADC_CLK = 1;			//write
    }
}

//=================================================================
//read AD7705

//branch1:read data of 8 bits-length

unsigned char ReadAD7705_8(void)
{
    unsigned char out = 0;
    bit Bit_DOUT;			//AD7705: Dout footprint
    unsigned char data i;

    while(ADC_DRDY == 1);		//wait for the falling edge
    for(i=0; i<8; i++)
    {
        ADC_CLK = 1;
        _nop_();
        ADC_CLK = 0;
        _nop_();
        _nop_();
        Bit_DOUT = ADC_DOUT;		//read data
        out += (Bit_DOUT == 1);
        ADC_CLK = 1;
    }
    return(out);
}

//-----------------------------------------------------------------
//branch2:read data of 16 bits-length

unsigned int ReadAD7705_16(void)
{
    unsigned int out = 0;
    bit Bit_DOUT;			//AD7705: Dout footprint
    unsigned char data i;

    while(ADC_DRDY == 1);		//wait for fall edge
    for(i=0; i<16; i++)
    {
        ADC_CLK = 1;
        _nop_();
        ADC_CLK = 0;
        _nop_();
        _nop_();
        Bit_DOUT = ADC_DOUT;		//read data
        out += (Bit_DOUT == 1);
        ADC_CLK = 1;
    }
    return(out);
}

//-----------------------------------------------------------------
//branch3:read data of 24 bits-length

unsigned long ReadAD7705_24(void)
{
    unsigned long out = 0;
    bit Bit_DOUT;			//AD7705: Dout footprint
    unsigned char data i;

    while(ADC_DRDY == 1);		//wait for fall edge
    for(i=0; i<24; i++)
    {
        ADC_CLK = 1;
        _nop_();
        ADC_CLK = 0;
        _nop_();
        _nop_();
        Bit_DOUT = ADC_DOUT;		//read data
        out += (Bit_DOUT == 1);
        ADC_CLK = 1;
    }
    return(out);
}

//=================================================================
//ad7705初始化

void AD7705_Init(void)
{
    unsigned char i;
    
    ADC_CLK = 1;		//防止接口迷失
    ADC_DIN = 1;
    for(i=0; i<100; i++)	//prevent interface from losting(at least 32 serial clock cycles)
    {
        ADC_CLK = 0;
        ADC_CLK = 1;
    }
    
    WriteAD7705(0x20);	//write 0x20 to communication register to choose channel 0
  			//and clock register for the next one to write
    WriteAD7705(0x04);	//write 0x04 to clock register, specify the crystal as 2.4576MHz, set output rate be 50Hz.
    
    WriteAD7705(0x10);	//write 0x10 to communication register to choose channel 0
  			//and setup register for the next one to write
    WriteAD7705(0x46);	//write 0x44 to setup register, specify the gain as 1, buffer off,FSYNC=0, and self-calibration
}

//=================================================================
//calibration
//when either of the calibration is performed,gain become 1, the tube with standard sample is placed in the tube socket
//zero-scale system calibration

unsigned long Zero_Calibr(void)
{
    WriteAD7705(0x10);	//write 0x10 to communication register to choose channel 0
    			//and setup register for next one to write
    WriteAD7705(0x86);	//write 0x86 to setup register to choose zero-scale system calibration,
    			//gain setting equal to 1,unipolar operation,filter start
    //while(ADC_DRDY == 0);	//the DRDY output or bit goes hith when calibration is initialed
    while(ADC_DRDY == 1);	//and returns low when this zero-scale calibration is completed
    
    WriteAD7705(0x68);	//write 0x68 to communication register to choose channel 0
    			//and offset register for next one to read
    return(ReadAD7705_24());
}

//-----------------------------------------------------------------
//full-scale system calibration

unsigned long Full_Calibr(void)
{
    WriteAD7705(0x10);	//write 0x10 to communication register to choose channel 0
    			//and setup register for next one to write
    WriteAD7705(0xc6);	//write 0xc0 to setup register to choose full-scale system calibration,
    			//gain setting equal to 1,unipolar operation,filter start    
    
    //while(ADC_DRDY == 0);	//the DRDY output or bit goes high when calibration is initialed
    while(ADC_DRDY == 1);	//and returns low when this zero-scale calibration is completed
    
    WriteAD7705(0x78);	//write 0x78 to communication register to choose channel 0
    			//and gain register for next one to read
    return(ReadAD7705_24());
}

//=================================================================
//read data register

unsigned int ReadAD7705_Data(void)
{
    WriteAD7705(0x38);	//write 0x38 to communication register to choose channel 0
    			//and data register for next to read
    
    return(ReadAD7705_16());	//read data register
}
//=================================================================
//change gain of AD7705 manually
//gain: xx=1,2,4,8,16,32,64,128

void AD7705_GainManu(unsigned char xx)
{   
    WriteAD7705(0x10);	//write 0x10 to communication register to choose channel 0
    			//and setup register for next one to write
    WriteAD7705( (xx<<3)|0x06 );
}

//=================================================================
/*
void main(void)
{
    unsigned int data_out;

    AD7705_Init();

    while(ADC_DRDY==0)
    {
        WriteAD7705(0x1c);		//给通讯寄存器发0x38,指定下一个读数据寄存器
        data_out=ReadAD7705();		//从数据寄存器中读16位数据
        voltage=5.0*(data_out/65536.0);	//将所读数据转换为电压值
        display();
    }
}
*/
//=================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -