00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include "mscbemb.h"
00049
00050 #include "powerswitch.h"
00051 #include "Devices/adc_internal.h"
00052
00053 #ifdef _ADT7486A_
00054 #include "Devices/ADT7486A_tsensor.h"
00055 #endif
00056
00057 char code node_name[] = "PWRSWITCH";
00058 char idata svn_rev_code[] = "$Rev: 743 $";
00059
00060
00061 unsigned char idata _n_sub_addr = 1;
00062 extern SYS_INFO sys_info;
00063
00064
00065 unsigned char xdata ADT7486A_addrArray[] = {0x48, 0x49, 0x4A};
00066
00067 #define VREF 3.293f
00068
00069 MSCB_INFO_VAR code vars[] = {
00070 1, UNIT_BYTE, 0, 0, 0, "Shutdown" , &user_data.error,
00071 1, UNIT_BYTE, 0, 0, 0, "Control" , &user_data.control,
00072 1, UNIT_BYTE, 0, 0, 0, "Status" , &user_data.status,
00073
00074 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "in Volt1" , &user_data.value[0],
00075 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "in Volt2" , &user_data.value[1],
00076 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "in Volt3" , &user_data.value[2],
00077
00078 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "outVolt1" , &user_data.value[3],
00079 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "outVolt2" , &user_data.value[4],
00080 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "outVolt3" , &user_data.value[5],
00081 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "outVolt4" , &user_data.value[6],
00082 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "outVolt5" , &user_data.value[7],
00083 4, UNIT_VOLT, 0, 0, MSCBF_FLOAT, "outVolt6" , &user_data.value[8],
00084
00085 4, UNIT_AMPERE, 0, 0, MSCBF_FLOAT, "outcur1" , &user_data.value[9],
00086 4, UNIT_AMPERE, 0, 0, MSCBF_FLOAT, "outCur2" , &user_data.value[10],
00087 4, UNIT_AMPERE, 0, 0, MSCBF_FLOAT, "outCur3" , &user_data.value[11],
00088 4, UNIT_AMPERE, 0, 0, MSCBF_FLOAT, "outCur4" , &user_data.value[12],
00089 4, UNIT_AMPERE, 0, 0, MSCBF_FLOAT, "outCur5" , &user_data.value[13],
00090 4, UNIT_AMPERE, 0, 0, MSCBF_FLOAT, "outCur6" , &user_data.value[14],
00091
00092 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "Temp1" , &user_data.temperature[0],
00093 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "Temp2" , &user_data.temperature[1],
00094 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "Temp3" , &user_data.temperature[2],
00095 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "Temp4" , &user_data.temperature[3],
00096 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "Temp5" , &user_data.temperature[4],
00097 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "Temp6" , &user_data.temperature[5],
00098
00099 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "inTemp1" , &user_data.localtemp[0],
00100 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "inTemp2" , &user_data.localtemp[1],
00101 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "inTemp3" , &user_data.localtemp[2],
00102
00103 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "LTemp1" , &user_data.limit[0],
00104 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "LTemp2" , &user_data.limit[1],
00105 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "LTemp3" , &user_data.limit[2],
00106 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "LTemp4" , &user_data.limit[3],
00107 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "LTemp5" , &user_data.limit[4],
00108 4, UNIT_CELSIUS, 0, 0, MSCBF_FLOAT, "LTemp6" , &user_data.limit[5],
00109
00110 0
00111 };
00112
00113 MSCB_INFO_VAR *variables = vars;
00114
00115
00116
00117
00118
00119
00120
00121 sbit LED_RED = LED_0;
00122 sbit SST = MSCB_SST1;
00123
00124 sbit ON6 = P0 ^ 0;
00125 sbit ON3 = P0 ^ 1;
00126 sbit ON4 = P0 ^ 2;
00127 sbit ON5 = P3 ^ 1;
00128 sbit ON1 = P0 ^ 6;
00129 sbit ON2 = P0 ^ 7;
00130 sbit SHDWN = P3 ^ 3;
00131
00132
00133 void user_write(unsigned char index) reentrant;
00134 float read_voltage(unsigned char channel, float coeff, float offset);
00135
00136
00137
00138
00139 void publishErr() {
00140 DISABLE_INTERRUPTS;
00141 user_data.error = rESR;
00142 ENABLE_INTERRUPTS;
00143 }
00144
00145
00146
00147 void PublishVariable(float xdata * pvarDest, float varSrce) {
00148 DISABLE_INTERRUPTS;
00149 *pvarDest = varSrce;
00150 ENABLE_INTERRUPTS;
00151 }
00152
00153
00154
00155 void user_init(unsigned char init)
00156 {
00157 idata char i;
00158
00159
00160 for (i=0;i<4;i++) {
00161 if (svn_rev_code[6+i] < 48) {
00162 svn_rev_code[6+i] = '0';
00163 }
00164 }
00165 sys_info.svn_revision = (svn_rev_code[6]-'0')*1000+
00166 (svn_rev_code[7]-'0')*100+
00167 (svn_rev_code[8]-'0')*10+
00168 (svn_rev_code[9]-'0');
00169
00170 if (init) {
00171 user_data.error = 0;
00172 user_data.control = 0;
00173 user_data.status = 0;
00174 sys_info.node_addr = cur_sub_addr();
00175 for (i=0;i<6;i++)
00176 user_data.limit[i] = 50.0;
00177 }
00178
00179
00180 P0MDOUT = 0xFF;
00181
00182
00183 P1MDIN = 0x00;
00184 P2MDIN = 0xC0;
00185 P3MDIN = 0x0F;
00186 P3MDOUT = 0x06;
00187
00188
00189
00190
00191
00192
00193 CPT1CN = 0x80;
00194 CPT1MX = 0x33;
00195
00196
00197
00198 CPT1MD = 0x02;
00199
00200
00201
00202
00203
00204 adc_internal_init();
00205
00206 #ifdef _ADT7486A_
00207 ADT7486A_Init(SST_LINE1);
00208 #endif
00209
00210
00211 for (i=0;i<N_POWER_CHANNELS;i++)
00212 user_data.value[i] = 0;
00213 for (i=0;i<6;i++)
00214 user_data.temperature[i] = 0.0;
00215 for (i=0;i<3;i++)
00216 user_data.temperature[i] = 0.0;
00217
00218 user_data.control = 0;
00219 user_data.status = 0;
00220 user_data.error = 0;
00221 }
00222
00223
00224 void user_write(unsigned char index) reentrant
00225 {
00226 if (index==1) {
00227 ON2 = !(user_data.control & 0x1);
00228 ON1 = !(user_data.control & 0x2);
00229 ON4 = !(user_data.control & 0x4);
00230 ON3 = !(user_data.control & 0x8);
00231 ON6 = !(user_data.control & 0x10);
00232 ON5 = !(user_data.control & 0x20);
00233 user_data.status = user_data.control;
00234 user_data.control = 0;
00235 user_data.error = 0;
00236 rESR = 0;
00237 }
00238 return;
00239 }
00240
00241
00242 unsigned char user_read(unsigned char index)
00243 {
00244 if(index);
00245
00246 return 0;
00247 }
00248
00249
00250 unsigned char user_func(unsigned char *data_in, unsigned char *data_out)
00251 {
00252
00253 data_out[0] = data_in[0];
00254 data_out[1] = data_in[1];
00255 return 2;
00256 }
00257
00258
00259
00260 float read_voltage(unsigned char channel, float coeff, float offset)
00261 {
00262 unsigned int xdata i, rvalue;
00263 float xdata voltage;
00264 unsigned int xdata rawbin;
00265 unsigned long xdata rawsum = 0;
00266
00267
00268 for (i=0 ; i<10 ; i++) {
00269 rawbin = adc_read(channel, 1);
00270 rawsum += rawbin;
00271 yield();
00272 }
00273
00274
00275 rvalue = rawsum/10;
00276 voltage = (float) rvalue;
00277 voltage = (float) voltage / 1024.0 * VREF;
00278 if ( channel != TCHANNEL)
00279 voltage = voltage * coeff + offset;
00280
00281 return voltage;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290 unsigned char code adc_index[] = {0xc, 0xd, 0x14, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb};
00291 float code coeff[] = {2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00};
00292 float code offset[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 void user_loop(void)
00304 {
00305 unsigned i, j, channel;
00306 float xdata value[15], temperature;
00307
00308 #ifdef _ADT7486A_
00309
00310 RdssT = CLEAR;
00311 for (i=0 , j=0 ; j<3 ; i+=2, j++) {
00312 if (!ADT7486A_Cmd(ADT7486A_addrArray[j], GetExt1Temp, SST_LINE1, &temperature)) {
00313 if (temperature > user_data.limit[j]) rESR |= (1<<(i+2));
00314 else rESR &= ~(1<<(i+2));
00315 PublishVariable(&user_data.temperature[i+0], temperature);
00316 } else RdssT = SET;
00317
00318 if (!ADT7486A_Cmd(ADT7486A_addrArray[j], GetExt2Temp, SST_LINE1, &temperature)) {
00319 if (temperature > user_data.limit[j]) rESR |= (1<<(i+2));
00320 else rESR &= ~(1<<(i+2));
00321 PublishVariable(&user_data.temperature[i+1], temperature);
00322 } else RdssT = SET;
00323 }
00324 #endif
00325
00326 #ifdef _ADT7486A_
00327
00328 for (i=0;i<3;i++) {
00329 if (!ADT7486A_Cmd(ADT7486A_addrArray[i], GetIntTemp, SST_LINE1, &temperature)) {
00330 PublishVariable(&user_data.localtemp[i], temperature);
00331 } else RdssT = SET;
00332 }
00333 #endif
00334
00335
00336 for (channel=0 ; channel<N_POWER_CHANNELS ; channel++) {
00337 value[channel] = read_voltage(adc_index[channel], coeff[channel], offset[channel]);
00338 }
00339
00340 DISABLE_INTERRUPTS;
00341 for (channel=0 ; channel<=N_POWER_CHANNELS ; channel++) {
00342 user_data.value[channel] = value[channel];
00343 }
00344 ENABLE_INTERRUPTS;
00345
00346
00347 rESR |= !SHDWN;
00348
00349
00350 publishErr();
00351
00352
00353
00354 if (rESR) {
00355 ON1 = ON2 = ON3 = ON4 = ON5 = ON6 = 1;
00356 DISABLE_INTERRUPTS;
00357 user_data.status = 0;
00358 ENABLE_INTERRUPTS;
00359 }
00360 delay_ms(500);
00361 }