#include <reg51.h>
sbit CE=P3^0;
sbit RC=P3^1;
sbit STS=P3^2;
sbit LE=P2^5;
sbit A0=P3^6;
sbit CS=P3^3;
#define N 0x0fff //十二位转换
unsigned long result=0;
unsigned char seg[ ]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char buf[4];
void delay(n)
{
int i;
while(n--)
for(i=0;i<10;i++);
}
void inti()
{
CS=1;
CE=0;
}
unsigned int ad1674_convert()
{
unsigned int temp=0;
//STS=1;
inti();
CS=0;
delay(1);
A0=0;
delay(1);
RC=0;//
delay(1);
CE=1;
delay(1);
//while(STS==1);
{
CE=0;
delay(1);
RC=1;
delay(1);
A0=0;
delay(1);
CE=1;
delay(1);
temp=P0;
temp=P0<<4;
CE=0;
delay(1);
RC=1;
delay(1);
A0=1;
delay(1);
CE=1;
delay(1);
temp+=P0>>4;
}
CE=0;
CS=1;
return temp;
}
void dispute()
{
unsigned long result1; //用于中途的数据转换
unsigned long vol,vol_c; //电压显示,与电压的一个进位
result1=result*10000*2.2; //*100*10*10v 扩大一百倍,用来替代两位小数,扩大十倍用来小数点后第三位四舍五入?
//又乘以10,是最电压
vol=result1/N; // vol=result/0x0fff*10(v)
vol_c=vol%10;
if(vol_c>=5) //四舍五入
vol=vol+10;
vol=vol/10;
buf[0]=vol/1000;
buf[1]=vol/100%10;
buf[2]=vol/10%10;
buf[3]=vol%10;
}
void display() //显示子程序.
{
int i;
unsigned char temp=0xfe;
for(i=0;i<4;i++)
{
LE=0;
P2=temp;
if(i==2)
P1=seg[buf[ i]]+0x80;
else P1=seg[buf[ i]];
LE=1;
LE=0;
delay(10);
temp=temp*2+1;
}
P2=temp; //关显示,进行下一次测试
}
void main()
{
while(1)
{
result=ad1674_convert();
dispute();
display();
}
}
|