Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Fri Jan 11 21:45:55 2019 +0000
Revision:
21:fe0ea1860c9f
Parent:
20:5de24e4ae1c5
Child:
22:2c37ac12746e
v0p9 sent to APS

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 15:aed8f326c949 41 sprintf(strbuf, "\r\nV48_HI=%d\r\n", V48_HI);
mfwic 15:aed8f326c949 42 sendSerial(strbuf);
mfwic 15:aed8f326c949 43 sprintf(strbuf, "\r\nV48=%d\r\n", adcVals.v48);
mfwic 15:aed8f326c949 44 sendSerial(strbuf);
mfwic 15:aed8f326c949 45 sprintf(strbuf, "\r\nV48_LO=%d\r\n", V48_LO);
mfwic 15:aed8f326c949 46 sendSerial(strbuf);
mfwic 21:fe0ea1860c9f 47
mfwic 21:fe0ea1860c9f 48 en_out = 32;
mfwic 21:fe0ea1860c9f 49 setBoardEnables(8191);
mfwic 21:fe0ea1860c9f 50
mfwic 15:aed8f326c949 51 }
mfwic 15:aed8f326c949 52
mfwic 6:39442d493098 53 /*******************************************************************************
mfwic 6:39442d493098 54 delay
mfwic 6:39442d493098 55 *******************************************************************************/
mfwic 0:44a3005d4f20 56 void delay(long ticks)
mfwic 0:44a3005d4f20 57 {
mfwic 0:44a3005d4f20 58 long i;
mfwic 0:44a3005d4f20 59 for(i=0;i<ticks;i++);
mfwic 0:44a3005d4f20 60 }
mfwic 0:44a3005d4f20 61
mfwic 6:39442d493098 62 /*******************************************************************************
mfwic 6:39442d493098 63 setBoardEnables
mfwic 6:39442d493098 64 *******************************************************************************/
mfwic 6:39442d493098 65 unsigned int setBoardEnables(unsigned int tCode)
mfwic 0:44a3005d4f20 66 {
mfwic 20:5de24e4ae1c5 67 if(!all_on){
mfwic 21:fe0ea1860c9f 68 wr_out = tCode;
mfwic 21:fe0ea1860c9f 69 unsigned int en_out_save = en_out;
mfwic 21:fe0ea1860c9f 70 en_out = 0;
mfwic 20:5de24e4ae1c5 71 wr_out = ~tCode;
mfwic 21:fe0ea1860c9f 72 en_out = en_out_save;
mfwic 20:5de24e4ae1c5 73 }
mfwic 6:39442d493098 74 return tCode;
mfwic 0:44a3005d4f20 75 }
mfwic 0:44a3005d4f20 76
mfwic 6:39442d493098 77 /*******************************************************************************
mfwic 6:39442d493098 78 setBoardWeights
mfwic 6:39442d493098 79 *******************************************************************************/
mfwic 6:39442d493098 80 unsigned int setBoardWeights(unsigned int bCode)
mfwic 0:44a3005d4f20 81 {
mfwic 20:5de24e4ae1c5 82 if(!all_on){
mfwic 20:5de24e4ae1c5 83 en_out = bCode;
mfwic 20:5de24e4ae1c5 84 }
mfwic 0:44a3005d4f20 85 toggleLatchSignal();
mfwic 6:39442d493098 86 return bCode;
mfwic 0:44a3005d4f20 87 }
mfwic 0:44a3005d4f20 88
mfwic 6:39442d493098 89 /*******************************************************************************
mfwic 6:39442d493098 90 toggleLatchSignal
mfwic 6:39442d493098 91 *******************************************************************************/
mfwic 0:44a3005d4f20 92 void toggleLatchSignal(void){
mfwic 0:44a3005d4f20 93 extchlat = OFF;
mfwic 0:44a3005d4f20 94 extchlat = ON;
mfwic 0:44a3005d4f20 95 extchlat = OFF;
mfwic 0:44a3005d4f20 96 }
mfwic 1:9f8583ba2431 97
mfwic 1:9f8583ba2431 98 /************************************************************
mfwic 1:9f8583ba2431 99 * Routine: checkRange
mfwic 1:9f8583ba2431 100 * Input: setval
mfwic 1:9f8583ba2431 101 * limlo -- low limit
mfwic 1:9f8583ba2431 102 * limhi -- high limit
mfwic 1:9f8583ba2431 103 * Returns: 1 if entry validates and is written
mfwic 1:9f8583ba2431 104 * 0 if entry fails
mfwic 1:9f8583ba2431 105 * -1 if it slips past
mfwic 1:9f8583ba2431 106 * Description:
mfwic 1:9f8583ba2431 107 * Checks if setvalue is between the given limits.
mfwic 1:9f8583ba2431 108 *
mfwic 1:9f8583ba2431 109 **************************************************************/
mfwic 1:9f8583ba2431 110 int checkRange(int setvalue, int limlo, int limhi)
mfwic 1:9f8583ba2431 111 {
mfwic 6:39442d493098 112 if ((setvalue >= limlo) && (setvalue <= limhi)){
mfwic 1:9f8583ba2431 113 return 1;
mfwic 6:39442d493098 114 }else{
mfwic 12:fd1fd1857628 115 //showRangeError(0, 0, setvalue);
mfwic 1:9f8583ba2431 116 return 0;
mfwic 1:9f8583ba2431 117 }
mfwic 4:db38665c3727 118 }
mfwic 4:db38665c3727 119
mfwic 6:39442d493098 120 /*******************************************************************************
mfwic 6:39442d493098 121 startConverter
mfwic 6:39442d493098 122 *******************************************************************************/
mfwic 4:db38665c3727 123 void startConverter(unsigned int reg)
mfwic 4:db38665c3727 124 {
mfwic 15:aed8f326c949 125 if(!running){
mfwic 15:aed8f326c949 126 running = TRUE;
mfwic 15:aed8f326c949 127 // Fire in the hole!
mfwic 15:aed8f326c949 128 wr_out_code = setBoardEnables(reg);
mfwic 6:39442d493098 129
mfwic 15:aed8f326c949 130 sprintf(strbuf, "\r\nConverter started");
mfwic 15:aed8f326c949 131 sendSerial(strbuf);
mfwic 15:aed8f326c949 132 }
mfwic 4:db38665c3727 133 }
mfwic 4:db38665c3727 134
mfwic 6:39442d493098 135 /*******************************************************************************
mfwic 6:39442d493098 136 stopConverter - stop the converter and set outputs to 0
mfwic 6:39442d493098 137 *******************************************************************************/
mfwic 4:db38665c3727 138 void stopConverter(void)
mfwic 4:db38665c3727 139 {
mfwic 15:aed8f326c949 140 if(running){
mfwic 21:fe0ea1860c9f 141 en_out = 32;
mfwic 21:fe0ea1860c9f 142 wr_out_code = setBoardEnables(ALLON);
mfwic 15:aed8f326c949 143 running = FALSE;
mfwic 15:aed8f326c949 144 sprintf(strbuf, "\r\nConverter stopped");
mfwic 15:aed8f326c949 145 sendSerial(strbuf);
mfwic 15:aed8f326c949 146 }
mfwic 4:db38665c3727 147 }
mfwic 4:db38665c3727 148
mfwic 6:39442d493098 149 /*******************************************************************************
mfwic 15:aed8f326c949 150 checkLevels
mfwic 15:aed8f326c949 151 *******************************************************************************/
mfwic 15:aed8f326c949 152 struct statusValues checkLevels(struct adcValues adcVals){
mfwic 15:aed8f326c949 153
mfwic 15:aed8f326c949 154 struct statusValues statVals;
mfwic 15:aed8f326c949 155
mfwic 15:aed8f326c949 156 // Check 48V levels
mfwic 15:aed8f326c949 157 if(adcVals.v48 > V48_HI){
mfwic 17:454afe56eedb 158 //if(buck){
mfwic 17:454afe56eedb 159 // stopConverter();
mfwic 17:454afe56eedb 160 //}
mfwic 15:aed8f326c949 161 statVals.V48_IS_HI = TRUE;
mfwic 15:aed8f326c949 162 statVals.V48_IS_LO = FALSE;
mfwic 15:aed8f326c949 163 }else if(adcVals.v48 < V48_LO){
mfwic 17:454afe56eedb 164 //if(buck){
mfwic 17:454afe56eedb 165 // stopConverter();
mfwic 17:454afe56eedb 166 //}
mfwic 15:aed8f326c949 167 statVals.V48_IS_HI = FALSE;
mfwic 15:aed8f326c949 168 statVals.V48_IS_LO = TRUE;
mfwic 15:aed8f326c949 169 }else{
mfwic 15:aed8f326c949 170 statVals.V48_IS_HI = FALSE;
mfwic 15:aed8f326c949 171 statVals.V48_IS_LO = FALSE;
mfwic 15:aed8f326c949 172 }
mfwic 15:aed8f326c949 173
mfwic 15:aed8f326c949 174 // Check 24V levels
mfwic 15:aed8f326c949 175 if(adcVals.v24 > V24_HI){
mfwic 15:aed8f326c949 176 statVals.V24_IS_HI = TRUE;
mfwic 15:aed8f326c949 177 statVals.V24_IS_LO = FALSE;
mfwic 15:aed8f326c949 178 }else if(adcVals.v24 < V24_LO){
mfwic 15:aed8f326c949 179 statVals.V24_IS_HI = FALSE;
mfwic 15:aed8f326c949 180 statVals.V24_IS_LO = TRUE;
mfwic 15:aed8f326c949 181 }else{
mfwic 15:aed8f326c949 182 statVals.V24_IS_HI = FALSE;
mfwic 15:aed8f326c949 183 statVals.V24_IS_LO = FALSE;
mfwic 15:aed8f326c949 184 }
mfwic 15:aed8f326c949 185
mfwic 15:aed8f326c949 186 // Check 12V levels
mfwic 15:aed8f326c949 187 if(adcVals.v12 > V12_HI){
mfwic 17:454afe56eedb 188 //if(!buck){
mfwic 17:454afe56eedb 189 // stopConverter();
mfwic 17:454afe56eedb 190 //}
mfwic 15:aed8f326c949 191 statVals.V12_IS_HI = TRUE;
mfwic 15:aed8f326c949 192 statVals.V12_IS_LO = FALSE;
mfwic 15:aed8f326c949 193 }else if(adcVals.v12 < V12_LO){
mfwic 17:454afe56eedb 194 //if(!buck){
mfwic 17:454afe56eedb 195 // stopConverter();
mfwic 17:454afe56eedb 196 //}
mfwic 15:aed8f326c949 197 statVals.V12_IS_HI = FALSE;
mfwic 15:aed8f326c949 198 statVals.V12_IS_LO = TRUE;
mfwic 15:aed8f326c949 199 }else{
mfwic 15:aed8f326c949 200 statVals.V12_IS_HI = FALSE;
mfwic 15:aed8f326c949 201 statVals.V12_IS_LO = FALSE;
mfwic 15:aed8f326c949 202 }
mfwic 15:aed8f326c949 203 return statVals;
mfwic 15:aed8f326c949 204 }
mfwic 15:aed8f326c949 205
mfwic 15:aed8f326c949 206
mfwic 15:aed8f326c949 207 /*******************************************************************************
mfwic 6:39442d493098 208 updateControls
mfwic 6:39442d493098 209 *******************************************************************************/
mfwic 6:39442d493098 210 void updateControls(unsigned short ref){
mfwic 9:816b9a4e4f21 211
mfwic 6:39442d493098 212 unsigned int cBuf = getLUT_thermCode(ref);
mfwic 6:39442d493098 213 wr_out_code = setBoardEnables(cBuf);
mfwic 6:39442d493098 214
mfwic 6:39442d493098 215 cBuf = getLUT_binCode(ref);
mfwic 6:39442d493098 216 en_out_code = setBoardWeights(cBuf);
mfwic 9:816b9a4e4f21 217 }
mfwic 9:816b9a4e4f21 218
mfwic 9:816b9a4e4f21 219 void XupdateControls(unsigned short ref){
mfwic 11:01dcfb29fbc4 220
mfwic 9:816b9a4e4f21 221 sprintf(strbuf, "refr=%d\r\n", ref);
mfwic 9:816b9a4e4f21 222 sendSerial(strbuf);
mfwic 9:816b9a4e4f21 223
mfwic 9:816b9a4e4f21 224 ref = ref/64;
mfwic 9:816b9a4e4f21 225
mfwic 9:816b9a4e4f21 226 sprintf(strbuf, "refc=%d\r\n", ref);
mfwic 9:816b9a4e4f21 227 sendSerial(strbuf);
mfwic 9:816b9a4e4f21 228
mfwic 9:816b9a4e4f21 229 sendSerial("enter updateControls\r\n");
mfwic 9:816b9a4e4f21 230 unsigned int cBuf = getLUT_thermCode(ref);
mfwic 9:816b9a4e4f21 231 sendSerial("cBuf1 updateControls\r\n");
mfwic 9:816b9a4e4f21 232 wr_out_code = setBoardEnables(cBuf);
mfwic 9:816b9a4e4f21 233 sendSerial("wr_out_code updateControls\r\n");
mfwic 9:816b9a4e4f21 234
mfwic 9:816b9a4e4f21 235 cBuf = getLUT_binCode(ref);
mfwic 9:816b9a4e4f21 236 sendSerial("cBuf2 updateControls\r\n");
mfwic 9:816b9a4e4f21 237 en_out_code = setBoardWeights(cBuf);
mfwic 9:816b9a4e4f21 238 sendSerial("en_out_code updateControls\r\n");
mfwic 9:816b9a4e4f21 239
mfwic 9:816b9a4e4f21 240 wait(0.5);
mfwic 1:9f8583ba2431 241 }