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.
Dependencies: OLED_SSD1306 MCP9808
Diff: main.cpp
- Revision:
- 2:18d8f9d3286c
- Parent:
- 1:7749656733dd
- Child:
- 3:0bad8eec80ee
--- a/main.cpp Mon Aug 24 05:29:53 2020 +0000 +++ b/main.cpp Mon Aug 24 08:27:27 2020 +0000 @@ -144,6 +144,16 @@ int ds18b20_result = 0; float temp; + //Initialize sensor + if(ds1820_sensor.begin()) { + ds1820_sensor.startConversion(); // start temperature conversion + ThisThread::sleep_for(1); // let DS1820 complete the temperature conversion + pc.printf("Initial DS18B20 temp = %3.2f\r\n", ds1820_sensor.read()); // read temperature + //Without temperature sensor, charging will not work (unless minimum temperature set below -310.0) + }else{ + pc.printf("No DS1820 sensor found!\r\n"); + } + while(true) { ds1820_sensor.startConversion(); // start temperature conversion from analog to digital @@ -173,15 +183,20 @@ } // Detect battery presence by voltage activity -bool batteryPresent() +void BatteryPresenceThread() { - if(bat_voltage_avg > MIN_DETECT_VOLTAGE) + while(true) { - return true; - } - else - { - return false; + if(bat_voltage_avg > MIN_DETECT_VOLTAGE) + { + batteryPresenceState = true; + } + else + { + batteryPresenceState = false; + } + + ThisThread::sleep_for(250); } } @@ -198,15 +213,16 @@ //First check conditions to see if charging should be enabled or disabled bool temperature_en = ( ( ds18b20_temperature < MAX_TEMPERATURE ) && ( ds18b20_temperature > MIN_TEMPERATURE ) ); bool voltage_en = ( ( bat_voltage_avg < MAX_VOLTAGE ) && ( bat_voltage_avg > MIN_VOLTAGE ) ); - bool presence_en = batteryPresent(); + bool presence_en = batteryPresenceState; bool charge_time_exceeded = ( (Kernel::get_ms_count() - chargeStartTime ) > MAX_TIME_ms); //Charging can be enabled if battery is present, no protections triggered, and user starts the charge if(enableCharging == false) { - if( voltage_en && temperature_en && presence_en && buttonPressed ) + if( voltage_en && temperature_en && presence_en && !charge_time_exceeded && buttonPressed ) { enableCharging = true; + chargeStartTime = Kernel::get_ms_count(); buttonPressed = false; } } @@ -214,7 +230,7 @@ else { //Disable charging if any protection condition is triggered - if( !voltage_en || !temperature_en || !presence_en ) + if( !voltage_en || !temperature_en || !presence_en || charge_time_exceeded ) { enableCharging = false; } @@ -242,7 +258,7 @@ //Update flag that indicates state of TP4056 CE pin to other threads TP4056ChargingState = enableCharging; - ThisThread::sleep_for(100); + ThisThread::sleep_for(100); } } @@ -266,9 +282,12 @@ stream.str(""); - stream << std::fixed << std::setprecision(4) << vddref_voltage; - std::string vddref_output = "VDDREF: " + stream.str(); - oled_i2c.drawString(0,16*2, vddref_output.c_str() ); + stream << std::fixed << std::setprecision(4) << ds18b20_temperature; + std::string ds18_str = "Temp: " + stream.str(); + oled_i2c.drawString(0,16*2, ds18_str.c_str() ); + //stream << std::fixed << std::setprecision(4) << vddref_voltage; + //std::string vddref_output = "VDDREF: " + stream.str(); + //oled_i2c.drawString(0,16*2, vddref_output.c_str() ); oled_i2c.display(); @@ -291,10 +310,18 @@ //https://forums.mbed.com/t/how-to-configure-open-drain-output-pin-on-stm32/7007 toggleOut.mode(OpenDrainNoPull); + // TEMPERATURE INPUT THREAD + Thread ds18b20_temperature_thread; + ds18b20_temperature_thread.start(TemperatureInputThread); + // CHARGE ENABLE CONTROL THREAD Thread tp4056_control_thread; tp4056_control_thread.start(TP4056ControlThread); + // PRESENCE DETECTION THREAD + Thread bat_presence_thread; + bat_presence_thread.start(BatteryPresenceThread); + //OLED 128x64 INIT AND THREAD //initialize ssd1306 on i2c0 oled_i2c.init(); @@ -349,8 +376,10 @@ pc.printf("ADC0 1s min: %6.4f\r\n", bat_voltage_min); pc.printf("ADC0 1s max: %6.4f\r\n", bat_voltage_max); pc.printf("ADC0 1s avg: %6.4f\r\n", bat_voltage_avg); + pc.printf("ADC0 time (nominal 1000ms): %llu\r\n", (now-lastADCStartTime)); pc.printf("VDDREF Reading: %6.4f\r\n", vddref_voltage); - pc.printf("ADC0 time (nominal 1000ms): %llu\r\n", (now-lastADCStartTime)); + pc.printf("Temp: %6.4f\r\n", ds18b20_temperature); + pc.printf("Bat present: %d\r\n", batteryPresenceState); }