2017-11-2910:41:12
执行效果,即标准砝码自动矫正功能!
1 #include <HX711.h> // 包含库的头文件 2 #include <EEPROM.h> //寄存器库 3 #include <PinChangeInt.h> 4 long test; 5 int i=0; 6 HX711 hx(9, 10,64); // 数据接脚定义 7 unsigned char *dpointer; 8 union data { 9 double v; 10 unsigned char dchar[8]; 11 } dvalue; 12 /////////////////////////////////////////////// 13 long pyl (void) //偏移量 14 { 15 pingjunzhi(); 16 return pingjunzhi(); 17 } 18 double jiaozhun (void ) 19 { 20 double jieguo1; 21 double jieguo2; 22 double jieguo3; 23 pingjunzhi();//空走一遍初始化校准程序 24 jieguo1=pingjunzhi(); 25 Serial.println(jieguo1); 26 delay(1000); 27 Serial.println("现在进行校准!请在3秒内放入500g砝码,请放入500g砝码"); 28 delay(3000); 29 pingjunzhi();//空走一遍初始化校准程序 30 delay(1000); 31 jieguo2=pingjunzhi(); 32 Serial.println(jieguo2); 33 jieguo3=500/(jieguo2-jieguo1); 34 Serial.println("校准成功:修正系数为"); 35 Serial.print(jieguo3,10); 36 /////////////////////////////////////存储数据 37 dvalue.v =jieguo3; 38 dpointer = dvalue.dchar ; 39 for(i = 0; i < 8; i++) { 40 EEPROM.write(i,*dpointer); 41 dpointer++; 42 } 43 ///////////////////////////// 44 Serial.println("误差系数记忆成功"); 45 } 46 double pingjunzhi (void) 47 { 48 double zhi; 49 double sum = 0; // 为了减小误差,一次取出10个值后求平均值。 50 for (int i = 0; i < 10; i++) // 循环的越多精度越高,当然耗费的时间也越多 51 sum += hx.read(); // 累加 52 zhi=sum/10; 53 //Serial.println(zhi); // 求平均值进行均差 54 return zhi; 55 } 56 double jiance (void) 57 { 58 Serial.println("开机检测设备,,,,,,,"); 59 delay(3000); 60 if(digitalRead(4) ==HIGH) 61 jiaozhun (); 62 63 delay(500); 64 for(i = 0; i < 8; i++) dvalue.dchar[i] = EEPROM.read(i); 65 Serial.println("误差系数记忆值为"); 66 Serial.print(dvalue.v,10); 67 delay(500); 68 Serial.println("即将进入称量状态"); 69 return dvalue.v; 70 } 71 void setup() { 72 Serial.begin(9600); 73 long y =pyl(); 74 Serial.println(pyl()); 75 double x = jiance(); 76 Serial.println(x); 77 hx.set_co(x); //修改修正系数 78 hx.set_offset(y);//修改偏移值 79 delay(5000); 80 } 81 void loop() 82 { 83 delay(1000); 84 double sum0 = 0; 85 double sum1 = 0; 86 for (int i = 0; i < 10; i++) { 87 sum0 += hx.read(); 88 sum1 += hx.bias_read(); 89 } 90 Serial.print(sum0/10); 91 Serial.print(" "); 92 Serial.println(sum1/10); 93 }
时间: 2024-11-05 18:47:36