Monitor Bateria OLED

Dependencies:   mbed ssd1306_library

Revision:
1:7f60e5e44a1b
Parent:
0:b418a81d46b0
--- a/main.cpp	Fri Jan 08 11:18:24 2021 +0000
+++ b/main.cpp	Wed Aug 24 16:33:52 2022 +0000
@@ -1,226 +1,61 @@
-#include "mbed.h"
-//#include "USBSerial.h"
-#include "ssd1306.h"
-#include "SDFileSystem.h"
+// Monitoramento de Carga e Descarga de uma bateria selada
 
-#define fator_filtro 5
-#define ctoT 0.224f
- 
-SDFileSystem sd(D11, D12, D13, D10, "sd"); // the pinout on the mbed Cool Components workshop board
+#include "mbed.h"
+#include "ssd1306.h"
+
 
-FILE *myLogFile;
-  
-AnalogIn amp_pin(A0);
-AnalogIn volt_pin(A3);
+AnalogIn tensao(A0);
+AnalogIn corrente (A1);
+DigitalOut led(D2);
+//Serial pc(USBTX, US BRX);
+float v_lido ; // Variável que recebe o valor de tensão
+float a_lido ; // Variável que recebe o valor de corrent
 
-//Virtual serial port over USB
-//USBSerial device;
-Serial pc(USBTX, USBRX);
- 
+
+
+
+
+
 SSD1306 lcd (D14, D15); // assumes default I2C address of 0x78
 
-Ticker flipper;
-Timer fileOpenTimer;
-Timer debug_timer;
 
-//DigitalOut led(D13);
-DigitalIn sw(PC_13);
 
