#i nclude sbit AD7730_CS=P0^4;
sbit AD7730_SCLK=P0^0;
sbit AD7730_DIN=P0^1;
sbit AD7730_DOUT=P0^2;
sbit AD7730_RDY=P0^3;
unsigned char ReadByteFromAd7730(void);
void Ad7730_Ini(void);
long ReadAd7730ConversionData(void);/*
AD7730寫寄存器函數(shù)
WriteData:要寫的數(shù)據(jù)
*/
void WriteByteToAd7730(unsigned char WriteData)
{
unsigned char i;
AD7730_CS=0;
for(i=0;i<8;i++)
{
AD7730_SCLK=0;
if(WriteData&0x80)AD7730_DIN=1;
else AD7730_DIN=0;
WriteData=WriteData<<1;
AD7730_SCLK=1;
}
AD7730_DIN=0;
AD7730_CS=1;
}/*
AD7730讀寄存器函數(shù)
*/
unsigned char ReadByteFromAd7730(void)
{
unsigned char i;
unsigned char ReadData;
AD7730_CS=0;
AD7730_DIN=0;
ReadData=0;
for(i=0;i<8;i++)
{
AD7730_SCLK=0;
ReadData=ReadData<<1;
if(AD7730_DOUT)ReadData+=1;
AD7730_SCLK=1;
}
AD7730_CS=1;
return(ReadData);
}void Ad7730_Ini(void)
{
unsigned char i;
WriteByteToAd7730(0x03);
/* Writes to Communications Register Setting Next Operation as Write to
Filter Register*/
WriteByteToAd7730(0x80);
WriteByteToAd7730(0x00);
WriteByteToAd7730(0x10);
/* Writes to Filter Register Setting a 50 Hz Output Rate in CHOP Mode*/
WriteByteToAd7730(0x04);
/* Writes to Communications Register Setting Next Operation as Write to
DAC Register*/
WriteByteToAd7730(0x20);
/* Writes to DAC Register Setting a Subtraction Value of 0 mV (5 V Reference)
on the TARE DAC*/
WriteByteToAd7730(0x14);
i=ReadByteFromAd7730();
WriteByteToAd7730(0x02);
/* Writes to Communications Register Setting Next Operation as Write to
Mode Register*/
WriteByteToAd7730(0xb1);
WriteByteToAd7730(0x80);
/* Writes to Mode Register Initiating Internal Full-Scale Calibration for 0 mV
to +10 mV Input Range*/
while(AD7730_RDY)RstWDT();
/* Wait for RDY pin to go low to indicate end of calibration cycle*/
WriteByteToAd7730(0x02);
/* Writes to Communications Register Setting Next Operation as Write to
Mode Register*/
WriteByteToAd7730(0x91);
WriteByteToAd7730(0x80);
/* Writes to Mode Register Initiating Internal Zero-Scale Calibration for
0 mV to +10 mV Input Range*/
while(AD7730_RDY)RstWDT();
/* Wait for RDY pin to go low to indicate end of calibration cycle*/
WriteByteToAd7730(0x02);
/* Writes to Communications Register Setting Next Operation as Write to
Mode Register*/
WriteByteToAd7730(0x31);
WriteByteToAd7730(0x80);
/* Writes to Mode Register Starting Continuous Conversions for 0 mV to
+10 mV Input Range*/
while(AD7730_RDY)RstWDT();
/* Wait for RDY pin to go low to indicate end of calibration cycle*/
}long ReadAd7730ConversionData(void)
{
long ConverData;
WriteByteToAd7730(0x21);
/* Writes to Communications Register Setting Next Operation as Continuous
Read From Data Register*/
AD7730_DIN=0;
while(AD7730_RDY)RstWDT();
/* Wait for RDY pin to go low to indicate end of calibration cycle*/
if(!AD7730_RDY)
{
ConverData=0;
ConverData=ReadByteFromAd7730();
ConverData=ConverData<<8;
ConverData=ReadByteFromAd7730()+ConverData;
ConverData=ConverData<<8;
ConverData=ReadByteFromAd7730()+ConverData;
}
/* Read Conversion Result from AD7730's Data Register*/
WriteByteToAd7730(0x30);
/* Ends Continuous Read Operation and Places Part in Mode Where It
Expects Write to Communications Register*/
return(ConverData);
}