📄 7705.c
字号:
/*我的设计中ad7705的晶振是用的2.4576,电路中有接口和开发机进行单片机信号联接,并将转换结果显示在开发机的四个数码管上。这个程序是采样程序,P3口设置成串口0方式*/
#include<reg51.h>
#include<absacc.h>
#include<intrins.h>
#include<math.h>
#include<3310.h>
#define uchar unsigned char
uchar as
sbit ADC_DIN=P3^0; //这一段是指定ad7705的信号引脚
sbit ADC_CLK=P3^1; //指定时钟引脚
sbit ADC_DRDY=P3^2; //指定转换标志位
float voltage; //这个全局变量是用来给数显子程序传递采样到的数据的
unsigned char data display_buffer[5]; //显示缓冲
/*****************************************/
void send(unsigned char a) //通过P1口给数显发送8位数据
{
unsigned char i;
for(i=0;i<8;i++)
{
if(_crol_(a,i)&0x80)
D_DAT=1;
else
D_DAT=0;
D_CLK=0;
D_CLK=1;
}
}
/*****************************************/
void DISP_DLEAY(void) //显示延迟子程序
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<5;j++);
}
/****************************************************************/
unsigned char rearrange(unsigned char a) /*将8位数据a的顺序颠倒后赋给b。
因为串口发送顺序和ad7705的接收顺序刚好相反*/
{
unsigned char i,b;
b=a&0x01;
for(i=1; i<8; i++)
{
b=b<<1;
a=a>>1;
b=b+(a&0x01);
}
return(b);
}
/****************************************************************/
void WriteToReg_ADC(unsigned char a) //通过串口给ad7705发送命令字a
{
SCON=0;
TI=0;
SBUF=a;
while(!TI);
}
/*************************************************************/
void MX7705_Init() //ad7705初始化
{
unsigned char i;
ADC_CLK=1; //防止接口迷失
ADC_DIN=1;
for(i=0;i<100;i++)//prevent interface from losting
{
ADC_CLK=0;
ADC_CLK=1;
}
WriteToReg_ADC(0x04); //write 0x20 to communication register to choose channel 0
//and clock register for the next one to write
WriteToReg_ADC(0x20); //写0x04到时钟寄存器,指定晶振频率为2.4576MHz,采样率为50Hz.
WriteToReg_ADC(0x08); //write 0x10 to communication register to choose channel 0
//and setup register for the next one to write
WriteToReg_ADC(0x22); //写0x44到设置寄存器,指定增益为1, buffer off, FSYNC=0, and self-calibration
}
/*************************************************************/
unsigned int ReadWord()
{
unsigned char high8,low8;
unsigned int out;
SCON=0;
REN=1; //allow serial port to recieve data
RI=0;
while(!RI); //waiting the end of recieve of 8 bit
high8=SBUF;
ADC_CLK=1;
RI=0;
while(!RI);
low8=SBUF;
REN=0;
out=rearrange(high8);
out=out<<8;
out=out+rearrange(low8);
return(out);
}
main()
{
unsigned int data_out;
MX7705_Init();
while(ADC_DRDY==0)
{
WriteToReg_ADC(0x1C); //给通讯寄存器发0x38,指定下一个读数据寄存器
data_out=ReadWord(); //从数据寄存器中读16位数据
voltage=5.0*(data_out/65536.0); //将所读数据转换为电压值
display();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -