600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > c语言编程ad7799 51单片机驱动AD7799程序(keil c)

c语言编程ad7799 51单片机驱动AD7799程序(keil c)

时间:2023-07-15 14:31:16

相关推荐

c语言编程ad7799 51单片机驱动AD7799程序(keil c)

#include"VRS51L3074_keil.h"

#include#define uchar unsigned char

#define uint unsigned int

#define ulong unsigned long

typedef union{ //无符号字符型,整形,长整形数据联合体

uchar uc[2];

uint ui;

}UCI;

typedef union{ //无符号字符型,整形,长整形数据联合体

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后,内部自动上拉到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 算术单元、IO口使能*/

P0=P1=P2=P3=P6=0x00; P4=0x00; P5=0x04;

AD7799_CS=1;

UART0CFG=0x90; // 0x09:38400 BRADJ=0x02(19200)/波特率发生器使用内部晶振/9th不使用/1位停止位

UART0INT=0x00; //使能接收中断,使能发送完成中断,允许接收 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写寄存器函数

WriteData:要写的数据*/

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读寄存器函数*/

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关连续读00留用

WriteByteToAd7799(0x37);//增益为128 B0011 0111 00留用1开电流源1单端 0留用111是128倍增益

WriteByteToAd7799(0x00); //通道号 0 B0011 0000 00留用1基准默认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连续模式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.,Fadc=16.7HZ;*/

}

ulong ReadAd7799ConversionData(void)

{

ulong ConverData ;

unsigned char ADSAT ;

unsigned char ErrNUM=0;

WaiteRDY(); //等待数据READY

WriteByteToAd7799(0x40); //0100 0000 配置下一操作为:读状态寄存器

ADSAT=ReadByteFromAd7799(); //读出状态 8位

while((ADSAT&0x40)||(!(ADSAT&0x08))) //出错或者读写异常

{

WriteByteToAd7799(0xff); //复位

WriteByteToAd7799(0xff);

WriteByteToAd7799(0xff);

WriteByteToAd7799(0xff);

Ad7799_Ini(); //初始化

WaiteRDY(); //读状态

WriteByteToAd7799(0x40); //下一操作:读状态

ADSAT=ReadByteFromAd7799(); //读状态

ErrNUM++;

if(ErrNUM>5)return(0xffffff);//if(ErrNUM>5)break; //连续5次都读出错

}

WriteByteToAd7799(0x58); //0101 1000 配置下一操作:读数据寄存器,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个高电平,复位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));

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。