MAX34417 Demo

Dependencies:   max32625pico maxim-dev USBDevice

Revision:
1:f7e85b6d0117
Parent:
0:bc0f96339b73
Child:
2:39d6246ca2cb
--- a/main.cpp	Wed Nov 01 01:47:38 2017 +0000
+++ b/main.cpp	Tue Feb 19 22:27:27 2019 +0000
@@ -4,16 +4,34 @@
 
 USBSerial pc;
 
-int readTemp(I2C &i2cbus);
-void configTemp(I2C &i2cbus);
+double readPwrMonitor(I2C &i2cbus, int channel_no);
+void configPwrMonitor(I2C &i2cbus);
 
-#define WRITE_ADDRESS   0x90
-#define READ_ADDRESS    0x91
-#define TEMP_REG        0x00
-#define CONFIG_REG      0x01
+#define I2C_SLAVE_ADDRESS 0X10
+#define REG_UPDATE     0x00
+#define REG_CONTROL    0x01
+#define REG_ACC_COUNT  0x02
+#define REG_PWR_ACC1   0x03
+#define REG_PWR_ACC2   0x04
+#define REG_PWR_ACC3   0x05
+#define REG_PWR_ACC4   0x06
+#define REG_V_CH1      0x07
+#define REG_V_CH2      0x08
+#define REG_V_CH3      0x09
+#define REG_V_CH4      0x0A
+#define REG_DID        0x10
+#define REG_BULK_UPDATE 0x11
+#define REG_BULK_POWER_READOUT  0x06
+#define REG_BULK_VOLTAGE_READOUT 0x07
+
+
 
 int main()
 {
+    int channel_no;
+    int device_id;
+    char *dev_id;
+    char data_val[2]= {0x0F,0};
     //initialize the MAX32625PICO board for +3.3V logic levels
     MAX32625PICO pico(MAX32625PICO::IOH_3V3, MAX32625PICO::VIO_IOH, MAX32625PICO::VIO_IOH);
     
@@ -22,35 +40,54 @@
     DigitalOut gLED(LED2, LED_OFF);
     DigitalOut bLED(LED3, LED_OFF);
     
+    
     //initialize the I2C master interface
     I2C i2c(P1_6, P1_7);    //SDA, SCL
+
     
-    //delcare variables to store the raw temperature readings and the converted Celsius temperature
-    int temperature_raw;
-    float temperature_C;
+    //delcare variables to store the raw power readings and the converted power readings
+    int power_raw;
+    double power_W;
+    double step1results, step2results, step3results, senseresistor1;
     
+    i2c.write(I2C_SLAVE_ADDRESS,data_val, 1, false);
+    i2c.read(I2C_SLAVE_ADDRESS,dev_id, 1, false);
+    if(*dev_id == 0x07)
+    {
+        pc.printf ("Device Found \n");
+    }
+    else
+    {
+        pc.printf ("Device not found \n");
+    }
     //write the configuration register
-    configTemp(i2c);
+    configPwrMonitor(i2c);
     
-    //the while loop will read the temperature reading, convert to Celsius, and then
+    //the while loop will read the power raw reading, convert to actual power data, and then
     //print the result to a serial terminal. It will then toggle each on-board LED
-    //for 11ms for a total of 33ms. A new temperature value is available every 12.5ms.
+    //for 11ms for a total of 33ms. A new power value is available every 12.5ms.
     while (true) {
-        //the readTemp() function returns a raw 16-bit temperature reading
+        //the readPwrMonitor() function returns a raw 24-bit power accumulator reading
         //if successful and a zero if unsuccessful
-        temperature_raw = readTemp(i2c);
+        for (channel_no=0; channel_no <4 ; channel_no ++)
+        {
+        power_raw = readPwrMonitor(i2c,channel_no);
         
-        //print temperature only if valid data is received. Readings of exactly 0.00 degrees
-        //will be thrown out but really what are the chances?
-        if(temperature_raw != 0)
+        //print poweronly if valid data is received. 
+        if(power_raw != 0)
         {
-            temperature_C = temperature_raw/256.0;
+            step1results=power_raw;
+            step2results = (double)step1results / (double)(1073741824);
+            senseresistor1 = 0.001 *senseresistor1;
+            step3results = step2results * ((.1 / senseresistor1) * 24);
+            power_W = step3results;
             
             //print a floating point value with 4 decimal places
-            pc.printf("\r\n%.4f C",temperature_C);
+            pc.printf("\r\n%.4f C",power_W);
         }
         else {
-            pc.printf("\r\nTemp Read Error");
+            pc.printf("\r\nPowerMonitor Read Error");
+        }
         }
         
         //toggle LEDs one at a time for a total of 33ms
@@ -66,40 +103,68 @@
     }
 }
 
-void configTemp(I2C &i2cbus)
+void configPwrMonitor(I2C &i2cbus)
 {
     //creates an array to store the values to be written to the configuration register
-    //values chosen will program the MAX31875 for 8 conversions/second and 12-bit resolution
-    char data[3] = {CONFIG_REG, 0x00, 0x66};
+  
+    char data[2] = {REG_CONTROL, 0xA0};
     
-    //this built-in I2C write function from mbed sends the configuration data to the MAX31875
-    i2cbus.write(WRITE_ADDRESS, data, 3, false);
+    //this built-in I2C write function from mbed sends the configuration data to the MAX34417
+    i2cbus.write(I2C_SLAVE_ADDRESS, data, 2, false);
 }
 
-int readTemp(I2C &i2cbus)
+double readPwrMonitor(I2C &i2cbus, int channel)
 {
-    int temp_raw = 0;
-    char data[2] = {TEMP_REG, 0};
+    int pwr_raw = 0;
+    char data[2]= {0, 0};
     
-    //the first I2C command sets the MAX31875 to point to the temperature register
+    data[0] = REG_UPDATE;
+    data[1] =0;
+    
+   
     //it returns a zero on success or non-zero if unsuccessful
-    int rtn_val = i2cbus.write(WRITE_ADDRESS, data, 1, true);
+    int rtn_val = i2cbus.write(I2C_SLAVE_ADDRESS, data, 1, true);
     
-    //proceeds to read temperature only if previous command was successful
+    //proceeds to read power only if previous command was successful
     if(rtn_val == 0)
     {
-        //this I2C command reads the temperature and stores it in the 'data' array.
+        
+        {
+        //this I2C command reads the power and stores it in the 'data' array.
         //it returns a zero on success or non-zero if unsuccessful
-        rtn_val = i2cbus.read(READ_ADDRESS, data, 2, false);
+        if(channel == 0 )
+        {
+        data[0] = REG_PWR_ACC1;
+        data[1] =0;
+        }
+        else if (channel == 1)
+        {
+        data[0] = REG_PWR_ACC2;
+        data[1] =0;
+        }
+        else if (channel == 2)
+        {
+        data[0] = REG_PWR_ACC3;
+        data[1] =0;
+        }
+        else if (channel == 3)
+        {
+        data[0] = REG_PWR_ACC4;
+        data[1] =0;
+        }
+        rtn_val = i2cbus.read(I2C_SLAVE_ADDRESS, data, 1, false);
         
         //proceeds to format raw temperature data only if previous command was successful
         if(rtn_val == 0)
         {
-            //combine both 8-bit register readings into one 16-bit variable
-            temp_raw = ((data[0] << 8) | data[1]);
+            
+            pc.printf("Successful");
+        }
+        
         }
     }
-    //returns the 16-bit raw temperature reading
-    return temp_raw;
+    pwr_raw=data[0];
+    //returns the power reading
+    return pwr_raw;
 }
     
\ No newline at end of file