|
#include
sbit dio=P1^5;
sbit clk=P1^6;
sbit cs=P1^7;
//sbit one=P2^7;
//sbit two=P2^6;
unsigned char d1,d[8],d2,j=0,i,shi,shang1,shang2,shang3,shang4,yu1,yu2,yu3,buf[10],buf1[10],c; //d1中存储转换器发来的数据
chushihua0834()//初始化0834
{clk=0;
dio=0;
cs=1;
cs=0;//置片选低
dio=1;
clk=1;
clk=0;
dio=1;
clk=1;
clk=0;
dio=0;
clk=1;
clk=0;
dio=0;
clk=1;
clk=0;//通道0单端输入
clk=1;
clk=0;
dio=1;
/*clk=0;
dio=0;
cs=1;
cs=0;
dio=1;
clk=0;
clk=1;
dio=0;
clk=0;
clk=1;
dio=0;
clk=0;
clk=1;
clk=0;
clk=1;
dio=1;*/
}
zhuanhuan() //转换开始
{for(i=0;i<8;i++)
{clk=1;
clk=0;
//if(dio)
//date<<=1;
d1=dio;
d=dio;
d1<<=1;
}
if(d[7]==1)
d2|=0x80;
for(j=0;j<7;j++)
{clk=1;
clk=0;
//if(dio)
//date<<=1;
d2=dio;
d2>>=1;
if(dio)
d2|=0x80;
}
clk=1;
clk=0;
clk=1;
clk=0;
cs=1;
}
delay() //延时
{ int a,b;
for(a=0;a<10;a++)
for(b=0;b<50;b++);
}
/*ch(int m) //2的M次方
{int n,k=1;
for(n=1;n<=m;n++)
k*=2;
return(k);
} */
shuchu() //显示
{ //shang1=0;
//shang2=0;
//yu=0;
shi=d[7]*128+d[6]*64+d[5]*32+d[4]*16+d[3]*8+d[2]*4+d[1]*2+d[0];
shang1=(shi*5)/256;
yu1=(shi*5)%256;
shang2=(yu1*10)/256;
yu2=(yu1*10)%256;
shang3=(yu2*10)/256;
yu3=(yu2*10)%256;
shang4=(yu3*10)/256;
for(c=0;c<5;c++)
{P0=buf1[shang1];
P2=0x80;
delay();
P2=0;
P0=buf[shang2];
P2=0x40;
delay();
P2=0;
P0=buf[shang3];
P2=0x20;
delay();
P2=0;
P0=buf[shang4];
P2=0x10;
delay();
P2=0;
}
}
main()
{ //unsigned char buf[10];
buf[0]=0xc0; //0
buf[1]=0xf9; //1
buf[2]=0xa4;
buf[3]=0xb0;
buf[4]=0x99;
buf[5]=0x92;
buf[6]=0x82;
buf[7]=0xf8;
buf[8]=0x80;
buf[9]=0x90;
buf1[0]=0x40; //0.
buf1[1]=0x79; //1.
buf1[2]=0x24;
buf1[3]=0x30;
buf1[4]=0x19;
buf1[5]=0x12;
buf1[6]=0x02;
buf1[7]=0x78;
buf1[8]=0x00;
buf1[9]=0x10;
while(1)
{chushihua0834();
d1=0x00;
d2=0x00;
clk=0;
clk=1;
zhuanhuan();
if((d1^d2)==0)
shuchu();
cs=1;
}
}
以上是我写的ADC0834的程序,参考电压5v,转换结果在数码管上显示.在priteus 里仿真时,不正确,有时后显示cpu使用率100%。我调滑动变阻器时,本来应该显示结果变大,可是却变小了。比如应该显示3.7v,可是却显示0.6v.各位大虾帮帮忙,看看怎么回事。谢谢了
|
|