新聞中心

AD7705程序

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏

//--------------------------------------------------------------------------
//-------------------------讀取據(jù)函數(shù)--------------------------
//入口參數(shù):指向main()主函數(shù)定義的ad_data變量的常量指針
//--------------------------------------------------------------------------
voidReadData7705(unsignedint*constpdata)
{
unsignedintdata=0,fiter[5]={0};
unsignedlonginttemp=0;
unsignedchari=0,sort_flag=1;

AD_CS0;
_delay_us(5);

start_AD7705();

for(i=0;i<5;i++)
{
start_timer0();
_delay_ms(2);
while(AD_DRDY)
{
if(time_count>=time_read_data)
{
stop_timer0();
AD_CS1;
return;
}
}
stop_timer0();

write_AD7705_byte(RD_DATA_REG);
fiter[i]=read_AD7705_word();
}
AD_CS1;

sort_flag=1;
while(sort_flag)
{
sort_flag=0;
for(i=0;i<4;i++)
{
if(fiter[i]>fiter[i+1])
{
data=fiter[i];
fiter[i]=fiter[i+1];
fiter[i+1]=data;
sort_flag=1;
}
}
}

temp=((unsignedlongint)fiter[1]+(unsignedlongint)fiter[2]
+(unsignedlongint)fiter[3])/3;

data=(unsignedint)temp;

if((data<0xfff0)&&(data&0x000f)>0x0008)
{
data>>=4;
data++;
}
else
{
data>>=4;
}

if(1!=scale)
{
data-=0x0800;
}

*pdata=data&0x0fff;
}

//---------------------------------------------------------------------------------------
//校準(zhǔn)命令格式
//STXDataLongCommandCodeParameterCheckSumETX
//0x55數(shù)據(jù)長(zhǎng)度(2)量程指示00H/01HCRC16(2)0x0D

voidAD7705_calibration(void)
{
unsignedcharreadtimes=0;

unsignedcharcali_scale=0;

unsignedlonginttemp=0;

unsignedcharcoefficient[8]={0};//test[8]={0};

//16位校驗(yàn)和的臨時(shí)變量
unsignedintcrcvalue=0;

AD_CS1;

cali_scale=command[2];//獲取上位機(jī)發(fā)送的要校準(zhǔn)的量程類型

eeprom_busy_wait();
eeprom_read_block(&coefficient[0],(void*)(ADDR_EEPROM_1+(cali_scale-1)*10),8);
crcvalue=checksum(&coefficient[0],6);
if((coefficient[7]*256+coefficient[6])!=crcvalue)
{
readtimes++;
}

if(1==readtimes)
{
eeprom_busy_wait();
eeprom_read_block(&coefficient[0],(void*)(ADDR_EEPROM_2+(cali_scale-1)*10),8);
crcvalue=checksum(&coefficient[0],6);//
if((coefficient[7]*256+coefficient[6])!=crcvalue)
{
readtimes++;
}
}

if(2==readtimes)
{
eeprom_busy_wait();
eeprom_read_block(&coefficient[0],(void*)(ADDR_EEPROM_3+(cali_scale-1)*10),8);
}

ADDR409_MASK;
AD_CS0;
_delay_us(5);
reset_AD7705();

write_AD7705_byte(WR_CLOCK_REG);
write_AD7705_byte(CLOCK_REG_SET);

if(ZERO_CALIBRATION==command[3])//校準(zhǔn)命令為零校準(zhǔn)
{
write_AD7705_byte(WR_SETUP_REG);
write_AD7705_byte(text_of_setup[cali_scale-1]|SYS_ZERO_CALI);

//等待校準(zhǔn)完成
start_timer0();
while(time_countstop_timer0();

while(AD_DRDY);

//讀OFFSET寄存器
write_AD7705_byte(RD_OFFSET_REG);
temp=read_AD7705_dword();

if(cali_scale==scale)
{
ZS=temp;NO_CALI_TYPE=NO_FULL_CALIBRATION;
}

coefficient[0]=(unsignedchar)(temp%256);
coefficient[1]=(unsignedchar)((temp/256)%256);
coefficient[2]=(unsignedchar)((temp/65536)%256);
}
elseif(FULL_CALIBRATION==command[3])
{

temp=(unsignedlongint)(coefficient[0])+(unsignedlongint)(coefficient[1])*256
+(unsignedlongint)(coefficient[2])*65536;

write_AD7705_byte(WR_OFFSET_REG);
write_AD7705_dword(temp);

write_AD7705_byte(WR_SETUP_REG);
write_AD7705_byte(text_of_setup[cali_scale-1]|SYS_FULL_CALI);

//等待校準(zhǔn)完成
start_timer0();
while(time_countstop_timer0();

while(AD_DRDY);//
//讀FULL寄存器
write_AD7705_byte(RD_FULL_REG);
temp=read_AD7705_dword();

if(cali_scale==scale)
{
GS=temp;
NO_CALI_TYPE=ALREADY_CALIBRATION;
}

coefficient[3]=(unsignedchar)(temp%256);
coefficient[4]=(unsignedchar)((temp/256)%256);
coefficient[5]=(unsignedchar)((temp/65536)%256);
}
else
{
AD_CS1;
_delay_us(5);
return;
}

AD_CS1;
_delay_us(5);

crcvalue=checksum(&coefficient[0],6);
coefficient[6]=(unsignedchar)(crcvalue%256);//
coefficient[7]=(unsignedchar)(crcvalue/256);

eeprom_busy_wait();
eeprom_write_block(&coefficient[0],(void*)(ADDR_EEPROM_1+(cali_scale-1)*10),8);

eeprom_busy_wait();
eeprom_write_block(&coefficient[0],(void*)(ADDR_EEPROM_2+(cali_scale-1)*10),8);

eeprom_busy_wait();
eeprom_write_block(&coefficient[0],(void*)(ADDR_EEPROM_3+(cali_scale-1)*10),8);

Txout(&coefficient[0]);//輸出校準(zhǔn)數(shù)據(jù)給上位機(jī)

return;
}


//
voidstart_AD7705(void)
{
reset_AD7705();

//寫OFFSET寄存器
write_AD7705_byte(WR_OFFSET_REG);
write_AD7705_dword(ZS);

//寫滿量程校準(zhǔn)寄存器
write_AD7705_byte(WR_FULL_REG);
write_AD7705_dword(GS);

//CLOCK寄存器設(shè)置,無分頻,50HZ輸出更新速率
write_AD7705_byte(WR_CLOCK_REG);
write_AD7705_byte(CLOCK_REG_SET);

//寫設(shè)置寄存器
write_AD7705_byte(WR_SETUP_REG);
write_AD7705_byte(text_of_setup[scale-1]);

start_timer0();
while(time_countstop_timer0();
}

上一頁(yè) 1 2 下一頁(yè)

關(guān)鍵詞: AD7705轉(zhuǎn)換數(shù)

評(píng)論


技術(shù)專區(qū)

關(guān)閉