AD7799例子

例子一:

sbit AD7799_CS=P1^4;
sbit AD7799_RDY=P1^6;

//SPCR SPI控制寄存器

//SPSR SPI状态寄存器

//SPDAT SPI数据寄存器
void SPI_init(void)
{
SPCR=0x5e;//SPI控制寄存器,中断禁止,SPI使能,高位在前,主机模式,时钟空闲时为高,后沿触发移位,时钟分频64
SPSR=0x00;//清中断标志位
}
/*--------------------------------------------
写AD7799寄存器函数
WriteData:要写的数据
----------------------------------------------*/
void WriteByteToAd7799(unsigned char WriteData)
{
SPDAT= WriteData;
while(~SPSR&0x80); //等待数据发送完
SPSR=0x00; //清中断标志位

}
/*--------------------------------------------
防止时序混乱,实现再同步
----------------------------------------------*/
void WaiteRDY(void)
{
unsigned int count=0 ;
while(AD7799_RDY)
{
count++;
if(count>20000)
{
//reset ad7799
WriteByteToAd7799(0xff);

WriteByteToAd7799(0xff);
/*----------防止时序混乱,重新同步----------*/
WriteByteToAd7799(0xff);

WriteByteToAd7799(0xff);

AD7799_init();
break ;
}
}
}

/*--------------------------------------------
AD7799初始化函数
----------------------------------------------*/
void AD7799_init(void)
{
AD7799_CS=0;
/*------------------------增益为128,通道0----------------------------------------*/
WriteByteToAd7799(0x10); //写通信寄存器设置下一个操作为写配置寄存器

WriteByteToAd7799(0x37); //增益为128

WriteByteToAd7799(0x30); //通道0

/*------------------- 写模式寄存器初始化零值校准------------------------------------*/
WriteByteToAd7799(0x08); //写通信寄存器设置下一个操作为写模式寄存器

WriteByteToAd7799(0x80);

WriteByteToAd7799(0x0A);

WaiteRDY(); //Wait for RDY pin to go low to indicate end of calibration cycle*/
/*------------------写模式寄存器初始化全值校准-------------------------------------*/
WriteByteToAd7799(0x08); //写通信寄存器设置下一个操作为写模式寄存器

WriteByteToAd7799(0xA0);

WriteByteToAd7799(0x0A);

WaiteRDY(); // Wait for RDY pin to go low to indicate end of calibration cycle
/*------------------模式0,Continuous-Conversion Mode,Fadc=16.7HZ------------------*/
WriteByteToAd7799(0x08); //写通信寄存器设置下一个操作为写模式寄存器

WriteByteToAd7799(0x00);

WriteByteToAd7799(0x0A);

}
unsigned long ReadAd7799ConversionData(void)
{
unsigned long ConverData;
unsigned char ADSAT ;
unsigned char ErrNUM=0;
WaiteRDY();

WriteByteToAd7799(0x40); //写通信寄存器设置下一个操作为读状态STATUS寄存器

WriteByteToAd7799(0xff); //伪写通信寄存器,为读状态寄存器提供时钟

ADSAT=SPDAT; //读取接收到的数据
while((ADSAT&0x40)||(!(ADSAT&0x08))) //出错或者读写异常
{
//reset ad7799
WriteByteToAd7799(0xff);

WriteByteToAd7799(0xff);
/*----------防止时序混乱,重新同步----------*/
WriteByteToAd7799(0xff);

WriteByteToAd7799(0xff);

//-------------------------------------------------------------------------------------
AD7799_init();

WaiteRDY();
WriteByteToAd7799(0x40); //写通信寄存器设置下一个操作为读状态STATUS寄存器

WriteByteToAd7799(0xff); //伪写通信寄存器,为读状态寄存器提供时钟

ADSAT=SPDAT; //读取接收到的数据

ErrNUM++;
if(ErrNUM>5)break;
}

WriteByteToAd7799(0x58); //写通信寄存器设置下一个操作为连续读数据寄存器

WaiteRDY();
/* Wait for RDY pin to go low to indicate end of calibration cycle*/
if(!AD7799_RDY)
{
ConverData=0 ;
/*-----------------Read Conversion Result from AD7799's Data Register----------------*/

WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
ConverData=SPDAT;
ConverData=ConverData<<8 ;

WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
ConverData=ConverData+SPDAT;
ConverData=ConverData<<8 ;

WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
ConverData=ConverData+SPDAT;

}
if(ErrNUM>5)return(0);
else return(ConverData);
}

http://apps.hi.baidu.com/share/detail/31679497

例子二:

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();
}
}
http://hi.baidu.com/wkpthe/blog/item/9ea741da1c04cad1b7fd4859.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值