#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar temp0, temp1,temp2,temp3,temp4,temp5;
uint i;
uchar code table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};//0 1 2 3 4 5 6 7 8 9 -
uchar code table2[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0xbf};//0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -
uint out;
sbit AD7705_DIN=P3^0;
sbit AD7705_DOUT=P3^0;
sbit AD7705_SCLK=P3^1;
sbit AD7705_DRDY=P3^2;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void AD7705_Reset(void)
{
uchar i;
AD7705_SCLK =1;
for(i=0;i<40;i++)
{
AD7705_SCLK =0;
AD7705_DIN =1; // DIN端口连续写40个周期的高电平,保证复位
AD7705_SCLK =1;
}
delay(1); // 复位之后等待1MS
}
//****************************************************************************
//功能:往AD7705写1个字节数据
//参数:要写入AD7705的数据
//返回:无
//备注:无
//****************************************************************************
void AD7705_WriteByte(uchar d)
{
uchar i;
AD7705_SCLK =1;
for(i=0;i<8;i++)
{
AD7705_SCLK =0;
if(d & 0x80)
AD7705_DIN =1;
else
AD7705_DIN =0;
AD7705_SCLK =1;
d<<= 1;
}
AD7705_SCLK =1;
AD7705_DIN =1;
}
//****************************************************************************
//功能:从AD7705读一个字节的数据
//参数:无
//返回:读到的一字节数据
//备注:无
//****************************************************************************
uchar AD7705_ReadByte()
{
uchar i, d=0;
while(AD7705_DRDY);// 写数据的时候不需要判断忙,只有读数据的时候才需?
AD7705_WriteByte(0x38);
AD7705_SCLK =1;
for(i=0;i<8;i++) // 读数据,高位在前
{
d<<= 1;
AD7705_SCLK =0;
if(AD7705_DOUT)
d++;
AD7705_SCLK =1;
}
AD7705_SCLK =1;
AD7705_DIN =1;
AD7705_DRDY=1;
return d;
}
void AD7705_Init()
{
AD7705_Reset(); // 复位,防止接口迷失
AD7705_WriteByte(0x20); // 写ch0 clock register
AD7705_WriteByte(0x04); // 2.4576MHz时钟,20Hz数据更新速率
AD7705_WriteByte(0x10);
AD7705_WriteByte(0x46);
delay(50);
}
float AD7705_ReadVol()
{
uchar adc_h=0, adc_l=0;
uint ADValue=0;
adc_h=AD7705_ReadByte();
adc_l=AD7705_ReadByte();
ADValue |= adc_h;
ADValue<<= 8;
ADValue |= adc_l;
return ADValue;
}
void main()
{
AD7705_Init();
while(1)
{
out=AD7705_ReadVol();
temp1=out/10000;
temp2=out%10000/1000;
temp3=out%10000%1000/100;
temp4=out%10000%1000%100/10;
temp5=out%10000%1000%100%10;
P0=table1[temp1];
P1=P1&0xf8;
delay(1);
P0=table1[temp2];
P1++;
delay(1);
P0=table1[temp3];
P1++;
delay(1);
P0=table1[temp4];
P1++;
delay(1);
P0=table1[temp5];
P1++;
delay(1);
}
} |