|
奉献给大家AD7705的双通道应用程序~~~~~
#include<reg52.h>
#define CH1 1 /*AD7705输入通道IN1*/
#define CH2 2 /*AD7705输入通道IN2*/
#define BIPOLAR 0 /*AD7705双极性输入*/
#define UNIPOLAR 1 /*AD7705单极性输入*/
#define AD_CLK_WIDTH 1 /*AD7705串口时钟脉冲宽度*/
sbit AdClk=P1^0; /*AD7705时钟脉冲*/
sbit AdDin=P1^1; /*AD7705数据输入脉冲*/
sbit AdDout=P1^2; /*AD7705数据输出的反相脉冲*/
sbit AdDrdy=P1^3; /*指定转换标志位*/
sbit AdReset=P1^4; /*复位*/
void Wait7705(unsigned int t);/* AD7705软件延时 */
void WriteByte7705(unsigned char x);/* 向AD7705写入一个字节 */
unsigned int ReadWord7705(void);/* 从AD7705读一个字节 */
void Reset7705(void);/* AD7705通信端口复位 */
void Init7705_1();
void Init7705_2();
unsigned int GetData7705_ch1();/* 读AD7705转换数据 输入通道channel ----1=IN1,2=IN2;*/
unsigned int GetData7705_ch2();/* 读AD7705转换数据 输入通道channe2 ----1=IN1,2=IN2;*/
void main()
{
_nop_();
_nop_();
_nop_();
AdClk=1; //AD7705时钟脉冲
AdDin=1; //AD7705数据输入脉冲
AdDout=1; //AD7705数据输出的反相脉冲
AdReset=1;
Wait7705(100);//软件延时
AdReset=0; //复位
Wait7705(100);
AdReset=1;
Wait7705(100);
while(1)
{
get_ad();
}
}
void Wait7705(unsigned int t)/* AD7705软件延时 */
{
for( ;t>0 ;t-- );
}
/* 向AD7705写入一个字节 */
void WriteByte7705(unsigned char x)
{
unsigned char i;
for (i=0;i<8;i++)
{
AdClk=0;
Wait7705(AD_CLK_WIDTH);
AdDin=(bit)(x&0x80);
Wait7705(AD_CLK_WIDTH);
AdClk=1;
Wait7705(AD_CLK_WIDTH);
x<<=1;
}
}
/* 从AD7705读一个字 */
unsigned int ReadWord7705(void)
{
unsigned char i;
unsigned int x ;
AdClk=1;
for(i=0,x=0;i<16;i++)
{
AdClk=0;
Wait7705(AD_CLK_WIDTH);
x<<=1;
if(AdDout)
x|=0x01;
AdClk=1;
Wait7705(AD_CLK_WIDTH);
}
return x;
}
/* AD7705通信端口复位 */
void Reset7705(void)
{
unsigned char i;
for(i=0;i<64;i++)
{ /* 多于连续32个 DIN=1 使串口复位 */
AdClk=0;
Wait7705(AD_CLK_WIDTH);
AdDin =1;
Wait7705(AD_CLK_WIDTH);
AdClk=1;
Wait7705(AD_CLK_WIDTH);
}
}
void Init7705_1()
{
unsigned char i;
for(i=10;i>0;i--)
{WriteByte7705(0xff);} //持续DIN高电平写操作,恢复AD7705接口
Reset7705(); /* 防止串口失步 */
WriteByte7705(0x20); /* 写ch0 clock register */
WriteByte7705(0x00); /* 2.4576MHz时钟,20Hz数据更新速率 */
WriteByte7705(0x10); /*选择设置寄存器,使用chnanel 1*/
WriteByte7705(0x44);
}
void Init7705_2()
{
unsigned char i;
for(i=10;i>0;i--)
{WriteByte7705(0xff);} //持续DIN高电平写操作,恢复AD7705接口
Reset7705(); /* 防止串口失步 */
WriteByte7705(0x21); /* 写ch1 clock register */
WriteByte7705(0x00); /* 2.4576MHz时钟,20Hz数据更新速率 */
WriteByte7705(0x11); /*选择设置寄存器,使用chnanel 2*/
WriteByte7705(0x44);
}
/* 读AD7705转换数据 输入通道channel ----1=IN1,2=IN2;*/
unsigned int GetData7705_ch1()
{
unsigned int temp1=0;
Init7705_1();
Wait7705(10);
while(AdDrdy==1); //数据准备好
WriteByte7705(0x38);
temp1=ReadWord7705();//读取
return temp1;
}
/* 读AD7705转换数据 输入通道channe2 ----1=IN1,2=IN2;*/
unsigned int GetData7705_ch2()
{
unsigned int temp2=0;
Init7705_2();
Wait7705(10);
while(AdDrdy==1) //数据准备好
WriteByte7705(0x39);
temp2=ReadWord7705();//读取
return temp2;
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|