MAX34417 Demo

Dependencies:   max32625pico maxim-dev USBDevice

main.cpp

Committer:
MadhuraT
Date:
2019-02-19
Revision:
1:f7e85b6d0117
Parent:
0:bc0f96339b73
Child:
2:39d6246ca2cb

File content as of revision 1:f7e85b6d0117:

#include "mbed.h"
#include "USBSerial.h"
#include "max32625pico.h"

USBSerial pc;

double readPwrMonitor(I2C &i2cbus, int channel_no);
void configPwrMonitor(I2C &i2cbus);

#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);
    
    //initialize RGB LED channels and turn them off
    DigitalOut rLED(LED1, LED_OFF);
    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 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
    configPwrMonitor(i2c);
    
    //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 power value is available every 12.5ms.
    while (true) {
        //the readPwrMonitor() function returns a raw 24-bit power accumulator reading
        //if successful and a zero if unsuccessful
        for (channel_no=0; channel_no <4 ; channel_no ++)
        {
        power_raw = readPwrMonitor(i2c,channel_no);
        
        //print poweronly if valid data is received. 
        if(power_raw != 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",power_W);
        }
        else {
            pc.printf("\r\nPowerMonitor Read Error");
        }
        }
        
        //toggle LEDs one at a time for a total of 33ms
        bLED = LED_OFF;
        rLED = LED_ON;
        wait(0.11);
        rLED = LED_OFF;
        gLED = LED_ON;
        wait(0.11);
        gLED = LED_OFF;
        bLED = LED_ON;
        wait(0.11);
    }
}

void configPwrMonitor(I2C &i2cbus)
{
    //creates an array to store the values to be written to the configuration register
  
    char data[2] = {REG_CONTROL, 0xA0};
    
    //this built-in I2C write function from mbed sends the configuration data to the MAX34417
    i2cbus.write(I2C_SLAVE_ADDRESS, data, 2, false);
}

double readPwrMonitor(I2C &i2cbus, int channel)
{
    int pwr_raw = 0;
    char data[2]= {0, 0};
    
    data[0] = REG_UPDATE;
    data[1] =0;
    
   
    //it returns a zero on success or non-zero if unsuccessful
    int rtn_val = i2cbus.write(I2C_SLAVE_ADDRESS, data, 1, true);
    
    //proceeds to read power only if previous command was successful
    if(rtn_val == 0)
    {
        
        {
        //this I2C command reads the power and stores it in the 'data' array.
        //it returns a zero on success or non-zero if unsuccessful
        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)
        {
            
            pc.printf("Successful");
        }
        
        }
    }
    pwr_raw=data[0];
    //returns the power reading
    return pwr_raw;
}