Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Tue Jan 08 22:05:53 2019 +0000
Revision:
20:5de24e4ae1c5
Parent:
17:454afe56eedb
Child:
21:fe0ea1860c9f
Corrected BusOut order

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