This is the code used on my video series "Hybrid Supercapacitor Car Battery" for my own hardware monitoring system. THe videos can be found on madelectronengineering.com

Dependencies:   BurstSPI Fonts INA219 mbed LPC1114_WakeInterruptIn

Fork of SharpMemoryLCD by Paul Staron

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* This is a project for madelectronengineering.com
00002  This code is used for a hardware monitoring system for the Hybrid Supercapacitor Car Battery to monitor the supercap voltage, the LiFeP04 voltage,
00003 current, and wattage, plus also monitor 3 seperate temperature points.
00004 
00005 Required hardware:
00006     STM32 NUCLEO-L073RZ
00007     TMP36 Temperature Sensors (x3)
00008     INA219 Current & Voltage Breakout (modify by removing 0.1Ohm resistor and replacing with 0.01Ohm Resistor to measure up to 32Amps
00009     ST NUCLEO Protoboard
00010     30k & 7.5k resistors for voltage divider to measure supercap voltage
00011     0.1Ohm 25W current limiting power resistor in between supercap array and LiFeP04 battery
00012     Sharp Memory LCD 400x240
00013     
00014 The INA219 measures the current and amperage on the battery side of the current limiting resistor to keep an eye on the LiFeP04 battery.
00015 voltage divider connects directly to + of supercap array
00016 
00017 Arduino equivelant connections:
00018 A0 - TMP36 #1
00019 A1 - TMP36 #2
00020 A2 - TMP36 #3
00021 A3 - Voltage divider Input
00022 A4&A5 - I2C for INA219
00023 
00024 Arduino equivelant connections for LCD Screen:
00025 D13 - SClk
00026 D11 - MOSI
00027 D10 - CD
00028 D9 - Enable
00029 D8 - Extcom
00030 
00031 */
00032 #include "mbed.h"
00033 #include "SharpLCD.h"
00034 #include "INA219.hpp"
00035 #include "Neu44x36.h"
00036 #include "Neu31x26.h"
00037 #include "WakeUp.h"
00038 
00039 SharpLCD display(PA_7, NC, PA_5, PB_6, PC_7, PA_9); //mosi, miso(not used), sck, cs, enable, extcom
00040 INA219 ina219(PC_1, PC_0, 0x40, 400000, RES_12BITS);
00041 AnalogIn   ain1(PA_0); // connect A0 to Vout(Temp36)
00042 AnalogIn   ain2(PA_1); // connect A1 to Vout(Temp36)
00043 AnalogIn   ain3(PA_4); // connect A2 to Vout(Temp36)
00044 AnalogIn   ain4(PB_0); // Connect A3 to Voltage Divider
00045 
00046 int main()
00047 {        
00048     display.enableDisplay();  //enable sharp memory lcd
00049     display.clearImmediate();   //clear the screen buffer and screen
00050     
00051     display.set_font(Neu44x36);
00052        
00053        //only send graphics and text once to the screen during bootup
00054        display.locate(17,0);
00055        display.printf("Hybrid Supercap");
00056        display.locate(75,35);
00057        display.printf("Car Battery");
00058        
00059        display.rect(0,73,195,239, Black);
00060        display.rect(205,73,399,239, Black);
00061        display.fillrect(195,73,10,167, Black);
00062        display.line(1,174,194,174, Black);
00063        display.line(1,176,194,176, Black);
00064        display.line(1,107,194,107, Black);
00065        display.line(206,107,399,107, Black);
00066        display.line(1,141,194,141, Black);
00067        display.line(206,141,399,141, Black);
00068        display.line(1,175,194,175, Black);
00069        display.line(206,175,399,175, Black);
00070        display.line(1,209,194,209, Black);
00071        display.line(206,209,399,209, Black);
00072        
00073        display.set_font(Neu31x26);
00074        display.locate(15,76);
00075        display.printf("Supercaps");
00076        display.locate(240,76);
00077        display.printf("LiFePo4");
00078        display.locate(30,179);
00079        display.printf("Resistor");
00080        
00081        display.locate(360,110);
00082        display.printf("V");
00083        display.locate(360,143);
00084        display.printf("A");
00085        display.locate(360,177);
00086        display.printf("W");
00087        display.locate(155,212);
00088        display.printf("'F");
00089        display.locate(360,212);
00090        display.printf("'F");
00091        display.locate(155,143);
00092        display.printf("'F");
00093        display.locate(155,110);
00094        display.printf("V");
00095        
00096   while(1) {    
00097        
00098        float volt;
00099        float current_ma;
00100        float power;
00101                   
00102        volt       = ina219.read_bus_voltage();
00103        current_ma = ina219.read_current_mA() / 1000;
00104        power      = volt * current_ma;
00105        
00106        float V1 = ain1.read() * 3.3; // connect Vs(Tmp36) to 3.3V        
00107        float tempC1 = (V1-0.5) * 100; // calculate temperature C
00108        float tempF1 = (tempC1 * 9 / 5) + 32.0; // calculate temperature F
00109        
00110        float V2 = ain2.read() * 3.3; // connect Vs(Tmp36) to 3.3V        
00111        float tempC2 = (V2-0.5) * 100; // calculate temperature C
00112        float tempF2 = (tempC2 * 9 / 5) + 32.0; // calculate temperature F
00113        
00114        float V3 = ain3.read() * 3.3; // connect Vs(Tmp36) to 3.3V        
00115        float tempC3 = (V3-0.5) * 100; // calculate temperature C
00116        float tempF3 = (tempC3 * 9 / 5) + 32.0; // calculate temperature F
00117  
00118        float V4 = (ain4.read()) * 3.3; // Voltage divider for supercap voltage
00119        float voltage = (V4 * 5);   // Voltage divider is 1 to 5 ratio
00120    
00121        display.set_font(Neu31x26);
00122        display.locate(210,110);
00123        display.printf("      ");
00124        display.locate(210,110);
00125        display.printf("%+05.2f", volt);      
00126        display.locate(210,143);
00127        display.printf("      "); 
00128        display.locate(210,143);
00129        display.printf("%+04.1f", current_ma);      
00130        display.locate(210,177);
00131        display.printf("      ");
00132        display.locate(210,177);
00133        display.printf("%+05.1f", power);      
00134        display.locate(20,212);
00135        display.printf("      ");
00136        display.locate(20,212);
00137        display.printf("%+03.0f", tempF1);      
00138        display.locate(210,212);
00139        display.printf("      ");
00140        display.locate(210,212);
00141        display.printf("%+03.0f", tempF2);      
00142        display.locate(20,143);
00143        display.printf("      ");
00144        display.locate(20,143);
00145        display.printf("%+03.0f", tempF3);      
00146        display.locate(20,110);
00147        display.printf("      ");
00148        display.locate(20,110);
00149        display.printf("%+05.2f", voltage);       
00150 
00151        display.update();
00152  
00153 //Set wakeup time for 1 second
00154 //        WakeUp::set_ms(500);
00155         
00156         //Enter deepsleep, the program won't go beyond this point until it is woken up
00157 //        deepsleep();   
00158 }
00159 }