|
ardino制作,简单体重计,使用压力传感器,通过公式转换,串口显示数据。
- #include "hx711.h"
- #define ValueGap 630
- //该数值可用砝码校准
- long HX711_Buffer = 0;
- long Weight_Maopi = 0,Weight_Shiwu = 0;
- //****************************************************
- //初始化HX711
- //****************************************************
- void Init_Hx711()
- {
- pinMode(HX711_SCK, OUTPUT);
- pinMode(HX711_DT, INPUT);
- }
- //****************************************************
- //获取毛皮重量
- //****************************************************
- void Get_Maopi()
- {
- Weight_Maopi = HX711_Read();
- }
- //****************************************************
- //称重
- //****************************************************
- unsigned int Get_Weight()
- {
- HX711_Buffer = HX711_Read();
- Weight_Shiwu = HX711_Buffer;
- Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
-
- Weight_Shiwu = (int)((float)Weight_Shiwu/ValueGap+0.05);
- return Weight_Shiwu;
- }
- //****************************************************
- //读取HX711
- //****************************************************
- unsigned long HX711_Read(void) //增益128
- {
- unsigned long count;
- unsigned char i;
- bool Flag = 0;
- digitalWrite(HX711_DT, HIGH);
- delayMicroseconds(1);
- digitalWrite(HX711_SCK, LOW);
- delayMicroseconds(1);
- count=0;
- while(digitalRead(HX711_DT));
- for(i=0;i<24;i++)
- {
- digitalWrite(HX711_SCK, HIGH);
- delayMicroseconds(1);
- count=count<<1;
- digitalWrite(HX711_SCK, LOW);
- delayMicroseconds(1);
- if(digitalRead(HX711_DT))
- count++;
- }
- digitalWrite(HX711_SCK, HIGH);
- delayMicroseconds(1);
- digitalWrite(HX711_SCK, LOW);
- delayMicroseconds(1);
- count ^= 0x800000;
- return(count);
- }
复制代码
|
-
-
体重秤.zip
4.28 KB, 下载次数: 18, 下载积分: 黑币 -5
|