| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
#include "xparameters.h" |
| 48 |
#include "system_config.h" |
| 49 |
#include "xil_io.h" |
| 50 |
#include "ad5160.h" |
| 51 |
|
| 52 |
volatile int rxData; |
| 53 |
|
| 54 |
#if(USE_PS7 == 1) |
| 55 |
extern XScuGic IntcInstance; |
| 56 |
#endif |
| 57 |
|
| 58 |
extern u32 (*SPI_Init)(u32, char, char, char); |
| 59 |
extern u32 (*SPI_TransferData)(u32, char, char*, char, char*, char); |
| 60 |
|
| 61 |
static int rab = 10000; |
| 62 |
static int rwp = 60; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
void AD5160_DisplayMainMenu(void) |
| 72 |
{ |
| 73 |
xil_printf("\n\rPmod-DPOT Demo Program\n\r"); |
| 74 |
xil_printf("The software is calibrated for the following values: \n\r"); |
| 75 |
xil_printf(" Rab = %d ohms\n\r", rab); |
| 76 |
xil_printf(" Rwp = %d ohms\n\r", rwp); |
| 77 |
xil_printf("These value may differ for your PmodDPOT\n\r"); |
| 78 |
xil_printf("\n\r"); |
| 79 |
xil_printf("Available options: \n\r"); |
| 80 |
xil_printf(" [d] Set PmodDPOT division factor\n\r"); |
| 81 |
xil_printf(" [c] Calibrate PmodDPOT\n\r"); |
| 82 |
xil_printf(" [m] Display this menu\n\r"); |
| 83 |
xil_printf("Press key to select the desired option\n\r"); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void AD5160_DisplayMenu(void) |
| 94 |
{ |
| 95 |
xil_printf("\n\rAvailable options: \n\r"); |
| 96 |
xil_printf(" [d] Set PmodDPOT division factor\n\r"); |
| 97 |
xil_printf(" [c] Calibrate PmodDPOT\n\r"); |
| 98 |
xil_printf(" [m] Display this menu\n\r"); |
| 99 |
rxData = 0; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
int AD5160_Set(void) |
| 110 |
{ |
| 111 |
char rx; |
| 112 |
char c[] = "000"; |
| 113 |
char *c_ptr; |
| 114 |
int i = 0; |
| 115 |
int cnt = 0; |
| 116 |
char valid = 0; |
| 117 |
int value = 0; |
| 118 |
float rwb; |
| 119 |
float rwa; |
| 120 |
int whole; |
| 121 |
int retValue = 0; |
| 122 |
char txBuffer[1] = {0x00}; |
| 123 |
char rxBuffer[1] = {0x00}; |
| 124 |
|
| 125 |
c_ptr = c; |
| 126 |
|
| 127 |
|
| 128 |
DisableInterrupts(UART_INTERRUPT); |
| 129 |
|
| 130 |
|
| 131 |
while(rx != 'q') |
| 132 |
{ |
| 133 |
cnt = 0; |
| 134 |
i = 0; |
| 135 |
valid = 0; |
| 136 |
value = 0; |
| 137 |
c_ptr = c; |
| 138 |
|
| 139 |
if(i == 0) |
| 140 |
{ |
| 141 |
xil_printf("\n\rPlease enter a value between 0x00 and 0xFF or [q] to return to menu: 0x"); |
| 142 |
} |
| 143 |
while(i < 4) |
| 144 |
{ |
| 145 |
rx = UartReadChar(); |
| 146 |
xil_printf("%c",rx); |
| 147 |
|
| 148 |
|
| 149 |
if(rx == 0x0D) |
| 150 |
{ |
| 151 |
i = 3; |
| 152 |
} |
| 153 |
else if(rx == 0x0A) |
| 154 |
{ |
| 155 |
i = 3; |
| 156 |
} |
| 157 |
else if(rx == 'q') |
| 158 |
{ |
| 159 |
i = 4; |
| 160 |
valid = 0; |
| 161 |
} |
| 162 |
else if(((rx > 0x00)&&(rx < 0x30))|| |
| 163 |
((rx > 0x39)&&(rx < 0x41))|| |
| 164 |
((rx > 0x46)&&(rx < 0x61))|| |
| 165 |
(rx > 0x66)) |
| 166 |
{ |
| 167 |
xil_printf("\n\rCharacters entered must be HEX values (0 to 9 and A B C D E F)\n\r"); |
| 168 |
i = 4; |
| 169 |
valid = 0; |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
*c_ptr++ = rx; |
| 174 |
cnt = cnt + 1; |
| 175 |
valid = 1; |
| 176 |
} |
| 177 |
if(cnt == 2) |
| 178 |
{ |
| 179 |
i = 4; |
| 180 |
} |
| 181 |
i++; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
for(i = 0; i < cnt; i++) |
| 186 |
{ |
| 187 |
if(c[i] > 0x60) |
| 188 |
{ |
| 189 |
value = value * 16 + (c[i] - 0x57); |
| 190 |
} |
| 191 |
else if(c[i] > 0x39) |
| 192 |
{ |
| 193 |
value = value * 16 + (c[i] - 0x37); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
value = value * 16 + (c[i] - 0x30); |
| 198 |
} |
| 199 |
} |
| 200 |
if(valid == 1) |
| 201 |
{ |
| 202 |
|
| 203 |
|
| 204 |
rwb = (float)((value * rab) / 256) + rwp; |
| 205 |
whole = (int)rwb; |
| 206 |
xil_printf("\n\r >>Rwb = %d ohms\n\r",whole); |
| 207 |
|
| 208 |
rwa = (float)(((256 - value) * rab)/256) + rwp; |
| 209 |
whole = (int)rwa; |
| 210 |
xil_printf(" >>Rwa = %d ohms\n\r",whole); |
| 211 |
|
| 212 |
retValue = value; |
| 213 |
|
| 214 |
txBuffer[0] = value; |
| 215 |
SPI_TransferData(SPI_BASEADDR, 1, txBuffer, 1, rxBuffer, 1); |
| 216 |
} |
| 217 |
} |
| 218 |
rxData = 'm'; |
| 219 |
|
| 220 |
EnableInterrupts(UART_INTERRUPT); |
| 221 |
return(retValue); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
void AD5160_CalibrateRab(void) |
| 232 |
{ |
| 233 |
char rx = 0; |
| 234 |
char c[] = "00000"; |
| 235 |
char *c_ptr; |
| 236 |
int i = 0; |
| 237 |
char cnt = 0; |
| 238 |
char valid = 0; |
| 239 |
int value = 0; |
| 240 |
|
| 241 |
|
| 242 |
DisableInterrupts(UART_INTERRUPT); |
| 243 |
|
| 244 |
while(!valid) |
| 245 |
{ |
| 246 |
xil_printf("\n\rPlease measure the resistance between A and B. Enter the value in ohms: "); |
| 247 |
cnt = 0; |
| 248 |
i = 0; |
| 249 |
value = 0; |
| 250 |
c_ptr = c; |
| 251 |
|
| 252 |
while(i < 7) |
| 253 |
{ |
| 254 |
|
| 255 |
rx = UartReadChar(); |
| 256 |
xil_printf("%c",rx); |
| 257 |
|
| 258 |
if(rx == 0x0D) |
| 259 |
{ |
| 260 |
i = 6; |
| 261 |
} |
| 262 |
else if(rx == 0x0A) |
| 263 |
{ |
| 264 |
i = 6; |
| 265 |
} |
| 266 |
else if(((rx > 0x00)&&(rx < 0x30)) || ((rx > 0x39))) |
| 267 |
{ |
| 268 |
xil_printf("\n\rCharacters entered must be valid numbers (0 to 9)"); |
| 269 |
i = 7; |
| 270 |
valid = 0; |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
*c_ptr++ = rx; |
| 275 |
cnt = cnt + 1; |
| 276 |
valid = 1; |
| 277 |
} |
| 278 |
if(cnt == 5) |
| 279 |
{ |
| 280 |
i = 7; |
| 281 |
} |
| 282 |
i++; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
for(i = 0; i < cnt; i++) |
| 287 |
{ |
| 288 |
value = value * 10 + (c[i] - 0x30); |
| 289 |
} |
| 290 |
|
| 291 |
if(valid == 1) |
| 292 |
{ |
| 293 |
rab = value; |
| 294 |
xil_printf("\n\rNew value stored for Rab: %d\n\r", rab); |
| 295 |
} |
| 296 |
else |
| 297 |
{ |
| 298 |
xil_printf("\n\rValue entered is not valid.\n\r"); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
EnableInterrupts(UART_INTERRUPT); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
void AD5160_CalibrateRwp(void) |
| 313 |
{ |
| 314 |
char rx = 0; |
| 315 |
char c[] = "000"; |
| 316 |
char *c_ptr; |
| 317 |
int i = 0; |
| 318 |
char cnt = 0; |
| 319 |
char valid = 0; |
| 320 |
int value = 0; |
| 321 |
char txBuffer[1] = {0x00}; |
| 322 |
char rxBuffer[1] = {0x00}; |
| 323 |
|
| 324 |
|
| 325 |
DisableInterrupts(UART_INTERRUPT); |
| 326 |
|
| 327 |
|
| 328 |
SPI_TransferData(SPI_BASEADDR, 1, txBuffer, 1, rxBuffer, 1); |
| 329 |
|
| 330 |
while(!valid) |
| 331 |
{ |
| 332 |
xil_printf("\n\rPlease measure the resistance between W and B. Enter the value in ohms: "); |
| 333 |
c_ptr = c; |
| 334 |
cnt = 0; |
| 335 |
i = 0; |
| 336 |
value = 0; |
| 337 |
|
| 338 |
|
| 339 |
while(i < 5) |
| 340 |
{ |
| 341 |
|
| 342 |
rx = UartReadChar(); |
| 343 |
xil_printf("%c",rx); |
| 344 |
|
| 345 |
if(rx == 0x0D) |
| 346 |
{ |
| 347 |
i = 4; |
| 348 |
} |
| 349 |
else if(rx == 0x0A) |
| 350 |
{ |
| 351 |
i = 4; |
| 352 |
} |
| 353 |
else if(((rx > 0x00)&&(rx < 0x30)) || ((rx > 0x39))) |
| 354 |
{ |
| 355 |
xil_printf("\n\rCharacters entered must be valid numbers (0 to 9)"); |
| 356 |
i = 5; |
| 357 |
valid = 0; |
| 358 |
} |
| 359 |
else |
| 360 |
{ |
| 361 |
*c_ptr++ = rx; |
| 362 |
cnt = cnt + 1; |
| 363 |
valid = 1; |
| 364 |
} |
| 365 |
if(cnt == 3) |
| 366 |
{ |
| 367 |
i = 5; |
| 368 |
} |
| 369 |
i++; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
for(i = 0; i < cnt; i++) |
| 374 |
{ |
| 375 |
value = value * 10 + (c[i] - 0x30); |
| 376 |
} |
| 377 |
|
| 378 |
if(valid == 1) |
| 379 |
{ |
| 380 |
rwp = value; |
| 381 |
xil_printf("\n\rNew value stored for Rwp: %d\n\r", rwp); |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
xil_printf("\n\rValue entered is not valid.\n\r"); |
| 386 |
} |
| 387 |
} |
| 388 |
rxData = 'm'; |
| 389 |
|
| 390 |
EnableInterrupts(UART_INTERRUPT); |
| 391 |
xil_printf("\n\r>Returning to Menu...\n\r"); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
void DemoProgram(void) |
| 402 |
{ |
| 403 |
AD5160_DisplayMainMenu(); |
| 404 |
|
| 405 |
while(1) |
| 406 |
{ |
| 407 |
switch(rxData) |
| 408 |
{ |
| 409 |
case 'd': |
| 410 |
AD5160_Set(); |
| 411 |
break; |
| 412 |
case 'c': |
| 413 |
AD5160_CalibrateRab(); |
| 414 |
AD5160_CalibrateRwp(); |
| 415 |
break; |
| 416 |
case 'm': |
| 417 |
AD5160_DisplayMenu(); |
| 418 |
break; |
| 419 |
case 0: |
| 420 |
break; |
| 421 |
default: |
| 422 |
xil_printf("\n\rWrong option! Please select one of the options below."); |
| 423 |
AD5160_DisplayMenu(); |
| 424 |
break; |
| 425 |
} |
| 426 |
} |
| 427 |
} |