| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#include "Command.h" |
| 45 |
#include "AD7193.h" |
| 46 |
#include "Console.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
const char* cmdList[] = |
| 53 |
{"help?", |
| 54 |
"reset!", |
| 55 |
"polarity?", |
| 56 |
"polarity=", |
| 57 |
"range?", |
| 58 |
"range=", |
| 59 |
"pseudoBit?", |
| 60 |
"pseudoBit=", |
| 61 |
"register?", |
| 62 |
"voltage?", |
| 63 |
"temperature?"}; |
| 64 |
const char* cmdDescription[] ={ |
| 65 |
" - Displays all available commands.", |
| 66 |
" - Resets the AD7193.", |
| 67 |
" - Displays the polarity value.", |
| 68 |
" - Sets the polarity value. Accepted values: \r\n\ |
| 69 |
\t0 - Bipolar mode. \r\n\ |
| 70 |
\t1 - Unipolar mode.", |
| 71 |
" - Displays the range value.", |
| 72 |
" - Sets the range value. Accepted values: \r\n\ |
| 73 |
\t0 - Gain=1. (Input voltage range must be +/-2.5V) \r\n\ |
| 74 |
\t3 - Gain=8. (Input voltage range must be +/-312.5mV)\r\n\ |
| 75 |
\t4 - Gain=16. (Input voltage range must be +/-156.2mV)\r\n\ |
| 76 |
\t5 - Gain=32. (Input voltage range must be +/-78.12mV) \r\n\ |
| 77 |
\t6 - Gain=64. (Input voltage range must be +/-39.06mV) \r\n\ |
| 78 |
\t7 - Gain=128. (Input voltage range must be +/-19.53mV)", |
| 79 |
" - Displays the Pseudo Bit value (AD7193_REG_CONF).", |
| 80 |
" - Sets the Pseudo Bit Value (AD7193_REG_CONF). Accepted values: \r\n\ |
| 81 |
\t0 - Disables the pseudo differential measuring. \r\n\ |
| 82 |
\t1 - Enables the pseudo differential measuring.", |
| 83 |
" - Displays the value of the data register (AD7193_REG_DATA) for specified \ |
| 84 |
channel. Accepted values: \r\n\ |
| 85 |
\t0..7 - selected channel.", |
| 86 |
" - Displays the voltage applied to specified channel. Accepted values: \r\n\ |
| 87 |
\t0..7 - selected channel.", |
| 88 |
" - Displays the temperature."}; |
| 89 |
const char* cmdExample[] = { |
| 90 |
"", |
| 91 |
"", |
| 92 |
"", |
| 93 |
"To set the bipolar mode, type: polarity=0", |
| 94 |
"", |
| 95 |
"To set the gain=16, type range=4", |
| 96 |
"", |
| 97 |
"To enable the pseudo differential measuring, type: pseudoBit=1", |
| 98 |
"To display the data register for channel 1, type: register?1", |
| 99 |
"To display the voltage applied to channel 3, type: voltage?3", |
| 100 |
""}; |
| 101 |
const char cmdNo = (sizeof(cmdList) / sizeof(const char*)); |
| 102 |
const float V_REF = 2.5; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
cmdFunction cmdFunctions[13] = {GetHelp, DoReset, GetPolarity, SetPolarity, |
| 108 |
GetRange, SetRange, GetPseudoBit, SetPseudoBit, |
| 109 |
GetRegister, GetVoltage, GetTemperature}; |
| 110 |
|
| 111 |
|
| 112 |
unsigned char channel = 0; |
| 113 |
unsigned char pseudoBit = 0; |
| 114 |
unsigned char selectedPolarity = 0; |
| 115 |
unsigned char selectedRange = 0; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
void GetHelp(double* param, char paramNo) |
| 123 |
{ |
| 124 |
char displayCmd; |
| 125 |
|
| 126 |
CONSOLE_Print("Available commands:\r\n"); |
| 127 |
for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 128 |
{ |
| 129 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd], |
| 130 |
(char*)cmdDescription[displayCmd]); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
char DoDeviceInit(void) |
| 144 |
{ |
| 145 |
if(AD7193_Init() == 0) |
| 146 |
{ |
| 147 |
CONSOLE_Print("AD7193 OK\r\n"); |
| 148 |
GetHelp(NULL, 0); |
| 149 |
return SUCCESS; |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
CONSOLE_Print("AD7193 Error\r\n"); |
| 154 |
return ERROR; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
void DoReset(double* param, char paramNo) |
| 164 |
{ |
| 165 |
AD7193_Reset(); |
| 166 |
|
| 167 |
|
| 168 |
selectedPolarity = 0; |
| 169 |
selectedRange = 7; |
| 170 |
pseudoBit = 0; |
| 171 |
|
| 172 |
CONSOLE_Print("Device was reset.\r\n"); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void GetPolarity(double* param, char paramNo) |
| 181 |
{ |
| 182 |
|
| 183 |
CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],selectedPolarity); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
void SetPolarity(double* param, char paramNo) |
| 192 |
{ |
| 193 |
|
| 194 |
if(paramNo >= 1) |
| 195 |
{ |
| 196 |
if(param[0] < 0) |
| 197 |
{ |
| 198 |
param[0] = 0; |
| 199 |
} |
| 200 |
if(param[0] > 1) |
| 201 |
{ |
| 202 |
param[0] = 1; |
| 203 |
} |
| 204 |
|
| 205 |
selectedPolarity = (unsigned char)param[0]; |
| 206 |
|
| 207 |
AD7193_RangeSetup(selectedPolarity, selectedRange); |
| 208 |
|
| 209 |
|
| 210 |
CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],selectedPolarity); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
|
| 215 |
CONSOLE_Print("Invalid parameter!\r\n"); |
| 216 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]); |
| 217 |
CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
void GetRange(double* param, char paramNo) |
| 227 |
{ |
| 228 |
|
| 229 |
CONSOLE_Print("%s%d\r\n",(char*)cmdList[5],selectedRange); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void SetRange(double* param, char paramNo) |
| 238 |
{ |
| 239 |
|
| 240 |
if(paramNo >= 1) |
| 241 |
{ |
| 242 |
if(param[0] < 3) |
| 243 |
{ |
| 244 |
param[0] = 0; |
| 245 |
} |
| 246 |
if(param[0] > 7) |
| 247 |
{ |
| 248 |
param[0] = 7; |
| 249 |
} |
| 250 |
|
| 251 |
selectedRange = (unsigned char)param[0]; |
| 252 |
|
| 253 |
AD7193_RangeSetup(selectedPolarity, selectedRange); |
| 254 |
|
| 255 |
|
| 256 |
CONSOLE_Print("%s%d\r\n",(char*)cmdList[5],selectedRange); |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
|
| 261 |
CONSOLE_Print("Invalid parameter!\r\n"); |
| 262 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[5], (char*)cmdDescription[5]); |
| 263 |
CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[5]); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
void GetPseudoBit(double* param, char paramNo) |
| 273 |
{ |
| 274 |
|
| 275 |
CONSOLE_Print("%s%d\r\n",(char*)cmdList[7],pseudoBit); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
void SetPseudoBit(double* param, char paramNo) |
| 284 |
{ |
| 285 |
unsigned long regValue = 0; |
| 286 |
|
| 287 |
|
| 288 |
if(paramNo >= 1) |
| 289 |
{ |
| 290 |
if(param[0] < 0) |
| 291 |
{ |
| 292 |
param[0] = 0; |
| 293 |
} |
| 294 |
if(param[0] > 1) |
| 295 |
{ |
| 296 |
param[0] = 1; |
| 297 |
} |
| 298 |
|
| 299 |
switch((unsigned char)param[0]) |
| 300 |
{ |
| 301 |
case 1: |
| 302 |
pseudoBit = 1; |
| 303 |
regValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1); |
| 304 |
regValue |= AD7193_CONF_PSEUDO; |
| 305 |
AD7193_SetRegisterValue(AD7193_REG_CONF, regValue, 3, 1); |
| 306 |
break; |
| 307 |
default: |
| 308 |
pseudoBit = 0; |
| 309 |
regValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1); |
| 310 |
regValue &= ~ AD7193_CONF_PSEUDO; |
| 311 |
AD7193_SetRegisterValue(AD7193_REG_CONF, regValue, 3, 1); |
| 312 |
break; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
CONSOLE_Print("%s%d\r\n",(char*)cmdList[7],pseudoBit); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
|
| 321 |
CONSOLE_Print("Invalid parameter!\r\n"); |
| 322 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[7], (char*)cmdDescription[7]); |
| 323 |
CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[7]); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
void GetRegister(double* param, char paramNo) |
| 333 |
{ |
| 334 |
unsigned long data = 0; |
| 335 |
|
| 336 |
|
| 337 |
if(paramNo >= 1) |
| 338 |
{ |
| 339 |
if(param[0] < 0) |
| 340 |
{ |
| 341 |
param[0] = 0; |
| 342 |
} |
| 343 |
if(param[0] > 7) |
| 344 |
{ |
| 345 |
param[0] = 7; |
| 346 |
} |
| 347 |
|
| 348 |
channel = (unsigned char)param[0]; |
| 349 |
AD7193_ChannelSelect(channel); |
| 350 |
|
| 351 |
data = AD7193_SingleConversion(); |
| 352 |
|
| 353 |
CONSOLE_Print("Register=%d for channel %d.\r\n",data,channel); |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
|
| 358 |
CONSOLE_Print("Invalid parameter!\r\n"); |
| 359 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[8], (char*)cmdDescription[8]); |
| 360 |
CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[8]); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
void GetTemperature(double* param, char paramNo) |
| 370 |
{ |
| 371 |
float temperatureValue = 0; |
| 372 |
|
| 373 |
|
| 374 |
temperatureValue = AD7193_TemperatureRead(); |
| 375 |
|
| 376 |
CONSOLE_Print("temperature=%.2f [C]\r\n",temperatureValue); |
| 377 |
|
| 378 |
AD7193_RangeSetup(selectedPolarity, selectedRange); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
void GetVoltage(double* param, char paramNo) |
| 387 |
{ |
| 388 |
unsigned long data = 0; |
| 389 |
double voltage = 0; |
| 390 |
|
| 391 |
|
| 392 |
if(paramNo >= 1) |
| 393 |
{ |
| 394 |
if(param[0] < 0) |
| 395 |
{ |
| 396 |
param[0] = 0; |
| 397 |
} |
| 398 |
if(param[0] > 7) |
| 399 |
{ |
| 400 |
param[0] = 7; |
| 401 |
} |
| 402 |
|
| 403 |
channel = (unsigned char)param[0]; |
| 404 |
AD7193_ChannelSelect(channel); |
| 405 |
|
| 406 |
data = AD7193_SingleConversion(); |
| 407 |
voltage = AD7193_ConvertToVolts(data, V_REF); |
| 408 |
|
| 409 |
CONSOLE_Print("Voltage=%.3f [V] for channel %d.\r\n",voltage,channel); |
| 410 |
} |
| 411 |
else |
| 412 |
{ |
| 413 |
|
| 414 |
CONSOLE_Print("Invalid parameter!\r\n"); |
| 415 |
CONSOLE_Print("%s%s\r\n", (char*)cmdList[9], (char*)cmdDescription[9]); |
| 416 |
CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[9]); |
| 417 |
} |
| 418 |
} |