Used for testing battery sense circuit, looking for max and min levels. Allow for finding true empty, half and full values for driving LEDs for example

Dependencies:   mbed MPL3115A2 TSI WiGo_BattCharger

Revision:
5:4438d5665b4f
Parent:
3:3b88d6ae24f2
--- a/main.cpp	Thu May 23 11:44:24 2013 +0000
+++ b/main.cpp	Tue Sep 03 08:59:49 2013 +0000
@@ -1,89 +1,25 @@
 #include "mbed.h"
 #include "WiGo_BattCharger.h"
-#include "WiGo_AmbLight.h"
 #include "TSISensor.h"
 #include "math.h"
 #include "MPL3115A2.h"
+#include "main.h"
  
-#define MPL3115A2_I2C_ADDRESS (0x60<<1)
- MPL3115A2 wigo_sensor1(PTE0, PTE1, MPL3115A2_I2C_ADDRESS);
-
-#define MAG_ADDR 0x1D
-
-// define registers
-#define MAG_DR_STATUS 0x00
-#define MAG_OUT_X_MSB 0x01
-#define MAG_OUT_X_LSB 0x02
-#define MAG_OUT_Y_MSB 0x03
-#define MAG_OUT_Y_LSB 0x04
-#define MAG_OUT_Z_MSB 0x05
-#define MAG_OUT_Z_LSB 0x06
-#define MAG_WHO_AM_I  0x07
-#define MAG_SYSMOD    0x08
-#define MAG_OFF_X_MSB 0x09
-#define MAG_OFF_X_LSB 0x0A
-#define MAG_OFF_Y_MSB 0x0B
-#define MAG_OFF_Y_LSB 0x0C
-#define MAG_OFF_Z_MSB 0x0D
-#define MAG_OFF_Z_LSB 0x0E
-#define MAG_DIE_TEMP  0x0F
-#define MAG_CTRL_REG1 0x10
-#define MAG_CTRL_REG2 0x11
-
-// what should WHO_AM_I return?
-#define MAG_3110_WHO_AM_I_VALUE 0xC4
-
-
-// Fields in registers
-// CTRL_REG1: dr2,dr1,dr0  os1,os0  fr tm ac
-
-// Sampling rate from 80Hz down to 0.625Hz
-#define MAG_3110_SAMPLE80 0
-#define MAG_3110_SAMPLE40 0x20
-#define MAG_3110_SAMPLE20 0x40
-#define MAG_3110_SAMPLE10 0x60
-#define MAG_3110_SAMPLE5 0x80
-#define MAG_3110_SAMPLE2_5 0xA0
-#define MAG_3110_SAMPLE1_25 0xC0
-#define MAG_3110_SAMPLE0_625 0xE0
-
-// How many samples to average (lowers data rate)
-#define MAG_3110_OVERSAMPLE1 0
-#define MAG_3110_OVERSAMPLE2 0x08
-#define MAG_3110_OVERSAMPLE3 0x10
-#define MAG_3110_OVERSAMPLE4 0x18
-
-// read only 1 byte per axis
-#define MAG_3110_FASTREAD 0x04
-// do one measurement (even if in standby mode)
-#define MAG_3110_TRIGGER 0x02
-// put in active mode
-#define MAG_3110_ACTIVE 0x01
-
-// CTRL_REG2: AUTO_MRST_EN  _ RAW MAG_RST _ _ _ _ _
-// reset sensor after each reading
-#define MAG_3110_AUTO_MRST_EN 0x80
-// don't subtract user offsets
-#define MAG_3110_RAW 0x20
-// reset magnetic sensor after too-large field
-#define MAG_3110_MAG_RST 0x10
-
-// DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR
-#define MAG_3110_ZYXDR  0x08
-
-
-#define PI 3.14159265359
-#define ON  0
-#define OFF 1
-
-#define RGB_LED_ON  0
-#define RGB_LED_OFF 1
+MPL3115A2 wigo_sensor1(PTE0, PTE1, MPL3115A2_I2C_ADDRESS);
 
 // Some LEDs for showing status
 DigitalOut redLed(LED_RED);
 DigitalOut greenLed(LED_GREEN);
 DigitalOut blueLed(LED_BLUE);
 
+// test analog input pin
+AnalogIn NTC(PTE20);
+AnalogIn testPin1(PTE30);
+AnalogIn testPin2(PTE22);
+AnalogIn testPin3(PTC0);
+AnalogIn testPin4(PTE29);
+AnalogIn testPin5(PTD1);
+
 // Slide sensor acts as a button
 TSISensor tsi;
 
@@ -102,7 +38,6 @@
 Serial pc(USBTX, USBRX);
 
 WiGo_BattCharger Batt( BATT_LOW, BATT_MED, BATT_FULL, CHRG_EN1, CHRG_EN2, CHRG_SNS_EN, CHRG_SNS, CHRG_POK, CHRG_CHG);