-float mAmp_S;
-float mAmp;
-float mAmp_filtred;
-float mAmp_filtred_old;
-
-int second = 0;
-int minute = 0;
-int hour = 0;
-bool flag;
- 
-FILE *nextLogFile(void)
+int main()
 {
-    static unsigned int fileNumber = 0;
-    char fileName[32];
-    FILE *filePtr = NULL;
-    do 
-    {
-        if (filePtr != NULL)
-            fclose(filePtr);
-        sprintf(fileName,"/sd/log%04u.csv",fileNumber++);
-        filePtr = fopen(fileName,"r");
-    } while (filePtr != NULL);
-    return fopen( fileName,"w");
-} 
- 
- 
-void mAmp_time_base()
-{
-    mAmp_S = mAmp_S + mAmp_filtred; 
-    //led=!led;
-    second++;
-    flag =! flag;
-} 
- 
-int main(void) 
-{
-    
-    float volt;
-    float volt_filtred;
-    float volt_filtred_old;
+
+
+//***********************Iniciaçização Display********************************//
+
+    lcd.speed (SSD1306::Medium);  // set working frequency
+    lcd.init();
+    lcd.set_contrast(200);
+    lcd.cls();                    // clear frame buffer
+    lcd.locate (1,0);                 // set text cursor to line 3, column 1
+    lcd.printf (" MONITOR BATERIA"); // print to frame buffer
     
-    float mAmp_H;
-    bool start = 0;
-    
-    lcd.speed (SSD1306::Medium);  // set working frequency
-    lcd.init();                   // initialize SSD1306
-    lcd.set_contrast(100);
-    lcd.cls();                    // clear frame buffer
-    lcd.locate (3,2);             // set text cursor to line 3, column 1
-    lcd.printf ("Starting up"); // print to frame buffer
-    for( int i = 0; i < 16; i++)
-    {
-        lcd.locate (4,i);
-        lcd.printf (".");
-        lcd.redraw();
-        wait_ms(150);
-    }
-    wait_ms(300);
-    lcd.cls();
-    lcd.redraw();
-        
-    myLogFile = nextLogFile();
-    if (!myLogFile) {
-        // ERROR failed to open the first log file for writing.
-        // The SD card is missing, not working, read only or full?
- 
-        return 1; // probably want to exit the program in this situation
-    }
-    float fator_filtro_x_ctoT = fator_filtro * ctoT;
-    float soma_fator_filtro_x_ctoT = 1.0f + fator_filtro_x_ctoT;
-    
-    printf("%f\n",fator_filtro_x_ctoT);  
-    printf("%f\n",soma_fator_filtro_x_ctoT);  
-      
-    while(1)
-    {     
-        //debug_timer.reset();     
-        //debug_timer.start();
-        
-         
-        if((sw == 0) && (start == 0))
-        {
-            wait_ms(200);
-            if((sw == 0) && (start == 0))
-            {
-                start = 1;
-                wait_ms(1000);
-                flipper.attach(&mAmp_time_base, 1.0f); 
-                fileOpenTimer.start();  
-                lcd.cls();  
-            }
-        }
+//*********************Calculo Divisor AD8210*********************************//
+    while(1) {
+        v_lido= tensao;
+        a_lido = corrente;
+        v_lido = (v_lido*4.0)*3.3; // O valor 4.0 é realação entre a tensão de entrada (12V) com a tensão que foi calculada no divisor de tensão que é 3V
+        a_lido =(a_lido*3.3)/1.375; // O valor de 1.375 é a razão(resistência) entre a tensão Vshunt (3.6 V) com a corrente do circuito (2.4 A)
         
-        if((sw == 0) && (start == 1))
-        {
-            wait_ms(200);
-            if((sw == 0) && (start == 1))
-            {
-                start = 0;
-                flipper.detach(); 
-                lcd.locate(1,1);
-                lcd.printf("Log finished");
-                lcd.redraw();
-                fileOpenTimer.stop(); 
-                fileOpenTimer.reset(); // restart the timer 
-                fclose(myLogFile); // close the current file
-                myLogFile = nextLogFile(); // open a new file   
-                wait_ms(5000);
-                mAmp_S = 0;
-                mAmp_H = 0;
-                second = 0;
-                minute = 0;
-                hour = 0;  
-                lcd.cls();
-                lcd.redraw();
-            }
-        }
-        
-        
-        //mAmp = amp_pin.read() * 3.3f * 14.6f;
-        //mAmp = mAmp * 1.37f * 100.0f;
-        
-        mAmp = amp_pin.read() * 4870.0f;//6600.66f ;
-        
-        mAmp_filtred =  ((fator_filtro_x_ctoT * mAmp) + (mAmp_filtred_old)) / soma_fator_filtro_x_ctoT;
-        mAmp_filtred_old = mAmp_filtred;
-        
-        
-        volt = volt_pin.read() * 36.45f; //3.3f * divisor resistivo 10k / 1k
-         
-        volt_filtred =  ((fator_filtro_x_ctoT * volt) + (volt_filtred_old)) / soma_fator_filtro_x_ctoT;
-        volt_filtred_old = volt_filtred; 
-         
-         
-        if(start == 0)
-        {
-            lcd.locate(1,1);
-            lcd.printf("Press to log"); 
-        }
+//******************************************************************************
+
+
+
+
+        lcd.locate (3,0);
+        lcd.printf ("VOLTS: %1.2f",v_lido); // print to frame buffer
         
-        if(start == 1)
-        {
-            mAmp_H = mAmp_S / 3600.0f;  
-            lcd.locate(1,1);
-            lcd.printf("Logging..."); 
-            lcd.locate(5,1);
-            lcd.printf("%8.2f mAh",mAmp_H); 
-            lcd.locate(7,1);
-            lcd.printf("%02i:%02i:%02i",hour, minute, second); 
-        }
-             
-             
-        if(second > 59)
-        {
-            second = 0;
-            minute++;
-        }
+        lcd.locate (5,0);
+        lcd.printf ("Corrente: %1.2f",a_lido); // print to frame buffer
         
-        if(minute > 59)
-        {
-            minute = 0;
-            hour++; 
-        }
-        
-        if(hour > 23)
-        {
-            hour = 0; 
-        }
+        wait(0.5);
         
-        lcd.locate(3,1);
-        lcd.printf("%8.2f V",volt_filtred);
-        
-        lcd.locate(4,1);
-        lcd.printf("%8.2f mA",mAmp_filtred); 
-          
         lcd.redraw();
-        
-        if(flag == 1)
-        {
-            flag = 0;
-            if(start == 1)
-            {
-                fprintf(myLogFile, "%02i:%02i:%02i ; %8.2f ; %8.2f ; %8.2f\n", hour, minute, second, volt_filtred, mAmp_filtred, mAmp_H);
-            }
         }
-        
-        if (fileOpenTimer > (5*60)) 
-        { // file has been open 5 minutes
-            fclose(myLogFile); // close the current file
-            myLogFile = nextLogFile(); // open a new file   
-            fileOpenTimer.reset(); // restart the timer
-        }
-        wait_ms(200);  
-        
-        //debug_timer.stop();
-        //printf("%f\n", debug_timer.read());
-    }
+       
 }
+
+