大家好,AD5420输出4-20ma电流,但是输出误差较大,附件为两张原理图和程序。
设定输出12ma但是以上来输出11.9,后面自己一点点往上涨,很久后等到11.97这里,但是还差0.03而且实时性不好。
设定4ma直接输出3.97,之后逐渐上涨。
设定12ma直接输出11.5,之后上涨。
设定20ma直接输出19.9,之后上涨。
请大神赐教,看看是电路或者程序哪里出了什么问题。
程序都是在网上官网找的,控制寄存器那里改了改,其余没动。谢谢大家参与,不吝赐教。
#define ADDRESS_RESET 0x56
#define ADDRESS_CONTROL 0x55
#define ADDRESS_DATA 0x01
unsigned int ADC_data; //写入5420的16位控制字
unsigned int AD5420BACK;//写入ad5420的数值
bit idata AD5420_FAULT;
unsigned int ADMS;
bit go;
void ad5420_delay (unsigned int lth)
{
while (lth >0)
lth--;
}
void Delay1ms() //@24.000MHz
{
unsigned char i, j;
i = 24;
j = 85;
do
{
while (--j);
} while (--i);
}
void CLR_LATCH() //latch
{
AD5420_latch=0;
}
void SET_LATCH()
{
AD5420_latch=1;
}
void CLR_SCL() //sclk
{
AD5420_sclk=0;
}
void SET_SCL()
{
AD5420_sclk=1;
}
void SET_SDO()
{
AD5420_sdin=1;
}
void CLR_SDO()
{
AD5420_sdin=0;
}
void Delay100us() //@24.000MHz
{
unsigned char i, j;
i = 3;
j = 82;
do
{
while (--j);
} while (--i);
}
void WriteToAD5420(unsigned char *Buf)
{
unsigned char ValueToWrite = 0;
unsigned char i = 0;
unsigned char j = 0;
CLR_LATCH(); //CS=0
ad5420_delay(300);
for ( i=3;i>0;i-- ) //写入24位数据
{
ValueToWrite = *(Buf+i-1); //第3个字节先被送出
for (j=0; j<8; j++)
{
CLR_SCL();
ad5420_delay(30);
if(0x80 == (ValueToWrite & 0x80)) //数据高位先被送出
{
SET_SDO(); //Send one to SDIN pin of AD5420
}
else
{
CLR_SDO(); //Send zero to SDIN pin of AD5420
}
ad5420_delay(30);
SET_SCL();
ad5420_delay(30);
ValueToWrite <<= 1; //Rotate data
}
}
CLR_SCL();
Delay100us();
SET_LATCH();
Delay100us(); //CS=1,锁存数据
CLR_LATCH();
}
//初始化
void ad5420_init()
{
unsigned char buf[3];
AD5420_sclk =0;
AD5420_sdin =0;
AD5420_latch=0;
Delay100us();
AD5420_latch=1;
Delay100us();
AD5420_latch=0; //产生LANCH的脉冲,清除移位寄存器中多余的数据
buf[2] = ADDRESS_RESET; //复位寄存器
buf[1] = 0x00; //DATA_High_8bit----Disable Slew Rate
buf[0] = 0x01; //DATA_LOW_8bit
WriteToAD5420(buf); //Write 560001 to SHIFT REGISTER to write 0001 to control register
Delay100us();
buf[2] = ADDRESS_CONTROL; //控制寄存器
buf[1] = 0x1f; //DATA_High_8bit----Disable Slew Rate
buf[0] = 0x05; //DATA_LOW_8bit
WriteToAD5420(buf); //Write 551005 to SHIFT REGISTER to write 1005 to control register
Delay100us();
}
void ad5420_setValue(unsigned int value)
{
unsigned char buf[3];
AD5420_sclk =0;
AD5420_sdin =0;
AD5420_latch=0;
Delay100us();
AD5420_latch=1;
Delay100us();
AD5420_latch=0; //产生LANCH的脉冲,清除移位寄存器中多余的数据
buf[2] = ADDRESS_DATA; //数据寄存器
buf[1] = value>>8; //DATA_High_8bit
buf[0] = value; //DATA_LOW_8bit
WriteToAD5420(buf); //Write 01 99 66H to SHIFT REGISTER to write 99 66 to DATA REGISTER
Delay100us();
}
void Delay3000ms() //@24.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 250;
j = 150;
k = 212;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
AD5420_sclk =0;
AD5420_sdin =0;
AD5420_latch=0;
Delay3000ms();
ad5420_init();
AD5420BACK=32768;
ad5420_setValue(AD5420BACK);
while(1)
{
}
}
本文来自论坛,点击查看完整帖子内容。