⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cewendianlu.cpp

📁 ad7705的晶振是用的2.4576,这个程序是采样程序,P3口设置成串口0方式
💻 CPP
字号:
 
//*我的设计中ad7705的晶振是用的2.4576,电路中有接口和开发机进行单片机信号联接,并将转换结果显示在开发机的四个数码管上。这个程序是采样程序,P3口设置成串口0方式*/


#include<iostream.h>
#include<math.h>

int sbit D_DAT=P1^0; //这一段是用来指定数显的,我用的是周立功dp-51开发机,该开发机有4个数码管 
sbit D_CLK=P1^1;
sbit DISPB0=P1^7;
sbit DISPB1=P1^6;
sbit DISPB2=P1^5;
sbit DISPB3=P1^4;
sbit DISPB4=P1^3;

sbit ADC_DIN=P3^0; //这一段是指定ad7705的信号引脚
sbit ADC_CLK=P3^1; //指定时钟引脚
sbit ADC_DRDY=P3^2; //指定转换标志位

float voltage; //这个全局变量是用来给数显子程序传递采样到的数据的

unsigned char data display_buffer[5]; //显示缓冲

unsigned char code led[17]= //显示段码表
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};


//*****************************************/
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++);
}
//*****************************************/
void disp(void) //给数码管发显示缓冲区的段码
{
unsigned char a;
a=0xff;
send(a);
DISPB0=0;
DISP_DLEAY();
DISPB0=1;

a=led[display_buffer[1]];
send(a);
DISPB1=0;
DISP_DLEAY();
DISPB1=1;

a=led[display_buffer[2]];
send(a);
DISPB2=0;
DISP_DLEAY();
DISPB2=1;

a=led[display_buffer[3]];
send(a);
DISPB3=0;
DISP_DLEAY();
DISPB3=1;

a=led[display_buffer[4]];
send(a);
DISPB4=0;
DISP_DLEAY();
DISPB4=1; 
}

//*****************************************/

void display() /*将数据转换成相应的段码,
并给显示缓冲区赋段码*/
{

float y,z,x; 

x=voltage;
x=x/10.0;
y=modf(x,&z);
display_buffer[1]=(unsigned char)z;

x=10.0*y;
y=modf(x,&z);
display_buffer[2]=(unsigned char)z;

x=10.0*y;
y=modf(x,&z);
display_buffer[3]=(unsigned char)z;

x=10.0*y;
y=modf(x,&z);
display_buffer[4]=(unsigned char)z;


disp();
disp();
disp();
disp();
disp();
}

//****************************************************************/

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 + -