Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Sun Jan 20 16:56:52 2019 +0000
Revision:
25:8bcc8bea0e31
Parent:
22:2c37ac12746e
Child:
26:55e8e1a9cc84
Added multiple LUTs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfwic 0:44a3005d4f20 1 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 2 //
mfwic 0:44a3005d4f20 3 // Treehouse Designs Inc.
mfwic 0:44a3005d4f20 4 // Colorado Springs, Colorado
mfwic 0:44a3005d4f20 5 //
mfwic 0:44a3005d4f20 6 // Copyright (c) 2016 by Treehouse Designs Inc.
mfwic 0:44a3005d4f20 7 // Copyright (c) 2018 by Agility Power Systems Inc.
mfwic 0:44a3005d4f20 8 //
mfwic 0:44a3005d4f20 9 // This code is the property of Treehouse Designs, Inc. (Treehouse) and
mfwic 0:44a3005d4f20 10 // Agility Power Systems Inc. (Agility) and may not be redistributed
mfwic 0:44a3005d4f20 11 // in any form without prior written permission from
mfwic 0:44a3005d4f20 12 // both copyright holders, Treehouse and Agility.
mfwic 0:44a3005d4f20 13 //
mfwic 0:44a3005d4f20 14 // The above copyright notice and this permission notice shall be included in
mfwic 0:44a3005d4f20 15 // all copies or substantial portions of the Software.
mfwic 0:44a3005d4f20 16 //
mfwic 0:44a3005d4f20 17 //
mfwic 0:44a3005d4f20 18 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 19 //
mfwic 0:44a3005d4f20 20 // REVISION HISTORY:
mfwic 0:44a3005d4f20 21 //
mfwic 0:44a3005d4f20 22 // $Author: $
mfwic 0:44a3005d4f20 23 // $Rev: $
mfwic 0:44a3005d4f20 24 // $Date: $
mfwic 0:44a3005d4f20 25 // $URL: $
mfwic 0:44a3005d4f20 26 //
mfwic 0:44a3005d4f20 27 //-------------------------------------------------------------------------------
mfwic 0:44a3005d4f20 28
mfwic 0:44a3005d4f20 29 #include "mbed.h"
mfwic 0:44a3005d4f20 30 #include "globals.h"
mfwic 11:01dcfb29fbc4 31 #include "parameters.h"
mfwic 0:44a3005d4f20 32 #include "boards.h"
mfwic 11:01dcfb29fbc4 33 #include "lut.h"
mfwic 0:44a3005d4f20 34 #include "all_io.h"
mfwic 0:44a3005d4f20 35 #include "serial.h"
mfwic 0:44a3005d4f20 36 #include "stdio.h"
mfwic 0:44a3005d4f20 37
mfwic 0:44a3005d4f20 38 unsigned int boardEnableBits;
mfwic 0:44a3005d4f20 39
mfwic 15:aed8f326c949 40 void initBoards(struct adcValues adcVals){
mfwic 22:2c37ac12746e 41 /*
mfwic 15:aed8f326c949 42 sprintf(strbuf, "\r\nV48_HI=%d\r\n", V48_HI);
mfwic 15:aed8f326c949 43 sendSerial(strbuf);
mfwic 15:aed8f326c949 44 sprintf(strbuf, "\r\nV48=%d\r\n", adcVals.v48);
mfwic 15:aed8f326c949 45 sendSerial(strbuf);
mfwic 15:aed8f326c949 46 sprintf(strbuf, "\r\nV48_LO=%d\r\n", V48_LO);
mfwic 15:aed8f326c949 47 sendSerial(strbuf);
mfwic 22:2c37ac12746e 48 */
mfwic 21:fe0ea1860c9f 49 en_out = 32;
mfwic 21:fe0ea1860c9f 50 setBoardEnables(8191);
mfwic 21:fe0ea1860c9f 51
mfwic 15:aed8f326c949 52 }
mfwic 15:aed8f326c949 53
mfwic 6:39442d493098 54 /*******************************************************************************
mfwic 6:39442d493098 55 delay
mfwic 6:39442d493098 56 *******************************************************************************/
mfwic 0:44a3005d4f20 57 void delay(long ticks)
mfwic 0:44a3005d4f20 58 {
mfwic 0:44a3005d4f20 59 long i;
mfwic 0:44a3005d4f20 60 for(i=0;i<ticks;i++);
mfwic 0:44a3005d4f20 61 }
mfwic 0:44a3005d4f20 62
mfwic 6:39442d493098 63 /*******************************************************************************
mfwic 25:8bcc8bea0e31 64 setBoardControls
mfwic 25:8bcc8bea0e31 65 *******************************************************************************/
mfwic 25:8bcc8bea0e31 66 void setBoardControls(unsigned int tCode)
mfwic 25:8bcc8bea0e31 67 {
mfwic 25:8bcc8bea0e31 68 for(unsigned int b=0; b<max_boards; b++){
mfwic 25:8bcc8bea0e31 69 en_out = bCodeRow[b];
mfwic 25:8bcc8bea0e31 70 wr_out = 0;
mfwic 25:8bcc8bea0e31 71 wr_out = 1<<b;
mfwic 25:8bcc8bea0e31 72 }
mfwic 25:8bcc8bea0e31 73 }
mfwic 25:8bcc8bea0e31 74
mfwic 25:8bcc8bea0e31 75 /*******************************************************************************
mfwic 6:39442d493098 76 setBoardEnables
mfwic 6:39442d493098 77 *******************************************************************************/
mfwic 6:39442d493098 78 unsigned int setBoardEnables(unsigned int tCode)
mfwic 0:44a3005d4f20 79 {
mfwic 25:8bcc8bea0e31 80 wr_out = tCode;
mfwic 25:8bcc8bea0e31 81 unsigned int en_out_save = en_out;
mfwic 25:8bcc8bea0e31 82 en_out = 0;
mfwic 25:8bcc8bea0e31 83 wr_out = ~tCode;
mfwic 25:8bcc8bea0e31 84 en_out = en_out_save;
mfwic 25:8bcc8bea0e31 85 return tCode;
mfwic 0:44a3005d4f20 86 }
mfwic 0:44a3005d4f20 87
mfwic 6:39442d493098 88 /*******************************************************************************
mfwic 6:39442d493098 89 setBoardWeights
mfwic 6:39442d493098 90 *******************************************************************************/
mfwic 6:39442d493098 91 unsigned int setBoardWeights(unsigned int bCode)
mfwic 0:44a3005d4f20 92 {
mfwic 25:8bcc8bea0e31 93 en_out = bCode;
mfwic 0:44a3005d4f20 94 toggleLatchSignal();
mfwic 6:39442d493098 95 return bCode;
mfwic 0:44a3005d4f20 96 }
mfwic 0:44a3005d4f20 97
mfwic 6:39442d493098 98 /*******************************************************************************
mfwic 6:39442d493098 99 toggleLatchSignal
mfwic 6:39442d493098 100 *******************************************************************************/
mfwic 0:44a3005d4f20 101 void toggleLatchSignal(void){
mfwic 0:44a3005d4f20 102 extchlat = OFF;
mfwic 0:44a3005d4f20 103 extchlat = ON;
mfwic 0:44a3005d4f20 104 extchlat = OFF;
mfwic 0:44a3005d4f20 105 }
mfwic 1:9f8583ba2431 106
mfwic 1:9f8583ba2431 107 /************************************************************
mfwic 1:9f8583ba2431 108 * Routine: checkRange
mfwic 1:9f8583ba2431 109 * Input: setval
mfwic 1:9f8583ba2431 110 * limlo -- low limit
mfwic 1:9f8583ba2431 111 * limhi -- high limit
mfwic 1:9f8583ba2431 112 * Returns: 1 if entry validates and is written
mfwic 1:9f8583ba2431 113 * 0 if entry fails
mfwic 1:9f8583ba2431 114 * -1 if it slips past
mfwic 1:9f8583ba2431 115 * Description:
mfwic 1:9f8583ba2431 116 * Checks if setvalue is between the given limits.
mfwic 1:9f8583ba2431 117 *
mfwic 1:9f8583ba2431 118 **************************************************************/
mfwic 1:9f8583ba2431 119 int checkRange(int setvalue, int limlo, int limhi)
mfwic 1:9f8583ba2431 120 {
mfwic 6:39442d493098 121 if ((setvalue >= limlo) && (setvalue <= limhi)){
mfwic 1:9f8583ba2431 122 return 1;
mfwic 6:39442d493098 123 }else{
mfwic 12:fd1fd1857628 124 //showRangeError(0, 0, setvalue);
mfwic 1:9f8583ba2431 125 return 0;
mfwic 1:9f8583ba2431 126 }
mfwic 4:db38665c3727 127 }
mfwic 4:db38665c3727 128
mfwic 6:39442d493098 129 /*******************************************************************************
mfwic 6:39442d493098 130 startConverter
mfwic 6:39442d493098 131 *******************************************************************************/
mfwic 4:db38665c3727 132 void startConverter(unsigned int reg)
mfwic 4:db38665c3727 133 {
mfwic 15:aed8f326c949 134 if(!running){
mfwic 15:aed8f326c949 135 running = TRUE;
mfwic 15:aed8f326c949 136 // Fire in the hole!
mfwic 15:aed8f326c949 137 wr_out_code = setBoardEnables(reg);
mfwic 6:39442d493098 138
mfwic 15:aed8f326c949 139 sprintf(strbuf, "\r\nConverter started");
mfwic 15:aed8f326c949 140 sendSerial(strbuf);
mfwic 15:aed8f326c949 141 }
mfwic 4:db38665c3727 142 }
mfwic 4:db38665c3727 143
mfwic 6:39442d493098 144 /*******************************************************************************
mfwic 6:39442d493098 145 stopConverter - stop the converter and set outputs to 0
mfwic 6:39442d493098 146 *******************************************************************************/
mfwic 4:db38665c3727 147 void stopConverter(void)
mfwic 4:db38665c3727 148 {
mfwic 15:aed8f326c949 149 if(running){
mfwic 21:fe0ea1860c9f 150 en_out = 32;
mfwic 21:fe0ea1860c9f 151 wr_out_code = setBoardEnables(ALLON);
mfwic 15:aed8f326c949 152 running = FALSE;
mfwic 15:aed8f326c949 153 sprintf(strbuf, "\r\nConverter stopped");
mfwic 15:aed8f326c949 154 sendSerial(strbuf);
mfwic 15:aed8f326c949 155 }
mfwic 4:db38665c3727 156 }
mfwic 4:db38665c3727 157
mfwic 6:39442d493098 158 /*******************************************************************************
mfwic 15:aed8f326c949 159 checkLevels
mfwic 15:aed8f326c949 160 *******************************************************************************/
mfwic 15:aed8f326c949 161 struct statusValues checkLevels(struct adcValues adcVals){
mfwic 15:aed8f326c949 162
mfwic 15:aed8f326c949 163 struct statusValues statVals;
mfwic 15:aed8f326c949 164
mfwic 15:aed8f326c949 165 // Check 48V levels
mfwic 15:aed8f326c949 166 if(adcVals.v48 > V48_HI){
mfwic 17:454afe56eedb 167 //if(buck){
mfwic 17:454afe56eedb 168 // stopConverter();
mfwic 17:454afe56eedb 169 //}
mfwic 15:aed8f326c949 170 statVals.V48_IS_HI = TRUE;
mfwic 15:aed8f326c949 171 statVals.V48_IS_LO = FALSE;
mfwic 15:aed8f326c949 172 }else if(adcVals.v48 < V48_LO){
mfwic 17:454afe56eedb 173 //if(buck){
mfwic 17:454afe56eedb 174 // stopConverter();
mfwic 17:454afe56eedb 175 //}
mfwic 15:aed8f326c949 176 statVals.V48_IS_HI = FALSE;
mfwic 15:aed8f326c949 177 statVals.V48_IS_LO = TRUE;
mfwic 15:aed8f326c949 178 }else{
mfwic 15:aed8f326c949 179 statVals.V48_IS_HI = FALSE;
mfwic 15:aed8f326c949 180 statVals.V48_IS_LO = FALSE;
mfwic 15:aed8f326c949 181 }
mfwic 15:aed8f326c949 182
mfwic 15:aed8f326c949 183 // Check 24V levels
mfwic 15:aed8f326c949 184 if(adcVals.v24 > V24_HI){
mfwic 15:aed8f326c949 185 statVals.V24_IS_HI = TRUE;
mfwic 15:aed8f326c949 186 statVals.V24_IS_LO = FALSE;
mfwic 15:aed8f326c949 187 }else if(adcVals.v24 < V24_LO){
mfwic 15:aed8f326c949 188 statVals.V24_IS_HI = FALSE;
mfwic 15:aed8f326c949 189 statVals.V24_IS_LO = TRUE;
mfwic 15:aed8f326c949 190 }else{
mfwic 15:aed8f326c949 191 statVals.V24_IS_HI = FALSE;
mfwic 15:aed8f326c949 192 statVals.V24_IS_LO = FALSE;
mfwic 15:aed8f326c949 193 }
mfwic 15:aed8f326c949 194
mfwic 15:aed8f326c949 195 // Check 12V levels
mfwic 15:aed8f326c949 196 if(adcVals.v12 > V12_HI){
mfwic 17:454afe56eedb 197 //if(!buck){
mfwic 17:454afe56eedb 198 // stopConverter();
mfwic 17:454afe56eedb 199 //}
mfwic 15:aed8f326c949 200 statVals.V12_IS_HI = TRUE;
mfwic 15:aed8f326c949 201 statVals.V12_IS_LO = FALSE;
mfwic 15:aed8f326c949 202 }else if(adcVals.v12 < V12_LO){
mfwic 17:454afe56eedb 203 //if(!buck){
mfwic 17:454afe56eedb 204 // stopConverter();
mfwic 17:454afe56eedb 205 //}
mfwic 15:aed8f326c949 206 statVals.V12_IS_HI = FALSE;
mfwic 15:aed8f326c949 207 statVals.V12_IS_LO = TRUE;
mfwic 15:aed8f326c949 208 }else{
mfwic 15:aed8f326c949 209 statVals.V12_IS_HI = FALSE;
mfwic 15:aed8f326c949 210 statVals.V12_IS_LO = FALSE;
mfwic 15:aed8f326c949 211 }
mfwic 15:aed8f326c949 212 return statVals;
mfwic 15:aed8f326c949 213 }
mfwic 15:aed8f326c949 214
mfwic 15:aed8f326c949 215
mfwic 15:aed8f326c949 216 /*******************************************************************************
mfwic 6:39442d493098 217 updateControls
mfwic 6:39442d493098 218 *******************************************************************************/
mfwic 6:39442d493098 219 void updateControls(unsigned short ref){
mfwic 9:816b9a4e4f21 220
mfwic 25:8bcc8bea0e31 221 unsigned int tBuf = getLUT_thermCode(ref);
mfwic 25:8bcc8bea0e31 222 unsigned int bBuf = getLUT_binCode(ref);
mfwic 25:8bcc8bea0e31 223 if(max_boards <= 3){
mfwic 25:8bcc8bea0e31 224 getLUT_binCodeArray(ref);
mfwic 25:8bcc8bea0e31 225 setBoardControls(tBuf);
mfwic 25:8bcc8bea0e31 226 }else{
mfwic 25:8bcc8bea0e31 227 wr_out_code = setBoardEnables(tBuf);
mfwic 25:8bcc8bea0e31 228 en_out_code = setBoardWeights(bBuf);
mfwic 25:8bcc8bea0e31 229 }
mfwic 9:816b9a4e4f21 230 }
mfwic 9:816b9a4e4f21 231
mfwic 9:816b9a4e4f21 232 void XupdateControls(unsigned short ref){
mfwic 11:01dcfb29fbc4 233
mfwic 9:816b9a4e4f21 234 sprintf(strbuf, "refr=%d\r\n", ref);
mfwic 9:816b9a4e4f21 235 sendSerial(strbuf);
mfwic 9:816b9a4e4f21 236
mfwic 9:816b9a4e4f21 237 ref = ref/64;
mfwic 9:816b9a4e4f21 238
mfwic 9:816b9a4e4f21 239 sprintf(strbuf, "refc=%d\r\n", ref);
mfwic 9:816b9a4e4f21 240 sendSerial(strbuf);
mfwic 9:816b9a4e4f21 241
mfwic 9:816b9a4e4f21 242 sendSerial("enter updateControls\r\n");
mfwic 9:816b9a4e4f21 243 unsigned int cBuf = getLUT_thermCode(ref);
mfwic 9:816b9a4e4f21 244 sendSerial("cBuf1 updateControls\r\n");
mfwic 9:816b9a4e4f21 245 wr_out_code = setBoardEnables(cBuf);
mfwic 9:816b9a4e4f21 246 sendSerial("wr_out_code updateControls\r\n");
mfwic 9:816b9a4e4f21 247
mfwic 9:816b9a4e4f21 248 cBuf = getLUT_binCode(ref);
mfwic 9:816b9a4e4f21 249 sendSerial("cBuf2 updateControls\r\n");
mfwic 9:816b9a4e4f21 250 en_out_code = setBoardWeights(cBuf);
mfwic 9:816b9a4e4f21 251 sendSerial("en_out_code updateControls\r\n");
mfwic 9:816b9a4e4f21 252
mfwic 9:816b9a4e4f21 253 wait(0.5);
mfwic 1:9f8583ba2431 254 }