AD7991采用I2C总线,12位AD,我写的程序具体内容如下:
uchar dat[6]={0,0,0,0,0,0};
void delay()
{_nop_();_nop_();}
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i=0;
scl=1;
delay();
while((sda==1)&&(i<255))i++;
scl=0;
delay();
}
void writebyte(uchar dat)
{
uchar i,temp;
temp=dat;
for(i=0;i<8;i++)
{
temp=temp>>1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar readbyte()
{
uchar i,k=0;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_AD7991(uchar reg)
{
start();
writebyte(0x52); //AD7991-0 Address:010 1000;AD7991-1 Address:010 1001
respons();
writebyte(reg);
respons();
stop();
}
void read_AD7991()
{
uchar i;
start();
write_AD7991(0x53);
respons();
for(i=0;i<3;i++) //read 6 bytes,3 ADC value
{
dat[2*i]=readbyte();
sda=0;
delay();
scl=1;
delay();
dat[2*i+1]=readbyte();
}
sda=1;
delay();
scl=1;
delay();
scl=0;
stop();
}
存在问题:把AD转换后的值通过串口送到上位机,AD值都是FF。现已确定串口部分程序无问题,问题应该出在上面的程序,请各位前辈指点! |