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
- Committer:
- hi1000
- Date:
- 2019-02-16
- Revision:
- 1:eb499e2a1b9b
- Parent:
- 0:765cf978c3e5
- Child:
- 2:61a0169765bf
File content as of revision 1:eb499e2a1b9b:
#include "mbed.h" #include <HX711.h> CAN can1(PD_0, PD_1); CAN can2(PB_5, PB_6); DigitalOut led1(LED1); DigitalOut led2(LED2); //FlashIAP flashIAP; /* scale */ HX711 hx711(PB_11, PB_10); 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; }; 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 ScaleCalibrationData customVar; long zero_value; long calibration_value; unsigned int calibration_times; // must calibration 3 times unsigned int calibration_done = 0; float scale_value; /* scale */ void scaleCalibration() { printf("Start Calibration.\r\n"); calibration_done = 0; if (!calibration_done) { led1 = 1; led2 = 0; 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; while (( calibration_times < 5)) { calibration_value = hx711.averageValue(20); if (calibration_value > (zero_value + WEIGHT_DIFFERENCE)) { calibration_times++; } else calibration_times = 0; } printf("calibration_value=%d calibration_times=%d\r\n", calibration_value, calibration_times); 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; customVar.scaleValue = scale_value; // 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 calibration_done = 1; } } } int a = 0; int b = 0; void print_char(char c = '*') { printf("%c\r\n", c); fflush(stdout); } Thread thread; CANMessage msg; InterruptIn button1(USER_BUTTON); volatile bool button1_pressed = false; // Used in the main loop volatile bool button1_enabled = true; // Used for debouncing Timeout button1_timeout; // Used for debouncing // Enables button when bouncing is over void button1_enabled_cb(void) { button1_enabled = true; } // ISR handling button pressed event void button1_onpressed_cb(void) { if (button1_enabled) { // Disabled while the button is bouncing button1_enabled = false; button1_pressed = true; // To be read by the main loop button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms } } void print_thread() { while (true) { #if 1 if(can1.read(msg)) { print_char(); printf("got message id=%d 0x%08x\r\n", msg.id, msg.id); // b = *reinterpret_cast<int*>(msg.data); b = msg.data[0]; printf("got data %d 0x%08x \r\n", b, b); if(msg.id == 1337) { led2 = !led2; b = *reinterpret_cast<int*>(msg.data); printf("got message %d\r\n", b); if(b % 5 == 0) led2 = !led2; } } // wait(0.2); #endif } } int main() { printf("\n\n*** RTOS basic example ***\n"); scaleCalibration(); 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); // can2.frequency(100000); //button1.mode(PullUp); // Activate pull-up button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event int idx = 0; // Just for printf below while(1) { 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)); led1 = !led1; a++; } } #if 0 while(1) { // can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a))); #if can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1)); #endif printf("loop a=%d\n", a); led1 = !led1; a++; wait(0.2); } #endif }