Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
scale.cpp
00001 #include "mbed.h" 00002 #include <HX711.h> 00003 #include <eeprom.h> 00004 #include <SB1602E.h> 00005 #include "yoda2.h" 00006 #include "TextLCD.h" 00007 #if 1 00008 extern EventFlags LCD_update_flags; 00009 extern DigitalOut led1; 00010 extern DigitalOut led2; 00011 extern EEPROM ep; 00012 #ifdef STM32F207xx 00013 SB1602E lcd( PB_9, PB_8 ); // SDA, SCL 00014 #endif 00015 #ifdef STM32F303xE 00016 #ifdef LCD_1602 00017 extern SB1602E lcd; // SDA, SCL 00018 #endif 00019 #ifdef YODA2 00020 extern TextLCD_I2C_N lcd; 00021 #endif 00022 #endif 00023 extern DigitalOut output1; 00024 extern DigitalOut output2; 00025 extern DigitalOut output3; 00026 extern DigitalIn input1; 00027 extern DigitalIn input2; 00028 extern int current_weight; 00029 00030 ScaleCalibrationData customVar; 00031 00032 unsigned int calibration_ADC_value; 00033 //#define CALIBRATION_VALUE 10 // 10g as the calibration weight 00034 00035 00036 /* scale */ 00037 extern HX711 hx711; 00038 long zero_value; 00039 long calibration_value; 00040 unsigned int calibration_times; // must calibration 3 times 00041 unsigned int calibration_done = 0; 00042 float scale_value; 00043 00044 00045 /* scale */ 00046 00047 void scaleCalibration(bool release_led) 00048 { 00049 unsigned char eeprom_data[sizeof(customVar)]; 00050 unsigned char checksum = 0; 00051 printf("Start Calibration.\r\n"); 00052 if (release_led) 00053 LCD_update_flags.set(LCD_DISPLAY_HOLD_EVENT); 00054 #ifdef LCD_1602 00055 lcd.clear(); 00056 lcd.printf(0, 0, "Calibration"); 00057 lcd.printf(0, 1, "1.Empty the scale"); 00058 #endif 00059 #ifdef YODA2 00060 lcd.cls(); 00061 lcd.setAddress(0,0); 00062 lcd.printf("Calibration"); 00063 lcd.setAddress(0,1); 00064 lcd.printf("1.Empty the scale"); 00065 #endif 00066 calibration_done = 0; 00067 if (!calibration_done) 00068 { 00069 led1 = 1; 00070 led2 = 0; 00071 if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT)||(customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT)) 00072 customVar.calibrationWeight = CALIBRATION_WEIGHT; 00073 zero_value = hx711.averageValue(10); // skip first 10 readings 00074 zero_value = hx711.averageValue(20); 00075 printf("zero_value=%d \r\n", zero_value); 00076 #ifdef LCD_1602 00077 lcd.clear(); 00078 lcd.printf(0, 0, "Calibration"); 00079 lcd.printf(0, 1, "2.put %dg weights", customVar.calibrationWeight); 00080 #endif 00081 #ifdef YODA2 00082 lcd.cls(); 00083 lcd.setAddress(0,0); 00084 lcd.printf("Calibration"); 00085 lcd.setAddress(0,1); 00086 lcd.printf("2.put %dg weights", customVar.calibrationWeight); 00087 #endif 00088 calibration_value = 0; 00089 scale_value = 0; 00090 calibration_times = 0; 00091 led2 = 1; 00092 while (( calibration_times < 5)) 00093 { 00094 calibration_value = hx711.averageValue(20); 00095 printf("calibration_value=%d\r\n", calibration_value); 00096 if (calibration_value > (zero_value + WEIGHT_DIFFERENCE)) 00097 { 00098 calibration_times++; 00099 } 00100 else 00101 calibration_times = 0; 00102 } 00103 printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times); 00104 if (calibration_times >=5) 00105 { 00106 // calibration is OK 00107 calibration_times = 0; 00108 scale_value = (calibration_value - zero_value) / customVar.calibrationWeight; 00109 customVar.offsetValue = zero_value; 00110 customVar.scaleValue = scale_value; 00111 // EEPROM.put(0x00, customVar); 00112 hx711.setOffset(zero_value); 00113 hx711.setScale(scale_value); // this value is obtained by calibrating the scale with known weights; see the README for details 00114 memcpy(eeprom_data, &customVar, sizeof(customVar)); 00115 for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here 00116 { 00117 checksum += eeprom_data[cnt]; 00118 } 00119 customVar.checksum = checksum; 00120 printf("EEPROM write calibration data: \r\n"); 00121 printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); 00122 printf("offsetValue=%d \r\n", customVar.offsetValue); 00123 printf("scaleValue=%f \r\n", customVar.scaleValue); 00124 printf("checksum=0x%02x \r\n", customVar.checksum); 00125 ep.write((uint32_t)0x00,(void *)&customVar,sizeof(customVar)); // write a structure eeprom_size - 32 00126 #ifdef LCD_1602 00127 lcd.clear(); 00128 lcd.printf(0, 0, "Calibration"); 00129 lcd.printf(0, 1, "Done"); 00130 #endif 00131 #ifdef YODA2 00132 lcd.cls(); 00133 lcd.setAddress(0,0); 00134 lcd.printf("Calibration"); 00135 lcd.setAddress(0,1); 00136 lcd.printf("Done"); 00137 #endif 00138 if (release_led) 00139 LCD_update_flags.set(LCD_DISPLAY_RELEASE_EVENT); 00140 calibration_done = 1; 00141 led1 = 1; 00142 printf("Calibration Done\r\n"); 00143 } 00144 } 00145 if (release_led) 00146 LCD_update_flags.set(LCD_DISPLAY_RELEASE_EVENT); 00147 } 00148 00149 int change_calibrationWeight(unsigned int new_weight) 00150 { 00151 unsigned char eeprom_data[sizeof(customVar)]; 00152 unsigned char checksum = 0; 00153 00154 if ((new_weight <= MAXIMUM_CALIBRATION_WEIGHT)&&(new_weight > MINIMUM_CALIBRATION_WEIGHT)) 00155 customVar.calibrationWeight = new_weight; 00156 else 00157 return -1; 00158 memcpy(eeprom_data, &customVar, sizeof(customVar)); 00159 for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here 00160 { 00161 checksum += eeprom_data[cnt]; 00162 } 00163 customVar.checksum = checksum; 00164 printf("EEPROM write calibration data: \r\n"); 00165 printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); 00166 printf("offsetValue=%d \r\n", customVar.offsetValue); 00167 printf("scaleValue=%f \r\n", customVar.scaleValue); 00168 printf("checksum=0x%02x \r\n", customVar.checksum); 00169 ep.write((uint32_t)0x00,(void *)&customVar,sizeof(customVar)); // write a structure eeprom_size - 32 00170 } 00171 00172 void init_scale() 00173 { 00174 unsigned char eeprom_data; 00175 unsigned char checksum = 0; 00176 customVar.calibrationWeight = CALIBRATION_WEIGHT; 00177 00178 #if 1 00179 printf("sizeof(customVar)=%d \r\n", sizeof(customVar)); 00180 ep.read((uint32_t)0,(void *)&customVar, sizeof(customVar)); 00181 printf("EEPROM read calibration data: \r\n"); 00182 printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); 00183 printf("offsetValue=%d \r\n", customVar.offsetValue); 00184 printf("scaleValue=%f \r\n", customVar.scaleValue); 00185 printf("checksum=0x%02x \r\n", customVar.checksum); 00186 printf("\r\n calculate checksum: \r\n"); 00187 for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here 00188 { 00189 ep.read(cnt, (int8_t&)eeprom_data); 00190 printf("0x%02x ", eeprom_data); 00191 checksum += eeprom_data; 00192 printf("checksum=0x%02x\r\n", checksum); 00193 } 00194 printf("\r\ncalculated checksum=0x%02x \r\n", checksum); 00195 00196 if (checksum == customVar.checksum) 00197 { 00198 if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT) || (customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT)) 00199 { 00200 customVar.calibrationWeight = CALIBRATION_WEIGHT; 00201 scaleCalibration(true); 00202 } 00203 if ((customVar.offsetValue < 10000)) 00204 { 00205 customVar.offsetValue = 10000; 00206 scaleCalibration(true); 00207 } 00208 if ((customVar.scaleValue < 100)) 00209 { 00210 customVar.scaleValue = 100; 00211 scaleCalibration(true); 00212 } 00213 // delay(200); 00214 hx711.setOffset(customVar.offsetValue); 00215 hx711.setScale(customVar.scaleValue); 00216 } 00217 else 00218 { 00219 scaleCalibration(true); 00220 } 00221 #endif 00222 } 00223 void scale_reading() 00224 { 00225 float weight; 00226 int gpioState1, gpioState2; 00227 uint32_t flags_read = 0; 00228 bool hold_lcd = true; 00229 #ifdef LCD_1602 00230 lcd.clear(); 00231 lcd.printf(0, 0, "Weight (g)"); 00232 lcd.printf(0, 1, "0 "); 00233 #endif 00234 #ifdef YODA2 00235 lcd.cls(); 00236 lcd.setAddress(0,0); 00237 lcd.printf("Weight (gram)"); 00238 lcd.setAddress(0,1); 00239 lcd.printf("0 "); 00240 #endif 00241 while (true) 00242 { 00243 flags_read = LCD_update_flags.wait_any((LCD_DISPLAY_HOLD_EVENT|LCD_DISPLAY_RELEASE_EVENT), 10); 00244 if (flags_read == LCD_DISPLAY_HOLD_EVENT) 00245 { 00246 hold_lcd = false; 00247 } 00248 if (flags_read == LCD_DISPLAY_RELEASE_EVENT) 00249 { 00250 hold_lcd = true; 00251 #ifdef LCD_1602 00252 lcd.clear(); 00253 lcd.printf(0, 0, "Weight (g)"); // line# (0 or 1), string 00254 #endif 00255 #ifdef YODA2 00256 lcd.cls(); 00257 lcd.setAddress(0,0); 00258 lcd.printf("Weight (gram)"); 00259 #endif 00260 } 00261 if (hold_lcd) 00262 { 00263 weight= hx711.getGram(5)+ 0.5; 00264 current_weight = (int)weight; 00265 #ifdef LCD_1602 00266 lcd.printf(0, 1, " "); // line# (0 or 1), string 00267 lcd.printf(0, 1, "%d ", (int)weight ); // line# (0 or 1), string 00268 #endif 00269 #ifdef YODA2 00270 lcd.setAddress(0,1); 00271 lcd.printf(" "); 00272 lcd.setAddress(0,1); 00273 lcd.printf("%d ", (int)weight); 00274 #endif 00275 } 00276 } 00277 } 00278 #endif 00279 /*scale end*/
Generated on Tue Jul 12 2022 11:49:34 by
