A simple example for analog input and EPD usage.

Dependencies:   GDEP015OC1 acn_nrf52_saadc aconno_bsp

Fork of acd52832_Car_battery_ch by Jurica Resetar

Revision:
8:c9f0aea93832
Parent:
6:e848c82b5248
Child:
9:eef3ea422bfe
--- a/main.cpp	Tue Jul 04 18:25:56 2017 +0000
+++ b/main.cpp	Wed Jul 05 13:04:57 2017 +0000
@@ -13,11 +13,12 @@
 #define DEBUG                   (0)
 #define ADC_MAX_VALUE           (4095)
 #define ADC_REF_VOLTAGE         (3.6)
-#define VOLTAGE_DIVIDER_RATION  (130.0/30)
+#define VOLTAGE_DIVIDER_RATION  (4.42)
+#define VOLTAGE_LOSS            (0.4)
 #define CURRENT_FACTOR          (36.0)
-#define BATTERY_MAX_V           (12.8)
-#define BATTERY_MIN_V           (12.0)
-#define BATTERY_STEP            (0.2) 
+#define BATTERY_MAX_V           (12.6)
+#define BATTERY_MIN_V           (11.85)
+#define BATTERY_STEP            (0.15) 
 #define QUICK_CURRENT           (float)(0.8)
 #define LOW_CURRENT             (float)(0.4)
 #define NO_CURRENT              (float)(0.01)
@@ -40,7 +41,6 @@
 AnalogIn  usb1      (p29);
 AnalogIn  usb2      (p30);
 
-
 uint8_t get_battery_level(float battery_voltage){
      if(battery_voltage > BATTERY_MAX_V - BATTERY_STEP){
                 return _BS_5;
@@ -95,6 +95,9 @@
     
     #if DEBUG
         char buffer[25] = {0};
+        float old_battery_voltage = 0;
+        float old_usb1_current = 0;
+        float old_usb2_current = 0;
     #endif
     
     NRF_SAADC->RESOLUTION = 0x00000002;             // Set 12b resolution
@@ -108,24 +111,21 @@
         adc2_mean += usb1.read_u16();
         adc3_mean += usb2.read_u16();
         count ++;
-                
-        #if DEBUG
-            epd.empty();
-            epd.writeFull(); 
-        #endif            
+                            
         if (count == NUM_OF_MEASUREMENTS){ 
      
             adc1_mean /= NUM_OF_MEASUREMENTS;
             adc2_mean /= NUM_OF_MEASUREMENTS;
             adc3_mean /= NUM_OF_MEASUREMENTS;
             count = 0;
-            
-            battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION;
-            usb1_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean;
-            usb2_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean;
+                
+            battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION + VOLTAGE_LOSS;
+            usb1_current = (adc2_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)) * CURRENT_FACTOR;
+            usb2_current = (adc3_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)) * CURRENT_FACTOR;
                     
             battery_level = get_battery_level(battery_voltage);
-            if(battery_level != old_battery_level){
+            if(battery_level != old_battery_level || change_flag){
+                
                 old_battery_level = battery_level;
                 change_flag = 1;
                 switch(battery_level){
@@ -161,6 +161,7 @@
                             }
                     default: break;
                 }
+                
                 epd.write();
             }
             
@@ -215,18 +216,28 @@
             }
 
             
-            #if DEBUG            
+            #if DEBUG    
                 // Print voltage and current values in debug mode
-                sprintf(buffer, "Battery: %5.5fV", adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION);     // Create a string
+                sprintf(buffer, "Battery: %5.5fV", old_battery_voltage);     // Create a string
+                epd.writeString(buffer,25,95,1);                        // Write new data to the buffer    
+                epd.write();                                            // Write string to the EPD
+                old_battery_voltage = battery_voltage;
+                
+                sprintf(buffer, "Battery: %5.5fV", battery_voltage);     // Create a string
                 epd.writeString(buffer,25,95,0);                        // Write new data to the buffer    
-                epd.write();                                            // Write string to the EPD
-                 
-                sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean);           // Create a string
+
+                sprintf(buffer, "USB1: %5.5f", old_usb1_current);           // Create a string
+                epd.writeString(buffer,5,190,1);                        // Write new data to the buffer    
+                old_usb1_current = usb1_current;
+                sprintf(buffer, "USB1: %5.5f", usb1_current);           // Create a string
                 epd.writeString(buffer,5,190,0);                        // Write new data to the buffer    
     
-                sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean);           // Create a string
+                sprintf(buffer, "USB2: %5.5fA", old_usb2_current);           // Create a string
+                epd.writeString(buffer,105,190,1);                        // Write new data to the buffer    
+                old_usb2_current = usb2_current;
+                sprintf(buffer, "USB2: %5.5fA", usb2_current);           // Create a string
                 epd.writeString(buffer,105,190,0);                       // Write new data to the buffer    
-            
+                
                 epd.write(); 
             #endif