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.
Diff: main.cpp
- Revision:
- 2:61a0169765bf
- Parent:
- 1:eb499e2a1b9b
- Child:
- 4:40bb33497de4
--- a/main.cpp Sat Feb 16 07:02:56 2019 +0000 +++ b/main.cpp Sat Mar 02 08:16:23 2019 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include <HX711.h> +#include <eeprom.h> CAN can1(PD_0, PD_1); CAN can2(PB_5, PB_6); @@ -7,45 +8,62 @@ DigitalOut led2(LED2); //FlashIAP flashIAP; -/* scale */ -HX711 hx711(PB_11, PB_10); +#define EEPROM_ADDR 0x0 // I2c EEPROM address is 0x00 + +#define SDA PB_9 // I2C SDA pin +#define SCL PB_8 // I2C SCL pin + +#define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) +#define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) + +EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256); struct ScaleCalibrationData { unsigned int calibrationWeight; // the weight (g) used for calibration for example 1000g or 10g. The maximum value is 3000. long offsetValue; // the value for scale offset float scaleValue; // the ADC increment for 1g - unsigned char checksum; + uint8_t checksum; }; +ScaleCalibrationData customVar; + unsigned int calibration_ADC_value; //#define CALIBRATION_VALUE 10 // 10g as the calibration weight #define WEIGHT_DIFFERENCE 200 // 10g ADC value minimum difference #define CALIBRATION_WEIGHT 2000 // calibration weight +#define MAXIMUM_CALIBRATION_WEIGHT 5000 +#define MINIMUM_CALIBRATION_WEIGHT 100 -ScaleCalibrationData customVar; +/* scale */ +HX711 hx711(PB_11, PB_10); long zero_value; long calibration_value; unsigned int calibration_times; // must calibration 3 times unsigned int calibration_done = 0; float scale_value; +int init_id = 0x537; // first 8 bit is the address /* scale */ + void scaleCalibration() { + unsigned char eeprom_data[sizeof(customVar)]; + unsigned char checksum = 0; printf("Start Calibration.\r\n"); calibration_done = 0; if (!calibration_done) { led1 = 1; led2 = 0; - customVar.calibrationWeight = CALIBRATION_WEIGHT; + if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT)||(customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT)) + customVar.calibrationWeight = CALIBRATION_WEIGHT; zero_value = hx711.averageValue(10); // skip first 10 readings zero_value = hx711.averageValue(20); printf("zero_value=%d \r\n", zero_value); calibration_value = 0; scale_value = 0; calibration_times = 0; - + led2 = 1; while (( calibration_times < 5)) { @@ -61,8 +79,6 @@ if (calibration_times >=5) { // calibration is OK - led1 = 0; - led2 = 1; calibration_times = 0; scale_value = (calibration_value - zero_value) / customVar.calibrationWeight; customVar.offsetValue = zero_value; @@ -70,11 +86,78 @@ // EEPROM.put(0x00, customVar); hx711.setOffset(zero_value); hx711.setScale(scale_value); // this value is obtained by calibrating the scale with known weights; see the README for details + memcpy(eeprom_data, &customVar, sizeof(customVar)); + for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here + { + checksum += eeprom_data[cnt]; + } + customVar.checksum = checksum; + printf("EEPROM write calibration data: \r\n"); + printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); + printf("offsetValue=%d \r\n", customVar.offsetValue); + printf("scaleValue=%f \r\n", customVar.scaleValue); + printf("checksum=0x%02x \r\n", customVar.checksum); + ep.write((uint32_t)0x00,(void *)&customVar,sizeof(customVar)); // write a structure eeprom_size - 32 calibration_done = 1; + led1 = 1; + printf("Calibration Done\r\n"); } } } +void init_scale() +{ + unsigned char eeprom_data; + unsigned char checksum = 0; + customVar.calibrationWeight = CALIBRATION_WEIGHT; + +#if 1 + printf("sizeof(customVar)=%d \r\n", sizeof(customVar)); + ep.read((uint32_t)0,(void *)&customVar, sizeof(customVar)); + printf("EEPROM read calibration data: \r\n"); + printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); + printf("offsetValue=%d \r\n", customVar.offsetValue); + printf("scaleValue=%f \r\n", customVar.scaleValue); + printf("checksum=0x%02x \r\n", customVar.checksum); + printf("\r\n calculate checksum: \r\n"); + for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here + { + ep.read(cnt, (int8_t&)eeprom_data); + printf("0x%02x ", eeprom_data); + checksum += eeprom_data; + printf("checksum=0x%02x\r\n", checksum); + } + printf("\r\ncalculated checksum=0x%02x \r\n", checksum); + + if (checksum == customVar.checksum) + { + if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT) || (customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT)) + { + customVar.calibrationWeight = CALIBRATION_WEIGHT; + scaleCalibration(); + } + if ((customVar.offsetValue < 10000)) + { + customVar.offsetValue = 10000; + scaleCalibration(); + } + if ((customVar.scaleValue < 100)) + { + customVar.scaleValue = 100; + scaleCalibration(); + } + // delay(200); + hx711.setOffset(customVar.offsetValue); + hx711.setScale(customVar.scaleValue); + } + else + { + scaleCalibration(); + } +#endif +} + +/*scale end*/ int a = 0; int b = 0; @@ -135,20 +218,320 @@ } } +typedef struct _MyData { + int16_t sdata; + int32_t idata; + float fdata; + } MyData; + +static void myerror(std::string msg) +{ + printf("Error %s\n",msg.c_str()); + exit(1); +} + +void eeprom_test(void) +{ + // EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64); // 24C64 eeprom with sda = p9 and scl = p10 + uint8_t data[256],data_r[256]; + int8_t ival; + uint16_t s; + int16_t sdata,sdata_r; + int32_t ldata[1024]; + int32_t eeprom_size,max_size; + uint32_t addr; + int32_t idata,idata_r; + uint32_t i,j,k,l,t,id; + float fdata,fdata_r; + MyData md,md_r; + + eeprom_size = ep.getSize(); + max_size = MIN(eeprom_size,256); + + printf("Test EEPROM I2C model %s of %d bytes\r\n",ep.getName(),eeprom_size); + + // Test sequential read byte (max_size first bytes) + for(i = 0;i < max_size;i++) { + ep.read(i,ival); + data_r[i] = ival; + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + printf("Test sequential read %d first bytes :\r\n",max_size); + for(i = 0;i < max_size/16;i++) { + for(j = 0;j < 16;j++) { + addr = i * 16 + j; + printf("%3d ",(uint8_t)data_r[addr]); + } + printf("\r\n"); + } + + // Test sequential read byte (max_size last bytes) + for(i = 0;i < max_size;i++) { + addr = eeprom_size - max_size + i; + ep.read(addr,ival); + data_r[i] = ival; + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + printf("\nTest sequential read %d last bytes :\r\n",max_size); + for(i = 0;i < max_size/16;i++) { + for(j = 0;j < 16;j++) { + addr = i * 16 + j; + printf("%3d ",(uint8_t)data_r[addr]); + } + printf("\r\n"); + } + + // Test write byte (max_size first bytes) + for(i = 0;i < max_size;i++) + data[i] = i; + + for(i = 0;i < max_size;i++) { + ep.write(i,(int8_t)data[i]); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + // Test read byte (max_size first bytes) + for(i = 0;i < max_size;i++) { + ep.read(i,(int8_t&)ival); + data_r[i] = (uint8_t)ival; + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + printf("\nTest write and read %d first bytes :\r\n",max_size); + for(i = 0;i < max_size/16;i++) { + for(j = 0;j < 16;j++) { + addr = i * 16 + j; + printf("%3d ",(uint8_t)data_r[addr]); + } + printf("\r\n"); + } + + // Test current address read byte (max_size first bytes) + ep.read((uint32_t)0,(int8_t&)ival); // current address is 0 + data_r[0] = (uint8_t)ival; + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + for(i = 1;i < max_size;i++) { + ep.read((int8_t&)ival); + data_r[i] = (uint8_t)ival; + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + printf("\nTest current address read %d first bytes :\r\n",max_size); + for(i = 0;i < max_size/16;i++) { + for(j = 0;j < 16;j++) { + addr = i * 16 + j; + printf("%3d ",(uint8_t)data_r[addr]); + } + printf("\n"); + } + + // Test sequential read byte (first max_size bytes) + ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("\nTest sequential read %d first bytes :\r\n",max_size); + for(i = 0;i < max_size/16;i++) { + for(j = 0;j < 16;j++) { + addr = i * 16 + j; + printf("%3d ",(uint8_t)data_r[addr]); + } + printf("\r\n"); + } + + // Test write short, long, float + sdata = -15202; + addr = eeprom_size - 16; + ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16 + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + idata = 45123; + addr = eeprom_size - 12; + ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12 + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + fdata = -12.26; + addr = eeprom_size - 8; + ep.write(addr,(float)fdata); // float write at address eeprom_size - 8 + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + // Test read short, long, float + printf("\nTest write and read short (%d), long (%d), float (%f) :\r\n", + sdata,idata,fdata); + + ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + printf("sdata %d\r\n",sdata_r); + + ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + printf("idata %d\r\n",idata_r); + + ep.read((uint32_t)(eeprom_size - 8),fdata_r); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + printf("fdata %f\r\n",fdata_r); + + // Test read and write a structure + md.sdata = -15203; + md.idata = 45124; + md.fdata = -12.27; + + ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32 + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("\nTest write and read a structure (%d %d %f) :\r\n",md.sdata,md.idata,md.fdata); + + ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r)); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("md.sdata %d\r\n",md_r.sdata); + printf("md.idata %d\r\n",md_r.idata); + printf("md.fdata %f\r\n",md_r.fdata); + + // Test read and write of an array of the first max_size bytes + for(i = 0;i < max_size;i++) + data[i] = max_size - i - 1; + + ep.write((uint32_t)(0),data,(uint32_t)max_size); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + ep.read((uint32_t)(0),data_r,(uint32_t)max_size); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("\nTest write and read an array of the first %d bytes :\r\n",max_size); + for(i = 0;i < max_size/16;i++) { + for(j = 0;j < 16;j++) { + addr = i * 16 + j; + printf("%3d ",(uint8_t)data_r[addr]); + } + printf("\r\n"); + } + printf("\r\n"); + #if 0 + // Test write and read an array of int32 + s = eeprom_size / 4; // size of eeprom in int32 + int ldata_size = sizeof(ldata) / 4; // size of data array in int32 + l = s / ldata_size; // loop index + + // size of read / write in bytes + t = eeprom_size; + if(t > ldata_size * 4) + t = ldata_size * 4; + + printf("Test write and read an array of %d int32 (write entire memory) :\r\n",t/4); + + // Write entire eeprom + if(l) { + for(k = 0;k < l;k++) { + for(i = 0;i < ldata_size;i++) + ldata[i] = ldata_size * k + i; + + addr = k * ldata_size * 4; + ep.write(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + } + + printf("Write OK\n"); + + // Read entire eeprom + id = 0; + for(k = 0;k < l;k++) { + addr = k * ldata_size * 4; + ep.read(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + // format outputs with 8 words rows + for(i = 0;i < ldata_size / 8;i++) { + id++; + printf("%4d ",id); + for(j = 0;j < 8;j++) { + addr = i * 8 + j; + printf("%5d ",ldata[addr]); + } + printf("\n"); + } + } + } + else { + for(i = 0;i < s;i++) + ldata[i] = i; + + addr = 0; + ep.write(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("Write OK\n"); + + // Read entire eeprom + id = 0; + + addr = 0; + ep.read(addr,(void *)ldata,t); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + // format outputs with 8 words rows + for(i = 0;i < s / 8;i++) { + id++; + printf("%4d ",id); + for(j = 0;j < 8;j++) { + addr = i * 8 + j; + printf("%5d ",ldata[addr]); + } + printf("\n"); + } + } +#endif + // clear eeprom + printf("\nClear eeprom\n"); + + ep.clear(); + if(ep.getError() != 0) + myerror(ep.getErrorMessage()); + + printf("End\n"); + +} + + int main() { - printf("\n\n*** RTOS basic example ***\n"); - scaleCalibration(); + wait(1); + printf("\n\n*** RTOS basic example ***\r\n"); + init_scale(); thread.start(print_thread); // flashIAP.init(); // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size()); // can1.reset(); // can2.reset(); -// can1.frequency(100000); + can1.frequency(100000); // can2.frequency(100000); //button1.mode(PullUp); // Activate pull-up - button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event + button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event +// eeprom_test(); int idx = 0; // Just for printf below @@ -156,8 +539,9 @@ if (button1_pressed) { // Set when button is pressed printf("scale value %f. \r\n", hx711.getGram()); button1_pressed = false; - printf("Button pressed %d\n", idx++); - can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); + printf("Button pressed %d\r\n", idx++); + printf("ID=%d. \r\n", init_id + idx%10); + can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1)); led1 = !led1; a++; }