我用的是AD7799BRUZ芯片,打算应用于称重。 电路连接参照数据手册(Rev.0) 27页的那个电路, 我只用了AIN1一个通道,其余未用。
出现的问题是:采不出数据。
进行满量程校准后读出的数据全为FF,读状态寄存器有错误标志。把REF_DEC置位后有NOREF错误标志,可是我的参考电压接是是正5V啊,是不是AD7799内部的参考电压电路部分损坏了啊!! #include <intrins.h> /* use _nop_() function */ /* AD7799写寄存器函数 WriteData:要写的数据 */ void WriteByteToAd7799(unsigned char WriteData) { unsigned char i; //AD7799_CS=0; for(i=0;i<8;i++) { AD7799_SCLK=0; if(WriteData&0x80)AD7799_DIN=1; else AD7799_DIN=0; WriteData=WriteData<<1; AD7799_SCLK=1; } //AD7799_CS=1; }
/* AD7799读寄存器函数 */ unsigned char ReadByteFromAd7799(void) { unsigned char i; unsigned char ReadData; //AD7799_CS=0; ReadData=0; for(i=0;i<8;i++) { AD7799_SCLK=0; ReadData=ReadData<<1; if(AD7799_DOUT)ReadData+=1; AD7799_SCLK=1; } //AD7799_CS=1; return(ReadData); }
void WaiteRDY(void) { unsigned int iint; iint=0; while(AD7799_RDY) { iint++; if(iint>20000) { //reset ad7799 WriteByteToAd7799(0xff); WriteByteToAd7799(0xff); WriteByteToAd7799(0xff); WriteByteToAd7799(0xff); for(iint=0;iint<1000;iint++) { _nop_(); _nop_(); } Ad7799_Ini(0); break; } } }
void Ad7799_Ini(unsigned char ChannelNum) { WriteByteToAd7799(0x10);//b0000 1000 /* Writes to Communications Register Setting Next Operation as Write to CONFIGURATION Register*/ WriteByteToAd7799(0x00); //增益为0 WriteByteToAd7799(0x10+ChannelNum); //1通道 /*CONFIGURATION REGISTER[00,BO(0),U/B(0), 0(0),G2(1),G1(1),G0(1),0,0,REF_DET(0),BUF(1),0(0),CH2(0),CH1(0),CH0(0)]*/
WriteByteToAd7799(0x08); //b0000 1000 /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/ WriteByteToAd7799(0x80); WriteByteToAd7799(0x0a); /* Writes to Mode Register Initiating Internal Zero-Scale Calibration*/ WaiteRDY(); /* Wait for RDY pin to go low to indicate end of calibration cycle*/ WriteByteToAd7799(0x08); /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/ WriteByteToAd7799(0xa0); WriteByteToAd7799(0x0a); /* Writes to Mode Register Initiating Internal Full-Scale Calibration*/ WaiteRDY(); /* Wait for RDY pin to go low to indicate end of calibration cycle*/
WriteByteToAd7799(0x08); //b0000 1000 /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/ WriteByteToAd7799(0x00); WriteByteToAd7799(0x0A); /* Mode Register[MD2(0),MD1(0),MD0(0),PSW(0),0(0),0(0),0(0),0(0), (0),(0),0(0),0(0),FS3(1),FS2(0),FS1(1),FS0(0)]*/ /*模式0 Continuous-Conversion Mode.,Fadc=16.7HZ;*/ }
long ReadAd7799ConversionData(void) { long ConverData; unsigned char ADSAT; unsigned int iint;
WaiteRDY(); WriteByteToAd7799(0x40); ADSAT=ReadByteFromAd7799(); if((ADSAT&0x40)||(ADSAT& 0x08)==0) //出错或者读写异常 { WriteByteToAd7799(0xff); WriteByteToAd7799(0xff); WriteByteToAd7799(0xff); WriteByteToAd7799(0xff); for(iint=0;iint<3000;iint++) { _nop_(); } Ad7799_Ini(0); return(0); } else { WriteByteToAd7799(0x58);
/* Writes to Communications Register Setting Next Operation as Continuous Read From Data Register*/ WaiteRDY(); /* Wait for RDY pin to go low to indicate end of calibration cycle*/ if(!AD7799_RDY) { ConverData=0; ConverData=ReadByteFromAd7799(); ConverData=ConverData<<8; ConverData=ReadByteFromAd7799()+ConverData; ConverData=ConverData<<8; ConverData=ReadByteFromAd7799()+ConverData; } /* Read Conversion Result from AD7730's Data Register*/
return(ConverData);
} }
#include <reg52.h> #include "ad7799.h"
idata long addata;
main() { Ad7799_Ini(0); while(1) { addata=ReadAd7799ConversionData(); } }
|