Treehouse Mbed Team / Mbed 2 deprecated APS_1U5x

Dependencies:   mbed

src/adc.cpp

Committer:
mfwic
Date:
2018-12-14
Revision:
18:78e982f31c6b
Parent:
17:454afe56eedb
Child:
22:2c37ac12746e

File content as of revision 18:78e982f31c6b:

//-------------------------------------------------------------------------------
// 
//  Treehouse Designs Inc.
//  Colorado Springs, Colorado
// 
//  Copyright (c) 2016 by Treehouse Designs Inc. 
//  Copyright (c) 2018 by Agility Power Systems Inc. 
// 
//  This code is the property of Treehouse Designs, Inc. (Treehouse) and
//  Agility Power Systems Inc. (Agility) and may not be redistributed
//  in any form without prior written permission from 
//  both copyright holders, Treehouse and Agility.
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
//   
//-------------------------------------------------------------------------------
// 
//  REVISION HISTORY:
//  
//   $Author: $
//   $Rev: $
//   $Date: $
//   $URL: $
// 
//-------------------------------------------------------------------------------

#include "mbed.h"
#include "adc_defs.h"
#include "adc.h"
#include "all_io.h"

void initADC(void){
    
    //Auto-zero current values
    struct adcValues adcVals = getADCresults();
    
    CURRENT_48_OFFSET = adcVals.i48;
    CURRENT_24_OFFSET = adcVals.i24;
    CURRENT_12_OFFSET = adcVals.i12;
    
}

/*******************************************************************************
 getADCresults
*******************************************************************************/
struct adcValues getADCresults(void){
    
    unsigned int v48x = 0; 
    unsigned int v24x = 0;
    unsigned int v12x = 0;
    unsigned int i48x = 0; 
    unsigned int i24x = 0;
    unsigned int i12x = 0;
    
    struct adcValues avals;
    unsigned int loopCounter = LOOP_COUNTER;
    
    for(unsigned int i=0;i<loopCounter;i++){
        v48x = v48x + VIN48.read_u16();
        i48x = i48x + IIN48.read_u16();
        v24x = v24x + VIN24.read_u16();
        i24x = i24x + IIN24.read_u16();
        v12x = v12x + VIN12.read_u16();
        i12x = i12x + IIN12.read_u16();
    }
    avals.v48 = v48x/loopCounter; 
    avals.v24 = v24x/loopCounter;
    avals.v12 = v12x/loopCounter;
    avals.i48 = i48x/loopCounter; 
    avals.i24 = i24x/loopCounter;
    avals.i12 = i12x/loopCounter;
    
    return avals;
}

/*******************************************************************************
 getADCvolts
*******************************************************************************/
struct adcValues getADCvolts(void){
    
    unsigned int v48x = 0; 
    unsigned int v24x = 0;
    unsigned int v12x = 0;
    
    struct adcValues avals;
    
    for(unsigned int i=0;i<100;i++){
        v48x = v48x + VIN48.read_u16();
        v24x = v24x + VIN24.read_u16();
        v12x = v12x + VIN12.read_u16();
    }
    avals.v48 = v48x/100; 
    avals.v24 = v24x/100;
    avals.v12 = v12x/100;
    
    return avals;
}

/*******************************************************************************
 getADCamps
*******************************************************************************/
struct adcValues getADCamps(void){
    
    unsigned int i48x = 0; 
    unsigned int i24x = 0;
    unsigned int i12x = 0;
    
    struct adcValues avals;
    
    for(unsigned int i=0;i<100;i++){
        i48x = i48x + IIN48.read_u16();
        i24x = i24x + IIN24.read_u16();
        i12x = i12x = IIN12.read_u16();
    }
    avals.i48 = i48x/100; 
    avals.i24 = i24x/100;
    avals.i12 = i12x/100;
    
    return avals;
}

/*******************************************************************************
 calcDisplayValues
*******************************************************************************/
struct displayValues calcDisplayValues(struct adcValues avals){
    
    struct displayValues dvals;
    
    if(!raw){
        dvals.v48f = VOLTAGE_48_FACTOR*avals.v48;
        dvals.v24f = VOLTAGE_24_FACTOR*avals.v24;
        dvals.v12f = VOLTAGE_12_FACTOR*avals.v12;
        //dvals.i48f = ((avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR)-CURRENT_CONTROL_OFFSET;
        dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR;
        dvals.i24f = (avals.i24-CURRENT_24_OFFSET)/CURRENT_24_DIV_FACTOR;
        dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR;
    }else{
        //dvals.v48f = 1.0*avals.v48-CURRENT_CONTROL_OFFSET;
        dvals.v48f = 1.0*avals.v48;
        dvals.v24f = 1.0*avals.v24;
        dvals.v12f = 1.0*avals.v12;
        dvals.i48f = 1.0*avals.i48;
        dvals.i24f = 1.0*avals.i24;
        dvals.i12f = 1.0*avals.i12;
    }
    return dvals;
}