Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
adc.cpp
00001 //------------------------------------------------------------------------------- 00002 // 00003 // Treehouse Designs Inc. 00004 // Colorado Springs, Colorado 00005 // 00006 // Copyright (c) 2016 by Treehouse Designs Inc. 00007 // Copyright (c) 2018 by Agility Power Systems Inc. 00008 // 00009 // This code is the property of Treehouse Designs, Inc. (Treehouse) and 00010 // Agility Power Systems Inc. (Agility) and may not be redistributed 00011 // in any form without prior written permission from 00012 // both copyright holders, Treehouse and Agility. 00013 // 00014 // The above copyright notice and this permission notice shall be included in 00015 // all copies or substantial portions of the Software. 00016 // 00017 // 00018 //------------------------------------------------------------------------------- 00019 // 00020 // REVISION HISTORY: 00021 // 00022 // $Author: $ 00023 // $Rev: $ 00024 // $Date: $ 00025 // $URL: $ 00026 // 00027 //------------------------------------------------------------------------------- 00028 00029 #include "mbed.h" 00030 #include "adc_defs.h" 00031 #include "adc.h" 00032 #include "all_io.h" 00033 00034 void initADC(void){ 00035 00036 //Auto-zero current values 00037 struct adcValues adcVals = getADCresults(); 00038 00039 CURRENT_48_OFFSET = adcVals.i48; 00040 CURRENT_24_OFFSET = adcVals.i24; 00041 CURRENT_12_OFFSET = adcVals.i12; 00042 00043 } 00044 00045 /******************************************************************************* 00046 getADCresults 00047 *******************************************************************************/ 00048 struct adcValues getADCresults(void){ 00049 00050 unsigned int v48x = 0; 00051 unsigned int v24x = 0; 00052 unsigned int v12x = 0; 00053 unsigned int i48x = 0; 00054 unsigned int i24x = 0; 00055 unsigned int i12x = 0; 00056 00057 struct adcValues avals; 00058 unsigned int loopCounter = LOOP_COUNTER; 00059 00060 for(unsigned int i=0;i<loopCounter;i++){ 00061 v48x = v48x + VIN48.read_u16(); 00062 i48x = i48x + IIN48.read_u16(); 00063 v24x = v24x + VIN24.read_u16(); 00064 i24x = i24x + IIN24.read_u16(); 00065 v12x = v12x + VIN12.read_u16(); 00066 i12x = i12x + IIN12.read_u16(); 00067 } 00068 avals.v48 = v48x/loopCounter; 00069 avals.v24 = v24x/loopCounter; 00070 avals.v12 = v12x/loopCounter; 00071 avals.i48 = i48x/loopCounter; 00072 avals.i24 = i24x/loopCounter; 00073 avals.i12 = i12x/loopCounter; 00074 00075 return avals; 00076 } 00077 00078 /******************************************************************************* 00079 getADCvolts 00080 *******************************************************************************/ 00081 struct adcValues getADCvolts(void){ 00082 00083 unsigned int v48x = 0; 00084 unsigned int v24x = 0; 00085 unsigned int v12x = 0; 00086 00087 struct adcValues avals; 00088 00089 for(unsigned int i=0;i<100;i++){ 00090 v48x = v48x + VIN48.read_u16(); 00091 v24x = v24x + VIN24.read_u16(); 00092 v12x = v12x + VIN12.read_u16(); 00093 } 00094 avals.v48 = v48x/100; 00095 avals.v24 = v24x/100; 00096 avals.v12 = v12x/100; 00097 00098 return avals; 00099 } 00100 00101 /******************************************************************************* 00102 getADCamps 00103 *******************************************************************************/ 00104 struct adcValues getADCamps(void){ 00105 00106 unsigned int i48x = 0; 00107 unsigned int i24x = 0; 00108 unsigned int i12x = 0; 00109 00110 struct adcValues avals; 00111 00112 for(unsigned int i=0;i<100;i++){ 00113 i48x = i48x + IIN48.read_u16(); 00114 i24x = i24x + IIN24.read_u16(); 00115 i12x = i12x + IIN12.read_u16(); 00116 } 00117 avals.i48 = i48x/100; 00118 avals.i24 = i24x/100; 00119 avals.i12 = i12x/100; 00120 00121 return avals; 00122 } 00123 00124 /******************************************************************************* 00125 calcDisplayValues 00126 *******************************************************************************/ 00127 struct displayValues calcDisplayValues(struct adcValues avals){ 00128 00129 struct displayValues dvals; 00130 00131 if(!raw){ 00132 dvals.v48f = VOLTAGE_48_FACTOR*avals.v48; 00133 dvals.v24f = VOLTAGE_24_FACTOR*avals.v24; 00134 dvals.v12f = VOLTAGE_12_FACTOR*avals.v12; 00135 //dvals.i48f = ((avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR)-CURRENT_CONTROL_OFFSET; 00136 00137 // The adc results are linear above CURRENT_48_DIV_THRESH5. Only apply a linear correction to it above that threshold. 00138 // The multiple correction factors below CURRENT_48_DIV_THRESH5 linearize the curve below CURRENT_48_DIV_THRESH5. 00139 if(abs((avals.i48-CURRENT_48_OFFSET)) > CURRENT_48_DIV_THRESH5){ 00140 dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR5; 00141 }else if(abs((avals.i48-CURRENT_48_OFFSET)) > CURRENT_48_DIV_THRESH4){ 00142 dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR4; 00143 }else if(abs((avals.i48-CURRENT_48_OFFSET)) > CURRENT_48_DIV_THRESH3){ 00144 dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR3; 00145 }else if(abs((avals.i48-CURRENT_48_OFFSET)) > CURRENT_48_DIV_THRESH2){ 00146 dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR2; 00147 }else if(abs((avals.i48-CURRENT_48_OFFSET)) > CURRENT_48_DIV_THRESH1){ 00148 dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR1; 00149 }else{ 00150 dvals.i48f = (avals.i48-CURRENT_48_OFFSET)/CURRENT_48_DIV_FACTOR0; 00151 } 00152 00153 dvals.i24f = (avals.i24-CURRENT_24_OFFSET)/CURRENT_24_DIV_FACTOR; 00154 00155 // The adc results are linear above CURRENT_12_DIV_THRESH5. Only apply a linear correction to it above that threshold. 00156 // The multiple correction factors below CURRENT_12_DIV_THRESH5 linearize the curve below CURRENT_12_DIV_THRESH5. 00157 if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH5){ 00158 dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR5; 00159 }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH4){ 00160 dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR4; 00161 }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH3){ 00162 dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR3; 00163 }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH2){ 00164 dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR2; 00165 }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH1){ 00166 dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR1; 00167 }else{ 00168 dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR0; 00169 } 00170 }else{ 00171 //dvals.v48f = 1.0*avals.v48-CURRENT_CONTROL_OFFSET; 00172 dvals.v48f = 1.0*avals.v48; 00173 dvals.v24f = 1.0*avals.v24; 00174 dvals.v12f = 1.0*avals.v12; 00175 dvals.i48f = 1.0*avals.i48; 00176 dvals.i24f = 1.0*avals.i24; 00177 dvals.i12f = 1.0*avals.i12; 00178 } 00179 return dvals; 00180 }
Generated on Sat Aug 27 2022 12:36:03 by
1.7.2