电压不能正确采集?(PROTUES上仿真出错)
还没做出实物,在protues上仿真时采集0.5V的电压出现的却是1.71V,如图所示,[img=http://hi.csdn.net/space-3394646-do-album-picid-756407.html][/img]代码如下,
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit ST=P3^0;
sbit OE=P3^1;
sbit CLK=P3^2;
sbit EOC=P3^3;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sfr LedPort=0x90; //P1口接段码
sfr DataPort = 0x80;
void init();
float AD();
void delay(uint z);
void display(uint b);
uint jisuan(float m);
uint suhuanchong[4]={0,0,0,0};
uchar code ntable[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
,0x00}; //不带小数点编码表
uchar code htable[11]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef
,0x80}; //带小数点编码表
void init()
{
TMOD=0x01;
TH0=(65536-45)/256;
TL0=(65536-45)%256;
EA=1;
ET0=1;
TR0=1;
}
float AD()
{ uint getdata;
float result;
OE=0;
ST=0;
ST=1;
delay(1);
ST=0;
while(!EOC);
OE=1;
getdata=DataPort;
OE=0;
result=getdata*5/256.0;
return result;
}
uint jisuan(float m)
{ uint b;
uint shi,ge,xiao1,xiao2;
uint x;float y;
//m=m*5/256;
x=(int)m; // 整数部分
y=m-x; // 小数部分
if(x>=100)
b=1;
shi=x/10; // 求十位
ge=x%10; // 求个位
xiao1= (int)(y*10); // 求小数点后第一位
xiao2=(int)(y*100)%10;// 求小数点后第二位
if(shi==0)
{
shi=10; //如最高位为0则消隐
if(ge==0) //如最高位为0且次高位也为0则都消隐
ge=10;
}
suhuanchong[0]=shi;//把计算出来的各位存入临时数组
suhuanchong[1]=ge;
suhuanchong[2]=xiao1;
suhuanchong[3]=xiao2;
return b;
}
void display(uint b)
{ P2 = 0xff; //关所有的位选
if(b!=1)
{
LedPort = ntable[suhuanchong[0]]; //动态扫态
P20 = 0;
delay(1);
P20 = 1;
LedPort = htable[suhuanchong[1]];
P21 = 0;
delay(1);
P21 = 1;
LedPort = ntable[suhuanchong[2]];
P22 = 0;
delay(1);
P22 = 1;
LedPort = ntable[suhuanchong[3]];
P23 = 0;
delay(1);
P23 = 1;
}
}
void t0(void) interrupt 1 using 1
{
TH0=(65536-45)/256;
TL0=(65536-45)%256;
CLK=~CLK;
}
void delay(uint z)
{
uint t1,y;
for(t1=z;t1>0;t1--)
for(y=110;y>0;y--);
}
void main()
{float x;
uint y;
init();
while(1)
{
x=AD();
y=jisuan(x);
display(y);
}
}
我找了很久了,实在找不出错误,旁边又没有什么学单片机的,在网上求解,谢谢。
[最优解释]
改之前 uint getdata;太小了吧
改制后 uint n;太小了吧
[其他解释]
uint getdata;
太小了吧
[其他解释]
float AD()?? 浮点 ??
ad是几位? 什么编码(偏移2进制 ?),什么对齐方式 ?
[其他解释]
ADC0808是单极性(0-》+5V)8位的,所以简单(没有 偏移2进制 ,对齐方式等问题)。
你把它转成浮点的,不好,1。单片机没浮点处理器,用软件仿真很慢。
2。另外你的float AD() 恐怕也不对。
[其他解释]
查查硬件,AD转换开始信号,AD的读信号,EOC信号,编一个专门测AD的小程序,如读AD的时序对不对。
[其他解释]
float AD()是浮点型,这个应该没错吧?我protues仿真时用的是ADC0808,做实物时我会把它改成ADC0809
你说的对齐方式是?这两种芯片应该不用设置对齐方式吧?
[其他解释]
难道大家讨厌我这种帖子的提问方式,怎么就一个回复呢?
[其他解释]
已经把float AD()改掉了,结果还是一样,程序如下。
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit ST=P3^0;
sbit OE=P3^1;
sbit CLK=P3^2;
sbit EOC=P3^3;
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sfr LedPort=0x90; //P1口接段码
sfr DataPort = 0x80;
void init();
uint AD();
void delay(uint z);
void display(uint b);
uint jisuan(uint n);
uint suhuanchong[4]={0,0,0,0};
uchar code ntable[11]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
,0x00}; //不带小数点编码表
uchar code htable[11]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef
,0x80}; //带小数点编码表
void init()
{
TMOD=0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
EA=1;
ET0=1;
TR0=1;
}
uint AD()
{ uint getdata;
OE=0;
ST=0;
ST=1;
delay(1);
ST=0;
while(!EOC);
OE=1;
getdata=DataPort;
OE=0;
return getdata;
}
uint jisuan(uint n)
{ uint b;
uint shi,ge,xiao1,xiao2;
n=n*100;
n=n*5/256;
if(n>500)
b=1;
shi=n/1000; // 求十位
ge=n/100; // 求个位
xiao1=n%100/10; // 求小数点后第一位
xiao2=n%10;// 求小数点后第二位
if(shi==0)
{
shi=10; //如最高位为0则消隐
if(ge==0) //如最高位为0且次高位也为0则都消隐
ge=10;
}
suhuanchong[0]=shi;//把计算出来的各位存入临时数组
suhuanchong[1]=ge;
suhuanchong[2]=xiao1;
suhuanchong[3]=xiao2;
return b;
}
void display(uint b)
{ P2 = 0xff; //关所有的位选
if(b!=1)
{
LedPort = ntable[suhuanchong[0]]; //动态扫态
P20 = 0;
delay(1);
P20 = 1;
LedPort = htable[suhuanchong[1]];
P21 = 0;
delay(1);
P21 = 1;
LedPort = ntable[suhuanchong[2]];
P22 = 0;
delay(1);
P22 = 1;
LedPort = ntable[suhuanchong[3]];
P23 = 0;
delay(1);
P23 = 1;
}
}
void t0(void) interrupt 1 using 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
CLK=~CLK;
}
void delay(uint z)
{
uint t1,y;
for(t1=z;t1>0;t1--)
for(y=110;y>0;y--);
}
void main()
{uint x;
uint y;
init();
while(1)
{
x=AD();
y=jisuan(x);
display(y);
}
}
[其他解释]
该回复于2011-03-08 15:45:59被版主删除
[其他解释]
有道理 不过我现在把它改成了INT或者是long int 显示结果都还是不变。难道还有其它原因?
[其他解释]
硬件我还没做呢,一直想等仿真正确才做板。。。
看来得先把板做好,让程序在真实环境中跑看看。Quote=引用:]
查查硬件,AD转换开始信号,AD的读信号,EOC信号,编一个专门测AD的小程序,如读AD的时序对不对。
[/Quote]
[其他解释]
我也一直觉得是时序出了问题,不过一直还没编出来。
[其他解释]
我再想应该只剩时序的问题了,但我按照ADC的时序图结合汇编nop指令来写,结果也不行,真是郁闷啊。
[其他解释]
解决了没?我也仿真不出来,来看看解决办法的