- 阅读权限
- 90
- UID
- 12
- 帖子
- 30
- 精华
- 2
- 注册时间
- 2013-2-26
- 在线时间
- 12 小时
- UID
- 12
- 帖子
- 30
- 精华
- 2
- 注册时间
- 2013-2-26
- 在线时间
- 12 小时
|
功能:取ADS7805的16位AD转换值,同时将转换来的值转成-2--+2
//---------------------------------------
//This procedure is prepared for the ADS7805 for AD convertion
//Charge the PIN definition for your configration.
//The procedure is complied under ICCAVR.
//ADC_init();ADC_to_um(void) must be do;
//Author:Sunany
//Date:2010-12-1
//-----------------------------------------
#define RC_DIR_OUT DDRD|=BIT(4)
#define RC_H PORTD|=BIT(4)
#define RC_L PORTD&=~BIT(4)
#define BYTE_DIR_OUT DDRD|=BIT(5)
#define BYTE_H PORTD|=BIT(5)
#define BYTE_L PORTD&=~BIT(5)
#define BUSY_DIR_IN DDRD&=~BIT(6)
#define BUSY_H PORTD|=BIT(6)
#define BUSY_read (PIND&0x40) //Get ADC Busy or not
#define Data_port1_IN DDRC&=0xC0 //BIT 0-BIT 5 is PC0-PC5
#define Data_port2_IN DDRD&=0xF3 //Bit 6 is PD2,BIT 7 is PD3
#define Data_port ( (PINC & 0x3F) | ((PIND&0x0C) << 4) )
void ADS7805_init(void)
{
RC_DIR_OUT;
RC_H;
BYTE_DIR_OUT;
BYTE_H;
BUSY_DIR_IN;
Data_port1_IN;
Data_port2_IN;
}
uint Start_ADC(void)
{
uint ADC_tempH=0;
uint ADC_tempL=0;
uchar conv_ok=0;
RC_L;
asm("nop");
RC_H;
delay_us(6);
conv_ok=1;
if(BUSY_read==0)
{
delay_us(2);
if(BUSY_read==0)
{
conv_ok=0;
}
}
if(conv_ok==1)
{
asm("nop");
BYTE_L;
delay_us(1);
ADC_tempH=Data_port;
BYTE_H;
delay_us(1);
ADC_tempL=Data_port;
ADC_tempH=(ADC_tempH<<8)+ADC_tempL;
}
return(ADC_tempH);
}
signed int Avg_ADC(signed int *data )
{
uchar count,i,j;
signed int value_buf[12];
signed int temp;
signed long sum=0;
for (count=0;count<12;count++)
{
value_buf[count] =data[count];
}
for (j=0;j<11;j++)
{
for (i=0;i<11-j;i++)
{
if ( value_buf[i]>value_buf[i+1] )
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
for(count=2;count<10;count++)
sum += value_buf[count];
return( (signed int)(sum>>3) );
}
signed int AD_conversion(void)
{
uchar j;
signed int temp;
signed int ADC0_temp[12]={0};
for(j=0;j<12;j++)
{
ADC0_temp[j]=Start_ADC();
delay_us(1);
}
temp=Avg_ADC(ADC0_temp);
return(temp);
}
signed int ADC_to_um(void)
{
signed long ADC_data_temp;
ADC_data_temp=AD_conversion();
ADC_data_temp=(signed int)(ADC_data_temp*0.62); //固定系数,按2mm对应32768得到
if( ADC_data_temp> 20000 )
{
ADC_data_temp=20000;
}
if( ADC_data_temp<-20000 )
{
ADC_data_temp=-20000;
}
return (ADC_data_temp);
}
//较准用测量函数
signed int ADC_Calib_um(signed int stander,signed int calib)
{
signed long ADC_data_temp;
ADC_data_temp=AD_conversion();
ADC_data_temp=(signed int)( (ADC_data_temp-calib)*0.62 )+stander; //计算校准后值
return (ADC_data_temp);
}
|
|