AD7705程序
//--------------------------------------------------------------------------
//-------------------------读取AD7705转换数据函数--------------------------
//入口参数:指向main()主函数定义的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;
}
//---------------------------------------------------------------------------------------
//校准命令格式
//STXDataLongCommandCodeParameterCheckSumETX
//0x55数据长度(2)量程指示00H/01HCRC16(2)0x0D
voidAD7705_calibration(void)
{
unsignedcharreadtimes=0;
unsignedcharcali_scale=0;
unsignedlonginttemp=0;
unsignedcharcoefficient[8]={0};//test[8]={0};
//16位校验和的临时变量
unsignedintcrcvalue=0;
AD_CS1;
cali_scale=command[2];//获取上位机发送的要校准的量程类型
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])//校准命令为零校准
{
write_AD7705_byte(WR_SETUP_REG);
write_AD7705_byte(text_of_setup[cali_scale-1]|SYS_ZERO_CALI);
//等待校准完成
start_timer0();
while(time_count
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);
//等待校准完成
start_timer0();
while(time_count
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]);//输出校准数据给上位机
return;
}
//
voidstart_AD7705(void)
{
reset_AD7705();
//写OFFSET寄存器
write_AD7705_byte(WR_OFFSET_REG);
write_AD7705_dword(ZS);
//写满量程校准寄存器
write_AD7705_byte(WR_FULL_REG);
write_AD7705_dword(GS);
//CLOCK寄存器设置,无分频,50HZ输出更新速率
write_AD7705_byte(WR_CLOCK_REG);
write_AD7705_byte(CLOCK_REG_SET);
//写设置寄存器
write_AD7705_byte(WR_SETUP_REG);
write_AD7705_byte(text_of_setup[scale-1]);
start_timer0();
while(time_count
}
评论