Components and supplies
610 Ohm voltage bias resistor
AD7793
10 µF tantalum capacitor
Pullup resistor 47 kOhm
10 nF and 100 nF ceramic capacitors
4.99 kOhm 0.1% 10 ppm/°C reference resistor
Arduino UNO
1 kOhm current limiting resistor
RTD PT100, 4 lead cable
Project description
Code
AD7793.cpp
c_cpp
1/***************************************************************************//** 2 * @file AD7793.c 3 * @brief Implementation of AD7793 Driver. 4 * @author Bancisor MIhai 5******************************************************************************** 6 * Copyright 2012(c) Analog Devices, Inc. 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * - Neither the name of Analog Devices, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * - The use of this software may or may not infringe the patent rights 22 * of one or more patent holders. This license does not release you 23 * from the requirement that you obtain separate licenses from these 24 * patent holders to use this software. 25 * - Use of the software either in source or binary form, must be run 26 * on or directly connected to an Analog Devices Inc. component. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39******************************************************************************** 40 * SVN Revision: 500 41******************************************************************************* 42 43******************************************************************************** 44 * Modified and adapted for Arduino by Ph. Sonnet. March 9, 2019, Version 1.1 45*******************************************************************************/ 46 47/******************************************************************************/ 48/* Include Files */ 49/******************************************************************************/ 50#include "AD7793.h" // AD7793 definitions. 51#include "Communication.h" // Communication definitions. 52#include "Arduino.h" 53#include "SPI.h" 54 55/***************************************************************************//** 56 * @brief Initializes the AD7793 and checks if the device is present. 57 * 58 * @return status - Result of the initialization procedure. 59 * Example: 1 - if initialization was successful (ID is 0x0B). 60 * 0 - if initialization was unsuccessful. 61*******************************************************************************/ 62 63unsigned char AD7793_Init(void) 64{ 65 unsigned char status = 0x1; 66 67 SPI_Init(); 68 if((AD7793_GetRegisterValue(AD7793_REG_ID, 1, 1) & AD7793_ID_MASK) != AD7793_ID) 69 { 70 status = 0x0; 71 } 72 73 return(status); 74} 75/***************************************************************************//** 76 * @brief Sends 32 consecutive 1's on SPI in order to reset the part. 77 * 78 * @return None. 79*******************************************************************************/ 80void AD7793_Reset(void) 81{ 82 unsigned char dataToSend[5] = {0x01, 0xff, 0xff, 0xff, 0xff}; 83 84 //ADI_PART_CS_LOW; 85 SPI_Write(dataToSend,4); 86 //ADI_PART_CS_HIGH; 87} 88/***************************************************************************//** 89 * @brief Reads the Ready Bit for ADC 90 * 91 * @return status: 1 - if conversion is not yet completed and data is not yet written to the data register. 92 * 0 - if data is written to the ADC data register. 93*******************************************************************************/ 94unsigned char AD7793_Ready(void) 95{ 96 unsigned char status = 0x0; 97 98 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_RDY) == 0x80) 99 { 100 status = 0x1; 101 } 102 103 return(status); 104} 105/***************************************************************************//** 106* @brief Reads the ADC error bit. 107 * 108 * @return status: 1 - result written to ADC data register all clamped has been all clamped to 1 or 0. 109 * 0 - No error due to overrrange, underrange or other error sources. 110*******************************************************************************/ 111unsigned char AD7793_Error(void) 112{ 113 unsigned char status = 0x0; 114 115 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_ERR) == 0x40) 116 { 117 status = 0x1; 118 } 119 120 return(status); 121} 122/***************************************************************************//** 123 * @brief Indicates that channel 3 is being converted by the ADC. 124 * 125 * @return status: 1 - Channel 3 is being converted by the ADC. 126 * 0 - Channel 3 is not being converted by the ADC.. 127*******************************************************************************/ 128unsigned char AD7793_Channel3(void) 129{ 130 unsigned char status = 0x0; 131 132 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_CH3) == 0x04) 133 { 134 status = 0x1; 135 } 136 137 return(status); 138} 139/***************************************************************************//** 140 * @brief Indicates that channel 2 is being converted by the ADC. 141 * 142 * @return status: 1 - Channel 2 is being converted by the ADC. 143 * 0 - Channel 2 is not being converted by the ADC.. 144*******************************************************************************/ 145unsigned char AD7793_Channel2(void) 146{ 147 unsigned char status = 0x0; 148 149 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_CH2) == 0x02) 150 { 151 status = 0x1; 152 } 153 154 return(status); 155} 156/***************************************************************************//** 157 * @brief Indicates that channel 1 is being converted by the ADC. 158 * 159 * @return status: 1 - Channel 1 is being converted by the ADC. 160 * 0 - Channel 1 is not being converted by the ADC.. 161*******************************************************************************/ 162unsigned char AD7793_Channel1(void) 163{ 164 unsigned char status = 0x0; 165 166 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_CH1) == 0x01) 167 { 168 status = 0x1; 169 } 170 171 return(status); 172} 173/***************************************************************************//** 174 * @brief Reads the value of the selected register 175 * 176 * @param regAddress - The address of the register to read. 177 * @param size - The size of the register to read. 178 * 179 * @return data - The value of the selected register register. 180*******************************************************************************/ 181unsigned long AD7793_GetRegisterValue(unsigned char regAddress, 182 unsigned char size, 183 unsigned char modifyCS) 184{ 185 unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00}; 186 unsigned long receivedData = 0x00; 187 unsigned char i = 0x00; 188 189 data[0] = 0x01 * modifyCS; 190 data[1] = AD7793_COMM_READ | AD7793_COMM_ADDR(regAddress); 191 SPI_Read(data,(1 + size)); 192 for(i = 1 ;i < size +1 ; i ++) 193 { 194 receivedData = (receivedData << 8) + data[i]; 195 196 } 197 198 return (receivedData); 199} 200/***************************************************************************//** 201 * @brief Writes the value to the register 202 * 203 * @param - regAddress - The address of the register to write to. 204 * @param - regValue - The value to write to the register. 205 * @param - size - The size of the register to write. 206 * 207 * @return None. 208*******************************************************************************/ 209void AD7793_SetRegisterValue(unsigned char regAddress, 210 unsigned long regValue, 211 unsigned char size, 212 unsigned char modifyCS) 213{ 214 unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00}; 215 unsigned char* dataPointer = (unsigned char*)®Value; 216 unsigned char bytesNr = size + 1; 217 218 data[0] = 0x01 * modifyCS; 219 data[1] = AD7793_COMM_WRITE | AD7793_COMM_ADDR(regAddress); 220 while(bytesNr > 1) 221 { 222 data[bytesNr] = *dataPointer; 223 dataPointer ++; 224 bytesNr --; 225 } 226 SPI_Write(data,(1 + size)); 227} 228/***************************************************************************//** 229 * @brief Waits for RDY pin to go low. 230 * 231 * @return None. 232*******************************************************************************/ 233void AD7793_WaitRdyGoLow(void) 234{ 235 while( AD7793_RDY_STATE ) 236 { 237 ; 238 } 239} 240 241/***************************************************************************//** 242 * @brief Sets the operating mode of AD7793. 243 * 244 * @param mode - Mode of operation. 245 * 246 * @return None. 247*******************************************************************************/ 248void AD7793_SetMode(unsigned long mode) 249{ 250 unsigned long command; 251 252 command = AD7793_GetRegisterValue(AD7793_REG_MODE, 253 2, 254 1); // CS is modified by SPI read/write functions. 255 command &= ~AD7793_MODE_SEL(0xFF); 256 command |= AD7793_MODE_SEL(mode); 257 AD7793_SetRegisterValue( 258 AD7793_REG_MODE, 259 command, 260 2, 261 1); // CS is modified by SPI read/write functions. 262} 263 264/***************************************************************************//** 265 * @brief Sets the ADC clock source of AD7793. 266 * 267 * @param mode - Clock source. 268 * 269 * @return None. 270*******************************************************************************/ 271void AD7793_SetClockSource(unsigned long clockSource) 272{ 273 unsigned long command; 274 275 command = AD7793_GetRegisterValue(AD7793_REG_MODE, 276 2, 277 1); // CS is modified by SPI read/write functions. 278 command &= ~AD7793_MODE_CLKSRC(0xFF); 279 command |= AD7793_MODE_CLKSRC(clockSource); 280 AD7793_SetRegisterValue( 281 AD7793_REG_MODE, 282 command, 283 2, 284 1); // CS is modified by SPI read/write functions. 285} 286 287/***************************************************************************//** 288 * @brief Sets the filter update rate of AD7793. 289 * 290 * @param mode - Filter update rate (Hz). 291 * 292 * @return None. 293*******************************************************************************/ 294void AD7793_SetFilterUpdateRate(unsigned long filterRate) 295{ 296 unsigned long command; 297 298 command = AD7793_GetRegisterValue(AD7793_REG_MODE, 299 2, 300 1); // CS is modified by SPI read/write functions. 301 command &= ~AD7793_MODE_RATE(0xFF); 302 command |= AD7793_MODE_RATE(filterRate); 303 AD7793_SetRegisterValue( 304 AD7793_REG_MODE, 305 command, 306 2, 307 1); // CS is modified by SPI read/write functions. 308} 309 310/***************************************************************************//** 311 * @brief Sets the direction of the internal current source. 312 313 * @param Current source direction. 314 * 315 * @return None. 316*******************************************************************************/ 317 318void AD7793_SetExcitDirection(unsigned long direction) 319{ 320 unsigned long command; 321 322 command = AD7793_GetRegisterValue(AD7793_REG_IO, 323 1, 324 1); // CS is modified by SPI read/write functions. 325 command &= ~AD7793_IEXCDIR(0xF); 326 command |= AD7793_IEXCDIR(direction); 327 AD7793_SetRegisterValue( 328 AD7793_REG_IO, 329 command, 330 1, 331 1); // CS is modified by SPI read/write functions. 332} 333 334/***************************************************************************//** 335 * @brief Sets the current of the internal current source 336 337 * @param Current source value. 338 * 339 * @return None. 340*******************************************************************************/ 341 342void AD7793_SetExcitCurrent(unsigned long current) 343{ 344 unsigned long command; 345 346 command = AD7793_GetRegisterValue(AD7793_REG_IO, 347 1, 348 1); // CS is modified by SPI read/write functions. 349 command &= ~AD7793_IEXCEN(0xF); 350 command |= AD7793_IEXCEN(current); 351 AD7793_SetRegisterValue( 352 AD7793_REG_IO, 353 command, 354 1, 355 1); // CS is modified by SPI read/write functions. 356} 357 358/***************************************************************************//** 359 * @brief Enable bias voltage generator of AD7793. 360 * 361 * @param Bias voltage. 362 * 363 * @return None. 364*******************************************************************************/ 365void AD7793_SetBiasVoltage(unsigned long voltage) 366{ 367 unsigned long command; 368 369 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 370 2, 371 1); // CS is modified by SPI read/write functions. 372 command &= ~AD7793_CONF_VBIAS(0xFF); 373 command |= AD7793_CONF_VBIAS(voltage); 374 AD7793_SetRegisterValue( 375 AD7793_REG_CONF, 376 command, 377 2, 378 1); // CS is modified by SPI read/write functions. 379} 380/***************************************************************************//** 381 * 382 * @param Enable burnout current of AD7793. 383 * 384 * @return None. 385*******************************************************************************/ 386void AD7793_EnableBurnoutCurr(void) 387 388{ 389 unsigned long command; 390 391 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 392 2, 393 1); // CS is modified by SPI read/write functions. 394 command &= ~AD7793_CONF_BO_EN; 395 command |= AD7793_CONF_BO_EN; 396 AD7793_SetRegisterValue( 397 AD7793_REG_CONF, 398 command, 399 2, 400 1); // CS is modified by SPI read/write functions. 401} 402 403/***************************************************************************//** 404 * @brief Disable burnout current of AD7793. 405 * 406 * @param None. 407 * 408 * @return None. 409*******************************************************************************/ 410void AD7793_DisableBurnoutCurr(void) 411 412{ 413 unsigned long command; 414 415 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 416 2, 417 1); // CS is modified by SPI read/write functions. 418 command &= ~AD7793_CONF_BO_EN; 419 AD7793_SetRegisterValue( 420 AD7793_REG_CONF, 421 command, 422 2, 423 1); // CS is modified by SPI read/write functions. 424} 425/***************************************************************************//** 426 * @brief Enable unipolar coding of AD7793. 427 * 428 * @param None. 429 * 430 * @return None. 431*******************************************************************************/ 432void AD7793_EnableUnipolar(void) 433 434{ 435 unsigned long command; 436 437 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 438 2, 439 1); // CS is modified by SPI read/write functions. 440 command &= ~AD7793_CONF_UNIPOLAR; 441 command |= AD7793_CONF_UNIPOLAR; 442 AD7793_SetRegisterValue( 443 AD7793_REG_CONF, 444 command, 445 2, 446 1); // CS is modified by SPI read/write functions. 447} 448/***************************************************************************//** 449 * @brief Enable bipolar coding of AD7793. 450 * 451 * @param None. 452 * 453 * @return None. 454*******************************************************************************/ 455void AD7793_DisableBipolar(void) 456 457{ 458 unsigned long command; 459 460 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 461 2, 462 1); // CS is modified by SPI read/write functions. 463 command &= ~AD7793_CONF_UNIPOLAR; 464 AD7793_SetRegisterValue( 465 AD7793_REG_CONF, 466 command, 467 2, 468 1); // CS is modified by SPI read/write functions. 469} 470/***************************************************************************//** 471 * @brief Enable bias voltage generator current boost of AD7793. 472 * 473 * @param None. 474 * 475 * @return None. 476*******************************************************************************/ 477void AD7793_EnableCurrBoost(void) 478 479{ 480 unsigned long command; 481 482 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 483 2, 484 1); // CS is modified by SPI read/write functions. 485 command &= ~AD7793_CONF_BOOST; 486 command |= AD7793_CONF_BOOST; 487 AD7793_SetRegisterValue( 488 AD7793_REG_CONF, 489 command, 490 2, 491 1); // CS is modified by SPI read/write functions. 492} 493/***************************************************************************//** 494 * @brief Disable bias voltage generator current boost of AD7793. 495 * 496 * @param None. 497 * 498 * @return None. 499*******************************************************************************/ 500void AD7793_DisableCurrBoost(void) 501 502{ 503 unsigned long command; 504 505 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 506 2, 507 1); // CS is modified by SPI read/write functions. 508 command &= ~AD7793_CONF_BOOST; 509 AD7793_SetRegisterValue( 510 AD7793_REG_CONF, 511 command, 512 2, 513 1); // CS is modified by SPI read/write functions. 514} 515/***************************************************************************//** 516 * @brief Set the gain of the In-amp. 517 * 518 * @param gain - Gain. 519 * 520 * @return None. 521*******************************************************************************/ 522void AD7793_SetGain(unsigned long gain) 523 { 524 unsigned long command; 525 526 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 527 2, 528 1); // CS is modified by SPI read/write functions. 529 command &= ~AD7793_CONF_GAIN(0xFF); 530 command |= AD7793_CONF_GAIN(gain); 531 AD7793_SetRegisterValue( 532 AD7793_REG_CONF, 533 command, 534 2, 535 1); // CS is modified by SPI read/write functions. 536} 537/***************************************************************************//** 538 * @brief Set the reference voltage source for the ADC. 539 * 540 * @param type - Type of the reference. 541 * Example: AD7793_REFSEL_EXT - External Reference Selected 542 * AD7793_REFSEL_INT - Internal Reference Selected. 543 * 544 * @return None. 545*******************************************************************************/ 546void AD7793_SetIntReference(unsigned char type) 547{ 548 unsigned long command = 0; 549 550 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 551 2, 552 1); // CS is modified by SPI read/write functions. 553 command &= ~AD7793_CONF_REFSEL(AD7793_REFSEL_INT); 554 command |= AD7793_CONF_REFSEL(type); 555 AD7793_SetRegisterValue(AD7793_REG_CONF, 556 command, 557 2, 558 1); // CS is modified by SPI read/write functions. 559} 560/***************************************************************************//** 561 * @brief Enable buffered mode of AD7793. 562 * 563 * @param None. 564 * 565 * @return None. 566*******************************************************************************/ 567void AD7793_EnableBufMode(void) 568{ 569 unsigned long command; 570 571 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 572 2, 573 1); // CS is modified by SPI read/write functions. 574 command &= ~AD7793_CONF_BUF; 575 command |= AD7793_CONF_BUF; 576 AD7793_SetRegisterValue( 577 AD7793_REG_CONF, 578 command, 579 2, 580 1); // CS is modified by SPI read/write functions. 581} 582/***************************************************************************//** 583 * @brief Disable buffered mode of AD7793. 584 * 585 * @param None. 586 * 587 * @return None. 588*******************************************************************************/ 589void AD7793_DisableBufMode(void) 590{ 591 unsigned long command; 592 593 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 594 2, 595 1); // CS is modified by SPI read/write functions. 596 command &= ~AD7793_CONF_BUF; 597 AD7793_SetRegisterValue( 598 AD7793_REG_CONF, 599 command, 600 2, 601 1); // CS is modified by SPI read/write functions. 602} 603/***************************************************************************//** 604 * @brief Selects the channel of AD7793. 605 * 606 * @param channel - ADC channel selection. 607 * 608 * @return None. 609*******************************************************************************/ 610void AD7793_SetChannel(unsigned long channel) 611{ 612 unsigned long command; 613 614 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 615 2, 616 1); // CS is modified by SPI read/write functions. 617 command &= ~AD7793_CONF_CHAN(0xFF); 618 command |= AD7793_CONF_CHAN(channel); 619 AD7793_SetRegisterValue( 620 AD7793_REG_CONF, 621 command, 622 2, 623 1); // CS is modified by SPI read/write functions. 624} 625/***************************************************************************//** 626 * @brief Performs the given calibration to the specified channel. 627 * 628 * @param mode - Calibration type. 629 * @param channel - Channel to be calibrated. 630 * 631 * @return none. 632*******************************************************************************/ 633void AD7793_Calibrate(unsigned char mode, unsigned char channel) 634{ 635 //unsigned short oldRegValue = 0x0; //J'ai remplacé short par long ???? 636 //unsigned short newRegValue = 0x0; 637 638 unsigned long oldRegValue = 0x0; 639 unsigned long newRegValue = 0x0; 640 641 AD7793_SetChannel(channel); 642 oldRegValue = AD7793_GetRegisterValue(AD7793_REG_MODE, 2, 1); // CS is modified by SPI read/write functions. 643 oldRegValue &= ~AD7793_MODE_SEL(0x7); 644 newRegValue = oldRegValue | AD7793_MODE_SEL(mode); 645 AD7793_SetRegisterValue(AD7793_REG_MODE, 646 newRegValue, 647 2, 648 0); // CS is not modified by SPI read/write functions. 649 AD7793_WaitRdyGoLow(); 650 ADI_PART_CS_HIGH; 651} 652 653/***************************************************************************//** 654 * @brief Returns the result of a single conversion. 655 * 656 * @return regData - Result of a single analog-to-digital conversion. 657*******************************************************************************/ 658unsigned long AD7793_SingleConversion(void) 659{ 660 unsigned long command = 0x0; 661 unsigned long regData = 0x0; 662 663 command = AD7793_MODE_SEL(AD7793_MODE_SINGLE); 664 AD7793_SetRegisterValue(AD7793_REG_MODE, 665 command, 666 2, 667 0);// CS is not modified by SPI read/write functions. 668 AD7793_WaitRdyGoLow(); 669 regData = AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions. 670 ADI_PART_CS_HIGH; 671 672 return(regData); 673} 674 675/***************************************************************************//** 676 * @brief Returns the average of several conversion results. 677 * 678 * @return samplesAverage - The average of the conversion results. 679*******************************************************************************/ 680unsigned long AD7793_ContinuousReadAvg(unsigned char sampleNumber) 681{ 682 unsigned long samplesAverage = 0x0; 683 unsigned long command = 0x0; 684 unsigned char count = 0x0; 685 686 command = AD7793_MODE_SEL(AD7793_MODE_CONT); 687 AD7793_SetRegisterValue(AD7793_REG_MODE, 688 command, 689 2, 690 0);// CS is not modified by SPI read/write functions. 691 for(count = 0;count < sampleNumber;count ++) 692 { 693 AD7793_WaitRdyGoLow(); 694 samplesAverage += AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions. 695 } 696 ADI_PART_CS_HIGH; 697 samplesAverage = samplesAverage / sampleNumber; 698 return(samplesAverage); 699} 700 701/***************************************************************************//** 702 * @brief Returns a single measurement, provided continuous mesurement mode has been set up. 703 * 704 * @return samplesAverage - Result of a single analog-to-digital conversion. 705*******************************************************************************/ 706unsigned long AD7793_ContinuousSingleRead() 707{ 708 unsigned long regData = 0x0; 709 710 AD7793_WaitRdyGoLow(); 711 regData = AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 1); // CS is modified by SPI read/write functions. 712 ADI_PART_CS_HIGH; 713 return(regData); 714}
AD7793.h
c_cpp
1/***************************************************************************//** 2 * @file AD7793.h 3 * @brief Header file of AD7793 Driver. 4 * @author Bancisor Mihai 5******************************************************************************** 6 * Copyright 2012(c) Analog Devices, Inc. 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * - Neither the name of Analog Devices, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * - The use of this software may or may not infringe the patent rights 22 * of one or more patent holders. This license does not release you 23 * from the requirement that you obtain separate licenses from these 24 * patent holders to use this software. 25 * - Use of the software either in source or binary form, must be run 26 * on or directly connected to an Analog Devices Inc. component. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39******************************************************************************** 40 * SVN Revision: 500 41******************************************************************************* 42 43******************************************************************************** 44 * Modified and adapted for Arduino by Ph. Sonnet. March 27, 2019, Version 1.1 45*******************************************************************************/ 46 47#ifndef __AD7793_H__ 48#define __AD7793_H__ 49 50/******************************************************************************/ 51/* AD7793 */ 52/******************************************************************************/ 53 54/* AD7793 GPIO */ 55#define AD7793_RDY_STATE GPIO1_STATE 56 57/*AD7793 Registers*/ 58#define AD7793_REG_COMM 0 /* Communications Register(WO, 8-bit) */ 59#define AD7793_REG_STAT 0 /* Status Register (RO, 8-bit) */ 60#define AD7793_REG_MODE 1 /* Mode Register (RW, 16-bit */ 61#define AD7793_REG_CONF 2 /* Configuration Register (RW, 16-bit)*/ 62#define AD7793_REG_DATA 3 /* Data Register (RO, 16-/24-bit) */ 63#define AD7793_REG_ID 4 /* ID Register (RO, 8-bit) */ 64#define AD7793_REG_IO 5 /* IO Register (RO, 8-bit) */ 65#define AD7793_REG_OFFSET 6 /* Offset Register (RW, 24-bit */ 66#define AD7793_REG_FULLSCAL 7 /* Full-Scale Register (RW, 24-bit */ 67 68/* Communications Register Bit Designations (AD7793_REG_COMM) */ 69#define AD7793_COMM_WEN (1 << 7) /* Write Enable */ 70#define AD7793_COMM_WRITE (0 << 6) /* Write Operation */ 71#define AD7793_COMM_READ (1 << 6) /* Read Operation */ 72#define AD7793_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */ 73#define AD7793_COMM_CREAD (1 << 2) /* Continuous Read of Data Register */ 74 75/* Status Register Bit Designation masks(AD7793_REG_STAT) */ 76#define AD7793_STAT_RDY (1 << 7) /* Ready */ 77#define AD7793_STAT_ERR (1 << 6) /* Error (Overrange, Underrange) */ 78#define AD7793_STAT_CH3 (1 << 2) /* Channel 3 */ 79#define AD7793_STAT_CH2 (1 << 1) /* Channel 2 */ 80#define AD7793_STAT_CH1 (1 << 0) /* Channel 1 */ 81 82/* Mode Register Bit Designations (AD7793_REG_MODE) */ 83#define AD7793_MODE_SEL(x) (((x) & 0x7) << 13) /* Operation Mode Select */ 84#define AD7793_MODE_CLKSRC(x) (((x) & 0x3) << 6) /* ADC Clock Source Select */ 85#define AD7793_MODE_RATE(x) ((x) & 0xF) /* Filter Update Rate Select */ 86 87/* AD7793_MODE_SEL(x) options */ 88#define AD7793_MODE_CONT 0 /* Continuous Conversion Mode */ 89#define AD7793_MODE_SINGLE 1 /* Single Conversion Mode */ 90#define AD7793_MODE_IDLE 2 /* Idle Mode */ 91#define AD7793_MODE_PWRDN 3 /* Power-Down Mode */ 92#define AD7793_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */ 93#define AD7793_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */ 94#define AD7793_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */ 95#define AD7793_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */ 96 97/* AD7793_MODE_CLKSRC(x) options */ 98#define AD7793_CLK_INT 0 /* Internal 64 kHz Clk not available at the CLK pin */ 99#define AD7793_CLK_INT_CO 1 /* Internal 64 kHz Clk available at the CLK pin */ 100#define AD7793_CLK_EXT 2 /* External 64 kHz Clock */ 101#define AD7793_CLK_EXT_DIV2 3 /* External Clock divided by 2 */ 102 103/* AD7793_MODE_RATE(x) options */ 104#define AD7793_RATE_NIL 0 105#define AD7793_RATE_470 1 106#define AD7793_RATE_242 2 107#define AD7793_RATE_123 3 108#define AD7793_RATE_62 4 109#define AD7793_RATE_50 5 110#define AD7793_RATE_39 6 111#define AD7793_RATE_33_2 7 112#define AD7793_RATE_19_6 8 /* 60 Hz only */ 113#define AD7793_RATE_16_7_50 9 /* 50 Hz only */ 114#define AD7793_RATE_16_7_50_60 10 /* 50 and 60 Hz */ 115#define AD7793_RATE_12_5 11 /* 50 and 60 Hz */ 116#define AD7793_RATE_10 12 /* 50 and 60 Hz */ 117#define AD7793_RATE_8_33 13 /* 50 and 60 Hz */ 118#define AD7793_RATE_6_25 14 /* 50 and 60 Hz */ 119#define AD7793_RATE_4_17 15 /* 50 and 60 Hz */ 120 121/* Configuration Register Bit Designations (AD7793_REG_CONF) */ 122#define AD7793_CONF_VBIAS(x) (((x) & 0x3) << 14) /* Bias Voltage Generator Enable */ 123#define AD7793_CONF_BO_EN (1 << 13) /* Burnout Current Enable */ 124#define AD7793_CONF_UNIPOLAR (1 << 12) /* Unipolar/Bipolar Enable */ 125#define AD7793_CONF_BOOST (1 << 11) /* Boost Enable */ 126#define AD7793_CONF_GAIN(x) (((x) & 0x7) << 8) /* Gain Select */ 127#define AD7793_CONF_REFSEL(x) (((x) & 0x1) << 7) /* INT/EXT Reference Select */ 128#define AD7793_CONF_BUF (1 << 4) /* Buffered Mode Enable */ 129#define AD7793_CONF_CHAN(x) ((x) & 0x7) /* Channel select */ 130 131/* AD7793_CONF_VBIAS(x) options */ 132#define AD7793_VBIAS_GEN_DISABL 0 /* Bias voltage generator disabled */ 133#define AD7793_VBIAS_AIN1_NEG 1 /* Bias voltage generator to AIN1(-) */ 134#define AD7793_VBIAS_AIN2_NEG 2 /* Bias voltage generator to AIN2(-) */ 135 136/* AD7793_CONF_GAIN(x) options Gain and ADC input range (2.5V reference)*/ 137#define AD7793_GAIN_1 0 /* 1X (In-amp nor used) 2.5 */ 138#define AD7793_GAIN_2 1 /* 2X (In-amp nor used) 1.25 */ 139#define AD7793_GAIN_4 2 /* 4X 625mV */ 140#define AD7793_GAIN_8 3 /* 8X 312.5 mV*/ 141#define AD7793_GAIN_16 4 /* 16X 156.2 mV*/ 142#define AD7793_GAIN_32 5 /* 32X 78.125 mV */ 143#define AD7793_GAIN_64 6 /* 64X 39.06 mV */ 144#define AD7793_GAIN_128 7 /* 128X 19.53 mV */ 145 146/* AD7793_CONF_REFSEL(x) options */ 147#define AD7793_REFSEL_INT 1 /* Internal reference selected. */ 148#define AD7793_REFSEL_EXT 0 /* External reference applied between REFIN(+) and REFIN(–). */ 149 150/* AD7793_CONF_CHAN(x) options */ 151#define AD7793_CH_AIN1P_AIN1M 0 /* Select channel AIN1(+) - AIN1(-) */ 152#define AD7793_CH_AIN2P_AIN2M 1 /* Select channel AIN2(+) - AIN2(-) */ 153#define AD7793_CH_AIN3P_AIN3M 2 /* Select channel AIN3(+) - AIN3(-) */ 154#define AD7793_CH_AIN1M_AIN1M 3 /* Select channel AIN1(-) - AIN1(-) */ 155#define AD7793_CH_TEMP 6 /* Temp Sensor, gain 1, Internal reference */ 156#define AD7793_CH_AVDD_MONITOR 7 /* AVDD voltage Monitor, gain 1/6, Internal 1.17V reference */ 157 158/* ID Register Bit Designations (AD7793_REG_ID) */ 159#define AD7793_ID 0xB 160#define AD7793_ID_MASK 0xF 161 162/* IO (Excitation Current Sources) Register Bit Designations (AD7793_REG_IO) */ 163#define AD7793_IEXCDIR(x) (((x) & 0x3) << 2) 164#define AD7793_IEXCEN(x) (((x) & 0x3) << 0) 165 166/* AD7793_IEXCDIR(x) options*/ 167#define AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2 0 /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */ 168#define AD7793_DIR_IEXC1_IOUT2_IEXC2_IOUT1 1 /* IEXC1 connect to IOUT2, IEXC2 connect to IOUT1 */ 169#define AD7793_DIR_IEXC1_IEXC2_IOUT1 2 /* Both current sources IEXC1,2 connect to IOUT1 */ 170#define AD7793_DIR_IEXC1_IEXC2_IOUT2 3 /* Both current sources IEXC1,2 connect to IOUT2 */ 171 172/* AD7793_IEXCEN(x) options*/ 173#define AD7793_EN_IXCEN_DISABLE 0 /* Disable excitation current*/ 174#define AD7793_EN_IXCEN_10uA 1 /* Excitation Current 10uA */ 175#define AD7793_EN_IXCEN_210uA 2 /* Excitation Current 210uA */ 176#define AD7793_EN_IXCEN_1mA 3 /* Excitation Current 1mA */ 177 178/******************************************************************************/ 179/* Functions Prototypes */ 180/******************************************************************************/ 181 182/* Initialize AD7793 and check if the device is present*/ 183unsigned char AD7793_Init(void); 184 185/* Sends 32 consecutive 1's on SPI in order to reset the part. */ 186void AD7793_Reset(void); 187 188/* Reads the Ready Bit for ADC */ 189unsigned char AD7793_Ready(); 190 191/* Reads the ADC Error Bit */ 192unsigned char AD7793_Error(); 193 194/* Indicates that channel 3 is being converted by the ADC */ 195unsigned char AD7793_Channel3(); 196 197/* Indicates that channel 2 is being converted by the ADC */ 198unsigned char AD7793_Channel2(); 199 200/* Indicates that channel 1 is being converted by the ADC */ 201unsigned char AD7793_Channel1(); 202 203 204/* Reads the value of the selected register. */ 205unsigned long AD7793_GetRegisterValue(unsigned char regAddress, 206 unsigned char size, 207 unsigned char modifyCS); 208 209/* Writes a value to the register. */ 210void AD7793_SetRegisterValue(unsigned char regAddress, 211 unsigned long regValue, 212 unsigned char size, 213 unsigned char modifyCS); 214 215/* Waits for RDY pin to go low. */ 216void AD7793_WaitRdyGoLow(void); 217 218/* Sets the operating mode of AD7793 */ 219void AD7793_SetMode(unsigned long mode); 220 221/* Sets the ADC clock source of AD7793 */ 222void AD7793_SetClockSource(unsigned long clockSource); 223 224/* Sets the filter update rate of AD7793 */ 225void AD7793_SetFilterUpdateRate(unsigned long filterRate); 226 227/* Sets the direction of the internal excitation current source */ 228void AD7793_SetExcitDirection(unsigned long direction); 229 230/* Sets the current of the internal excitation current source */ 231void AD7793_SetExcitCurrent(unsigned long current); 232 233/* Bias voltage generator enable */ 234void AD7793_SetBiasVoltage(unsigned long voltage); 235 236/* Enable burnout current */ 237void AD7793_EnableBurnoutCurr(void); 238 239/* Disable burnout current of AD7793 */ 240void AD7793_DisableBurnoutCurr(void); 241 242/* Enable unipolar coding of AD7793 */ 243void AD7793_EnableUnipolar(void); 244 245/* Disable bipolar coding of AD7793 */ 246void AD7793_DisableBipolar(void); 247 248/* Enable bias voltage generator current boost of AD7793 */ 249void AD7793_EnableCurrBoost(void); 250 251/* Disable bias voltage generator current boost of AD7793 */ 252void AD7793_DisableCurrBoost(void); 253 254/* Sets the gain of the In-Amp */ 255void AD7793_SetGain(unsigned long gain); 256 257/* Sets the reference source for the ADC */ 258void AD7793_SetIntReference(unsigned char type); 259 260/* Enable buffered mode of AD7793*/ 261void AD7793_EnableBufMode(void); 262 263/* Disable buffered mode of AD7793 */ 264void AD7793_DisableBufMode(void); 265 266/* Selects the channel of AD7793 */ 267void AD7793_SetChannel(unsigned long channel); 268 269/* Performs the given calibration to the specified channel. */ 270void AD7793_Calibrate(unsigned char mode, unsigned char channel); 271 272/* Returns the result of a single conversion. */ 273unsigned long AD7793_SingleConversion(void); 274 275/* Returns the average of several conversion results. */ 276unsigned long AD7793_ContinuousReadAvg(unsigned char sampleNumber); 277 278/* Returns a single measurement, provided continuous mesurement mode has been set up. */ 279unsigned long AD7793_ContinuousSingleRead(); 280 281#endif // _AD7793_H_ 282
AD7793_example.ino
arduino
This is an example sketch for testing communication between the Arduino and the AD7793 using the AD7793 library from Analog Devices which I have adapted for the Arduino. The sketch reads the ID number of the AD7793, then uses the ADC to measure the voltage on the analog voltage pin 13 and the temperature inside the chip. These are the 3 functions that can be performed by the barebone chip (not counting in the pullup resistors which must be present).
1/* 2This is an example sketch for testing communication between the Arduino and the 3AD7793 using the AD7793 library on the Analog Devices web site adapted for the Arduino 4by Ph. Sonnet, April 8, 2019 5 6The AD7793 and the Arduino Atmega328 R3 are connected in the following way : 7 8Arduino R3 pin# AD7793 pin# Pullup resistors 9 8 GPIO 3 ~CS 50 KOhm 10 9 RDY 15 DOUT/~RDY 11 12 MISO 15 DOUT/~RDY 12 11 MOSI 16 DIN 50 KOhm 13 10 CS not connected 14 13 SCK 1 CLK 50 KOhm 15 16The sketch reads the ID number of the AD7793, then uses the ADC to 17measure the voltage on the analog voltage pin 13 and the temperature 18inside the chip. These are the 3 functions that can be performed by the 19barebone chip (not counting in the pullup resistors which must be present). 20 21 22*/ 23 #include <AD7793.h> 24 #include <Communication.h> 25 #include <SPI.h> 26 float Vref = 1.17; /* AD7783 internal reference voltage */ 27 28void setup() { 29 30 Serial.begin(9600); 31 while (!Serial); 32 33 unsigned char answer = AD7793_Init(); /* Initializes the AD7793 and checks if the device is present*/ 34 Serial.print("AD7793 status = "); 35 Serial.println(answer); /* Answer is 1 when the device is initialized and the ID is read and recognized */ 36 Serial.println(""); 37} 38 39void loop() { 40 41 AD7793_SetChannel(AD7793_CH_AVDD_MONITOR); /* AVDD Monitor, gain 1/6, internal 1.17V reference */ 42 unsigned long conv = AD7793_SingleConversion(); /* Returns the result of a single conversion. */ 43 float AVDD = ((conv - 8388608.0) / 8388608.0) * 1.17 / (1/6.0) ; /* Note: 8388608 = 2exp(23) = 0x8000000 = the output code of 0 V in bipolar mode */ 44 Serial.print("Analog supply voltage (AVDD) = "); 45 Serial.print(AVDD, 4); 46 Serial.println(" V"); 47 48 AD7793_SetChannel(AD7793_CH_TEMP); /* Temp Sensor, gain 1, Internal current reference */ 49 conv = AD7793_SingleConversion(); /* Returns the result of a single conversion. */ 50 float Temp = (((conv - 8388608.0) / 8388608.0) * 1.17 * 1000 / 0.810) - 273; /* Sentitivity is approximately 0.81 mV/°K, according to AD7793 datasheet */ 51 /* To improve precision, it should be further calibrated by the user. */ 52 Serial.print("Chip temperature = "); 53 Serial.print(Temp, 2); 54 Serial.println(" C"); 55 56 delay(1000); 57} 58
Communication.cpp
c_cpp
1/***************************************************************************//** 2 * @file Communication.c 3 * @brief Implementation of Communication Driver. 4 * @author DBogdan (dragos.bogdan@analog.com) 5******************************************************************************** 6 * Copyright 2012(c) Analog Devices, Inc. 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * - Neither the name of Analog Devices, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * - The use of this software may or may not infringe the patent rights 22 * of one or more patent holders. This license does not release you 23 * from the requirement that you obtain separate licenses from these 24 * patent holders to use this software. 25 * - Use of the software either in source or binary form, must be run 26 * on or directly connected to an Analog Devices Inc. component. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39******************************************************************************** 40 * SVN Revision: 500 41******************************************************************************* 42 43******************************************************************************** 44 * Modified and adapted for Arduino by Ph. Sonnet. March 27, 2019, Version 1.1 45*******************************************************************************/ 46 47/******************************************************************************/ 48/* Include Files */ 49/******************************************************************************/ 50#include "AD7793.h" 51#include "Arduino.h" 52#include "Communication.h" 53#include "SPI.h" 54 55/***************************************************************************//** 56 * @brief Initializes the SPI communication peripheral. 57 * 58 * @param lsbFirst - Transfer format (0 or 1). 59 * Example: 0x0 - MSB first. 60 * 0x1 - LSB first. 61 * @param clockFreq - SPI clock frequency (Hz). 62 * Example: 1000 - SPI clock frequency is 1 kHz. 63 * @param clockPol - SPI clock polarity (0 or 1). 64 * Example: 0x0 - idle state for SPI clock is low. 65 * 0x1 - idle state for SPI clock is high. 66 * @param clockPha - SPI clock phase (0 or 1). 67 * Example: 0x0 - data is latched on the leading edge of SPI 68 * clock and data changes on trailing edge. 69 * 0x1 - data is latched on the trailing edge of SPI 70 * clock and data changes on the leading edge. 71 * 72 * @return 0 - Initialization failed, 1 - Initialization succeeded. 73*******************************************************************************/ 74 75unsigned char SPI_Init() 76{ ADI_PART_CS_PIN_OUT; 77 ADI_PART_CS_HIGH; 78 GPIO1_PIN_IN; 79 80 SPI.begin(); // configure the SPI port for your SPI device. 81 SPI.beginTransaction(SPI_SETTINGS); // implements the SPI communication settings you defined in Communication.h 82 return(1); 83} 84 85/***************************************************************************//** 86 * @brief Writes data to SPI. 87 * 88 * @param data - Write data buffer: 89 * - first byte is the chip select number; 90 * - from the second byte onwards are located data bytes to write. 91 * @param bytesNumber - Number of bytes to write. 92 * 93 * @return Number of written bytes. 94*******************************************************************************/ 95unsigned char SPI_Write(unsigned char* data, 96 unsigned char bytesNumber) 97{ 98 int SCNumber = data[0]; 99 100 ADI_PART_CS_LOW; 101 SPI.transfer(&data[1],bytesNumber); 102 if (SCNumber == 0x1) { 103 ADI_PART_CS_HIGH; 104 } 105 return(bytesNumber); 106} 107 108/***************************************************************************//** 109 * @brief Reads data from SPI. 110 * 111 * @param data - As an input parameter, data represents the write buffer: 112 * - first byte is the chip select number; 113 * - from the second byte onwards are located data bytes to write. 114 * As an output parameter, data represents the read buffer: 115 * - from the first byte onwards are located the read data bytes. 116 * @param bytesNumber - Number of bytes to write. 117 * 118 * @return Number of written bytes. 119*******************************************************************************/ 120unsigned char SPI_Read(unsigned char* data, 121 unsigned char bytesNumber) 122{ int i = 0; 123 124 125 ADI_PART_CS_LOW; 126 int SCNumber = data[0]; 127 for(i = 1 ;i < bytesNumber + 1 ; i ++) 128 { 129 data[i-1] = SPI.transfer(data[i]); 130 } 131 if (SCNumber == 0x1) { 132 ADI_PART_CS_HIGH; 133 } 134 return(bytesNumber); 135 } 136 137 138 139 140 141 142
Readme.txt
text
1How to implement the AD7793 library (Ph. Sonnet, Mach 29, 2019) 2=================================== 3 4Directory AD7793 must be created in the Aduino "libraries" directory 5and must contain the following files : 6 7AD7793.h 8AD7793.cpp 9Communication.h 10Communication.cpp 11 12 13How to connect the AD7793 to the Arduino Atmega328 or MEGA 14=========================================================== 15 16AD7793 pin 16 (DIN), Master Out Slave In (MOSI), must be connected to pin 11 (for Atmega328) or pin 51 (for MEGA). 17 18AD7793 pin 15 (DOUT/~RDY), Master In Slave Out (MISO), must be connected to pin 12 (for Atmega328) or pin 50 (for MEGA). 19It must also be connected to another digital pin af the Arduino. Suppose that you choose pin 9 for this purpose. 20This pin number must be specified in the Communication.h file by editing the #define ADI_PAR_CS_PIN line in the "GPIO Definitions" section. 21 22AD7793 pin 1 (CLK), Serial Clock (SCK), must be connected to Pin 13 (for Atmega328) or pin 52 (for MEGA). 23 24AD7793 pin 3(~CS), Chip Select (CS) should not be connected to pin 10 (for Atmega328) or pin 53 (for MEGA), as it is supposed by the SPI library. 25Instead, another digital pin of the Arduino must be used. Suppose that you choose pin 8 for this purpose. 26This pin number must be specified in the Communication.h file by editing the #define ADI_PAR_CS_PIN line in the "GPIO Definitions" section. 27 28From my experience, it is crucial to insert pullup resistors (for instance, 50 KOhm) in the MOSI, CS and CLK circuits, to ensure reliable 29communication between the AD7793 and the Arduino. 30 31 32Preprocessing instructions 33========================== 34 Here are the preprocessing instruction which must be inserted before setup() 35 into your sketch. 36 37#include <AD7793.h> 38#include <Communication.h> 39#include <SPI.h> 40 41Initialization 42============== 43 This must be inserted as such into setup(). No need to initialize the 44 SPI channel, because this is taken care of by the AD7793_Init()function. 45 46unsigned char answer = AD7793_Init(); /* Initializes the AD7793 and checks if the device is present*/ 47AD7793_Reset(); /* Sends 32 consecutive 1 on SPI in order to reset the part */ 48 49 50Preprogramed register setting functions 51======================================== 52 Just insert these functions into your sketch as they are listed here, and they will 53 access the various AD7793 registers (Status, Mode, Configuration, Data, IO and Data) and set parameters 54 according to the needs of your project. 55 56Status register 57............... 58 59unsigned char answer = AD7793_Ready(); /* Reads the Ready Bit for ADC */ 60unsigned char answer = AD7793_Error(); /* Reads the ADC Error Bit */ 61unsigned char answer = AD7793_Channel3(); /* Indicates that channel 3 is being converted by the ADC */ 62unsigned char answer = AD7793_Channel2(); /* Indicates that channel 2 is being converted by the ADC */ 63unsigned char answer = AD7793_Channel1(); /* Indicates that channel 1 is being converted by the ADC */ 64 65Mode register 66.............. 67 68AD7793_SetMode(AD7793_MODE_CONT); /* Continuous Conversion Mode */ 69D7793_SetMode(AD7793_MODE_SINGLE); /* Single Conversion Mode */ 70AD7793_SetMode(AD7793_MODE_IDLE); /* Idle Mode */ 71AD7793_SetMode(AD7793_MODE_PWRDN); /* Power-Down Mode */ 72AD7793_SetMode(AD7793_MODE_CAL_INT_ZERO); /* Internal Zero-Scale Calibration */ 73AD7793_SetMode(AD7793_MODE_CAL_INT_FULL); /* Internal Full-Scale Calibration */ 74AD7793_SetMode(AD7793_MODE_CAL_SYS_ZERO); /* System Zero-Scale Calibration */ 75AD7793_SetMode(AD7793_MODE_CAL_SYS_FULL); /* System Full-Scale Calibration */ 76 77AD7793_SetClockSource(AD7793_CLK_INT); /* Internal 64 kHz Clk not available at the CLK pin */ 78AD7793_SetClockSource(AD7793_CLK_INT_CO); /* Internal 64 kHz Clk available at the CLK pin */ 79AD7793_SetClockSource(AD7793_CLK_EXT); /* External 64 kHz Clock */ 80AD7793_SetClockSource(AD7793_CLK_EXT_DIV2); /* External Clock divided by 2 */ 81 82AD7793_SetFilterUpdateRate(AD7793_RATE_NIL); /* Filter Update Rate: nil */ 83AD7793_SetFilterUpdateRate(AD7793_RATE_470); /* Filter Update Rate: 470Hz */ 84AD7793_SetFilterUpdateRate(AD7793_RATE_242); /* Filter Update Rate: 242Hz */ 85AD7793_SetFilterUpdateRate(AD7793_RATE_123); /* Filter Update Rate: 123Hz */ 86AD7793_SetFilterUpdateRate(AD7793_RATE_62); /* Filter Update Rate: 62Hz 87AD7793_SetFilterUpdateRate(AD7793_RATE_50); /* Filter Update Rate: 50Hz */ 88AD7793_SetFilterUpdateRate(AD7793_RATE_39); /* Filter Update Rate: 39Hz */ 89AD7793_SetFilterUpdateRate(AD7793_RATE_33_2); /* Filter Update Rate: 33.2Hz */ 90AD7793_SetFilterUpdateRate(AD7793_RATE_19_6); /* Filter Update Rate: 19.6Hz 90dB (60Hz only) */ 91AD7793_SetFilterUpdateRate(AD7793_RATE_16_7_50_60); /* Filter Update Rate: 16.7Hz 80dB (50Hz only) */ 92AD7793_SetFilterUpdateRate(AD7793_RATE_12_5); /* Filter Update Rate: 12.5Hz 66dB (50 and 60Hz) */ 93AD7793_SetFilterUpdateRate(AD7793_RATE_10); /* Filter Update Rate: 10Hz 69dB (50 and 60Hz) */ 94AD7793_SetFilterUpdateRate(AD7793_RATE_8_33); /* Filter Update Rate: 8.33Hz 70dB (50 and 60Hz) */ 95AD7793_SetFilterUpdateRate(AD7793_RATE_6_25); /* Filter Update Rate: 6.25Hz 72dB (50 and 60Hz) */ 96AD7793_SetFilterUpdateRate(AD7793_RATE_4_17); /* Filter Update Rate: 4.17Hz 74dB (50 and 60Hz) */ 97 98Configuration register 99...................... 100 101AD7793_SetBiasVoltage(AD7793_VBIAS_GEN_DISABL); /* Bias voltage generator disabled */ 102AD7793_SetBiasVoltage(AD7793_VBIAS_AIN1_NEG); /* Bias voltage generator to AIN1(-) * 103AD7793_SetBiasVoltage(AD7793_VBIAS_AIN2_NEG); /* Bias voltage generator to AIN2(-) */ 104 105AD7793_EnableBurnoutCurr(); /* Enable burnout current of AD7793 */ 106AD7793_DisableBurnoutCurr(); /* Disable burnout current of AD7793 */ 107 108AD7793_EnableUnipolar(); /* Enable unipolar coding of AD7793 */ 109AD7793_DisableBipolar(); /* Enable bipolar coding of AD7793 */ 110 111AD7793_EnableCurrBoost(); /* Enable bias voltage generator current boost of AD7793 */ 112AD7793_DisableCurrBoost(); /* Disable bias voltage generator current boost of AD7793 */ 113 114AD7793_AD7793_SetGain(AD7793_GAIN_1); /* Gain of 1, In-amp not used */ 115AD7793_AD7793_SetGain(AD7793_GAIN_2); /* Gain of 2, In-amp not used */ 116AD7793_AD7793_SetGain(AD7793_GAIN_4); /* Set the gain of the In-amp to 4 */ 117AD7793_AD7793_SetGain(AD7793_GAIN_8); /* Set the gain of the In-amp to 8 */ 118AD7793_AD7793_SetGain(AD7793_GAIN_16); /* Set the gain of the In-amp to 16 */ 119AD7793_AD7793_SetGain(AD7793_GAIN_32); /* Set the gain of the In-amp to 32 */ 120AD7793_AD7793_SetGain(AD7793_GAIN_64); /* Set the gain of the In-amp to 64 */ 121AD7793_AD7793_SetGain(AD7793_GAIN_128); /* Set the gain of the In-amp to 128 */ 122 123AD7793_SetIntReference(AD7793_REFSEL_INT); /* Select internal reference voltage source for the ADC */ 124AD7793_SetIntReference(AD7793_REFSEL_EXT); /* External voltage reference applied between REFIN(+) and REFIN(–) */ 125 126AD7793_EnableBufMode(); /* Enable buffered mode of AD7793 */ 127AD7793_DisableBufMode(); /* Disable buffered mode of AD7793 */ 128 129AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M); /* Select channel AIN1(+) - AIN1(-) */ 130AD7793_SetChannel(AD7793_CH_AIN2P_AIN2M); /* Select channel AIN2(+) - AIN2(-) */ 131AD7793_SetChannel(AD7793_CH_AIN3P_AIN3M); /* Select channel AIN3(+) - AIN3(-) */ 132AD7793_SetChannel(AD7793_CH_TEMP); /* Temp Sensor, gain 1, Internal current reference */ 133AD7793_SetChannel(AD7793_CH_AVDD_MONITOR); /* AVDD Monitor, gain 1/6, internal 1.17V reference */ 134 135IO Register 136=========== 137 138AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2); /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */ 139AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT2_IEXC2_IOUT1); /* IEXC1 connect to IOUT2, IEXC2 connect to IOUT1 */ 140AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IEXC2_IOUT1); /* Both current sources IEXC1,2 connect to IOUT1 */ 141AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IEXC2_IOUT2); /* Both current sources IEXC1,2 connect to IOUT2 */ 142AD7793_SetExcitCurrent(AD7793_EN_IXCEN_DISABLE); /* Disable excitation current*/ 143AD7793_SetExcitCurrent(AD7793_EN_IXCEN_10uA); /* Excitation Current 10uA */ 144AD7793_SetExcitCurrent(AD7793_EN_IXCEN_210uA); /* Excitation Current 210uA */ 145AD7793_SetExcitCurrent(AD7793_EN_IXCEN_1mA); /* Excitation Current 1mA */ 146 147 148Return the result of conversion 149=============================== 150 151unsigned long conv = AD7793_SingleConversion(); /* Returns the result of a single conversion. */ 152unsigned long conv = AD7793_ContinuousReadAvg(unsigned char sampleNumber); /* Returns the average of several conversion results. */ 153unsigned long conv = AD7793_ContinuousSingleRead(); /* Returns a single measurement, provided continuous mesurement mode has been set up. */ 154 155 156Calibration 157=========== 158 159void AD7793_Calibrate(unsigned char mode, unsigned char channel); /* Performs the given calibration to the specified channel. */ 160 161Values for parameter mode: 162For performing an internal Zero-Scale Calibration: AD7793_MODE_CAL_INT_ZERO 163For performing an internal Full-Scale Calibration: AD7793_MODE_CAL_INT_FULL 164For performing a system Zero-Scale Calibration: AD7793_MODE_CAL_SYS_ZERO 165For performing a Full-Scale Calibration: AD7793_MODE_CAL_SYS_FULL 166 167Values for parameter mode: 168For selecting channel 1 AIN1(+) - AIN1(-): AD7793_CH_AIN1P_AIN1M 169For selecting channel 2 AIN2(+) - AIN2(-): AD7793_CH_AIN2P_AIN2M 170For selecting channel 3 AIN3(+) - AIN3(-): AD7793_CH_AIN3P_AIN3M 171 172 173Power-on/reset default register setting 174====================================== 175 176After powering on or resetting the AD7793, the registers are set just as if all the following commands had been executed: 177 178Mode register 179............... 180 181AD7793_SetMode(AD7793_MODE_CONT); /* Continuous Conversion Mode */ 182AD7793_SetClockSource(AD7793_CLK_INT); /* Internal 64 kHz Clk not available at the CLK pin */ 183AD7793_SetFilterUpdateRate(AD7793_RATE_NIL); /* Filter Update Rate: nil */ 184 185Configuration register 186...................... 187 188AD7793_SetBiasVoltage(AD7793_VBIAS_GEN_DISABL); /* Bias voltage generator disabled */ 189AD7793_DisableBurnoutCurr(); /* Disable burnout current of AD7793 */ 190AD7793_DisableBipolar(); /* Enable bipolar coding of AD7793 */ 191AD7793_DisableCurrBoost(); /* Disable bias voltage generator current boost of AD7793 */ 192AD7793_AD7793_SetGain(AD7793_GAIN_128); /* Set the gain of the In-amp to 128 */ 193AD7793_SetIntReference(AD7793_REFSEL_EXT); /* External voltage reference applied between REFIN(+) and REFIN(–) */ 194AD7793_EnableBufMode(); /* Enable buffered mode of AD7793 */ 195AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M); /* Select channel AIN1(+) - AIN1(-) */ 196 197IO register 198........... 199 200AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2); /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */ 201AD7793_SetExcitCurrent(AD7793_EN_IXCEN_DISABLE); /* Disable excitation current*/ 202 203 204 205 206 207
example_Volt_RTD_AD7793.ino
arduino
AD7793_example_Volt_RTD.ino Arduino This example shows how to use an AD7793 converter to perform successively two measurements: 1) a single voltage reading in the range from 0 to 0.58 V on AD7793 channel 2 2) a repeated temperature reading using a 4-wire PT100 RTD and a ratiometric measurement circuit on AD7793 channel 1.
1/* 2 This example shows how to use an AD7793 converter to perform successively two measurements: 3 1) a single voltage reading in the range from 0 to 0.58 V on AD7793 channel 2 (0.58 = 1.17V AD7793 internal reference divided by 2); 4 2) a repeated temperature reading using a 4-wire PT100 RTD and a ratiometric measurement circuit on AD7793 channel 1 (up to 144 C; 5 for higher temperature or to use PT1000, simply lower the gain). 6 7 Using RTDs in a ratiometric measurement has the advantage in that it eliminates sources of error such as the accuracy and drift of 8 the excitation current source (provided by the AD7793) and the influence of the RTC sensor lead resistance. 9 The basics for 4-wire RTD ratiometric measurement and calculation using an ADC can be found at: 10 http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/temperature-measurement/platinum-rtd-sensors/amplifier-circuit-schematics 11 12 The schematics and part specifications used in this example are according to Figure 7 in 13 "Analog Front-End Design Considerations for RTD Ratiometric Temperature Measurements" by B. Zhang and A. Buda: 14 https://www.analog.com/en/analog-dialogue/articles/afe-design-considerations-rtd-ratiometric.html 15 However, in the present example, reference resistor is 4.99 Kohm, 0.1%, 10 ppm/C. 16 17 For additional details about RTD signal conditioning, see "Completely Integrated 4-Wire RTD Measurement System 18 "Using a Low Power, Precision, 24-Bit, Sigma-Delta ADC circuit", Note CN-0381 from Analog Devices: 19 https://www.analog.com/media/en/reference-design-documentation/reference-designs/CN0381.pdf " 20 21 The AD7793 and the Arduino Atmega328 R3 are connected in the following way : 22 23 Arduino R3 pin# AD7793 pin# Pullup resistors 24 8 GPIO 3 ~CS 50 KOhm 25 9 RDY 15 DOUT/~RDY 26 12 MISO 15 DOUT/~RDY 27 11 MOSI 16 DIN 50 KOhm 28 10 CS not connected 29 13 SCK 1 CLK 50 KOhm 30 31 Ph. Sonnet, May 1, 2019 32 */ 33 34#include <AD7793.h> 35#include <Communication.h> 36#include <SPI.h> 37 38unsigned long conv; /* The 24 bit output code resulting from a conversion by the ADC and read from the data register */ 39float Vref; /* The external reference voltage applied between pins REFIN(+) and REFIN(-)and resulting from the excitation current flowing through the reference resistor */ 40float GAIN; /* Gain of the AD7793 unternal instrumentation amplifier */ 41float V; /* The voltage read on the analog input channel 2 (should be between -0.57 +0.57 when gain is set to 1) */ 42float RREF = 4990.0; /* The reference resistor: here, 4.99 Kohm, 0.1%, 10ppm/C */ 43float RRTD; /* The measured resistance of the RTD */ 44float temp; /* The temperature read on the analog input channel 1 */ 45float R0 = 100.0; /* RTD resistance at 0C */ 46float A = 3.9083E-3; /* Coefficient for t in the Callender-Van Dusen equation for temperature > 0C */ 47float B = -5.775E-7; /* Coefficient for t squared in the Callender-Van Dusen equation for temperature > 0C */ 48 49void setup() { 50 Serial.begin(9600); 51 52 53 unsigned char answer = AD7793_Init(); /* Initialize AD7793 and check if the device is present */ 54 Serial.print("AD7793 status = "); 55 if (answer == 1) { 56 Serial.println("OK"); 57 } 58 else { 59 Serial.println("Device is not present"); 60 } 61 62 AD7793_Reset(); /* Sends 32 consecutive 1's on SPI in order to reset the part */ 63 AD7793_SetChannel(AD7793_CH_AIN2P_AIN2M); /* Selects channel 2 of AD7793 */ 64 AD7793_SetGain(AD7793_GAIN_1); /* Sets the gain to 1 */ 65 AD7793_SetIntReference(AD7793_REFSEL_INT); /* Sets the reference source for the ADC. */ 66 conv = AD7793_SingleConversion(); 67 68 Vref = 1.17; /* This is the internal reference voltage provided by the AD7793, expressed in volt */ 69 GAIN = 1.0; 70 V = 1000 * (conv - 8388608.0) / (8388608.0 * GAIN/Vref); /* Computes the read voltage from the conversion code, in mV */ 71 Serial.print("Voltage (mV) = "); 72 Serial.println(V); 73 74 Serial.println("Temperature (Celsius)"); 75 Serial.println("===================="); 76 77 AD7793_Reset(); /* Sends 32 consecutive 1's on SPI in order to reset the part */ 78 AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M); /* Selects channel 1 of AD7793 */ 79 AD7793_SetGain(AD7793_GAIN_32); /* Sets the gain to 32 */ 80 GAIN = 32.0; 81 82 /* As the gain of the internal instrumentation amplifier has been changed, Analog Devices recommends performing a calibration */ 83 AD7793_Calibrate(AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN1P_AIN1M); /* Performs Internal Zero calibration to the specified channel. */ 84 AD7793_Calibrate(AD7793_MODE_CAL_INT_FULL, AD7793_CH_AIN1P_AIN1M); /* Performs Internal Full Calibration to the specified channel. */ 85 86 AD7793_SetIntReference(AD7793_REFSEL_EXT); /* Sets the voltage reference source for the ADC */ 87 AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2); /*Sets the direction of the internal excitation current source */ 88 89 AD7793_SetMode(AD7793_MODE_CONT); /* Continuous Conversion Mode */ 90 AD7793_SetExcitCurrent(AD7793_EN_IXCEN_210uA);/* Sets the current of the AD7793 internal excitation current source */ 91 delay(1000); /* Allows excitation current to settle before starting conversions*/ 92} 93 94void loop() { 95 conv = AD7793_ContinuousSingleRead(); 96 RRTD = RREF * (conv - 8388608.0) / (8388608.0 * GAIN); /* Computes the RTD resistance from the conversion code */ 97 temp = (sqrt(sq(A) - 4*B*(1.0 - RRTD/R0))-A)/(2*B); /* Callender-Van Dusen equation temp > 0C */ 98 Serial.println(temp); 99 delay(1000); 100 } 101 102
AD7793_example.ino
arduino
This is an example sketch for testing communication between the Arduino and the AD7793 using the AD7793 library from Analog Devices which I have adapted for the Arduino. The sketch reads the ID number of the AD7793, then uses the ADC to measure the voltage on the analog voltage pin 13 and the temperature inside the chip. These are the 3 functions that can be performed by the barebone chip (not counting in the pullup resistors which must be present).
1/* 2This is an example sketch for testing communication between the 3 Arduino and the 4AD7793 using the AD7793 library on the Analog Devices web site 5 adapted for the Arduino 6by Ph. Sonnet, April 8, 2019 7 8The AD7793 and the 9 Arduino Atmega328 R3 are connected in the following way : 10 11Arduino R3 pin# 12 AD7793 pin# Pullup resistors 13 8 GPIO 3 ~CS 50 KOhm 14 15 9 RDY 15 DOUT/~RDY 16 12 MISO 15 DOUT/~RDY 17 11 MOSI 18 16 DIN 50 KOhm 19 10 CS not connected 20 13 21 SCK 1 CLK 50 KOhm 22 23The sketch reads the ID number of 24 the AD7793, then uses the ADC to 25measure the voltage on the analog voltage pin 26 13 and the temperature 27inside the chip. These are the 3 functions that can be 28 performed by the 29barebone chip (not counting in the pullup resistors which must 30 be present). 31 32 33*/ 34 #include <AD7793.h> 35 #include <Communication.h> 36 37 #include <SPI.h> 38 float Vref = 1.17; /* AD7783 internal reference voltage 39 */ 40 41void setup() { 42 43 Serial.begin(9600); 44 while (!Serial); 45 46 47 unsigned char answer = AD7793_Init(); /* Initializes the AD7793 and checks 48 if the device is present*/ 49 Serial.print("AD7793 status = "); 50 Serial.println(answer); 51 /* Answer is 1 when the device is initialized and the ID is read and recognized 52 */ 53 Serial.println(""); 54} 55 56void loop() { 57 58 AD7793_SetChannel(AD7793_CH_AVDD_MONITOR); 59 /* AVDD Monitor, gain 1/6, internal 1.17V reference */ 60 unsigned long conv 61 = AD7793_SingleConversion(); /* Returns the result of a single conversion. */ 62 63 float AVDD = ((conv - 8388608.0) / 8388608.0) * 1.17 / (1/6.0) ; /* Note: 8388608 64 = 2exp(23) = 0x8000000 = the output code of 0 V in bipolar mode */ 65 Serial.print("Analog 66 supply voltage (AVDD) = "); 67 Serial.print(AVDD, 4); 68 Serial.println(" 69 V"); 70 71 AD7793_SetChannel(AD7793_CH_TEMP); /* Temp Sensor, gain 1, Internal 72 current reference */ 73 conv = AD7793_SingleConversion(); /* Returns the result 74 of a single conversion. */ 75 float Temp = (((conv - 8388608.0) / 8388608.0) * 76 1.17 * 1000 / 0.810) - 273; /* Sentitivity is approximately 0.81 mV/°K, according 77 to AD7793 datasheet */ 78 /* 79 To improve precision, it should be further calibrated by the user. */ 80 Serial.print("Chip 81 temperature = "); 82 Serial.print(Temp, 2); 83 Serial.println(" C"); 84 85 86 delay(1000); 87} 88
example_Volt_RTD_AD7793.ino
arduino
AD7793_example_Volt_RTD.ino Arduino This example shows how to use an AD7793 converter to perform successively two measurements: 1) a single voltage reading in the range from 0 to 0.58 V on AD7793 channel 2 2) a repeated temperature reading using a 4-wire PT100 RTD and a ratiometric measurement circuit on AD7793 channel 1.
1/* 2 This example shows how to use an AD7793 converter to perform successively 3 two measurements: 4 1) a single voltage reading in the range from 0 to 0.58 V 5 on AD7793 channel 2 (0.58 = 1.17V AD7793 internal reference divided by 2); 6 2) 7 a repeated temperature reading using a 4-wire PT100 RTD and a ratiometric measurement 8 circuit on AD7793 channel 1 (up to 144 C; 9 for higher temperature or to use 10 PT1000, simply lower the gain). 11 12 Using RTDs in a ratiometric measurement 13 has the advantage in that it eliminates sources of error such as the accuracy and 14 drift of 15 the excitation current source (provided by the AD7793) and the influence 16 of the RTC sensor lead resistance. 17 The basics for 4-wire RTD ratiometric measurement 18 and calculation using an ADC can be found at: 19 http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/temperature-measurement/platinum-rtd-sensors/amplifier-circuit-schematics 20 21 22 The schematics and part specifications used in this example are according 23 to Figure 7 in 24 "Analog Front-End Design Considerations for RTD Ratiometric 25 Temperature Measurements" by B. Zhang and A. Buda: 26 https://www.analog.com/en/analog-dialogue/articles/afe-design-considerations-rtd-ratiometric.html 27 28 However, in the present example, reference resistor is 4.99 Kohm, 0.1%, 10 ppm/C. 29 30 31 For additional details about RTD signal conditioning, see "Completely 32 Integrated 4-Wire RTD Measurement System 33 "Using a Low Power, Precision, 24-Bit, 34 Sigma-Delta ADC circuit", Note CN-0381 from Analog Devices: 35 https://www.analog.com/media/en/reference-design-documentation/reference-designs/CN0381.pdf 36 " 37 38 The AD7793 and the Arduino Atmega328 R3 are connected in the following 39 way : 40 41 Arduino R3 pin# AD7793 pin# Pullup resistors 42 8 GPIO 43 3 ~CS 50 KOhm 44 9 RDY 15 DOUT/~RDY 45 12 46 MISO 15 DOUT/~RDY 47 11 MOSI 16 DIN 50 KOhm 48 49 10 CS not connected 50 13 SCK 1 CLK 50 51 KOhm 52 53 Ph. Sonnet, May 1, 2019 54 */ 55 56#include <AD7793.h> 57#include 58 <Communication.h> 59#include <SPI.h> 60 61unsigned long conv; /* The 24 bit output 62 code resulting from a conversion by the ADC and read from the data register */ 63float 64 Vref; /* The external reference voltage applied between pins REFIN(+) and REFIN(-)and 65 resulting from the excitation current flowing through the reference resistor */ 66float 67 GAIN; /* Gain of the AD7793 unternal instrumentation amplifier */ 68float V; /* 69 The voltage read on the analog input channel 2 (should be between -0.57 +0.57 when 70 gain is set to 1) */ 71float RREF = 4990.0; /* The reference resistor: here, 4.99 72 Kohm, 0.1%, 10ppm/C */ 73float RRTD; /* The measured resistance of the RTD */ 74float 75 temp; /* The temperature read on the analog input channel 1 */ 76float R0 = 100.0; 77 /* RTD resistance at 0C */ 78float A = 3.9083E-3; /* Coefficient for t in the Callender-Van 79 Dusen equation for temperature > 0C */ 80float B = -5.775E-7; /* Coefficient for 81 t squared in the Callender-Van Dusen equation for temperature > 0C */ 82 83void 84 setup() { 85 Serial.begin(9600); 86 87 88 unsigned char 89 answer = AD7793_Init(); /* Initialize AD7793 and check if the device is present 90 */ 91 Serial.print("AD7793 status = "); 92 if (answer == 1) { 93 Serial.println("OK"); 94 95 } 96 else { 97 Serial.println("Device is not present"); 98 } 99 100 101 AD7793_Reset(); /* Sends 32 consecutive 1's on SPI in order to reset 102 the part */ 103 AD7793_SetChannel(AD7793_CH_AIN2P_AIN2M); /* Selects channel 104 2 of AD7793 */ 105 AD7793_SetGain(AD7793_GAIN_1); /* Sets the gain to 1 */ 106 107 AD7793_SetIntReference(AD7793_REFSEL_INT); /* Sets the reference source for 108 the ADC. */ 109 conv = AD7793_SingleConversion(); 110 111 Vref = 1.17; 112 /* This is the internal reference voltage provided by the AD7793, expressed in volt 113 */ 114 GAIN = 1.0; 115 V = 1000 * (conv - 8388608.0) / (8388608.0 * GAIN/Vref); 116 /* Computes the read voltage from the conversion code, in mV */ 117 Serial.print("Voltage 118 (mV) = "); 119 Serial.println(V); 120 121 Serial.println("Temperature (Celsius)"); 122 123 Serial.println("===================="); 124 125 AD7793_Reset(); 126 /* Sends 32 consecutive 1's on SPI in order to reset the part */ 127 AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M); 128 /* Selects channel 1 of AD7793 */ 129 AD7793_SetGain(AD7793_GAIN_32); /* Sets 130 the gain to 32 */ 131 GAIN = 32.0; 132 133 /* As the gain of the internal 134 instrumentation amplifier has been changed, Analog Devices recommends performing 135 a calibration */ 136 AD7793_Calibrate(AD7793_MODE_CAL_INT_ZERO, AD7793_CH_AIN1P_AIN1M); 137 /* Performs Internal Zero calibration to the specified channel. */ 138 AD7793_Calibrate(AD7793_MODE_CAL_INT_FULL, 139 AD7793_CH_AIN1P_AIN1M); /* Performs Internal Full Calibration to the specified channel. 140 */ 141 142 AD7793_SetIntReference(AD7793_REFSEL_EXT); /* Sets the voltage 143 reference source for the ADC */ 144 AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2); 145 /*Sets the direction of the internal excitation current source */ 146 147 AD7793_SetMode(AD7793_MODE_CONT); 148 /* Continuous Conversion Mode */ 149 AD7793_SetExcitCurrent(AD7793_EN_IXCEN_210uA);/* 150 Sets the current of the AD7793 internal excitation current source */ 151 delay(1000); 152 /* Allows excitation current to settle before starting conversions*/ 153} 154 155void 156 loop() { 157 conv = AD7793_ContinuousSingleRead(); 158 RRTD = RREF * (conv 159 - 8388608.0) / (8388608.0 * GAIN); /* Computes the RTD resistance from the conversion 160 code */ 161 temp = (sqrt(sq(A) - 4*B*(1.0 - RRTD/R0))-A)/(2*B); /* Callender-Van 162 Dusen equation temp > 0C */ 163 Serial.println(temp); 164 delay(1000); 165 166 } 167 168
Communication.cpp
c_cpp
1/***************************************************************************//** 2 3 * @file Communication.c 4 * @brief Implementation of Communication Driver. 5 6 * @author DBogdan (dragos.bogdan@analog.com) 7******************************************************************************** 8 9 * Copyright 2012(c) Analog Devices, Inc. 10 * 11 * All rights reserved. 12 * 13 14 * Redistribution and use in source and binary forms, with or without 15 * modification, 16 are permitted provided that the following conditions are met: 17 * - Redistributions 18 of source code must retain the above copyright 19 * notice, this list of conditions 20 and the following disclaimer. 21 * - Redistributions in binary form must reproduce 22 the above copyright 23 * notice, this list of conditions and the following disclaimer 24 in 25 * the documentation and/or other materials provided with the 26 * distribution. 27 28 * - Neither the name of Analog Devices, Inc. nor the names of its 29 * contributors 30 may be used to endorse or promote products derived 31 * from this software without 32 specific prior written permission. 33 * - The use of this software may or may 34 not infringe the patent rights 35 * of one or more patent holders. This license 36 does not release you 37 * from the requirement that you obtain separate licenses 38 from these 39 * patent holders to use this software. 40 * - Use of the software 41 either in source or binary form, must be run 42 * on or directly connected to 43 an Analog Devices Inc. component. 44 * 45 * THIS SOFTWARE IS PROVIDED BY ANALOG 46 DEVICES "AS IS" AND ANY EXPRESS OR 47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT 48 LIMITED TO, NON-INFRINGEMENT, 49 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 50 PURPOSE ARE DISCLAIMED. 51 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY 52 DIRECT, INDIRECT, 53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 54 (INCLUDING, BUT NOT 55 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT 56 OF SUBSTITUTE GOODS OR 57 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 58 INTERRUPTION) HOWEVER 59 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 60 STRICT LIABILITY, 61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 62 WAY OUT OF THE USE 63 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 SUCH DAMAGE. 65 * 66******************************************************************************** 67 68 * SVN Revision: 500 69******************************************************************************* 70 71******************************************************************************** 72 73 * Modified and adapted for Arduino by Ph. Sonnet. March 27, 2019, Version 1.1 74*******************************************************************************/ 75 76/******************************************************************************/ 77/* 78 Include Files */ 79/******************************************************************************/ 80#include 81 "AD7793.h" 82#include "Arduino.h" 83#include "Communication.h" 84#include 85 "SPI.h" 86 87/***************************************************************************//** 88 89 * @brief Initializes the SPI communication peripheral. 90 * 91 * @param lsbFirst 92 - Transfer format (0 or 1). 93 * Example: 0x0 - MSB first. 94 95 * 0x1 - LSB first. 96 * @param clockFreq - SPI clock 97 frequency (Hz). 98 * Example: 1000 - SPI clock frequency is 99 1 kHz. 100 * @param clockPol - SPI clock polarity (0 or 1). 101 * Example: 102 0x0 - idle state for SPI clock is low. 103 * 0x1 - idle 104 state for SPI clock is high. 105 * @param clockPha - SPI clock phase (0 or 1). 106 107 * Example: 0x0 - data is latched on the leading edge of SPI 108 109 * clock and data changes on trailing edge. 110 111 * 0x1 - data is latched on the trailing edge of SPI 112 113 * clock and data changes on the leading edge. 114 115 * 116 * @return 0 - Initialization failed, 1 - Initialization succeeded. 117*******************************************************************************/ 118 119unsigned 120 char SPI_Init() 121{ ADI_PART_CS_PIN_OUT; 122 ADI_PART_CS_HIGH; 123 GPIO1_PIN_IN; 124 125 SPI.begin(); 126 // configure the SPI port for your SPI device. 127 SPI.beginTransaction(SPI_SETTINGS); 128 // implements the SPI communication settings you defined in Communication.h 129 130 return(1); 131} 132 133/***************************************************************************//** 134 135 * @brief Writes data to SPI. 136 * 137 * @param data - Write data buffer: 138 * 139 - first byte is the chip select number; 140 * - from 141 the second byte onwards are located data bytes to write. 142 * @param bytesNumber 143 - Number of bytes to write. 144 * 145 * @return Number of written bytes. 146*******************************************************************************/ 147unsigned 148 char SPI_Write(unsigned char* data, 149 unsigned char bytesNumber) 150{ 151 int 152 SCNumber = data[0]; 153 154 ADI_PART_CS_LOW; 155 SPI.transfer(&data[1],bytesNumber); 156 if 157 (SCNumber == 0x1) { 158 ADI_PART_CS_HIGH; 159 } 160 return(bytesNumber); 161} 162 163/***************************************************************************//** 164 165 * @brief Reads data from SPI. 166 * 167 * @param data - As an input parameter, data 168 represents the write buffer: 169 * - first byte is the chip select 170 number; 171 * - from the second byte onwards are located data bytes 172 to write. 173 * As an output parameter, data represents the read buffer: 174 175 * - from the first byte onwards are located the read data bytes. 176 177 * @param bytesNumber - Number of bytes to write. 178 * 179 * @return Number of written 180 bytes. 181*******************************************************************************/ 182unsigned 183 char SPI_Read(unsigned char* data, 184 unsigned char bytesNumber) 185{ 186 int i = 0; 187 188 189 ADI_PART_CS_LOW; 190 int SCNumber = data[0]; 191 for(i 192 = 1 ;i < bytesNumber + 1 ; i ++) 193 { 194 data[i-1] = SPI.transfer(data[i]); 195 } 196 197 if (SCNumber == 0x1) { 198 ADI_PART_CS_HIGH; 199 } 200 return(bytesNumber); 201 } 202 203 204 205 206 207 208
Readme.txt
text
1How to implement the AD7793 library (Ph. Sonnet, Mach 29, 2019) 2=================================== 3 4Directory 5 AD7793 must be created in the Aduino "libraries" directory 6and must contain 7 the following files : 8 9AD7793.h 10AD7793.cpp 11Communication.h 12Communication.cpp 13 14 15How 16 to connect the AD7793 to the Arduino Atmega328 or MEGA 17=========================================================== 18 19AD7793 20 pin 16 (DIN), Master Out Slave In (MOSI), must be connected to pin 11 (for Atmega328) 21 or pin 51 (for MEGA). 22 23AD7793 pin 15 (DOUT/~RDY), Master In Slave Out (MISO), 24 must be connected to pin 12 (for Atmega328) or pin 50 (for MEGA). 25It must also 26 be connected to another digital pin af the Arduino. Suppose that you choose pin 27 9 for this purpose. 28This pin number must be specified in the Communication.h 29 file by editing the #define ADI_PAR_CS_PIN line in the "GPIO Definitions" section. 30 31 32AD7793 pin 1 (CLK), Serial Clock (SCK), must be connected to Pin 13 (for 33 Atmega328) or pin 52 (for MEGA). 34 35AD7793 pin 3(~CS), Chip Select (CS) should 36 not be connected to pin 10 (for Atmega328) or pin 53 (for MEGA), as it is supposed 37 by the SPI library. 38Instead, another digital pin of the Arduino must be used. 39 Suppose that you choose pin 8 for this purpose. 40This pin number must be specified 41 in the Communication.h file by editing the #define ADI_PAR_CS_PIN line in the "GPIO 42 Definitions" section. 43 44From my experience, it is crucial to insert pullup 45 resistors (for instance, 50 KOhm) in the MOSI, CS and CLK circuits, to ensure reliable 46communication 47 between the AD7793 and the Arduino. 48 49 50Preprocessing instructions 51========================== 52 53 Here are the preprocessing instruction which must be inserted before setup() 54 55 into your sketch. 56 57#include <AD7793.h> 58#include <Communication.h> 59#include 60 <SPI.h> 61 62Initialization 63============== 64 This must be inserted 65 as such into setup(). No need to initialize the 66 SPI channel, because this is 67 taken care of by the AD7793_Init()function. 68 69unsigned char answer = AD7793_Init(); 70 /* Initializes the AD7793 and checks if the device is present*/ 71AD7793_Reset(); 72 /* Sends 32 consecutive 1 on SPI in order to reset the part */ 73 74 75Preprogramed 76 register setting functions 77======================================== 78 Just 79 insert these functions into your sketch as they are listed here, and they will 80 81 access the various AD7793 registers (Status, Mode, Configuration, Data, IO and 82 Data) and set parameters 83 according to the needs of your project. 84 85Status 86 register 87............... 88 89unsigned char answer = AD7793_Ready(); /* Reads 90 the Ready Bit for ADC */ 91unsigned char answer = AD7793_Error(); /* Reads the 92 ADC Error Bit */ 93unsigned char answer = AD7793_Channel3(); /* Indicates that 94 channel 3 is being converted by the ADC */ 95unsigned char answer = AD7793_Channel2(); 96 /* Indicates that channel 2 is being converted by the ADC */ 97unsigned char answer 98 = AD7793_Channel1(); /* Indicates that channel 1 is being converted by the ADC */ 99 100 101Mode register 102.............. 103 104AD7793_SetMode(AD7793_MODE_CONT); 105 /* Continuous Conversion Mode */ 106D7793_SetMode(AD7793_MODE_SINGLE); /* Single 107 Conversion Mode */ 108AD7793_SetMode(AD7793_MODE_IDLE); /* Idle Mode */ 109AD7793_SetMode(AD7793_MODE_PWRDN); 110 /* Power-Down Mode */ 111AD7793_SetMode(AD7793_MODE_CAL_INT_ZERO); /* Internal 112 Zero-Scale Calibration */ 113AD7793_SetMode(AD7793_MODE_CAL_INT_FULL); /* Internal 114 Full-Scale Calibration */ 115AD7793_SetMode(AD7793_MODE_CAL_SYS_ZERO); /* System 116 Zero-Scale Calibration */ 117AD7793_SetMode(AD7793_MODE_CAL_SYS_FULL); /* System 118 Full-Scale Calibration */ 119 120AD7793_SetClockSource(AD7793_CLK_INT); /* Internal 121 64 kHz Clk not available at the CLK pin */ 122AD7793_SetClockSource(AD7793_CLK_INT_CO); 123 /* Internal 64 kHz Clk available at the CLK pin */ 124AD7793_SetClockSource(AD7793_CLK_EXT); 125 /* External 64 kHz Clock */ 126AD7793_SetClockSource(AD7793_CLK_EXT_DIV2); /* 127 External Clock divided by 2 */ 128 129AD7793_SetFilterUpdateRate(AD7793_RATE_NIL); 130 /* Filter Update Rate: nil */ 131AD7793_SetFilterUpdateRate(AD7793_RATE_470); /* 132 Filter Update Rate: 470Hz */ 133AD7793_SetFilterUpdateRate(AD7793_RATE_242); /* 134 Filter Update Rate: 242Hz */ 135AD7793_SetFilterUpdateRate(AD7793_RATE_123); /* 136 Filter Update Rate: 123Hz */ 137AD7793_SetFilterUpdateRate(AD7793_RATE_62); /* Filter 138 Update Rate: 62Hz 139AD7793_SetFilterUpdateRate(AD7793_RATE_50); /* Filter Update 140 Rate: 50Hz */ 141AD7793_SetFilterUpdateRate(AD7793_RATE_39); /* Filter Update Rate: 142 39Hz */ 143AD7793_SetFilterUpdateRate(AD7793_RATE_33_2); /* Filter Update Rate: 144 33.2Hz */ 145AD7793_SetFilterUpdateRate(AD7793_RATE_19_6); /* Filter Update Rate: 146 19.6Hz 90dB (60Hz only) */ 147AD7793_SetFilterUpdateRate(AD7793_RATE_16_7_50_60); 148 /* Filter Update Rate: 16.7Hz 80dB (50Hz only) */ 149AD7793_SetFilterUpdateRate(AD7793_RATE_12_5); 150 /* Filter Update Rate: 12.5Hz 66dB (50 and 60Hz) */ 151AD7793_SetFilterUpdateRate(AD7793_RATE_10); 152 /* Filter Update Rate: 10Hz 69dB (50 and 60Hz) */ 153AD7793_SetFilterUpdateRate(AD7793_RATE_8_33); 154 /* Filter Update Rate: 8.33Hz 70dB (50 and 60Hz) */ 155AD7793_SetFilterUpdateRate(AD7793_RATE_6_25); 156 /* Filter Update Rate: 6.25Hz 72dB (50 and 60Hz) */ 157AD7793_SetFilterUpdateRate(AD7793_RATE_4_17); 158 /* Filter Update Rate: 4.17Hz 74dB (50 and 60Hz) */ 159 160Configuration register 161...................... 162 163AD7793_SetBiasVoltage(AD7793_VBIAS_GEN_DISABL); 164 /* Bias voltage generator disabled */ 165AD7793_SetBiasVoltage(AD7793_VBIAS_AIN1_NEG); 166 /* Bias voltage generator to AIN1(-) * 167AD7793_SetBiasVoltage(AD7793_VBIAS_AIN2_NEG); 168 /* Bias voltage generator to AIN2(-) */ 169 170AD7793_EnableBurnoutCurr(); /* Enable 171 burnout current of AD7793 */ 172AD7793_DisableBurnoutCurr(); /* Disable burnout 173 current of AD7793 */ 174 175AD7793_EnableUnipolar(); /* Enable unipolar coding 176 of AD7793 */ 177AD7793_DisableBipolar(); /* Enable bipolar coding of AD7793 */ 178 179AD7793_EnableCurrBoost(); 180 /* Enable bias voltage generator current boost of AD7793 */ 181AD7793_DisableCurrBoost(); 182 /* Disable bias voltage generator current boost of AD7793 */ 183 184AD7793_AD7793_SetGain(AD7793_GAIN_1); 185 /* Gain of 1, In-amp not used */ 186AD7793_AD7793_SetGain(AD7793_GAIN_2); /* 187 Gain of 2, In-amp not used */ 188AD7793_AD7793_SetGain(AD7793_GAIN_4); /* Set 189 the gain of the In-amp to 4 */ 190AD7793_AD7793_SetGain(AD7793_GAIN_8); /* Set 191 the gain of the In-amp to 8 */ 192AD7793_AD7793_SetGain(AD7793_GAIN_16); /* Set 193 the gain of the In-amp to 16 */ 194AD7793_AD7793_SetGain(AD7793_GAIN_32); /* Set 195 the gain of the In-amp to 32 */ 196AD7793_AD7793_SetGain(AD7793_GAIN_64); /* Set 197 the gain of the In-amp to 64 */ 198AD7793_AD7793_SetGain(AD7793_GAIN_128); /* Set 199 the gain of the In-amp to 128 */ 200 201AD7793_SetIntReference(AD7793_REFSEL_INT); 202 /* Select internal reference voltage source for the ADC */ 203AD7793_SetIntReference(AD7793_REFSEL_EXT); 204 /* External voltage reference applied between REFIN(+) and REFIN(–) */ 205 206AD7793_EnableBufMode(); 207 /* Enable buffered mode of AD7793 */ 208AD7793_DisableBufMode(); /* Disable buffered 209 mode of AD7793 */ 210 211AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M); /* Select channel 212 AIN1(+) - AIN1(-) */ 213AD7793_SetChannel(AD7793_CH_AIN2P_AIN2M); /* 214 Select channel AIN2(+) - AIN2(-) */ 215AD7793_SetChannel(AD7793_CH_AIN3P_AIN3M); 216 /* Select channel AIN3(+) - AIN3(-) */ 217AD7793_SetChannel(AD7793_CH_TEMP); /* 218 Temp Sensor, gain 1, Internal current reference */ 219AD7793_SetChannel(AD7793_CH_AVDD_MONITOR); 220 /* AVDD Monitor, gain 1/6, internal 1.17V reference */ 221 222IO Register 223=========== 224 225AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2); 226 /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */ 227AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT2_IEXC2_IOUT1); 228 /* IEXC1 connect to IOUT2, IEXC2 connect to IOUT1 */ 229AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IEXC2_IOUT1); 230 /* Both current sources IEXC1,2 connect to IOUT1 */ 231AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IEXC2_IOUT2); 232 /* Both current sources IEXC1,2 connect to IOUT2 */ 233AD7793_SetExcitCurrent(AD7793_EN_IXCEN_DISABLE); 234 /* Disable excitation current*/ 235AD7793_SetExcitCurrent(AD7793_EN_IXCEN_10uA); 236 /* Excitation Current 10uA */ 237AD7793_SetExcitCurrent(AD7793_EN_IXCEN_210uA); 238 /* Excitation Current 210uA */ 239AD7793_SetExcitCurrent(AD7793_EN_IXCEN_1mA); 240 /* Excitation Current 1mA */ 241 242 243Return the result of conversion 244=============================== 245 246unsigned 247 long conv = AD7793_SingleConversion(); /* Returns the result of a single conversion. 248 */ 249unsigned long conv = AD7793_ContinuousReadAvg(unsigned char sampleNumber); 250 /* Returns the average of several conversion results. */ 251unsigned long conv = 252 AD7793_ContinuousSingleRead(); /* Returns a single measurement, provided continuous 253 mesurement mode has been set up. */ 254 255 256Calibration 257=========== 258 259void 260 AD7793_Calibrate(unsigned char mode, unsigned char channel); /* Performs the given 261 calibration to the specified channel. */ 262 263Values for parameter mode: 264For 265 performing an internal Zero-Scale Calibration: AD7793_MODE_CAL_INT_ZERO 266For performing 267 an internal Full-Scale Calibration: AD7793_MODE_CAL_INT_FULL 268For performing a 269 system Zero-Scale Calibration: AD7793_MODE_CAL_SYS_ZERO 270For performing a Full-Scale 271 Calibration: AD7793_MODE_CAL_SYS_FULL 272 273Values for parameter mode: 274For selecting 275 channel 1 AIN1(+) - AIN1(-): AD7793_CH_AIN1P_AIN1M 276For selecting channel 2 AIN2(+) 277 - AIN2(-): AD7793_CH_AIN2P_AIN2M 278For selecting channel 3 AIN3(+) - AIN3(-): AD7793_CH_AIN3P_AIN3M 279 280 281Power-on/reset 282 default register setting 283====================================== 284 285After 286 powering on or resetting the AD7793, the registers are set just as if all the following 287 commands had been executed: 288 289Mode register 290............... 291 292AD7793_SetMode(AD7793_MODE_CONT); 293 /* Continuous Conversion Mode */ 294AD7793_SetClockSource(AD7793_CLK_INT); /* 295 Internal 64 kHz Clk not available at the CLK pin */ 296AD7793_SetFilterUpdateRate(AD7793_RATE_NIL); 297 /* Filter Update Rate: nil */ 298 299Configuration register 300...................... 301 302AD7793_SetBiasVoltage(AD7793_VBIAS_GEN_DISABL); 303 /* Bias voltage generator disabled */ 304AD7793_DisableBurnoutCurr(); /* Disable 305 burnout current of AD7793 */ 306AD7793_DisableBipolar(); /* Enable bipolar coding 307 of AD7793 */ 308AD7793_DisableCurrBoost(); /* Disable bias voltage generator 309 current boost of AD7793 */ 310AD7793_AD7793_SetGain(AD7793_GAIN_128); /* Set the 311 gain of the In-amp to 128 */ 312AD7793_SetIntReference(AD7793_REFSEL_EXT); /* External 313 voltage reference applied between REFIN(+) and REFIN(–) */ 314AD7793_EnableBufMode(); 315 /* Enable buffered mode of AD7793 */ 316AD7793_SetChannel(AD7793_CH_AIN1P_AIN1M); 317 /* Select channel AIN1(+) - AIN1(-) */ 318 319IO register 320........... 321 322AD7793_SetExcitDirection(AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2); 323 /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */ 324AD7793_SetExcitCurrent(AD7793_EN_IXCEN_DISABLE); 325 /* Disable excitation current*/ 326 327 328 329 330 331
Communication.h
c_cpp
1/***************************************************************************//** 2 * @file Communication.h 3 * @brief Header file of Communication Driver. 4 * @author DBogdan (dragos.bogdan@analog.com) 5******************************************************************************** 6 * Copyright 2012(c) Analog Devices, Inc. 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * - Neither the name of Analog Devices, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * - The use of this software may or may not infringe the patent rights 22 * of one or more patent holders. This license does not release you 23 * from the requirement that you obtain separate licenses from these 24 * patent holders to use this software. 25 * - Use of the software either in source or binary form, must be run 26 * on or directly connected to an Analog Devices Inc. component. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39******************************************************************************** 40 * SVN Revision: 500 41******************************************************************************* 42 43******************************************************************************** 44 * Modified and adapted for Arduino by Ph. Sonnet. March 27, 2019, Version 1.1 45*******************************************************************************/ 46 47 48#ifndef _COMMUNICATION_H_ 49#define _COMMUNICATION_H_ 50#include "SPI.h" 51 52/******************************************************************************/ 53/* GPIO Definitions */ 54/******************************************************************************/ 55 56#define ADI_PART_CS_PIN 8 // Pin of the Arduino to which the ADC Chip Select pin is connected. 57 // Leave the Arduino #10 pin unconnected, contrarily to what is normally assumed by the SPI library. 58#define ADI_PART_CS_PIN_OUT pinMode(ADI_PART_CS_PIN,OUTPUT) 59#define ADI_PART_CS_LOW digitalWrite(ADI_PART_CS_PIN,LOW) 60#define ADI_PART_CS_HIGH digitalWrite(ADI_PART_CS_PIN,HIGH) 61#define GPIO1_PIN 9 // Pin of the Arduino to which the ADC Dout/Data Ready pin is connected for determining the end of conversion/calibration using a polling sequence. 62 // Note: the Aduino #12 pin must also be connected to the ADC Dout/Data Ready pin, as requested by the SPI library. 63#define GPIO1_PIN_IN pinMode(GPIO1_PIN,INPUT) 64#define GPIO1_STATE digitalRead(GPIO1_PIN) 65#define SPI_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE3) // 4 MHz clock, MSB first, mode 0 66 67/******************************************************************************/ 68/* Functions Prototypes */ 69/******************************************************************************/ 70 71/* Initializes the SPI communication peripheral. */ 72unsigned char SPI_Init(); 73 74/* Writes data to SPI. */ 75unsigned char SPI_Write(unsigned char* data, 76 unsigned char bytesNumber); 77/* Reads data from SPI. */ 78unsigned char SPI_Read(unsigned char* data, 79 unsigned char bytesNumber); 80 81#endif // _COMMUNICATION_H_ 82
AD7793.cpp
c_cpp
1/***************************************************************************//** 2 * @file AD7793.c 3 * @brief Implementation of AD7793 Driver. 4 * @author Bancisor MIhai 5******************************************************************************** 6 * Copyright 2012(c) Analog Devices, Inc. 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * - Neither the name of Analog Devices, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * - The use of this software may or may not infringe the patent rights 22 * of one or more patent holders. This license does not release you 23 * from the requirement that you obtain separate licenses from these 24 * patent holders to use this software. 25 * - Use of the software either in source or binary form, must be run 26 * on or directly connected to an Analog Devices Inc. component. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39******************************************************************************** 40 * SVN Revision: 500 41******************************************************************************* 42 43******************************************************************************** 44 * Modified and adapted for Arduino by Ph. Sonnet. March 9, 2019, Version 1.1 45*******************************************************************************/ 46 47/******************************************************************************/ 48/* Include Files */ 49/******************************************************************************/ 50#include "AD7793.h" // AD7793 definitions. 51#include "Communication.h" // Communication definitions. 52#include "Arduino.h" 53#include "SPI.h" 54 55/***************************************************************************//** 56 * @brief Initializes the AD7793 and checks if the device is present. 57 * 58 * @return status - Result of the initialization procedure. 59 * Example: 1 - if initialization was successful (ID is 0x0B). 60 * 0 - if initialization was unsuccessful. 61*******************************************************************************/ 62 63unsigned char AD7793_Init(void) 64{ 65 unsigned char status = 0x1; 66 67 SPI_Init(); 68 if((AD7793_GetRegisterValue(AD7793_REG_ID, 1, 1) & AD7793_ID_MASK) != AD7793_ID) 69 { 70 status = 0x0; 71 } 72 73 return(status); 74} 75/***************************************************************************//** 76 * @brief Sends 32 consecutive 1's on SPI in order to reset the part. 77 * 78 * @return None. 79*******************************************************************************/ 80void AD7793_Reset(void) 81{ 82 unsigned char dataToSend[5] = {0x01, 0xff, 0xff, 0xff, 0xff}; 83 84 //ADI_PART_CS_LOW; 85 SPI_Write(dataToSend,4); 86 //ADI_PART_CS_HIGH; 87} 88/***************************************************************************//** 89 * @brief Reads the Ready Bit for ADC 90 * 91 * @return status: 1 - if conversion is not yet completed and data is not yet written to the data register. 92 * 0 - if data is written to the ADC data register. 93*******************************************************************************/ 94unsigned char AD7793_Ready(void) 95{ 96 unsigned char status = 0x0; 97 98 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_RDY) == 0x80) 99 { 100 status = 0x1; 101 } 102 103 return(status); 104} 105/***************************************************************************//** 106* @brief Reads the ADC error bit. 107 * 108 * @return status: 1 - result written to ADC data register all clamped has been all clamped to 1 or 0. 109 * 0 - No error due to overrrange, underrange or other error sources. 110*******************************************************************************/ 111unsigned char AD7793_Error(void) 112{ 113 unsigned char status = 0x0; 114 115 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_ERR) == 0x40) 116 { 117 status = 0x1; 118 } 119 120 return(status); 121} 122/***************************************************************************//** 123 * @brief Indicates that channel 3 is being converted by the ADC. 124 * 125 * @return status: 1 - Channel 3 is being converted by the ADC. 126 * 0 - Channel 3 is not being converted by the ADC.. 127*******************************************************************************/ 128unsigned char AD7793_Channel3(void) 129{ 130 unsigned char status = 0x0; 131 132 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_CH3) == 0x04) 133 { 134 status = 0x1; 135 } 136 137 return(status); 138} 139/***************************************************************************//** 140 * @brief Indicates that channel 2 is being converted by the ADC. 141 * 142 * @return status: 1 - Channel 2 is being converted by the ADC. 143 * 0 - Channel 2 is not being converted by the ADC.. 144*******************************************************************************/ 145unsigned char AD7793_Channel2(void) 146{ 147 unsigned char status = 0x0; 148 149 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_CH2) == 0x02) 150 { 151 status = 0x1; 152 } 153 154 return(status); 155} 156/***************************************************************************//** 157 * @brief Indicates that channel 1 is being converted by the ADC. 158 * 159 * @return status: 1 - Channel 1 is being converted by the ADC. 160 * 0 - Channel 1 is not being converted by the ADC.. 161*******************************************************************************/ 162unsigned char AD7793_Channel1(void) 163{ 164 unsigned char status = 0x0; 165 166 if((AD7793_GetRegisterValue(AD7793_REG_STAT, 1, 1) & AD7793_STAT_CH1) == 0x01) 167 { 168 status = 0x1; 169 } 170 171 return(status); 172} 173/***************************************************************************//** 174 * @brief Reads the value of the selected register 175 * 176 * @param regAddress - The address of the register to read. 177 * @param size - The size of the register to read. 178 * 179 * @return data - The value of the selected register register. 180*******************************************************************************/ 181unsigned long AD7793_GetRegisterValue(unsigned char regAddress, 182 unsigned char size, 183 unsigned char modifyCS) 184{ 185 unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00}; 186 unsigned long receivedData = 0x00; 187 unsigned char i = 0x00; 188 189 data[0] = 0x01 * modifyCS; 190 data[1] = AD7793_COMM_READ | AD7793_COMM_ADDR(regAddress); 191 SPI_Read(data,(1 + size)); 192 for(i = 1 ;i < size +1 ; i ++) 193 { 194 receivedData = (receivedData << 8) + data[i]; 195 196 } 197 198 return (receivedData); 199} 200/***************************************************************************//** 201 * @brief Writes the value to the register 202 * 203 * @param - regAddress - The address of the register to write to. 204 * @param - regValue - The value to write to the register. 205 * @param - size - The size of the register to write. 206 * 207 * @return None. 208*******************************************************************************/ 209void AD7793_SetRegisterValue(unsigned char regAddress, 210 unsigned long regValue, 211 unsigned char size, 212 unsigned char modifyCS) 213{ 214 unsigned char data[5] = {0x00, 0x00, 0x00, 0x00, 0x00}; 215 unsigned char* dataPointer = (unsigned char*)®Value; 216 unsigned char bytesNr = size + 1; 217 218 data[0] = 0x01 * modifyCS; 219 data[1] = AD7793_COMM_WRITE | AD7793_COMM_ADDR(regAddress); 220 while(bytesNr > 1) 221 { 222 data[bytesNr] = *dataPointer; 223 dataPointer ++; 224 bytesNr --; 225 } 226 SPI_Write(data,(1 + size)); 227} 228/***************************************************************************//** 229 * @brief Waits for RDY pin to go low. 230 * 231 * @return None. 232*******************************************************************************/ 233void AD7793_WaitRdyGoLow(void) 234{ 235 while( AD7793_RDY_STATE ) 236 { 237 ; 238 } 239} 240 241/***************************************************************************//** 242 * @brief Sets the operating mode of AD7793. 243 * 244 * @param mode - Mode of operation. 245 * 246 * @return None. 247*******************************************************************************/ 248void AD7793_SetMode(unsigned long mode) 249{ 250 unsigned long command; 251 252 command = AD7793_GetRegisterValue(AD7793_REG_MODE, 253 2, 254 1); // CS is modified by SPI read/write functions. 255 command &= ~AD7793_MODE_SEL(0xFF); 256 command |= AD7793_MODE_SEL(mode); 257 AD7793_SetRegisterValue( 258 AD7793_REG_MODE, 259 command, 260 2, 261 1); // CS is modified by SPI read/write functions. 262} 263 264/***************************************************************************//** 265 * @brief Sets the ADC clock source of AD7793. 266 * 267 * @param mode - Clock source. 268 * 269 * @return None. 270*******************************************************************************/ 271void AD7793_SetClockSource(unsigned long clockSource) 272{ 273 unsigned long command; 274 275 command = AD7793_GetRegisterValue(AD7793_REG_MODE, 276 2, 277 1); // CS is modified by SPI read/write functions. 278 command &= ~AD7793_MODE_CLKSRC(0xFF); 279 command |= AD7793_MODE_CLKSRC(clockSource); 280 AD7793_SetRegisterValue( 281 AD7793_REG_MODE, 282 command, 283 2, 284 1); // CS is modified by SPI read/write functions. 285} 286 287/***************************************************************************//** 288 * @brief Sets the filter update rate of AD7793. 289 * 290 * @param mode - Filter update rate (Hz). 291 * 292 * @return None. 293*******************************************************************************/ 294void AD7793_SetFilterUpdateRate(unsigned long filterRate) 295{ 296 unsigned long command; 297 298 command = AD7793_GetRegisterValue(AD7793_REG_MODE, 299 2, 300 1); // CS is modified by SPI read/write functions. 301 command &= ~AD7793_MODE_RATE(0xFF); 302 command |= AD7793_MODE_RATE(filterRate); 303 AD7793_SetRegisterValue( 304 AD7793_REG_MODE, 305 command, 306 2, 307 1); // CS is modified by SPI read/write functions. 308} 309 310/***************************************************************************//** 311 * @brief Sets the direction of the internal current source. 312 313 * @param Current source direction. 314 * 315 * @return None. 316*******************************************************************************/ 317 318void AD7793_SetExcitDirection(unsigned long direction) 319{ 320 unsigned long command; 321 322 command = AD7793_GetRegisterValue(AD7793_REG_IO, 323 1, 324 1); // CS is modified by SPI read/write functions. 325 command &= ~AD7793_IEXCDIR(0xF); 326 command |= AD7793_IEXCDIR(direction); 327 AD7793_SetRegisterValue( 328 AD7793_REG_IO, 329 command, 330 1, 331 1); // CS is modified by SPI read/write functions. 332} 333 334/***************************************************************************//** 335 * @brief Sets the current of the internal current source 336 337 * @param Current source value. 338 * 339 * @return None. 340*******************************************************************************/ 341 342void AD7793_SetExcitCurrent(unsigned long current) 343{ 344 unsigned long command; 345 346 command = AD7793_GetRegisterValue(AD7793_REG_IO, 347 1, 348 1); // CS is modified by SPI read/write functions. 349 command &= ~AD7793_IEXCEN(0xF); 350 command |= AD7793_IEXCEN(current); 351 AD7793_SetRegisterValue( 352 AD7793_REG_IO, 353 command, 354 1, 355 1); // CS is modified by SPI read/write functions. 356} 357 358/***************************************************************************//** 359 * @brief Enable bias voltage generator of AD7793. 360 * 361 * @param Bias voltage. 362 * 363 * @return None. 364*******************************************************************************/ 365void AD7793_SetBiasVoltage(unsigned long voltage) 366{ 367 unsigned long command; 368 369 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 370 2, 371 1); // CS is modified by SPI read/write functions. 372 command &= ~AD7793_CONF_VBIAS(0xFF); 373 command |= AD7793_CONF_VBIAS(voltage); 374 AD7793_SetRegisterValue( 375 AD7793_REG_CONF, 376 command, 377 2, 378 1); // CS is modified by SPI read/write functions. 379} 380/***************************************************************************//** 381 * 382 * @param Enable burnout current of AD7793. 383 * 384 * @return None. 385*******************************************************************************/ 386void AD7793_EnableBurnoutCurr(void) 387 388{ 389 unsigned long command; 390 391 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 392 2, 393 1); // CS is modified by SPI read/write functions. 394 command &= ~AD7793_CONF_BO_EN; 395 command |= AD7793_CONF_BO_EN; 396 AD7793_SetRegisterValue( 397 AD7793_REG_CONF, 398 command, 399 2, 400 1); // CS is modified by SPI read/write functions. 401} 402 403/***************************************************************************//** 404 * @brief Disable burnout current of AD7793. 405 * 406 * @param None. 407 * 408 * @return None. 409*******************************************************************************/ 410void AD7793_DisableBurnoutCurr(void) 411 412{ 413 unsigned long command; 414 415 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 416 2, 417 1); // CS is modified by SPI read/write functions. 418 command &= ~AD7793_CONF_BO_EN; 419 AD7793_SetRegisterValue( 420 AD7793_REG_CONF, 421 command, 422 2, 423 1); // CS is modified by SPI read/write functions. 424} 425/***************************************************************************//** 426 * @brief Enable unipolar coding of AD7793. 427 * 428 * @param None. 429 * 430 * @return None. 431*******************************************************************************/ 432void AD7793_EnableUnipolar(void) 433 434{ 435 unsigned long command; 436 437 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 438 2, 439 1); // CS is modified by SPI read/write functions. 440 command &= ~AD7793_CONF_UNIPOLAR; 441 command |= AD7793_CONF_UNIPOLAR; 442 AD7793_SetRegisterValue( 443 AD7793_REG_CONF, 444 command, 445 2, 446 1); // CS is modified by SPI read/write functions. 447} 448/***************************************************************************//** 449 * @brief Enable bipolar coding of AD7793. 450 * 451 * @param None. 452 * 453 * @return None. 454*******************************************************************************/ 455void AD7793_DisableBipolar(void) 456 457{ 458 unsigned long command; 459 460 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 461 2, 462 1); // CS is modified by SPI read/write functions. 463 command &= ~AD7793_CONF_UNIPOLAR; 464 AD7793_SetRegisterValue( 465 AD7793_REG_CONF, 466 command, 467 2, 468 1); // CS is modified by SPI read/write functions. 469} 470/***************************************************************************//** 471 * @brief Enable bias voltage generator current boost of AD7793. 472 * 473 * @param None. 474 * 475 * @return None. 476*******************************************************************************/ 477void AD7793_EnableCurrBoost(void) 478 479{ 480 unsigned long command; 481 482 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 483 2, 484 1); // CS is modified by SPI read/write functions. 485 command &= ~AD7793_CONF_BOOST; 486 command |= AD7793_CONF_BOOST; 487 AD7793_SetRegisterValue( 488 AD7793_REG_CONF, 489 command, 490 2, 491 1); // CS is modified by SPI read/write functions. 492} 493/***************************************************************************//** 494 * @brief Disable bias voltage generator current boost of AD7793. 495 * 496 * @param None. 497 * 498 * @return None. 499*******************************************************************************/ 500void AD7793_DisableCurrBoost(void) 501 502{ 503 unsigned long command; 504 505 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 506 2, 507 1); // CS is modified by SPI read/write functions. 508 command &= ~AD7793_CONF_BOOST; 509 AD7793_SetRegisterValue( 510 AD7793_REG_CONF, 511 command, 512 2, 513 1); // CS is modified by SPI read/write functions. 514} 515/***************************************************************************//** 516 * @brief Set the gain of the In-amp. 517 * 518 * @param gain - Gain. 519 * 520 * @return None. 521*******************************************************************************/ 522void AD7793_SetGain(unsigned long gain) 523 { 524 unsigned long command; 525 526 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 527 2, 528 1); // CS is modified by SPI read/write functions. 529 command &= ~AD7793_CONF_GAIN(0xFF); 530 command |= AD7793_CONF_GAIN(gain); 531 AD7793_SetRegisterValue( 532 AD7793_REG_CONF, 533 command, 534 2, 535 1); // CS is modified by SPI read/write functions. 536} 537/***************************************************************************//** 538 * @brief Set the reference voltage source for the ADC. 539 * 540 * @param type - Type of the reference. 541 * Example: AD7793_REFSEL_EXT - External Reference Selected 542 * AD7793_REFSEL_INT - Internal Reference Selected. 543 * 544 * @return None. 545*******************************************************************************/ 546void AD7793_SetIntReference(unsigned char type) 547{ 548 unsigned long command = 0; 549 550 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 551 2, 552 1); // CS is modified by SPI read/write functions. 553 command &= ~AD7793_CONF_REFSEL(AD7793_REFSEL_INT); 554 command |= AD7793_CONF_REFSEL(type); 555 AD7793_SetRegisterValue(AD7793_REG_CONF, 556 command, 557 2, 558 1); // CS is modified by SPI read/write functions. 559} 560/***************************************************************************//** 561 * @brief Enable buffered mode of AD7793. 562 * 563 * @param None. 564 * 565 * @return None. 566*******************************************************************************/ 567void AD7793_EnableBufMode(void) 568{ 569 unsigned long command; 570 571 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 572 2, 573 1); // CS is modified by SPI read/write functions. 574 command &= ~AD7793_CONF_BUF; 575 command |= AD7793_CONF_BUF; 576 AD7793_SetRegisterValue( 577 AD7793_REG_CONF, 578 command, 579 2, 580 1); // CS is modified by SPI read/write functions. 581} 582/***************************************************************************//** 583 * @brief Disable buffered mode of AD7793. 584 * 585 * @param None. 586 * 587 * @return None. 588*******************************************************************************/ 589void AD7793_DisableBufMode(void) 590{ 591 unsigned long command; 592 593 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 594 2, 595 1); // CS is modified by SPI read/write functions. 596 command &= ~AD7793_CONF_BUF; 597 AD7793_SetRegisterValue( 598 AD7793_REG_CONF, 599 command, 600 2, 601 1); // CS is modified by SPI read/write functions. 602} 603/***************************************************************************//** 604 * @brief Selects the channel of AD7793. 605 * 606 * @param channel - ADC channel selection. 607 * 608 * @return None. 609*******************************************************************************/ 610void AD7793_SetChannel(unsigned long channel) 611{ 612 unsigned long command; 613 614 command = AD7793_GetRegisterValue(AD7793_REG_CONF, 615 2, 616 1); // CS is modified by SPI read/write functions. 617 command &= ~AD7793_CONF_CHAN(0xFF); 618 command |= AD7793_CONF_CHAN(channel); 619 AD7793_SetRegisterValue( 620 AD7793_REG_CONF, 621 command, 622 2, 623 1); // CS is modified by SPI read/write functions. 624} 625/***************************************************************************//** 626 * @brief Performs the given calibration to the specified channel. 627 * 628 * @param mode - Calibration type. 629 * @param channel - Channel to be calibrated. 630 * 631 * @return none. 632*******************************************************************************/ 633void AD7793_Calibrate(unsigned char mode, unsigned char channel) 634{ 635 //unsigned short oldRegValue = 0x0; //J'ai remplacé short par long ???? 636 //unsigned short newRegValue = 0x0; 637 638 unsigned long oldRegValue = 0x0; 639 unsigned long newRegValue = 0x0; 640 641 AD7793_SetChannel(channel); 642 oldRegValue = AD7793_GetRegisterValue(AD7793_REG_MODE, 2, 1); // CS is modified by SPI read/write functions. 643 oldRegValue &= ~AD7793_MODE_SEL(0x7); 644 newRegValue = oldRegValue | AD7793_MODE_SEL(mode); 645 AD7793_SetRegisterValue(AD7793_REG_MODE, 646 newRegValue, 647 2, 648 0); // CS is not modified by SPI read/write functions. 649 AD7793_WaitRdyGoLow(); 650 ADI_PART_CS_HIGH; 651} 652 653/***************************************************************************//** 654 * @brief Returns the result of a single conversion. 655 * 656 * @return regData - Result of a single analog-to-digital conversion. 657*******************************************************************************/ 658unsigned long AD7793_SingleConversion(void) 659{ 660 unsigned long command = 0x0; 661 unsigned long regData = 0x0; 662 663 command = AD7793_MODE_SEL(AD7793_MODE_SINGLE); 664 AD7793_SetRegisterValue(AD7793_REG_MODE, 665 command, 666 2, 667 0);// CS is not modified by SPI read/write functions. 668 AD7793_WaitRdyGoLow(); 669 regData = AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions. 670 ADI_PART_CS_HIGH; 671 672 return(regData); 673} 674 675/***************************************************************************//** 676 * @brief Returns the average of several conversion results. 677 * 678 * @return samplesAverage - The average of the conversion results. 679*******************************************************************************/ 680unsigned long AD7793_ContinuousReadAvg(unsigned char sampleNumber) 681{ 682 unsigned long samplesAverage = 0x0; 683 unsigned long command = 0x0; 684 unsigned char count = 0x0; 685 686 command = AD7793_MODE_SEL(AD7793_MODE_CONT); 687 AD7793_SetRegisterValue(AD7793_REG_MODE, 688 command, 689 2, 690 0);// CS is not modified by SPI read/write functions. 691 for(count = 0;count < sampleNumber;count ++) 692 { 693 AD7793_WaitRdyGoLow(); 694 samplesAverage += AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 0); // CS is not modified by SPI read/write functions. 695 } 696 ADI_PART_CS_HIGH; 697 samplesAverage = samplesAverage / sampleNumber; 698 return(samplesAverage); 699} 700 701/***************************************************************************//** 702 * @brief Returns a single measurement, provided continuous mesurement mode has been set up. 703 * 704 * @return samplesAverage - Result of a single analog-to-digital conversion. 705*******************************************************************************/ 706unsigned long AD7793_ContinuousSingleRead() 707{ 708 unsigned long regData = 0x0; 709 710 AD7793_WaitRdyGoLow(); 711 regData = AD7793_GetRegisterValue(AD7793_REG_DATA, 3, 1); // CS is modified by SPI read/write functions. 712 ADI_PART_CS_HIGH; 713 return(regData); 714}
AD7793.h
c_cpp
1/***************************************************************************//** 2 * @file AD7793.h 3 * @brief Header file of AD7793 Driver. 4 * @author Bancisor Mihai 5******************************************************************************** 6 * Copyright 2012(c) Analog Devices, Inc. 7 * 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * - Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * - Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * - Neither the name of Analog Devices, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * - The use of this software may or may not infringe the patent rights 22 * of one or more patent holders. This license does not release you 23 * from the requirement that you obtain separate licenses from these 24 * patent holders to use this software. 25 * - Use of the software either in source or binary form, must be run 26 * on or directly connected to an Analog Devices Inc. component. 27 * 28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, 30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR 34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 38 * 39******************************************************************************** 40 * SVN Revision: 500 41******************************************************************************* 42 43******************************************************************************** 44 * Modified and adapted for Arduino by Ph. Sonnet. March 27, 2019, Version 1.1 45*******************************************************************************/ 46 47#ifndef __AD7793_H__ 48#define __AD7793_H__ 49 50/******************************************************************************/ 51/* AD7793 */ 52/******************************************************************************/ 53 54/* AD7793 GPIO */ 55#define AD7793_RDY_STATE GPIO1_STATE 56 57/*AD7793 Registers*/ 58#define AD7793_REG_COMM 0 /* Communications Register(WO, 8-bit) */ 59#define AD7793_REG_STAT 0 /* Status Register (RO, 8-bit) */ 60#define AD7793_REG_MODE 1 /* Mode Register (RW, 16-bit */ 61#define AD7793_REG_CONF 2 /* Configuration Register (RW, 16-bit)*/ 62#define AD7793_REG_DATA 3 /* Data Register (RO, 16-/24-bit) */ 63#define AD7793_REG_ID 4 /* ID Register (RO, 8-bit) */ 64#define AD7793_REG_IO 5 /* IO Register (RO, 8-bit) */ 65#define AD7793_REG_OFFSET 6 /* Offset Register (RW, 24-bit */ 66#define AD7793_REG_FULLSCAL 7 /* Full-Scale Register (RW, 24-bit */ 67 68/* Communications Register Bit Designations (AD7793_REG_COMM) */ 69#define AD7793_COMM_WEN (1 << 7) /* Write Enable */ 70#define AD7793_COMM_WRITE (0 << 6) /* Write Operation */ 71#define AD7793_COMM_READ (1 << 6) /* Read Operation */ 72#define AD7793_COMM_ADDR(x) (((x) & 0x7) << 3) /* Register Address */ 73#define AD7793_COMM_CREAD (1 << 2) /* Continuous Read of Data Register */ 74 75/* Status Register Bit Designation masks(AD7793_REG_STAT) */ 76#define AD7793_STAT_RDY (1 << 7) /* Ready */ 77#define AD7793_STAT_ERR (1 << 6) /* Error (Overrange, Underrange) */ 78#define AD7793_STAT_CH3 (1 << 2) /* Channel 3 */ 79#define AD7793_STAT_CH2 (1 << 1) /* Channel 2 */ 80#define AD7793_STAT_CH1 (1 << 0) /* Channel 1 */ 81 82/* Mode Register Bit Designations (AD7793_REG_MODE) */ 83#define AD7793_MODE_SEL(x) (((x) & 0x7) << 13) /* Operation Mode Select */ 84#define AD7793_MODE_CLKSRC(x) (((x) & 0x3) << 6) /* ADC Clock Source Select */ 85#define AD7793_MODE_RATE(x) ((x) & 0xF) /* Filter Update Rate Select */ 86 87/* AD7793_MODE_SEL(x) options */ 88#define AD7793_MODE_CONT 0 /* Continuous Conversion Mode */ 89#define AD7793_MODE_SINGLE 1 /* Single Conversion Mode */ 90#define AD7793_MODE_IDLE 2 /* Idle Mode */ 91#define AD7793_MODE_PWRDN 3 /* Power-Down Mode */ 92#define AD7793_MODE_CAL_INT_ZERO 4 /* Internal Zero-Scale Calibration */ 93#define AD7793_MODE_CAL_INT_FULL 5 /* Internal Full-Scale Calibration */ 94#define AD7793_MODE_CAL_SYS_ZERO 6 /* System Zero-Scale Calibration */ 95#define AD7793_MODE_CAL_SYS_FULL 7 /* System Full-Scale Calibration */ 96 97/* AD7793_MODE_CLKSRC(x) options */ 98#define AD7793_CLK_INT 0 /* Internal 64 kHz Clk not available at the CLK pin */ 99#define AD7793_CLK_INT_CO 1 /* Internal 64 kHz Clk available at the CLK pin */ 100#define AD7793_CLK_EXT 2 /* External 64 kHz Clock */ 101#define AD7793_CLK_EXT_DIV2 3 /* External Clock divided by 2 */ 102 103/* AD7793_MODE_RATE(x) options */ 104#define AD7793_RATE_NIL 0 105#define AD7793_RATE_470 1 106#define AD7793_RATE_242 2 107#define AD7793_RATE_123 3 108#define AD7793_RATE_62 4 109#define AD7793_RATE_50 5 110#define AD7793_RATE_39 6 111#define AD7793_RATE_33_2 7 112#define AD7793_RATE_19_6 8 /* 60 Hz only */ 113#define AD7793_RATE_16_7_50 9 /* 50 Hz only */ 114#define AD7793_RATE_16_7_50_60 10 /* 50 and 60 Hz */ 115#define AD7793_RATE_12_5 11 /* 50 and 60 Hz */ 116#define AD7793_RATE_10 12 /* 50 and 60 Hz */ 117#define AD7793_RATE_8_33 13 /* 50 and 60 Hz */ 118#define AD7793_RATE_6_25 14 /* 50 and 60 Hz */ 119#define AD7793_RATE_4_17 15 /* 50 and 60 Hz */ 120 121/* Configuration Register Bit Designations (AD7793_REG_CONF) */ 122#define AD7793_CONF_VBIAS(x) (((x) & 0x3) << 14) /* Bias Voltage Generator Enable */ 123#define AD7793_CONF_BO_EN (1 << 13) /* Burnout Current Enable */ 124#define AD7793_CONF_UNIPOLAR (1 << 12) /* Unipolar/Bipolar Enable */ 125#define AD7793_CONF_BOOST (1 << 11) /* Boost Enable */ 126#define AD7793_CONF_GAIN(x) (((x) & 0x7) << 8) /* Gain Select */ 127#define AD7793_CONF_REFSEL(x) (((x) & 0x1) << 7) /* INT/EXT Reference Select */ 128#define AD7793_CONF_BUF (1 << 4) /* Buffered Mode Enable */ 129#define AD7793_CONF_CHAN(x) ((x) & 0x7) /* Channel select */ 130 131/* AD7793_CONF_VBIAS(x) options */ 132#define AD7793_VBIAS_GEN_DISABL 0 /* Bias voltage generator disabled */ 133#define AD7793_VBIAS_AIN1_NEG 1 /* Bias voltage generator to AIN1(-) */ 134#define AD7793_VBIAS_AIN2_NEG 2 /* Bias voltage generator to AIN2(-) */ 135 136/* AD7793_CONF_GAIN(x) options Gain and ADC input range (2.5V reference)*/ 137#define AD7793_GAIN_1 0 /* 1X (In-amp nor used) 2.5 */ 138#define AD7793_GAIN_2 1 /* 2X (In-amp nor used) 1.25 */ 139#define AD7793_GAIN_4 2 /* 4X 625mV */ 140#define AD7793_GAIN_8 3 /* 8X 312.5 mV*/ 141#define AD7793_GAIN_16 4 /* 16X 156.2 mV*/ 142#define AD7793_GAIN_32 5 /* 32X 78.125 mV */ 143#define AD7793_GAIN_64 6 /* 64X 39.06 mV */ 144#define AD7793_GAIN_128 7 /* 128X 19.53 mV */ 145 146/* AD7793_CONF_REFSEL(x) options */ 147#define AD7793_REFSEL_INT 1 /* Internal reference selected. */ 148#define AD7793_REFSEL_EXT 0 /* External reference applied between REFIN(+) and REFIN(–). */ 149 150/* AD7793_CONF_CHAN(x) options */ 151#define AD7793_CH_AIN1P_AIN1M 0 /* Select channel AIN1(+) - AIN1(-) */ 152#define AD7793_CH_AIN2P_AIN2M 1 /* Select channel AIN2(+) - AIN2(-) */ 153#define AD7793_CH_AIN3P_AIN3M 2 /* Select channel AIN3(+) - AIN3(-) */ 154#define AD7793_CH_AIN1M_AIN1M 3 /* Select channel AIN1(-) - AIN1(-) */ 155#define AD7793_CH_TEMP 6 /* Temp Sensor, gain 1, Internal reference */ 156#define AD7793_CH_AVDD_MONITOR 7 /* AVDD voltage Monitor, gain 1/6, Internal 1.17V reference */ 157 158/* ID Register Bit Designations (AD7793_REG_ID) */ 159#define AD7793_ID 0xB 160#define AD7793_ID_MASK 0xF 161 162/* IO (Excitation Current Sources) Register Bit Designations (AD7793_REG_IO) */ 163#define AD7793_IEXCDIR(x) (((x) & 0x3) << 2) 164#define AD7793_IEXCEN(x) (((x) & 0x3) << 0) 165 166/* AD7793_IEXCDIR(x) options*/ 167#define AD7793_DIR_IEXC1_IOUT1_IEXC2_IOUT2 0 /* IEXC1 connect to IOUT1, IEXC2 connect to IOUT2 */ 168#define AD7793_DIR_IEXC1_IOUT2_IEXC2_IOUT1 1 /* IEXC1 connect to IOUT2, IEXC2 connect to IOUT1 */ 169#define AD7793_DIR_IEXC1_IEXC2_IOUT1 2 /* Both current sources IEXC1,2 connect to IOUT1 */ 170#define AD7793_DIR_IEXC1_IEXC2_IOUT2 3 /* Both current sources IEXC1,2 connect to IOUT2 */ 171 172/* AD7793_IEXCEN(x) options*/ 173#define AD7793_EN_IXCEN_DISABLE 0 /* Disable excitation current*/ 174#define AD7793_EN_IXCEN_10uA 1 /* Excitation Current 10uA */ 175#define AD7793_EN_IXCEN_210uA 2 /* Excitation Current 210uA */ 176#define AD7793_EN_IXCEN_1mA 3 /* Excitation Current 1mA */ 177 178/******************************************************************************/ 179/* Functions Prototypes */ 180/******************************************************************************/ 181 182/* Initialize AD7793 and check if the device is present*/ 183unsigned char AD7793_Init(void); 184 185/* Sends 32 consecutive 1's on SPI in order to reset the part. */ 186void AD7793_Reset(void); 187 188/* Reads the Ready Bit for ADC */ 189unsigned char AD7793_Ready(); 190 191/* Reads the ADC Error Bit */ 192unsigned char AD7793_Error(); 193 194/* Indicates that channel 3 is being converted by the ADC */ 195unsigned char AD7793_Channel3(); 196 197/* Indicates that channel 2 is being converted by the ADC */ 198unsigned char AD7793_Channel2(); 199 200/* Indicates that channel 1 is being converted by the ADC */ 201unsigned char AD7793_Channel1(); 202 203 204/* Reads the value of the selected register. */ 205unsigned long AD7793_GetRegisterValue(unsigned char regAddress, 206 unsigned char size, 207 unsigned char modifyCS); 208 209/* Writes a value to the register. */ 210void AD7793_SetRegisterValue(unsigned char regAddress, 211 unsigned long regValue, 212 unsigned char size, 213 unsigned char modifyCS); 214 215/* Waits for RDY pin to go low. */ 216void AD7793_WaitRdyGoLow(void); 217 218/* Sets the operating mode of AD7793 */ 219void AD7793_SetMode(unsigned long mode); 220 221/* Sets the ADC clock source of AD7793 */ 222void AD7793_SetClockSource(unsigned long clockSource); 223 224/* Sets the filter update rate of AD7793 */ 225void AD7793_SetFilterUpdateRate(unsigned long filterRate); 226 227/* Sets the direction of the internal excitation current source */ 228void AD7793_SetExcitDirection(unsigned long direction); 229 230/* Sets the current of the internal excitation current source */ 231void AD7793_SetExcitCurrent(unsigned long current); 232 233/* Bias voltage generator enable */ 234void AD7793_SetBiasVoltage(unsigned long voltage); 235 236/* Enable burnout current */ 237void AD7793_EnableBurnoutCurr(void); 238 239/* Disable burnout current of AD7793 */ 240void AD7793_DisableBurnoutCurr(void); 241 242/* Enable unipolar coding of AD7793 */ 243void AD7793_EnableUnipolar(void); 244 245/* Disable bipolar coding of AD7793 */ 246void AD7793_DisableBipolar(void); 247 248/* Enable bias voltage generator current boost of AD7793 */ 249void AD7793_EnableCurrBoost(void); 250 251/* Disable bias voltage generator current boost of AD7793 */ 252void AD7793_DisableCurrBoost(void); 253 254/* Sets the gain of the In-Amp */ 255void AD7793_SetGain(unsigned long gain); 256 257/* Sets the reference source for the ADC */ 258void AD7793_SetIntReference(unsigned char type); 259 260/* Enable buffered mode of AD7793*/ 261void AD7793_EnableBufMode(void); 262 263/* Disable buffered mode of AD7793 */ 264void AD7793_DisableBufMode(void); 265 266/* Selects the channel of AD7793 */ 267void AD7793_SetChannel(unsigned long channel); 268 269/* Performs the given calibration to the specified channel. */ 270void AD7793_Calibrate(unsigned char mode, unsigned char channel); 271 272/* Returns the result of a single conversion. */ 273unsigned long AD7793_SingleConversion(void); 274 275/* Returns the average of several conversion results. */ 276unsigned long AD7793_ContinuousReadAvg(unsigned char sampleNumber); 277 278/* Returns a single measurement, provided continuous mesurement mode has been set up. */ 279unsigned long AD7793_ContinuousSingleRead(); 280 281#endif // _AD7793_H_ 282
Downloadable files
Temperature and voltage measurement circuit schematics.
Temperature and voltage measurement circuit schematics.
Temperature and voltage measurement circuit schematics.
Temperature and voltage measurement circuit schematics.
Comments
Only logged in users can leave comments
iamsolar
0 Followers
•0 Projects
Table of contents
Intro
5
0