📄 ad7730.c
字号:
/*********************************************************************
Author : ADI - CAST
Date : September 2003
File : AD7730.c
Hardware : ADuC832
Description :
*********************************************************************/
#include<stdio.h>
#include<aduc832.h>
/******************************************************************
DEFINE CONTROL PINS OF ADUC832 FOR THE PURPOSE OF AD7730 CONTROL.
Customers should define the pins according to their design.
If P0 is used as the control port, pull-up resistors should be added to each pin of P0.
******************************************************************/
sbit CS=0x085;
sbit DIN=0x0B4;
sbit DOUT=0x0B5;
sbit DRDY=0x0B3;
sbit RESET=0x084;
sbit SCLOCK=0x0A7;
sbit SYNC=0x0A6;
void writetoreg(unsigned char);
void readfromreg(int);
void read(int);
void main()
{
int tim;
/* Set up UART */
T3CON = 0x082;
T3FD = 0x02D;
SCON = 0x052;
tim=1000;
/* PRECONFIGURE...*/
RESET=0;
while(tim--);
RESET=1;
SYNC=1;
SCLOCK=1;
DIN=1;
DOUT=1;
CS=1;
DRDY=1;
printf("\n");
writetoreg(0x03); //write to communication register. The next step is writing to FILTER REGISTER
writetoreg(0x80); //set the FILTER register ,50Hz output rate
writetoreg(0x00); //set the FILTER register
writetoreg(0x10); //set the FILTER register,chop enable
writetoreg(0x04); //write to communication register. The next step is writing to DAC register
writetoreg(0x00); //plus 5mv
writetoreg(0x02);//write to communication register . The next step is writing to MODE register
writetoreg(0xB1);//internal fullscale calibration,unipolar input
writetoreg(0x32);//the reference is 2.5v,input range 0~80mv
while(DRDY);
printf("self fullscale calibration finished.\n");
writetoreg(0x02);//write to communication register . The next step is writing to MODE register
writetoreg(0x91);//internal zero-scale calibration
writetoreg(0x02);//the reference is 2.5v,input range 0~10mv
while(DRDY);
printf("self zero-scale calibration finished.\n");
writetoreg(0x10); //read from status
readfromreg(8);
printf("\n");
writetoreg(0x12); //read from mode
readfromreg(16);
printf("\n");
writetoreg(0x13); //read from filter
readfromreg(24);
printf("\n");
writetoreg(0x14); //read from DAC
readfromreg(8);
printf("\n");
writetoreg(0x15);//read from offset register
readfromreg(24);
printf("\n");
writetoreg(0x16);//read from gain register
readfromreg(24);
printf("\n");
writetoreg(0x02);//writing to communication register, the next step is write to MODE register
writetoreg(0x31);//continuous conversion mode,unipolar
writetoreg(0x30);//reference is 2.5v,input channel is AIN1-AIN1,input range 0~10mv
writetoreg(0x21);//wrting to communication register , the next step is continuous read from data register
read(200);
writetoreg(0x30);
}
void writetoreg(byteword)
unsigned char byteword;
{
unsigned char temp;
int i;
CS=0;
temp=0x80;
for(i=0;i<8;i++)
{
if((temp&byteword)==0)
DIN=0;
else DIN=1;
SCLOCK=0;
SCLOCK=1;
temp=temp>>1;
}
}
void readfromreg(bytenumber)
int bytenumber;
{
int j;
unsigned char temp1;
CS=0;
temp1=0x00;
for(j=0;j<bytenumber;j++)
{
SCLOCK=0;
SCLOCK=1;
if(DOUT==0)
temp1=temp1<<1;
else
{temp1=temp1<<1;
temp1=temp1+0x01;}
if(j==7||j==15||j==23)
{ printf("%02BX",temp1);
temp1=0x00;
}
}
CS=1;
}
void read(readtime)
int readtime;
{
unsigned char temp1;
int i,j;
CS=0;
DIN=0;
temp1=0x00;
for(i=0;i<readtime;i++)
{ while(DRDY);
for(j=0;j<24;j++)
{SCLOCK=0;
SCLOCK=1;
if(DOUT==0)
temp1=temp1<<1;
else
{temp1=temp1<<1;
temp1=temp1+0x01;}
if(j==7||j==15||j==23)
{ printf("%02BX",temp1);
temp1=0x00;
}
}
printf("\n");
}
printf("\n\n\n");
CS=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -