去年省比赛准备51模块程序之一.




以上部分截自于厂家数据手册。
#include <STC12C5A60S2.H>
#include"lcd12864.h"
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Maopi_0 = 0;
long Weight_Shiwu = 0;
sbit HX711_SCK=P1^0;
sbit HX711_DOUT=P1^1;
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
HX711_DOUT=1;
Delay__5us();
HX711_SCK=0;
count=0;
while(HX711_DOUT);
for(i=0;i<24;i++)
{
HX711_SCK=1;
count=count<<1;
HX711_SCK=0;
if(HX711_DOUT)
count++;
}
HX711_SCK=1;
count=count^0x800000;//第25个脉冲下降沿来时,转换数据
Delay__5us();
HX711_SCK=0;
return(count);
}
void Get_Weight()
{
HX711_Buffer = HX711_Read();
Weight_Shiwu=HX711_Buffer;
// Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取净重
if(Weight_Shiwu > 0)
{
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu*10/GapValue)-8530; //计算实物的实际重量
//
// if(Weight_Shiwu > 3000) //超重报警
// {
// Flag_ERROR = 1;
// }
// else
// {
// Flag_ERROR = 0;
// }
// }
// else
// {
// Weight_Shiwu = 0;
// // Flag_ERROR = 1; //负重报警
// }
}
delay(5);
Display_Weight();
}
注:我在实际使用中,是用法吗进行多点数据拟合而成的。并且用冒泡排序把前后一些过大过小值排除,取中间值。
版权声明:本文为zy19981110原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。