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.
main.cpp@4:40bb33497de4, 2019-03-23 (annotated)
- Committer:
- hi1000
- Date:
- Sat Mar 23 02:18:38 2019 +0000
- Revision:
- 4:40bb33497de4
- Parent:
- 2:61a0169765bf
- Child:
- 5:4585215afd11
1602LCD display works;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hi1000 | 0:765cf978c3e5 | 1 | #include "mbed.h" |
hi1000 | 1:eb499e2a1b9b | 2 | #include <HX711.h> |
hi1000 | 2:61a0169765bf | 3 | #include <eeprom.h> |
hi1000 | 4:40bb33497de4 | 4 | #include "digitLCD.h" |
hi1000 | 0:765cf978c3e5 | 5 | |
hi1000 | 0:765cf978c3e5 | 6 | CAN can1(PD_0, PD_1); |
hi1000 | 0:765cf978c3e5 | 7 | CAN can2(PB_5, PB_6); |
hi1000 | 1:eb499e2a1b9b | 8 | DigitalOut led1(LED1); |
hi1000 | 1:eb499e2a1b9b | 9 | DigitalOut led2(LED2); |
hi1000 | 1:eb499e2a1b9b | 10 | //FlashIAP flashIAP; |
hi1000 | 1:eb499e2a1b9b | 11 | |
hi1000 | 4:40bb33497de4 | 12 | digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA |
hi1000 | 1:eb499e2a1b9b | 13 | |
hi1000 | 2:61a0169765bf | 14 | #define EEPROM_ADDR 0x0 // I2c EEPROM address is 0x00 |
hi1000 | 2:61a0169765bf | 15 | |
hi1000 | 2:61a0169765bf | 16 | #define SDA PB_9 // I2C SDA pin |
hi1000 | 2:61a0169765bf | 17 | #define SCL PB_8 // I2C SCL pin |
hi1000 | 2:61a0169765bf | 18 | |
hi1000 | 2:61a0169765bf | 19 | #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
hi1000 | 2:61a0169765bf | 20 | #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) |
hi1000 | 2:61a0169765bf | 21 | |
hi1000 | 2:61a0169765bf | 22 | EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256); |
hi1000 | 1:eb499e2a1b9b | 23 | struct ScaleCalibrationData { |
hi1000 | 1:eb499e2a1b9b | 24 | unsigned int calibrationWeight; // the weight (g) used for calibration for example 1000g or 10g. The maximum value is 3000. |
hi1000 | 1:eb499e2a1b9b | 25 | long offsetValue; // the value for scale offset |
hi1000 | 1:eb499e2a1b9b | 26 | float scaleValue; // the ADC increment for 1g |
hi1000 | 2:61a0169765bf | 27 | uint8_t checksum; |
hi1000 | 1:eb499e2a1b9b | 28 | }; |
hi1000 | 2:61a0169765bf | 29 | ScaleCalibrationData customVar; |
hi1000 | 2:61a0169765bf | 30 | |
hi1000 | 1:eb499e2a1b9b | 31 | unsigned int calibration_ADC_value; |
hi1000 | 1:eb499e2a1b9b | 32 | //#define CALIBRATION_VALUE 10 // 10g as the calibration weight |
hi1000 | 1:eb499e2a1b9b | 33 | #define WEIGHT_DIFFERENCE 200 // 10g ADC value minimum difference |
hi1000 | 1:eb499e2a1b9b | 34 | #define CALIBRATION_WEIGHT 2000 // calibration weight |
hi1000 | 2:61a0169765bf | 35 | #define MAXIMUM_CALIBRATION_WEIGHT 5000 |
hi1000 | 2:61a0169765bf | 36 | #define MINIMUM_CALIBRATION_WEIGHT 100 |
hi1000 | 1:eb499e2a1b9b | 37 | |
hi1000 | 2:61a0169765bf | 38 | /* scale */ |
hi1000 | 2:61a0169765bf | 39 | HX711 hx711(PB_11, PB_10); |
hi1000 | 1:eb499e2a1b9b | 40 | |
hi1000 | 1:eb499e2a1b9b | 41 | long zero_value; |
hi1000 | 1:eb499e2a1b9b | 42 | long calibration_value; |
hi1000 | 1:eb499e2a1b9b | 43 | unsigned int calibration_times; // must calibration 3 times |
hi1000 | 1:eb499e2a1b9b | 44 | unsigned int calibration_done = 0; |
hi1000 | 1:eb499e2a1b9b | 45 | float scale_value; |
hi1000 | 2:61a0169765bf | 46 | int init_id = 0x537; // first 8 bit is the address |
hi1000 | 1:eb499e2a1b9b | 47 | |
hi1000 | 1:eb499e2a1b9b | 48 | /* scale */ |
hi1000 | 2:61a0169765bf | 49 | |
hi1000 | 1:eb499e2a1b9b | 50 | void scaleCalibration() |
hi1000 | 1:eb499e2a1b9b | 51 | { |
hi1000 | 2:61a0169765bf | 52 | unsigned char eeprom_data[sizeof(customVar)]; |
hi1000 | 2:61a0169765bf | 53 | unsigned char checksum = 0; |
hi1000 | 1:eb499e2a1b9b | 54 | printf("Start Calibration.\r\n"); |
hi1000 | 1:eb499e2a1b9b | 55 | calibration_done = 0; |
hi1000 | 1:eb499e2a1b9b | 56 | if (!calibration_done) |
hi1000 | 1:eb499e2a1b9b | 57 | { |
hi1000 | 1:eb499e2a1b9b | 58 | led1 = 1; |
hi1000 | 1:eb499e2a1b9b | 59 | led2 = 0; |
hi1000 | 2:61a0169765bf | 60 | if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT)||(customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT)) |
hi1000 | 2:61a0169765bf | 61 | customVar.calibrationWeight = CALIBRATION_WEIGHT; |
hi1000 | 1:eb499e2a1b9b | 62 | zero_value = hx711.averageValue(10); // skip first 10 readings |
hi1000 | 1:eb499e2a1b9b | 63 | zero_value = hx711.averageValue(20); |
hi1000 | 1:eb499e2a1b9b | 64 | printf("zero_value=%d \r\n", zero_value); |
hi1000 | 1:eb499e2a1b9b | 65 | calibration_value = 0; |
hi1000 | 1:eb499e2a1b9b | 66 | scale_value = 0; |
hi1000 | 1:eb499e2a1b9b | 67 | calibration_times = 0; |
hi1000 | 2:61a0169765bf | 68 | led2 = 1; |
hi1000 | 1:eb499e2a1b9b | 69 | while (( calibration_times < 5)) |
hi1000 | 1:eb499e2a1b9b | 70 | { |
hi1000 | 1:eb499e2a1b9b | 71 | |
hi1000 | 1:eb499e2a1b9b | 72 | calibration_value = hx711.averageValue(20); |
hi1000 | 1:eb499e2a1b9b | 73 | if (calibration_value > (zero_value + WEIGHT_DIFFERENCE)) |
hi1000 | 1:eb499e2a1b9b | 74 | { |
hi1000 | 1:eb499e2a1b9b | 75 | calibration_times++; |
hi1000 | 1:eb499e2a1b9b | 76 | } |
hi1000 | 1:eb499e2a1b9b | 77 | else |
hi1000 | 1:eb499e2a1b9b | 78 | calibration_times = 0; |
hi1000 | 1:eb499e2a1b9b | 79 | } |
hi1000 | 1:eb499e2a1b9b | 80 | printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times); |
hi1000 | 1:eb499e2a1b9b | 81 | if (calibration_times >=5) |
hi1000 | 1:eb499e2a1b9b | 82 | { |
hi1000 | 1:eb499e2a1b9b | 83 | // calibration is OK |
hi1000 | 1:eb499e2a1b9b | 84 | calibration_times = 0; |
hi1000 | 1:eb499e2a1b9b | 85 | scale_value = (calibration_value - zero_value) / customVar.calibrationWeight; |
hi1000 | 1:eb499e2a1b9b | 86 | customVar.offsetValue = zero_value; |
hi1000 | 1:eb499e2a1b9b | 87 | customVar.scaleValue = scale_value; |
hi1000 | 1:eb499e2a1b9b | 88 | // EEPROM.put(0x00, customVar); |
hi1000 | 1:eb499e2a1b9b | 89 | hx711.setOffset(zero_value); |
hi1000 | 1:eb499e2a1b9b | 90 | hx711.setScale(scale_value); // this value is obtained by calibrating the scale with known weights; see the README for details |
hi1000 | 2:61a0169765bf | 91 | memcpy(eeprom_data, &customVar, sizeof(customVar)); |
hi1000 | 2:61a0169765bf | 92 | for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here |
hi1000 | 2:61a0169765bf | 93 | { |
hi1000 | 2:61a0169765bf | 94 | checksum += eeprom_data[cnt]; |
hi1000 | 2:61a0169765bf | 95 | } |
hi1000 | 2:61a0169765bf | 96 | customVar.checksum = checksum; |
hi1000 | 2:61a0169765bf | 97 | printf("EEPROM write calibration data: \r\n"); |
hi1000 | 2:61a0169765bf | 98 | printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); |
hi1000 | 2:61a0169765bf | 99 | printf("offsetValue=%d \r\n", customVar.offsetValue); |
hi1000 | 2:61a0169765bf | 100 | printf("scaleValue=%f \r\n", customVar.scaleValue); |
hi1000 | 2:61a0169765bf | 101 | printf("checksum=0x%02x \r\n", customVar.checksum); |
hi1000 | 2:61a0169765bf | 102 | ep.write((uint32_t)0x00,(void *)&customVar,sizeof(customVar)); // write a structure eeprom_size - 32 |
hi1000 | 1:eb499e2a1b9b | 103 | calibration_done = 1; |
hi1000 | 2:61a0169765bf | 104 | led1 = 1; |
hi1000 | 2:61a0169765bf | 105 | printf("Calibration Done\r\n"); |
hi1000 | 1:eb499e2a1b9b | 106 | } |
hi1000 | 1:eb499e2a1b9b | 107 | } |
hi1000 | 1:eb499e2a1b9b | 108 | } |
hi1000 | 0:765cf978c3e5 | 109 | |
hi1000 | 2:61a0169765bf | 110 | void init_scale() |
hi1000 | 2:61a0169765bf | 111 | { |
hi1000 | 2:61a0169765bf | 112 | unsigned char eeprom_data; |
hi1000 | 2:61a0169765bf | 113 | unsigned char checksum = 0; |
hi1000 | 2:61a0169765bf | 114 | customVar.calibrationWeight = CALIBRATION_WEIGHT; |
hi1000 | 2:61a0169765bf | 115 | |
hi1000 | 2:61a0169765bf | 116 | #if 1 |
hi1000 | 2:61a0169765bf | 117 | printf("sizeof(customVar)=%d \r\n", sizeof(customVar)); |
hi1000 | 2:61a0169765bf | 118 | ep.read((uint32_t)0,(void *)&customVar, sizeof(customVar)); |
hi1000 | 2:61a0169765bf | 119 | printf("EEPROM read calibration data: \r\n"); |
hi1000 | 2:61a0169765bf | 120 | printf("calibrationWeight=%d \r\n", customVar.calibrationWeight); |
hi1000 | 2:61a0169765bf | 121 | printf("offsetValue=%d \r\n", customVar.offsetValue); |
hi1000 | 2:61a0169765bf | 122 | printf("scaleValue=%f \r\n", customVar.scaleValue); |
hi1000 | 2:61a0169765bf | 123 | printf("checksum=0x%02x \r\n", customVar.checksum); |
hi1000 | 2:61a0169765bf | 124 | printf("\r\n calculate checksum: \r\n"); |
hi1000 | 2:61a0169765bf | 125 | for (int cnt = 0; cnt < (sizeof(customVar)-4); cnt++) // compiler bug need to -4 here |
hi1000 | 2:61a0169765bf | 126 | { |
hi1000 | 2:61a0169765bf | 127 | ep.read(cnt, (int8_t&)eeprom_data); |
hi1000 | 2:61a0169765bf | 128 | printf("0x%02x ", eeprom_data); |
hi1000 | 2:61a0169765bf | 129 | checksum += eeprom_data; |
hi1000 | 2:61a0169765bf | 130 | printf("checksum=0x%02x\r\n", checksum); |
hi1000 | 2:61a0169765bf | 131 | } |
hi1000 | 2:61a0169765bf | 132 | printf("\r\ncalculated checksum=0x%02x \r\n", checksum); |
hi1000 | 2:61a0169765bf | 133 | |
hi1000 | 2:61a0169765bf | 134 | if (checksum == customVar.checksum) |
hi1000 | 2:61a0169765bf | 135 | { |
hi1000 | 2:61a0169765bf | 136 | if ((customVar.calibrationWeight > MAXIMUM_CALIBRATION_WEIGHT) || (customVar.calibrationWeight < MINIMUM_CALIBRATION_WEIGHT)) |
hi1000 | 2:61a0169765bf | 137 | { |
hi1000 | 2:61a0169765bf | 138 | customVar.calibrationWeight = CALIBRATION_WEIGHT; |
hi1000 | 2:61a0169765bf | 139 | scaleCalibration(); |
hi1000 | 2:61a0169765bf | 140 | } |
hi1000 | 2:61a0169765bf | 141 | if ((customVar.offsetValue < 10000)) |
hi1000 | 2:61a0169765bf | 142 | { |
hi1000 | 2:61a0169765bf | 143 | customVar.offsetValue = 10000; |
hi1000 | 2:61a0169765bf | 144 | scaleCalibration(); |
hi1000 | 2:61a0169765bf | 145 | } |
hi1000 | 2:61a0169765bf | 146 | if ((customVar.scaleValue < 100)) |
hi1000 | 2:61a0169765bf | 147 | { |
hi1000 | 2:61a0169765bf | 148 | customVar.scaleValue = 100; |
hi1000 | 2:61a0169765bf | 149 | scaleCalibration(); |
hi1000 | 2:61a0169765bf | 150 | } |
hi1000 | 2:61a0169765bf | 151 | // delay(200); |
hi1000 | 2:61a0169765bf | 152 | hx711.setOffset(customVar.offsetValue); |
hi1000 | 2:61a0169765bf | 153 | hx711.setScale(customVar.scaleValue); |
hi1000 | 2:61a0169765bf | 154 | } |
hi1000 | 2:61a0169765bf | 155 | else |
hi1000 | 2:61a0169765bf | 156 | { |
hi1000 | 2:61a0169765bf | 157 | scaleCalibration(); |
hi1000 | 2:61a0169765bf | 158 | } |
hi1000 | 2:61a0169765bf | 159 | #endif |
hi1000 | 2:61a0169765bf | 160 | } |
hi1000 | 2:61a0169765bf | 161 | |
hi1000 | 2:61a0169765bf | 162 | /*scale end*/ |
hi1000 | 0:765cf978c3e5 | 163 | int a = 0; |
hi1000 | 0:765cf978c3e5 | 164 | int b = 0; |
hi1000 | 0:765cf978c3e5 | 165 | |
hi1000 | 0:765cf978c3e5 | 166 | void print_char(char c = '*') |
hi1000 | 0:765cf978c3e5 | 167 | { |
hi1000 | 0:765cf978c3e5 | 168 | printf("%c\r\n", c); |
hi1000 | 0:765cf978c3e5 | 169 | fflush(stdout); |
hi1000 | 0:765cf978c3e5 | 170 | } |
hi1000 | 0:765cf978c3e5 | 171 | |
hi1000 | 0:765cf978c3e5 | 172 | Thread thread; |
hi1000 | 0:765cf978c3e5 | 173 | |
hi1000 | 1:eb499e2a1b9b | 174 | |
hi1000 | 0:765cf978c3e5 | 175 | CANMessage msg; |
hi1000 | 0:765cf978c3e5 | 176 | |
hi1000 | 0:765cf978c3e5 | 177 | |
hi1000 | 0:765cf978c3e5 | 178 | InterruptIn button1(USER_BUTTON); |
hi1000 | 0:765cf978c3e5 | 179 | volatile bool button1_pressed = false; // Used in the main loop |
hi1000 | 0:765cf978c3e5 | 180 | volatile bool button1_enabled = true; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 181 | Timeout button1_timeout; // Used for debouncing |
hi1000 | 0:765cf978c3e5 | 182 | |
hi1000 | 0:765cf978c3e5 | 183 | // Enables button when bouncing is over |
hi1000 | 0:765cf978c3e5 | 184 | void button1_enabled_cb(void) |
hi1000 | 0:765cf978c3e5 | 185 | { |
hi1000 | 0:765cf978c3e5 | 186 | button1_enabled = true; |
hi1000 | 0:765cf978c3e5 | 187 | } |
hi1000 | 0:765cf978c3e5 | 188 | |
hi1000 | 0:765cf978c3e5 | 189 | // ISR handling button pressed event |
hi1000 | 0:765cf978c3e5 | 190 | void button1_onpressed_cb(void) |
hi1000 | 0:765cf978c3e5 | 191 | { |
hi1000 | 0:765cf978c3e5 | 192 | if (button1_enabled) { // Disabled while the button is bouncing |
hi1000 | 0:765cf978c3e5 | 193 | button1_enabled = false; |
hi1000 | 0:765cf978c3e5 | 194 | button1_pressed = true; // To be read by the main loop |
hi1000 | 0:765cf978c3e5 | 195 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
hi1000 | 0:765cf978c3e5 | 196 | } |
hi1000 | 0:765cf978c3e5 | 197 | } |
hi1000 | 0:765cf978c3e5 | 198 | |
hi1000 | 0:765cf978c3e5 | 199 | void print_thread() |
hi1000 | 0:765cf978c3e5 | 200 | { |
hi1000 | 0:765cf978c3e5 | 201 | while (true) { |
hi1000 | 0:765cf978c3e5 | 202 | #if 1 |
hi1000 | 0:765cf978c3e5 | 203 | if(can1.read(msg)) { |
hi1000 | 0:765cf978c3e5 | 204 | print_char(); |
hi1000 | 0:765cf978c3e5 | 205 | printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); |
hi1000 | 0:765cf978c3e5 | 206 | // b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 0:765cf978c3e5 | 207 | b = msg.data[0]; |
hi1000 | 0:765cf978c3e5 | 208 | printf("got data %d 0x%08x \r\n", b, b); |
hi1000 | 0:765cf978c3e5 | 209 | if(msg.id == 1337) { |
hi1000 | 0:765cf978c3e5 | 210 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 211 | |
hi1000 | 0:765cf978c3e5 | 212 | b = *reinterpret_cast<int*>(msg.data); |
hi1000 | 0:765cf978c3e5 | 213 | printf("got message %d\r\n", b); |
hi1000 | 0:765cf978c3e5 | 214 | if(b % 5 == 0) |
hi1000 | 0:765cf978c3e5 | 215 | led2 = !led2; |
hi1000 | 0:765cf978c3e5 | 216 | } |
hi1000 | 0:765cf978c3e5 | 217 | } |
hi1000 | 0:765cf978c3e5 | 218 | // wait(0.2); |
hi1000 | 0:765cf978c3e5 | 219 | #endif |
hi1000 | 0:765cf978c3e5 | 220 | } |
hi1000 | 0:765cf978c3e5 | 221 | } |
hi1000 | 0:765cf978c3e5 | 222 | |
hi1000 | 2:61a0169765bf | 223 | typedef struct _MyData { |
hi1000 | 2:61a0169765bf | 224 | int16_t sdata; |
hi1000 | 2:61a0169765bf | 225 | int32_t idata; |
hi1000 | 2:61a0169765bf | 226 | float fdata; |
hi1000 | 2:61a0169765bf | 227 | } MyData; |
hi1000 | 2:61a0169765bf | 228 | |
hi1000 | 2:61a0169765bf | 229 | static void myerror(std::string msg) |
hi1000 | 2:61a0169765bf | 230 | { |
hi1000 | 2:61a0169765bf | 231 | printf("Error %s\n",msg.c_str()); |
hi1000 | 2:61a0169765bf | 232 | exit(1); |
hi1000 | 2:61a0169765bf | 233 | } |
hi1000 | 2:61a0169765bf | 234 | |
hi1000 | 2:61a0169765bf | 235 | void eeprom_test(void) |
hi1000 | 2:61a0169765bf | 236 | { |
hi1000 | 2:61a0169765bf | 237 | // EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C64); // 24C64 eeprom with sda = p9 and scl = p10 |
hi1000 | 2:61a0169765bf | 238 | uint8_t data[256],data_r[256]; |
hi1000 | 2:61a0169765bf | 239 | int8_t ival; |
hi1000 | 2:61a0169765bf | 240 | uint16_t s; |
hi1000 | 2:61a0169765bf | 241 | int16_t sdata,sdata_r; |
hi1000 | 2:61a0169765bf | 242 | int32_t ldata[1024]; |
hi1000 | 2:61a0169765bf | 243 | int32_t eeprom_size,max_size; |
hi1000 | 2:61a0169765bf | 244 | uint32_t addr; |
hi1000 | 2:61a0169765bf | 245 | int32_t idata,idata_r; |
hi1000 | 2:61a0169765bf | 246 | uint32_t i,j,k,l,t,id; |
hi1000 | 2:61a0169765bf | 247 | float fdata,fdata_r; |
hi1000 | 2:61a0169765bf | 248 | MyData md,md_r; |
hi1000 | 2:61a0169765bf | 249 | |
hi1000 | 2:61a0169765bf | 250 | eeprom_size = ep.getSize(); |
hi1000 | 2:61a0169765bf | 251 | max_size = MIN(eeprom_size,256); |
hi1000 | 2:61a0169765bf | 252 | |
hi1000 | 2:61a0169765bf | 253 | printf("Test EEPROM I2C model %s of %d bytes\r\n",ep.getName(),eeprom_size); |
hi1000 | 2:61a0169765bf | 254 | |
hi1000 | 2:61a0169765bf | 255 | // Test sequential read byte (max_size first bytes) |
hi1000 | 2:61a0169765bf | 256 | for(i = 0;i < max_size;i++) { |
hi1000 | 2:61a0169765bf | 257 | ep.read(i,ival); |
hi1000 | 2:61a0169765bf | 258 | data_r[i] = ival; |
hi1000 | 2:61a0169765bf | 259 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 260 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 261 | } |
hi1000 | 2:61a0169765bf | 262 | |
hi1000 | 2:61a0169765bf | 263 | printf("Test sequential read %d first bytes :\r\n",max_size); |
hi1000 | 2:61a0169765bf | 264 | for(i = 0;i < max_size/16;i++) { |
hi1000 | 2:61a0169765bf | 265 | for(j = 0;j < 16;j++) { |
hi1000 | 2:61a0169765bf | 266 | addr = i * 16 + j; |
hi1000 | 2:61a0169765bf | 267 | printf("%3d ",(uint8_t)data_r[addr]); |
hi1000 | 2:61a0169765bf | 268 | } |
hi1000 | 2:61a0169765bf | 269 | printf("\r\n"); |
hi1000 | 2:61a0169765bf | 270 | } |
hi1000 | 2:61a0169765bf | 271 | |
hi1000 | 2:61a0169765bf | 272 | // Test sequential read byte (max_size last bytes) |
hi1000 | 2:61a0169765bf | 273 | for(i = 0;i < max_size;i++) { |
hi1000 | 2:61a0169765bf | 274 | addr = eeprom_size - max_size + i; |
hi1000 | 2:61a0169765bf | 275 | ep.read(addr,ival); |
hi1000 | 2:61a0169765bf | 276 | data_r[i] = ival; |
hi1000 | 2:61a0169765bf | 277 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 278 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 279 | } |
hi1000 | 2:61a0169765bf | 280 | |
hi1000 | 2:61a0169765bf | 281 | printf("\nTest sequential read %d last bytes :\r\n",max_size); |
hi1000 | 2:61a0169765bf | 282 | for(i = 0;i < max_size/16;i++) { |
hi1000 | 2:61a0169765bf | 283 | for(j = 0;j < 16;j++) { |
hi1000 | 2:61a0169765bf | 284 | addr = i * 16 + j; |
hi1000 | 2:61a0169765bf | 285 | printf("%3d ",(uint8_t)data_r[addr]); |
hi1000 | 2:61a0169765bf | 286 | } |
hi1000 | 2:61a0169765bf | 287 | printf("\r\n"); |
hi1000 | 2:61a0169765bf | 288 | } |
hi1000 | 2:61a0169765bf | 289 | |
hi1000 | 2:61a0169765bf | 290 | // Test write byte (max_size first bytes) |
hi1000 | 2:61a0169765bf | 291 | for(i = 0;i < max_size;i++) |
hi1000 | 2:61a0169765bf | 292 | data[i] = i; |
hi1000 | 2:61a0169765bf | 293 | |
hi1000 | 2:61a0169765bf | 294 | for(i = 0;i < max_size;i++) { |
hi1000 | 2:61a0169765bf | 295 | ep.write(i,(int8_t)data[i]); |
hi1000 | 2:61a0169765bf | 296 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 297 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 298 | } |
hi1000 | 2:61a0169765bf | 299 | |
hi1000 | 2:61a0169765bf | 300 | // Test read byte (max_size first bytes) |
hi1000 | 2:61a0169765bf | 301 | for(i = 0;i < max_size;i++) { |
hi1000 | 2:61a0169765bf | 302 | ep.read(i,(int8_t&)ival); |
hi1000 | 2:61a0169765bf | 303 | data_r[i] = (uint8_t)ival; |
hi1000 | 2:61a0169765bf | 304 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 305 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 306 | } |
hi1000 | 2:61a0169765bf | 307 | |
hi1000 | 2:61a0169765bf | 308 | printf("\nTest write and read %d first bytes :\r\n",max_size); |
hi1000 | 2:61a0169765bf | 309 | for(i = 0;i < max_size/16;i++) { |
hi1000 | 2:61a0169765bf | 310 | for(j = 0;j < 16;j++) { |
hi1000 | 2:61a0169765bf | 311 | addr = i * 16 + j; |
hi1000 | 2:61a0169765bf | 312 | printf("%3d ",(uint8_t)data_r[addr]); |
hi1000 | 2:61a0169765bf | 313 | } |
hi1000 | 2:61a0169765bf | 314 | printf("\r\n"); |
hi1000 | 2:61a0169765bf | 315 | } |
hi1000 | 2:61a0169765bf | 316 | |
hi1000 | 2:61a0169765bf | 317 | // Test current address read byte (max_size first bytes) |
hi1000 | 2:61a0169765bf | 318 | ep.read((uint32_t)0,(int8_t&)ival); // current address is 0 |
hi1000 | 2:61a0169765bf | 319 | data_r[0] = (uint8_t)ival; |
hi1000 | 2:61a0169765bf | 320 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 321 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 322 | |
hi1000 | 2:61a0169765bf | 323 | for(i = 1;i < max_size;i++) { |
hi1000 | 2:61a0169765bf | 324 | ep.read((int8_t&)ival); |
hi1000 | 2:61a0169765bf | 325 | data_r[i] = (uint8_t)ival; |
hi1000 | 2:61a0169765bf | 326 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 327 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 328 | } |
hi1000 | 2:61a0169765bf | 329 | |
hi1000 | 2:61a0169765bf | 330 | printf("\nTest current address read %d first bytes :\r\n",max_size); |
hi1000 | 2:61a0169765bf | 331 | for(i = 0;i < max_size/16;i++) { |
hi1000 | 2:61a0169765bf | 332 | for(j = 0;j < 16;j++) { |
hi1000 | 2:61a0169765bf | 333 | addr = i * 16 + j; |
hi1000 | 2:61a0169765bf | 334 | printf("%3d ",(uint8_t)data_r[addr]); |
hi1000 | 2:61a0169765bf | 335 | } |
hi1000 | 2:61a0169765bf | 336 | printf("\n"); |
hi1000 | 2:61a0169765bf | 337 | } |
hi1000 | 2:61a0169765bf | 338 | |
hi1000 | 2:61a0169765bf | 339 | // Test sequential read byte (first max_size bytes) |
hi1000 | 2:61a0169765bf | 340 | ep.read((uint32_t)0,(int8_t *)data_r,(uint32_t) max_size); |
hi1000 | 2:61a0169765bf | 341 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 342 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 343 | |
hi1000 | 2:61a0169765bf | 344 | printf("\nTest sequential read %d first bytes :\r\n",max_size); |
hi1000 | 2:61a0169765bf | 345 | for(i = 0;i < max_size/16;i++) { |
hi1000 | 2:61a0169765bf | 346 | for(j = 0;j < 16;j++) { |
hi1000 | 2:61a0169765bf | 347 | addr = i * 16 + j; |
hi1000 | 2:61a0169765bf | 348 | printf("%3d ",(uint8_t)data_r[addr]); |
hi1000 | 2:61a0169765bf | 349 | } |
hi1000 | 2:61a0169765bf | 350 | printf("\r\n"); |
hi1000 | 2:61a0169765bf | 351 | } |
hi1000 | 2:61a0169765bf | 352 | |
hi1000 | 2:61a0169765bf | 353 | // Test write short, long, float |
hi1000 | 2:61a0169765bf | 354 | sdata = -15202; |
hi1000 | 2:61a0169765bf | 355 | addr = eeprom_size - 16; |
hi1000 | 2:61a0169765bf | 356 | ep.write(addr,(int16_t)sdata); // short write at address eeprom_size - 16 |
hi1000 | 2:61a0169765bf | 357 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 358 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 359 | |
hi1000 | 2:61a0169765bf | 360 | idata = 45123; |
hi1000 | 2:61a0169765bf | 361 | addr = eeprom_size - 12; |
hi1000 | 2:61a0169765bf | 362 | ep.write(addr,(int32_t)idata); // long write at address eeprom_size - 12 |
hi1000 | 2:61a0169765bf | 363 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 364 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 365 | |
hi1000 | 2:61a0169765bf | 366 | fdata = -12.26; |
hi1000 | 2:61a0169765bf | 367 | addr = eeprom_size - 8; |
hi1000 | 2:61a0169765bf | 368 | ep.write(addr,(float)fdata); // float write at address eeprom_size - 8 |
hi1000 | 2:61a0169765bf | 369 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 370 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 371 | |
hi1000 | 2:61a0169765bf | 372 | // Test read short, long, float |
hi1000 | 2:61a0169765bf | 373 | printf("\nTest write and read short (%d), long (%d), float (%f) :\r\n", |
hi1000 | 2:61a0169765bf | 374 | sdata,idata,fdata); |
hi1000 | 2:61a0169765bf | 375 | |
hi1000 | 2:61a0169765bf | 376 | ep.read((uint32_t)(eeprom_size - 16),(int16_t&)sdata_r); |
hi1000 | 2:61a0169765bf | 377 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 378 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 379 | printf("sdata %d\r\n",sdata_r); |
hi1000 | 2:61a0169765bf | 380 | |
hi1000 | 2:61a0169765bf | 381 | ep.read((uint32_t)(eeprom_size - 12),(int32_t&)idata_r); |
hi1000 | 2:61a0169765bf | 382 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 383 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 384 | printf("idata %d\r\n",idata_r); |
hi1000 | 2:61a0169765bf | 385 | |
hi1000 | 2:61a0169765bf | 386 | ep.read((uint32_t)(eeprom_size - 8),fdata_r); |
hi1000 | 2:61a0169765bf | 387 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 388 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 389 | printf("fdata %f\r\n",fdata_r); |
hi1000 | 2:61a0169765bf | 390 | |
hi1000 | 2:61a0169765bf | 391 | // Test read and write a structure |
hi1000 | 2:61a0169765bf | 392 | md.sdata = -15203; |
hi1000 | 2:61a0169765bf | 393 | md.idata = 45124; |
hi1000 | 2:61a0169765bf | 394 | md.fdata = -12.27; |
hi1000 | 2:61a0169765bf | 395 | |
hi1000 | 2:61a0169765bf | 396 | ep.write((uint32_t)(eeprom_size - 32),(void *)&md,sizeof(md)); // write a structure eeprom_size - 32 |
hi1000 | 2:61a0169765bf | 397 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 398 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 399 | |
hi1000 | 2:61a0169765bf | 400 | printf("\nTest write and read a structure (%d %d %f) :\r\n",md.sdata,md.idata,md.fdata); |
hi1000 | 2:61a0169765bf | 401 | |
hi1000 | 2:61a0169765bf | 402 | ep.read((uint32_t)(eeprom_size - 32),(void *)&md_r,sizeof(md_r)); |
hi1000 | 2:61a0169765bf | 403 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 404 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 405 | |
hi1000 | 2:61a0169765bf | 406 | printf("md.sdata %d\r\n",md_r.sdata); |
hi1000 | 2:61a0169765bf | 407 | printf("md.idata %d\r\n",md_r.idata); |
hi1000 | 2:61a0169765bf | 408 | printf("md.fdata %f\r\n",md_r.fdata); |
hi1000 | 2:61a0169765bf | 409 | |
hi1000 | 2:61a0169765bf | 410 | // Test read and write of an array of the first max_size bytes |
hi1000 | 2:61a0169765bf | 411 | for(i = 0;i < max_size;i++) |
hi1000 | 2:61a0169765bf | 412 | data[i] = max_size - i - 1; |
hi1000 | 2:61a0169765bf | 413 | |
hi1000 | 2:61a0169765bf | 414 | ep.write((uint32_t)(0),data,(uint32_t)max_size); |
hi1000 | 2:61a0169765bf | 415 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 416 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 417 | |
hi1000 | 2:61a0169765bf | 418 | ep.read((uint32_t)(0),data_r,(uint32_t)max_size); |
hi1000 | 2:61a0169765bf | 419 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 420 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 421 | |
hi1000 | 2:61a0169765bf | 422 | printf("\nTest write and read an array of the first %d bytes :\r\n",max_size); |
hi1000 | 2:61a0169765bf | 423 | for(i = 0;i < max_size/16;i++) { |
hi1000 | 2:61a0169765bf | 424 | for(j = 0;j < 16;j++) { |
hi1000 | 2:61a0169765bf | 425 | addr = i * 16 + j; |
hi1000 | 2:61a0169765bf | 426 | printf("%3d ",(uint8_t)data_r[addr]); |
hi1000 | 2:61a0169765bf | 427 | } |
hi1000 | 2:61a0169765bf | 428 | printf("\r\n"); |
hi1000 | 2:61a0169765bf | 429 | } |
hi1000 | 2:61a0169765bf | 430 | printf("\r\n"); |
hi1000 | 2:61a0169765bf | 431 | #if 0 |
hi1000 | 2:61a0169765bf | 432 | // Test write and read an array of int32 |
hi1000 | 2:61a0169765bf | 433 | s = eeprom_size / 4; // size of eeprom in int32 |
hi1000 | 2:61a0169765bf | 434 | int ldata_size = sizeof(ldata) / 4; // size of data array in int32 |
hi1000 | 2:61a0169765bf | 435 | l = s / ldata_size; // loop index |
hi1000 | 2:61a0169765bf | 436 | |
hi1000 | 2:61a0169765bf | 437 | // size of read / write in bytes |
hi1000 | 2:61a0169765bf | 438 | t = eeprom_size; |
hi1000 | 2:61a0169765bf | 439 | if(t > ldata_size * 4) |
hi1000 | 2:61a0169765bf | 440 | t = ldata_size * 4; |
hi1000 | 2:61a0169765bf | 441 | |
hi1000 | 2:61a0169765bf | 442 | printf("Test write and read an array of %d int32 (write entire memory) :\r\n",t/4); |
hi1000 | 2:61a0169765bf | 443 | |
hi1000 | 2:61a0169765bf | 444 | // Write entire eeprom |
hi1000 | 2:61a0169765bf | 445 | if(l) { |
hi1000 | 2:61a0169765bf | 446 | for(k = 0;k < l;k++) { |
hi1000 | 2:61a0169765bf | 447 | for(i = 0;i < ldata_size;i++) |
hi1000 | 2:61a0169765bf | 448 | ldata[i] = ldata_size * k + i; |
hi1000 | 2:61a0169765bf | 449 | |
hi1000 | 2:61a0169765bf | 450 | addr = k * ldata_size * 4; |
hi1000 | 2:61a0169765bf | 451 | ep.write(addr,(void *)ldata,t); |
hi1000 | 2:61a0169765bf | 452 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 453 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 454 | } |
hi1000 | 2:61a0169765bf | 455 | |
hi1000 | 2:61a0169765bf | 456 | printf("Write OK\n"); |
hi1000 | 2:61a0169765bf | 457 | |
hi1000 | 2:61a0169765bf | 458 | // Read entire eeprom |
hi1000 | 2:61a0169765bf | 459 | id = 0; |
hi1000 | 2:61a0169765bf | 460 | for(k = 0;k < l;k++) { |
hi1000 | 2:61a0169765bf | 461 | addr = k * ldata_size * 4; |
hi1000 | 2:61a0169765bf | 462 | ep.read(addr,(void *)ldata,t); |
hi1000 | 2:61a0169765bf | 463 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 464 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 465 | |
hi1000 | 2:61a0169765bf | 466 | // format outputs with 8 words rows |
hi1000 | 2:61a0169765bf | 467 | for(i = 0;i < ldata_size / 8;i++) { |
hi1000 | 2:61a0169765bf | 468 | id++; |
hi1000 | 2:61a0169765bf | 469 | printf("%4d ",id); |
hi1000 | 2:61a0169765bf | 470 | for(j = 0;j < 8;j++) { |
hi1000 | 2:61a0169765bf | 471 | addr = i * 8 + j; |
hi1000 | 2:61a0169765bf | 472 | printf("%5d ",ldata[addr]); |
hi1000 | 2:61a0169765bf | 473 | } |
hi1000 | 2:61a0169765bf | 474 | printf("\n"); |
hi1000 | 2:61a0169765bf | 475 | } |
hi1000 | 2:61a0169765bf | 476 | } |
hi1000 | 2:61a0169765bf | 477 | } |
hi1000 | 2:61a0169765bf | 478 | else { |
hi1000 | 2:61a0169765bf | 479 | for(i = 0;i < s;i++) |
hi1000 | 2:61a0169765bf | 480 | ldata[i] = i; |
hi1000 | 2:61a0169765bf | 481 | |
hi1000 | 2:61a0169765bf | 482 | addr = 0; |
hi1000 | 2:61a0169765bf | 483 | ep.write(addr,(void *)ldata,t); |
hi1000 | 2:61a0169765bf | 484 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 485 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 486 | |
hi1000 | 2:61a0169765bf | 487 | printf("Write OK\n"); |
hi1000 | 2:61a0169765bf | 488 | |
hi1000 | 2:61a0169765bf | 489 | // Read entire eeprom |
hi1000 | 2:61a0169765bf | 490 | id = 0; |
hi1000 | 2:61a0169765bf | 491 | |
hi1000 | 2:61a0169765bf | 492 | addr = 0; |
hi1000 | 2:61a0169765bf | 493 | ep.read(addr,(void *)ldata,t); |
hi1000 | 2:61a0169765bf | 494 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 495 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 496 | |
hi1000 | 2:61a0169765bf | 497 | // format outputs with 8 words rows |
hi1000 | 2:61a0169765bf | 498 | for(i = 0;i < s / 8;i++) { |
hi1000 | 2:61a0169765bf | 499 | id++; |
hi1000 | 2:61a0169765bf | 500 | printf("%4d ",id); |
hi1000 | 2:61a0169765bf | 501 | for(j = 0;j < 8;j++) { |
hi1000 | 2:61a0169765bf | 502 | addr = i * 8 + j; |
hi1000 | 2:61a0169765bf | 503 | printf("%5d ",ldata[addr]); |
hi1000 | 2:61a0169765bf | 504 | } |
hi1000 | 2:61a0169765bf | 505 | printf("\n"); |
hi1000 | 2:61a0169765bf | 506 | } |
hi1000 | 2:61a0169765bf | 507 | } |
hi1000 | 2:61a0169765bf | 508 | #endif |
hi1000 | 2:61a0169765bf | 509 | // clear eeprom |
hi1000 | 2:61a0169765bf | 510 | printf("\nClear eeprom\n"); |
hi1000 | 2:61a0169765bf | 511 | |
hi1000 | 2:61a0169765bf | 512 | ep.clear(); |
hi1000 | 2:61a0169765bf | 513 | if(ep.getError() != 0) |
hi1000 | 2:61a0169765bf | 514 | myerror(ep.getErrorMessage()); |
hi1000 | 2:61a0169765bf | 515 | |
hi1000 | 2:61a0169765bf | 516 | printf("End\n"); |
hi1000 | 2:61a0169765bf | 517 | |
hi1000 | 2:61a0169765bf | 518 | } |
hi1000 | 2:61a0169765bf | 519 | |
hi1000 | 2:61a0169765bf | 520 | |
hi1000 | 0:765cf978c3e5 | 521 | int main() |
hi1000 | 0:765cf978c3e5 | 522 | { |
hi1000 | 2:61a0169765bf | 523 | wait(1); |
hi1000 | 4:40bb33497de4 | 524 | lcd.clear(); // clears display |
hi1000 | 4:40bb33497de4 | 525 | lcd.allsegson(); |
hi1000 | 4:40bb33497de4 | 526 | // lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display |
hi1000 | 2:61a0169765bf | 527 | printf("\n\n*** RTOS basic example ***\r\n"); |
hi1000 | 2:61a0169765bf | 528 | init_scale(); |
hi1000 | 1:eb499e2a1b9b | 529 | thread.start(print_thread); |
hi1000 | 0:765cf978c3e5 | 530 | |
hi1000 | 1:eb499e2a1b9b | 531 | // flashIAP.init(); |
hi1000 | 1:eb499e2a1b9b | 532 | // printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size()); |
hi1000 | 0:765cf978c3e5 | 533 | // can1.reset(); |
hi1000 | 0:765cf978c3e5 | 534 | // can2.reset(); |
hi1000 | 2:61a0169765bf | 535 | can1.frequency(100000); |
hi1000 | 0:765cf978c3e5 | 536 | // can2.frequency(100000); |
hi1000 | 0:765cf978c3e5 | 537 | //button1.mode(PullUp); // Activate pull-up |
hi1000 | 2:61a0169765bf | 538 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
hi1000 | 2:61a0169765bf | 539 | // eeprom_test(); |
hi1000 | 0:765cf978c3e5 | 540 | |
hi1000 | 0:765cf978c3e5 | 541 | int idx = 0; // Just for printf below |
hi1000 | 0:765cf978c3e5 | 542 | |
hi1000 | 0:765cf978c3e5 | 543 | while(1) { |
hi1000 | 0:765cf978c3e5 | 544 | if (button1_pressed) { // Set when button is pressed |
hi1000 | 1:eb499e2a1b9b | 545 | printf("scale value %f. \r\n", hx711.getGram()); |
hi1000 | 0:765cf978c3e5 | 546 | button1_pressed = false; |
hi1000 | 2:61a0169765bf | 547 | printf("Button pressed %d\r\n", idx++); |
hi1000 | 2:61a0169765bf | 548 | printf("ID=%d. \r\n", init_id + idx%10); |
hi1000 | 2:61a0169765bf | 549 | can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1)); |
hi1000 | 0:765cf978c3e5 | 550 | led1 = !led1; |
hi1000 | 0:765cf978c3e5 | 551 | a++; |
hi1000 | 0:765cf978c3e5 | 552 | } |
hi1000 | 0:765cf978c3e5 | 553 | } |
hi1000 | 0:765cf978c3e5 | 554 | #if 0 |
hi1000 | 0:765cf978c3e5 | 555 | while(1) { |
hi1000 | 0:765cf978c3e5 | 556 | // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a))); |
hi1000 | 0:765cf978c3e5 | 557 | #if |
hi1000 | 0:765cf978c3e5 | 558 | can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); |
hi1000 | 0:765cf978c3e5 | 559 | #endif |
hi1000 | 0:765cf978c3e5 | 560 | printf("loop a=%d\n", a); |
hi1000 | 0:765cf978c3e5 | 561 | led1 = !led1; |
hi1000 | 0:765cf978c3e5 | 562 | a++; |
hi1000 | 0:765cf978c3e5 | 563 | wait(0.2); |
hi1000 | 0:765cf978c3e5 | 564 | } |
hi1000 | 0:765cf978c3e5 | 565 | #endif |
hi1000 | 0:765cf978c3e5 | 566 | } |