//--------------------------------------------------------------------------
//-------------------------读取AD7705转换数据函数--------------------------
//入口参数:指向main()主函数定义的ad_data变量的常量指针
//--------------------------------------------------------------------------
void ReadData7705(unsigned int *const pdata)
{
unsigned int data=0, fiter[5]={0};
unsigned long int temp=0;
unsigned char i=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 = ( (unsigned long int)fiter[1] + (unsigned long int)fiter[2]
+ (unsigned long int)fiter[3] )/3;
data = (unsigned int)temp;
if( (data<0xfff0) && (data&0x000f)>0x0008 )
{
data >>= 4;
data++;
}
else
{
data >>= 4;
}
if( 1 != scale )
{
data -= 0x0800;
}
*pdata = data & 0x0fff;
}
//---------------------------------------------------------------------------------------
//校准命令格式
//STX Data Long Command Code Parameter CheckSum ETX
//0x55 数据长度(2) 量程指示 00H/01H CRC16(2) 0x0D
void AD7705_calibration(void)
{
unsigned char readtimes =0;
unsigned char cali_scale =0;
unsigned long int temp =0;
unsigned char coefficient[8] ={0}; //test[8]={0};
//16位校验和的临时变量
unsigned int crcvalue =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 < time_sys_cali );
stop_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] = (unsigned char)( temp%256 );
coefficient[1] = (unsigned char)( (temp/256)%256 );
coefficient[2] = (unsigned char)( (temp/65536)%256 );
}
else if( FULL_CALIBRATION == command[3] )
{
temp = (unsigned long int)(coefficient[0]) + (unsigned long int)(coefficient[1])*256
+ (unsigned long int)(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 < time_sys_cali );
stop_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] = (unsigned char)( temp%256 );
coefficient[4] = (unsigned char)( (temp/256)%256 );
coefficient[5] = (unsigned char)( (temp/65536)%256 );
}
else
{
AD_CS1;
_delay_us(5);
return;
}
AD_CS1;
_delay_us(5);
crcvalue = checksum(&coefficient[0],6);
coefficient[6] = (unsigned char)(crcvalue%256);//
coefficient[7] = (unsigned char)(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;
}
|