-WiGo_AmbLight Light( AMBLIGHT_EN, AMBLIGHT_LVL);
 
 // Read a single byte form 8 bit register, return as int
 int readReg(char regAddr)
@@ -117,7 +52,6 @@
     return (int)( cmd[0]);
 }
 
-
 // read a register per, pass first reg value, reading 2 bytes increments register
 // Reads MSB first then LSB
 int readVal(char regAddr)
@@ -133,7 +67,6 @@
     return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
 }
 
-
 void initMag() {
     char cmd[2];
 
@@ -146,7 +79,6 @@
     i2c.write(addr, cmd, 2);
     }
     
-    
 void calXY() //magnetometer calibration: finding max and min of X, Y axis
 {
     int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
@@ -211,23 +143,27 @@
     }
     greenLed = OFF;
     wait(1.0);
-
+}
 
-}
+// ================================ MAIN CODE ================================
 
 float max_batt;
 float batt_lvl;
-float sensor_data[3];
+float sensor_data[2];
+float testVoltage;
 
 int main()
 {
+    //unsigned int mode=1;
+    
+    testVoltage = testPin.read();
     
     Batt.init(CHRG_500MA);
     Batt.sense_en(1);
-    Light.en(1);
     wait(0.5);
     max_batt = Batt.read();
     wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_32);
+    wigo_sensor1.Barometric_Mode();
 
     printf("MAG3110 Test\n");
 
@@ -250,10 +186,14 @@
     printf("CTRL_REG1 %X\n", readReg( MAG_CTRL_REG1 ));
     printf("CTRL_REG2 %X\n", readReg( MAG_CTRL_REG2 ));
 
-    printf("calibrate\n");
-    calXY();
-    printf("....Finished\n");
-    printf("avgX = %d, avgY = %d\n", avgX, avgY);
+    // Mag calibration or store defaults
+    //===================================
+    //printf("calibrate\n");
+    //calXY();
+    //printf("....Finished\n");
+    //printf("avgX = %d, avgY = %d\n", avgX, avgY);
+    avgX = 64366;
+    avgY = 1624;
 
     redLed = OFF;
     greenLed = OFF;
@@ -264,6 +204,7 @@
         wait(2);
         int xVal = readVal(MAG_OUT_X_MSB);
         int yVal = readVal(MAG_OUT_Y_MSB);
+        int zVal = readVal(MAG_OUT_Z_MSB);
         float heading = (atan2((double)(yVal-avgY),(double)(xVal-avgX)))*180/PI;
         
         // Do something with heading - display direction and turn on blue LED if heading approx north
@@ -277,8 +218,9 @@
         if (heading < -112.5 && heading > -157.5) printf("SW\n");
 
         if (heading < 0) heading += 360.0;
-        printf("xVal - avgX = %d, yVal - avgY = %d  ", xVal-avgX, yVal-avgY);
-        printf("X = %d, Y = %d, Heading %f\n", xVal, yVal, heading);
+        //printf("xVal - avgX = %d, yVal - avgY = %d  ", xVal-avgX, yVal-avgY);
+        //printf("X = %d, Y = %d, Z = %d, Heading %f\n", xVal, yVal, zVal, heading);
+        printf("Heading %f\n", heading);
 
         Batt.LEDupdate(1);
 
@@ -287,11 +229,33 @@
             max_batt = batt_lvl;
         }
 
-        pc.printf(">%f Max:%f %i %i\n", batt_lvl, max_batt, Batt.level(), Light.level());
+        pc.printf("Batt = %f Max:%f %i%\n", batt_lvl, max_batt, Batt.level());
         
         if ( wigo_sensor1.isDataAvailable()) {
             wigo_sensor1.getAllData( &sensor_data[0]);
-            pc.printf("\Altitude: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
+            pc.printf("Bar: %f\tTemperature: %f\n", sensor_data[0]/100, sensor_data[1]);
         }
+        
+        //if ( mode & 0x0001) {
+        //    pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
+        //    wigo_sensor1.Altimeter_Mode();
+        //} else {
+        //    pc.printf("\Altitude: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
+        //    wigo_sensor1.Barometric_Mode();
+        //}
+        //mode++;
+        
+        //testVoltage = testPin.read();
+        //pc.printf("testVoltage  = %f\n", testVoltage );
+        //testVoltage = testPin1.read();
+        //pc.printf("testVoltage1 = %f\n", testVoltage );
+        //testVoltage = testPin2.read();
+        //pc.printf("testVoltage2 = %f\n", testVoltage );
+        //testVoltage = testPin3.read();
+        //pc.printf("testVoltage3 = %f\n", testVoltage );
+        //testVoltage = testPin4.read();
+        //pc.printf("testVoltage4 = %f\n", testVoltage );
+        pc.printf("------------------------------------------------\n");
+        
     }
 }