/*************** AD7705 测试程序000002****************/
#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define AD_CLK_WIDTH 1 /*AD7705串口时钟脉冲宽度*/
sbit LED=P3^2;
uchar shi,ge,bai,k;
uint wendu=0;
uchar Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
// 0 1 2 3 4 5 6 7 8 9
void Delay_ms(unsigned int t) //Delay t ms for 12MHz
{
unsigned char i;
while(t--)
{
for(i=0;i<125;i++);
}
}
/*sbit DS=P1^4; //串行数据输入
sbit SH=P1^2; //移位时钟脉冲
sbit ST=P1^3; //输出锁存器控制脉冲
sbit lcden=P1^1; //lcd使能
sbit lcdrs=P1^0; //lcd读写控制*/
sbit AdClk = P2^0; /*AD7705时钟脉冲*/
sbit AdDout = P2^3;/*AD7705数据输出的反相脉冲*/
sbit AdDin = P2^2; /*AD7705数据输入脉冲*/
sbit drty = P2^4;
sbit REST=P2^1;
uint temp=0;
/****************************************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/* AD7705软件延时 */
void Wait7705(unsigned int t)
{
for( ;t>0 ;t-- );
}
/* 向AD7705写入一个字节 */
void WriteByte7705(unsigned char x)
{
unsigned char i;
AdClk = 1;
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;
}
AdDin =1;
}
/* 从AD7705读一个字 */
unsigned int ReadWord7705(void)
{
unsigned char i;
unsigned int x=0 ;
AdClk = 1 ;
for (i = 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<35;i++) /* 多于连续32个 DIN=1 使串口复位 */
{
AdClk=0;
Wait7705(AD_CLK_WIDTH);
AdDin =1;
Wait7705(AD_CLK_WIDTH);
AdClk=1;
Wait7705(AD_CLK_WIDTH);
}
AdDin =1;
}
/*初始化*/
void Init7705(unsigned char channel , bit polarity ,unsigned char gain)
{
unsigned char x;
Reset7705(); /* 复位 */
WriteByte7705(0x20); /* 写ch0 clock register */
WriteByte7705(0x04); /* 2.4576MHz时钟,20Hz数据更新速率 */
//WriteByte7705(0x21); /* 写CH1 clock register */
//WriteByte7705(0x00); /* 2.4576MHz时钟,20Hz数据更新速率 */
if(channel==1) /* com register ,选择下步操作 setup register */
WriteByte7705(0x10);/*设置寄存器 写 ch0 */
else
WriteByte7705(0x11); /*设置寄存器 写 ch1 */
if(polarity) /* setup register */
x=0x04; /*0x04正常模式 增益1 单极性 不缓冲 滤波器同步(0x46自校准模式 增益1 单极性 缓冲 滤波器同步) */
else
x=0x42; /*0x00正常模式 增益1 双极性 不缓冲 滤波器同步(0x42) */
switch(gain) /*增益设置*/
{
case 1 : x|=0x40;break;
case 2 : x|=0x48;break;
case 4 : x|=0x50;break;
case 8 : x|=0x58;break;
case 16 : x|=0x60;break;
case 32 : x|=0x68;break;
case 64 : x|=0x70;break;
case 128 : x|=0x78;break;
default : x|=0x40;break;
}
WriteByte7705(x);
}
/* 读AD7705转换数据 输入通道channel ----1=IN1,2=IN2;*/
unsigned int GetData7705(unsigned char channel)
{
if(channel==1)
WriteByte7705(0x38);/*数据寄存器 读 通道1*/
else
WriteByte7705(0x39); /*数据寄存器 读 通道2*/
return ( ReadWord7705() );
}
void main()
{
//LED=0;
REST=1;
drty=1;
Reset7705();
Init7705(1,1,1);
bai=wendu/100;
shi=wendu/10%10;
ge=wendu%10;
if((bai==0)&&(shi==0)){shi=0x00;}
if(bai==0) bai=0x00;
//init1602();
while(!drty)
{
LED=0;
temp=GetData7705(1);
wendu=temp;
bai=wendu/100;
shi=wendu/10%10;
ge=wendu%10;
if((bai==0)&&(shi==0)){shi=0x00;}
if(bai==0) bai=0x00;
/*bai=wendu/10000;
shi=wendu/1000%10;
ge=wendu/100%10;*/
P1=0x00;
P0=Tab[bai];
//P0=0x00&&P0;
Delay_ms(1);
P0=0x00&&P0;
P1=0x04;
P0=Tab[shi];
//P0=0x00&&P0;
Delay_ms(1);
P0=0x00&&P0;
P1=0x02;
P0=Tab[ge];
//P0=0x00&&P0;
Delay_ms(1);
P0=0x00&&P0;
//write_dt1602(temp);
delay(5);
}
} |