下面提供一個完整版本的ad7799程序,Keil C寫的ADI的24位A/DAD7799使用SPI接口程序:(VRS51L3074單片機 是一顆基于8051內(nèi)核集成了多種模塊并可多 范圍的在嵌入式設(shè)計中應(yīng)用的芯片,完整的工程文件及hex下載地址:http://www.zg4o1577.cn/f/Keil_C_AD7799.rar )
ad7798,ad7799的詳細(xì)中文資料請看: http://www.zg4o1577.cn/chip/4000.html
#pragma CODE DEBUG SYMBOLS OBJECTEXTEND //SRC 當(dāng)與匯編混合編譯時需要
#include"VRS51L3074_keil.h"
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
typedef union{ //無符號字符型,整形,長整形數(shù)據(jù)聯(lián)合體
uchar uc[2];
uint ui;
}UCI;
typedef union{ //無符號字符型,整形,長整形數(shù)據(jù)聯(lián)合體
uchar uc[4];
uint ui[2];
ulong ul;
}UCIL;
#define DELAY5 delay() //_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
sbit P1_5 = P1^5; //1
sbit P1_6 = P1^6; //2
sbit P1_7 = P1^7; //3
sbit P4_5 = P4^5; //6 pin
sbit P5_0 = P5^0; //7
sbit P5_1 = P5^1; //8
sbit P5_2 = P5^2; //9
sbit P5_3 = P5^3; //10
sbit P5_4 = P5^4; //20
sbit P5_5 = P5^5; //21
sbit P5_6 = P5^6; //22
sbit P5_7 = P5^7; //23
#define AD7799_CS P4_5 //輸出 6pin
#define AD7799_SCLK P5_1 //輸出 8pin
#define AD7799_DOUT P5_2 //輸入 9pin
#define AD7799_RDY P5_2 //輸入 9pin 同上是一個引腳
#define AD7799_DIN P5_0 //輸出 7pin
UCIL MainData,demoul;
ulong xdata XMainData,xdemoul;
void Ad7799_Ini();
#define DELAYS10 _nop_();_nop_();_nop_();_nop_()
void delay(char i)
{uchar j;
for(;i>0;i--); for(j=0;j<10;j++)_nop_();
}
void CPUInit(void)
{
/*I/O口配置:輸入配置為1(配置為1后,內(nèi)部自動上拉到2.5V,輸出配置為0;空腳要配置為0*/
PERIPHEN1=0x18; //BIT7:SPICSEN 6:SPIEN 5:I2CEN 4:U1EN 3:U0EN 2:T2EN 1;T1EN 0:T0EN //TIMER2和TIMER0使能
P0PINCFG=0x00;
P1PINCFG=0x04;
P2PINCFG=0x00; //00100100
P3PINCFG=0x01;
P4PINCFG=0x00;
P5PINCFG=0x04; //00000100
P6PINCFG=0x00;
PORTINEN=0xff; //0xff全部I/O口均不使用
P0=P1=P2=P3=P6=0x00; P4=0x00;P5=0x04;
PERIPHEN2=0x28;//BIN7:PWC1EN 6:PWC0EN 5:AUEN 4:XRAM2CODE 3:IOPORTEN 2:WDTEN 1:PWMSFREN 0:FPIEN 算術(shù)單元、IO口使能*/
P0=P1=P2=P3=P6=0x00; P4=0x00; P5=0x04;
AD7799_CS=1;
UART0CFG=0x90; // 0x09:38400 BRADJ=0x02(19200)/波特率發(fā)生器使用內(nèi)部晶振/9th不使用/1位停止位
UART0INT=0x00; //使能接收中斷,使能發(fā)送完成中斷,允許接收 Enable RX AV + RXOVint + Enable Reception
UART0EXT=0x00; //Not using UART0 Extensions
UART0BRL=0x1f; //0040=19200 001f=38400
UART0BRH=0x00; //波特率19200
U0IEN=1;
GENINTEN = 0x01; //Enable Global interrupt
UART0BUF=0xff;
while(!(UART0INT & 0x01));
UART0BUF=0xff;
while(!(UART0INT & 0x01));
UART0BUF=0xff;
while(!(UART0INT & 0x01));
UART0BUF=0xff;
while(!(UART0INT & 0x01));
UART0BUF=0x0d;
while(!(UART0INT & 0x01));
}
/* AD7799寫寄存器函數(shù)
WriteData:要寫的數(shù)據(jù)*/
void WriteByteToAd7799(unsigned char WriteData)
{
unsigned char i ;
AD7799_CS=0;
for(i=0;i<8;i++)
{
DELAYS10;
AD7799_SCLK=0 ;
DELAYS10;
if(WriteData&0x80)AD7799_DIN=1 ;
else AD7799_DIN=0 ;
WriteData=WriteData<<1 ;
DELAYS10;
AD7799_SCLK=1 ;
DELAYS10;
}
AD7799_CS=1;
}
/*AD7799讀寄存器函數(shù)*/
unsigned char ReadByteFromAd7799(void)
{
unsigned char i ;
unsigned char ReadData ;
AD7799_CS=0;
ReadData=0 ;
for(i=0;i<8;i++)
{
DELAYS10;
AD7799_SCLK=0 ;
DELAYS10;
ReadData=ReadData<<1 ;
if(AD7799_DOUT)ReadData+=1 ;
DELAYS10;
AD7799_SCLK=1 ;
DELAYS10;
}
AD7799_DOUT=1 ;
AD7799_CS=1;
return(ReadData);
}
void WaiteRDY(void)
{
unsigned int iint ;
iint=0 ;
while(AD7799_RDY)
{
iint++;
if(iint>65530)
{
//reset ad7799
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
Ad7799_Ini();
break ;
}
}
}
void Ad7799_Ini()
{
WriteByteToAd7799(0x10);
//b0001 0000
/* Writes to Communications Register Setting Next Operation as Write to CONFIGURATION Register*/
//寫通訊寄存器為:下一操作寫配置寄存器 WriteByteToAd7799(0x10)b0001 0000 0寫通訊0下操作寫010配置寄存器0關(guān)連續(xù)讀00留用
WriteByteToAd7799(0x37);//增益為128 B0011 0111 00留用1開電流源1單端 0留用111是128倍增益
WriteByteToAd7799(0x00); //通道號 0 B0011 0000 00留用1基準(zhǔn)默認(rèn)1開緩沖 0留用000通道0
//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); //000連續(xù)模式0斷PSW0000留用
WriteByteToAd7799(0x09); //0000留用0011(123Hz)1010(16.7HZ65dB)
/* 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.,F(xiàn)adc=16.7HZ;*/
}
ulong ReadAd7799ConversionData(void)
{
ulong ConverData ;
unsigned char ADSAT ;
unsigned char ErrNUM=0;
WaiteRDY(); //等待數(shù)據(jù)READY
WriteByteToAd7799(0x40); //0100 0000 配置下一操作為:讀狀態(tài)寄存器
ADSAT=ReadByteFromAd7799(); //讀出狀態(tài) 8位
while((ADSAT&0x40)||(!(ADSAT&0x08))) //出錯或者讀寫異常
{
WriteByteToAd7799(0xff); //復(fù)位
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
Ad7799_Ini(); //初始化
WaiteRDY(); //讀狀態(tài)
WriteByteToAd7799(0x40); //下一操作:讀狀態(tài)
ADSAT=ReadByteFromAd7799(); //讀狀態(tài)
ErrNUM++;
if(ErrNUM>5)return(0xffffff);//if(ErrNUM>5)break; //連續(xù)5次都讀出錯
}
WriteByteToAd7799(0x58); //0101 1000 配置下一操作:讀數(shù)據(jù)寄存器,000
/* 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 AD7799's Data Register*/
return(ConverData);
}
UCIL temp;
void main(void)
{
uint code *adtpoint; //
uchar i=0,j=0;
delay(100);
CPUInit();
//AD7799_CS=0;
delay(10);
WriteByteToAd7799(0xff); //寫入32個高電平,復(fù)位ADC
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
WriteByteToAd7799(0xff);
Ad7799_Ini();
while(1)
{
temp.ul=ReadAd7799ConversionData();
UART0BUF=temp.uc[0];
while(!(UART0INT & 0x01));
UART0BUF=temp.uc[1];
while(!(UART0INT & 0x01));
UART0BUF=temp.uc[2];
while(!(UART0INT & 0x01));
UART0BUF=temp.uc[3];
while(!(UART0INT & 0x01));
}
}