Treehouse Mbed Team / Mbed 2 deprecated APS_DCM1SL

Dependencies:   mbed

Committer:
mfwic
Date:
Tue Jan 15 01:32:33 2019 +0000
Revision:
22:2c37ac12746e
Parent:
21:fe0ea1860c9f
Child:
25:8bcc8bea0e31
Added hysteresis; Changed LUT to use v12*i12 instead of just i12.

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