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
src/adc.cpp@52:2b3b194144da, 2019-03-14 (annotated)
- Committer:
- mfwic
- Date:
- Thu Mar 14 00:21:35 2019 +0000
- Revision:
- 52:2b3b194144da
- Parent:
- 42:3ae73b61f657
Added ifdef IS_MASTER & IS_SLAVE; Corrected Slave_RXstr; Added call to Slave_RXstr in loop.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mfwic | 42:3ae73b61f657 | 1 | //------------------------------------------------------------------------------- |
| mfwic | 42:3ae73b61f657 | 2 | // |
| mfwic | 42:3ae73b61f657 | 3 | // Treehouse Designs Inc. |
| mfwic | 42:3ae73b61f657 | 4 | // Colorado Springs, Colorado |
| mfwic | 42:3ae73b61f657 | 5 | // |
| mfwic | 42:3ae73b61f657 | 6 | // Copyright (c) 2016 by Treehouse Designs Inc. |
| mfwic | 42:3ae73b61f657 | 7 | // Copyright (c) 2018 by Agility Power Systems Inc. |
| mfwic | 42:3ae73b61f657 | 8 | // |
| mfwic | 42:3ae73b61f657 | 9 | // This code is the property of Treehouse Designs, Inc. (Treehouse) and |
| mfwic | 42:3ae73b61f657 | 10 | // Agility Power Systems Inc. (Agility) and may not be redistributed |
| mfwic | 42:3ae73b61f657 | 11 | // in any form without prior written permission from |
| mfwic | 42:3ae73b61f657 | 12 | // both copyright holders, Treehouse and Agility. |
| mfwic | 42:3ae73b61f657 | 13 | // |
| mfwic | 42:3ae73b61f657 | 14 | // The above copyright notice and this permission notice shall be included in |
| mfwic | 42:3ae73b61f657 | 15 | // all copies or substantial portions of the Software. |
| mfwic | 42:3ae73b61f657 | 16 | // |
| mfwic | 42:3ae73b61f657 | 17 | // |
| mfwic | 42:3ae73b61f657 | 18 | //------------------------------------------------------------------------------- |
| mfwic | 42:3ae73b61f657 | 19 | // |
| mfwic | 42:3ae73b61f657 | 20 | // REVISION HISTORY: |
| mfwic | 42:3ae73b61f657 | 21 | // |
| mfwic | 42:3ae73b61f657 | 22 | // $Author: $ |
| mfwic | 42:3ae73b61f657 | 23 | // $Rev: $ |
| mfwic | 42:3ae73b61f657 | 24 | // $Date: $ |
| mfwic | 42:3ae73b61f657 | 25 | // $URL: $ |
| mfwic | 42:3ae73b61f657 | 26 | // |
| mfwic | 42:3ae73b61f657 | 27 | //------------------------------------------------------------------------------- |
| mfwic | 42:3ae73b61f657 | 28 | |
| mfwic | 42:3ae73b61f657 | 29 | #include "mbed.h" |
| mfwic | 42:3ae73b61f657 | 30 | #include "adc_defs.h" |
| mfwic | 42:3ae73b61f657 | 31 | #include "adc.h" |
| mfwic | 42:3ae73b61f657 | 32 | #include "all_io.h" |
| mfwic | 42:3ae73b61f657 | 33 | |
| mfwic | 52:2b3b194144da | 34 | AnalogIn A_TEMP(ADC_TEMP); |
| mfwic | 52:2b3b194144da | 35 | |
| mfwic | 42:3ae73b61f657 | 36 | void initADC(void){ |
| mfwic | 42:3ae73b61f657 | 37 | |
| mfwic | 42:3ae73b61f657 | 38 | //Auto-zero current values |
| mfwic | 42:3ae73b61f657 | 39 | struct adcValues adcVals = getADCresults(); |
| mfwic | 42:3ae73b61f657 | 40 | |
| mfwic | 42:3ae73b61f657 | 41 | CURRENT_12_OFFSET = adcVals.i12; |
| mfwic | 42:3ae73b61f657 | 42 | |
| mfwic | 52:2b3b194144da | 43 | |
| mfwic | 52:2b3b194144da | 44 | } |
| mfwic | 52:2b3b194144da | 45 | |
| mfwic | 52:2b3b194144da | 46 | /******************************************************************************* |
| mfwic | 52:2b3b194144da | 47 | getADCresults |
| mfwic | 52:2b3b194144da | 48 | *******************************************************************************/ |
| mfwic | 52:2b3b194144da | 49 | unsigned int getTEMPERATUREresults(void){ |
| mfwic | 52:2b3b194144da | 50 | unsigned int aTemp = A_TEMP.read_u16(); |
| mfwic | 52:2b3b194144da | 51 | return aTemp; |
| mfwic | 42:3ae73b61f657 | 52 | } |
| mfwic | 42:3ae73b61f657 | 53 | |
| mfwic | 42:3ae73b61f657 | 54 | /******************************************************************************* |
| mfwic | 42:3ae73b61f657 | 55 | getADCresults |
| mfwic | 42:3ae73b61f657 | 56 | *******************************************************************************/ |
| mfwic | 42:3ae73b61f657 | 57 | struct adcValues getADCresults(void){ |
| mfwic | 42:3ae73b61f657 | 58 | |
| mfwic | 42:3ae73b61f657 | 59 | unsigned int v48x = 0; |
| mfwic | 42:3ae73b61f657 | 60 | unsigned int v12x = 0; |
| mfwic | 42:3ae73b61f657 | 61 | unsigned int i12x = 0; |
| mfwic | 42:3ae73b61f657 | 62 | |
| mfwic | 42:3ae73b61f657 | 63 | struct adcValues avals; |
| mfwic | 42:3ae73b61f657 | 64 | unsigned int loopCounter = LOOP_COUNTER; |
| mfwic | 42:3ae73b61f657 | 65 | |
| mfwic | 42:3ae73b61f657 | 66 | for(unsigned int i=0;i<loopCounter;i++){ |
| mfwic | 42:3ae73b61f657 | 67 | v48x = v48x + VIN48.read_u16(); |
| mfwic | 42:3ae73b61f657 | 68 | v12x = v12x + VIN12.read_u16(); |
| mfwic | 42:3ae73b61f657 | 69 | i12x = i12x + IIN12.read_u16(); |
| mfwic | 42:3ae73b61f657 | 70 | } |
| mfwic | 42:3ae73b61f657 | 71 | avals.v48 = v48x/loopCounter; |
| mfwic | 42:3ae73b61f657 | 72 | avals.v12 = v12x/loopCounter; |
| mfwic | 42:3ae73b61f657 | 73 | avals.i12 = i12x/loopCounter; |
| mfwic | 42:3ae73b61f657 | 74 | |
| mfwic | 42:3ae73b61f657 | 75 | return avals; |
| mfwic | 42:3ae73b61f657 | 76 | } |
| mfwic | 42:3ae73b61f657 | 77 | |
| mfwic | 42:3ae73b61f657 | 78 | /******************************************************************************* |
| mfwic | 42:3ae73b61f657 | 79 | getADCvolts |
| mfwic | 42:3ae73b61f657 | 80 | *******************************************************************************/ |
| mfwic | 42:3ae73b61f657 | 81 | struct adcValues getADCvolts(void){ |
| mfwic | 42:3ae73b61f657 | 82 | |
| mfwic | 42:3ae73b61f657 | 83 | unsigned int v48x = 0; |
| mfwic | 42:3ae73b61f657 | 84 | unsigned int v12x = 0; |
| mfwic | 42:3ae73b61f657 | 85 | |
| mfwic | 42:3ae73b61f657 | 86 | struct adcValues avals; |
| mfwic | 42:3ae73b61f657 | 87 | |
| mfwic | 42:3ae73b61f657 | 88 | for(unsigned int i=0;i<100;i++){ |
| mfwic | 42:3ae73b61f657 | 89 | v48x = v48x + VIN48.read_u16(); |
| mfwic | 42:3ae73b61f657 | 90 | v12x = v12x + VIN12.read_u16(); |
| mfwic | 42:3ae73b61f657 | 91 | } |
| mfwic | 42:3ae73b61f657 | 92 | avals.v48 = v48x/100; |
| mfwic | 42:3ae73b61f657 | 93 | avals.v12 = v12x/100; |
| mfwic | 42:3ae73b61f657 | 94 | |
| mfwic | 42:3ae73b61f657 | 95 | return avals; |
| mfwic | 42:3ae73b61f657 | 96 | } |
| mfwic | 42:3ae73b61f657 | 97 | |
| mfwic | 42:3ae73b61f657 | 98 | /******************************************************************************* |
| mfwic | 42:3ae73b61f657 | 99 | getADCamps |
| mfwic | 42:3ae73b61f657 | 100 | *******************************************************************************/ |
| mfwic | 42:3ae73b61f657 | 101 | struct adcValues getADCamps(void){ |
| mfwic | 42:3ae73b61f657 | 102 | |
| mfwic | 42:3ae73b61f657 | 103 | unsigned int i12x = 0; |
| mfwic | 42:3ae73b61f657 | 104 | |
| mfwic | 42:3ae73b61f657 | 105 | struct adcValues avals; |
| mfwic | 42:3ae73b61f657 | 106 | |
| mfwic | 42:3ae73b61f657 | 107 | for(unsigned int i=0;i<100;i++){ |
| mfwic | 42:3ae73b61f657 | 108 | i12x = i12x + IIN12.read_u16(); |
| mfwic | 42:3ae73b61f657 | 109 | } |
| mfwic | 42:3ae73b61f657 | 110 | avals.i12 = i12x/100; |
| mfwic | 42:3ae73b61f657 | 111 | |
| mfwic | 42:3ae73b61f657 | 112 | return avals; |
| mfwic | 42:3ae73b61f657 | 113 | } |
| mfwic | 42:3ae73b61f657 | 114 | |
| mfwic | 42:3ae73b61f657 | 115 | /******************************************************************************* |
| mfwic | 42:3ae73b61f657 | 116 | calcDisplayValues |
| mfwic | 42:3ae73b61f657 | 117 | *******************************************************************************/ |
| mfwic | 42:3ae73b61f657 | 118 | struct displayValues calcDisplayValues(struct adcValues avals){ |
| mfwic | 42:3ae73b61f657 | 119 | |
| mfwic | 42:3ae73b61f657 | 120 | struct displayValues dvals; |
| mfwic | 42:3ae73b61f657 | 121 | |
| mfwic | 42:3ae73b61f657 | 122 | if(!raw){ |
| mfwic | 42:3ae73b61f657 | 123 | dvals.v48f = VOLTAGE_48_FACTOR*avals.v48; |
| mfwic | 42:3ae73b61f657 | 124 | dvals.v12f = VOLTAGE_12_FACTOR*avals.v12; |
| mfwic | 42:3ae73b61f657 | 125 | |
| mfwic | 42:3ae73b61f657 | 126 | // The adc results are linear above CURRENT_12_DIV_THRESH5. Only apply a linear correction to it above that threshold. |
| mfwic | 42:3ae73b61f657 | 127 | // The multiple correction factors below CURRENT_12_DIV_THRESH5 linearize the curve below CURRENT_12_DIV_THRESH5. |
| mfwic | 42:3ae73b61f657 | 128 | if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH5){ |
| mfwic | 42:3ae73b61f657 | 129 | dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR5; |
| mfwic | 42:3ae73b61f657 | 130 | }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH4){ |
| mfwic | 42:3ae73b61f657 | 131 | dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR4; |
| mfwic | 42:3ae73b61f657 | 132 | }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH3){ |
| mfwic | 42:3ae73b61f657 | 133 | dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR3; |
| mfwic | 42:3ae73b61f657 | 134 | }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH2){ |
| mfwic | 42:3ae73b61f657 | 135 | dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR2; |
| mfwic | 42:3ae73b61f657 | 136 | }else if(abs((avals.i12-CURRENT_12_OFFSET)) > CURRENT_12_DIV_THRESH1){ |
| mfwic | 42:3ae73b61f657 | 137 | dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR1; |
| mfwic | 42:3ae73b61f657 | 138 | }else{ |
| mfwic | 42:3ae73b61f657 | 139 | dvals.i12f = (avals.i12-CURRENT_12_OFFSET)/CURRENT_12_DIV_FACTOR0; |
| mfwic | 42:3ae73b61f657 | 140 | } |
| mfwic | 42:3ae73b61f657 | 141 | }else{ |
| mfwic | 42:3ae73b61f657 | 142 | dvals.v48f = 1.0*avals.v48; |
| mfwic | 42:3ae73b61f657 | 143 | dvals.v12f = 1.0*avals.v12; |
| mfwic | 42:3ae73b61f657 | 144 | dvals.i12f = 1.0*avals.i12; |
| mfwic | 42:3ae73b61f657 | 145 | } |
| mfwic | 42:3ae73b61f657 | 146 | return dvals; |
| mfwic | 42:3ae73b61f657 | 147 | } |