压力传感器HX711的C函数

内容所有权属于:http://www.xinduofen.com/

#define ADDO P1_2

#define ADSK P1_3

#define AdCnt 10//定义AD采样平均值的次数(AdCnt>2)

//读HX711的AD转换数据函数

unsigned long ReadAdValue(void)

unsigned long Count = 0;

unsigned char i;

ADDO=1;

ADSK=0;

Count=0;

while(ADDO);

for(i=0;i<24;i++)//读取24位数据并发送24个脉冲

{

ADSK=1;

Count=Count<<1;

ADSK=0;

if(ADDO)Count++;

}

ADSK=1; //发送第25个脉冲

delay_us(1); 

ADSK=0;

return(Count);

}

signed long getAdcValue(){//获得一次体重秤的AD值函数

unsigned char k;

unsigned long adValue;//将要读取的单个AD值缓存区(二进制补码)

signed long adValueY;//将读取的单个AD值转换成原码的缓存区

signed long adMinValue;//用于存储AdCnt次AD值当中的最小值,初始化为‘第一个AD采样值’

signed long adMaxValue;//用于存储AdCnt次AD值当中的最大值,初始化为‘第一个AD采样值’

signed long adValueAll = 0;//将要读取的AdCnt次AD值总和缓存区,初始化为‘0’

for(k=0;k<AdCnt;k++){

adValue = http://xinduofen.blog.51cto.com/8697804/ReadAdValue();

if(adValue&0x00800000){//如果为负数的补码,将负数的补码转换成正数(正数的补吗就是原码)

adValue=http://xinduofen.blog.51cto.com/8697804/((~adValue)+1)&0x007FFFFF;

adValueY = -adValue;

}else{//如果为正数的补码,正数的补码就是原码

adValueY = adValue;

}

if(k==0){//如果为第一个AD采样值

adMinValue = http://xinduofen.blog.51cto.com/8697804/adValueY;//初始化最小值

adMaxValue = http://xinduofen.blog.51cto.com/8697804/adValueY;//初始化最大值

}else{

if(adValueY<adMinValue){

adMinValue = http://xinduofen.blog.51cto.com/8697804/adValueY;//更新最小值

}

if(adValueY>adMaxValue){

adMaxValue = http://xinduofen.blog.51cto.com/8697804/adValueY;//更新最大值

}

}

adValueAll = adValueAll + adValueY;//累计10次AD值的总和

}

adValueAll = adValueAll - adMinValue - adMaxValue;//去掉一个最小值和一个最大值

adValueY = adValueAll/(AdCnt-2);//取(AdCnt-2)次AD值的平均值

return adValueY;//返回AD值

}

内容来自:越康体育

赞 (0) 评论 分享 ()