00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __MSPM_H__
00013 #define __MSPM_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #pragma systemFile
00036
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040
00041
00042
00043
00044 #define MSPM_I2C_ADDR 0x12
00045 #define MSPM_CMD_REG 0x41
00046
00047 #define MSPM_PCURRENT 0x42
00048 #define MSPM_PVOLTAGE 0x44
00049 #define MSPM_CAPUSED 0x46
00050 #define MSPM_TIME 0x56
00051
00052 tByteArray MSPM_I2CRequest;
00053 tByteArray MSPM_I2CReply;
00054
00055
00056
00057
00058
00059 int MSPMreadCurrent(tSensors link, ubyte address = MSPM_I2C_ADDR);
00060 int MSPMreadVoltage(tSensors link, ubyte address = MSPM_I2C_ADDR);
00061 bool MSPMreadVoltageCurrent(tSensors link, int &voltage, int ¤t, ubyte address = MSPM_I2C_ADDR);
00062 long MSPMreadTime(tSensors link, ubyte address = MSPM_I2C_ADDR);
00063 bool MSPMresetCounters(tSensors link, ubyte address = MSPM_I2C_ADDR);
00064
00065
00066
00067
00068 bool _MSPMsendCommand(tSensors link, byte command, ubyte address);
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 bool _MSPMsendCommand(tSensors link, byte command, ubyte address) {
00081 MSPM_I2CRequest[0] = 3;
00082 MSPM_I2CRequest[1] = address;
00083 MSPM_I2CRequest[2] = MSPM_CMD_REG;
00084 MSPM_I2CRequest[3] = command;
00085
00086 return writeI2C(link, MSPM_I2CRequest, 0);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096 int MSPMreadCurrent(tSensors link, ubyte address) {
00097 memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00098
00099 MSPM_I2CRequest[0] = 2;
00100 MSPM_I2CRequest[1] = address;
00101 MSPM_I2CRequest[2] = MSPM_PCURRENT;
00102
00103 if (!writeI2C(link, MSPM_I2CRequest, 2))
00104 return -1;
00105
00106 if (!readI2C(link, MSPM_I2CReply, 2))
00107 return -1;
00108
00109 return (MSPM_I2CReply[0] + (MSPM_I2CReply[1]<<8));
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119 int MSPMreadVoltage(tSensors link, ubyte address) {
00120 memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00121
00122 MSPM_I2CRequest[0] = 2;
00123 MSPM_I2CRequest[1] = address;
00124 MSPM_I2CRequest[2] = MSPM_PVOLTAGE;
00125
00126 if (!writeI2C(link, MSPM_I2CRequest, 2))
00127 return -1;
00128
00129 if (!readI2C(link, MSPM_I2CReply, 2))
00130 return -1;
00131
00132 return (MSPM_I2CReply[0] + (MSPM_I2CReply[1]<<8));
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 bool MSPMreadVoltageCurrent(tSensors link, int &voltage, int ¤t, ubyte address) {
00146 memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00147
00148 MSPM_I2CRequest[0] = 2;
00149 MSPM_I2CRequest[1] = address;
00150 MSPM_I2CRequest[2] = MSPM_PCURRENT;
00151
00152 if (!writeI2C(link, MSPM_I2CRequest, 4))
00153 return false;
00154
00155 if (!readI2C(link, MSPM_I2CReply, 4))
00156 return false;
00157
00158 current = MSPM_I2CReply[0] + (MSPM_I2CReply[1]<<8);
00159 voltage = MSPM_I2CReply[2] + (MSPM_I2CReply[3]<<8);
00160 return true;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 long MSPMreadTime(tSensors link, ubyte address) {
00170 memset(MSPM_I2CRequest, 0, sizeof(tByteArray));
00171
00172 MSPM_I2CRequest[0] = 2;
00173 MSPM_I2CRequest[1] = address;
00174 MSPM_I2CRequest[2] = MSPM_TIME;
00175
00176 if (!writeI2C(link, MSPM_I2CRequest, 4))
00177 return -1;
00178
00179 if (!readI2C(link, MSPM_I2CReply, 4))
00180 return -1;
00181
00182 return (((long)MSPM_I2CReply[3] << 24) + ((long)MSPM_I2CReply[2] << 16) + ((long)MSPM_I2CReply[1] << 8) + (long)MSPM_I2CReply[0]);
00183
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193 bool MSPMresetCounters(tSensors link, ubyte address) {
00194 return _MSPMsendCommand(link, 'R', address);
00195 }
00196
00197
00198 #endif // __MSLL_H__
00199
00200
00201
00202
00203
00204