ESP32 Arduino AD7705 SPI Library

RobFox
Posts: 1
Joined: Wed Dec 06, 2017 2:44 pm

ESP32 Arduino AD7705 SPI Library

Postby RobFox » Wed Dec 06, 2017 2:46 pm

Does anyone know of a library that I can use with an AD7705, communicating using SPI, to and ESP32 using the Arduino IDE?

User avatar
jgustavoam
Posts: 134
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: ESP32 Arduino AD7705 SPI Library

Postby jgustavoam » Sat Feb 03, 2018 3:25 pm

Hi ,
My suggestion is that you study AD7705 datasheet and convert Arduino Libray to ESP32 Library .
http://www.analog.com/media/en/technica ... 5_7706.pdf
https://github.com/kerrydwong/AD770X
Retired IBM Brasil
Electronic hobbyist since 1976.

zacilaci
Posts: 1
Joined: Thu Apr 21, 2022 9:42 pm

Re: ESP32 Arduino AD7705 SPI Library

Postby zacilaci » Thu Apr 21, 2022 9:48 pm

Hi !

I have tried to do what you suggested jgustavoam, but with no luck sadly. Could you help me perhaps? Or is there any open-source code available now? I tried github but with no luck sadly.

You can see my modifications below:

.cpp file
  1. /*
  2.  * AD7705/AD7706 Library
  3.  * Kerry D. Wong
  4.  * http://www.kerrywong.com
  5.  * Initial version 1.0 3/2011
  6.  * Updated 1.1 4/2012
  7.  */
  8.  
  9. #include "AD7705.h"
  10.  
  11. //write communication register
  12. //   7        6      5      4      3      2      1      0
  13. //0/DRDY(0) RS2(0) RS1(0) RS0(0) R/W(0) STBY(0) CH1(0) CH0(0)
  14.  
  15. SPIClass vspi(VSPI);
  16.  
  17. void AD770X::setNextOperation(byte reg, byte channel, byte readWrite) {
  18.     byte r = 0;
  19.     r = reg << 4 | readWrite << 3 | channel;
  20.  
  21.     //digitalWrite(pinCS, LOW);
  22.     spiTransfer(r);
  23.     //digitalWrite(pinCS, HIGH);
  24. }
  25.  
  26. //Clock Register
  27. //   7      6       5        4        3        2      1      0
  28. //ZERO(0) ZERO(0) ZERO(0) CLKDIS(0) CLKDIV(0) CLK(1) FS1(0) FS0(1)
  29. //
  30. //CLKDIS: master clock disable bit
  31. //CLKDIV: clock divider bit
  32.  
  33. void AD770X::writeClockRegister(byte CLKDIS, byte CLKDIV, byte outputUpdateRate) {
  34.     byte r = CLKDIS << 4 | CLKDIV << 3 | outputUpdateRate;
  35.  
  36.     r &= ~(1 << 2); // clear CLK
  37.  
  38.     //digitalWrite(pinCS, LOW);
  39.     spiTransfer(r);
  40.     //digitalWrite(pinCS, HIGH);
  41. }
  42.  
  43. //Setup Register
  44. //  7     6     5     4     3      2      1      0
  45. //MD10) MD0(0) G2(0) G1(0) G0(0) B/U(0) BUF(0) FSYNC(1)
  46.  
  47. void AD770X::writeSetupRegister(byte operationMode, byte gain, byte unipolar, byte buffered, byte fsync) {
  48.     byte r = operationMode << 6 | gain << 3 | unipolar << 2 | buffered << 1 | fsync;
  49.  
  50.     //digitalWrite(pinCS, LOW);
  51.     spiTransfer(r);
  52.     //digitalWrite(pinCS, HIGH);
  53. }
  54.  
  55. unsigned int AD770X::readADResult() {
  56.     //digitalWrite(pinCS, LOW);
  57.     byte b1 = spiTransfer(0x0);
  58.     byte b2 = spiTransfer(0x0);
  59.     //digitalWrite(pinCS, HIGH);
  60.  
  61.     unsigned int r = b1 << 8 | b2;
  62.  
  63.     return r;
  64. }
  65.  
  66. unsigned int AD770X::readADResultRaw(byte channel) {
  67.     while (!dataReady(channel)) {
  68.     };
  69.     setNextOperation(REG_DATA, channel, 1);
  70.  
  71.     return readADResult();
  72. }
  73.  
  74. double AD770X::readADResult(byte channel, float refOffset) {
  75.     return readADResultRaw(channel) * 1.0 / 65536.0 * VRef - refOffset;
  76. }
  77.  
  78. bool AD770X::dataReady(byte channel) {
  79.     setNextOperation(REG_CMM, channel, 1);
  80.  
  81.     //digitalWrite(pinCS, LOW);
  82.     byte b1 = spiTransfer(0x0);
  83.     //digitalWrite(pinCS, HIGH);
  84.  
  85.     return (b1 & 0x80) == 0x0;
  86. }
  87.  
  88. void AD770X::reset() {
  89.     //digitalWrite(pinCS, LOW);
  90.     /*for (int i = 0; i < 100; i++)
  91.         spiTransfer(0xff);*/
  92.  
  93.     digitalWrite(0, LOW);
  94.     delay(100);
  95.     digitalWrite(0, HIGH);
  96.     //digitalWrite(pinCS, HIGH);
  97. }
  98.  
  99. AD770X::AD770X(double vref) {
  100.     VRef = vref;
  101.     pinMode(pinMOSI, OUTPUT);
  102.     pinMode(pinMISO, INPUT);
  103.     pinMode(pinSPIClock, OUTPUT);
  104.     pinMode(pinCS, OUTPUT);
  105.     pinMode(0, OUTPUT); // RST
  106.  
  107.     vspi.begin();//pinSPIClock, pinMISO, pinMOSI, pinCS);
  108.  
  109.     digitalWrite(pinCS, HIGH);
  110.     //SPCR = _BV(SPE) | _BV(MSTR) | _BV(CPOL) | _BV(CPHA) | _BV(SPI2X) | _BV(SPR1) | _BV(SPR0);
  111. }
  112.  
  113. void AD770X::init(byte channel, byte clkDivider, byte polarity, byte gain, byte updRate) {
  114.     setNextOperation(REG_CLOCK, channel, 0);
  115.     writeClockRegister(0, clkDivider, updRate);
  116.  
  117.     setNextOperation(REG_SETUP, channel, 0);
  118.     writeSetupRegister(MODE_SELF_CAL, gain, polarity, 0, 0);
  119.  
  120.     while (!dataReady(channel)) {
  121.     };
  122. }
  123.  
  124. void AD770X::init(byte channel) {
  125.     init(channel, CLK_DIV_1, BIPOLAR, GAIN_1, UPDATE_RATE_25);
  126. }



.h file:
  1. #ifndef AD770X_H
  2. #define AD770X_H
  3.  
  4. #include <Arduino.h>
  5.  
  6. #include <SPI.h>
  7.  
  8. /*
  9.  * AD7705/AD7706 Library
  10.  * Kerry D. Wong
  11.  * http://www.kerrywong.com
  12.  * Initial version 1.0 3/2011
  13.  * Updated 1.1 4/2012
  14.  */
  15.  
  16. //#define SPI_CLK 16000000UL // 16 MHz
  17. #define SPI_CLK 400000UL // 400 kHz
  18.  
  19. class AD770X {
  20. public:
  21.     //register selection
  22.     //RS2 RS1 RS0
  23.     static const byte REG_CMM = 0x0; //communication register 8 bit
  24.     static const byte REG_SETUP = 0x1; //setup register 8 bit
  25.     static const byte REG_CLOCK = 0x2; //clock register 8 bit
  26.     static const byte REG_DATA = 0x3; //data register 16 bit, contains conversion result
  27.     static const byte REG_TEST = 0x4; //test register 8 bit, POR 0x0
  28.     static const byte REG_NOP = 0x5; //no operation
  29.     static const byte REG_OFFSET = 0x6; //offset register 24 bit
  30.     static const byte REG_GAIN = 0x7; // gain register 24 bit
  31.  
  32.     //channel selection for AD7706 (for AD7705 use the first two channel definitions)
  33.     //CH1 CH0
  34.     static const byte CHN_AIN1 = 0x0; //AIN1; calibration register pair 0
  35.     static const byte CHN_AIN2 = 0x1; //AIN2; calibration register pair 1
  36.     static const byte CHN_COMM = 0x2; //common; calibration register pair 0
  37.     static const byte CHN_AIN3 = 0x3; //AIN3; calibration register pair 2
  38.  
  39.     //output update rate
  40.     //CLK FS1 FS0
  41.     static const byte UPDATE_RATE_20 = 0x0; // 20 Hz
  42.     static const byte UPDATE_RATE_25 = 0x1; // 25 Hz
  43.     static const byte UPDATE_RATE_100 = 0x2; // 100 Hz
  44.     static const byte UPDATE_RATE_200 = 0x3; // 200 Hz
  45.     static const byte UPDATE_RATE_50 = 0x4; // 50 Hz
  46.     static const byte UPDATE_RATE_60 = 0x5; // 60 Hz
  47.     static const byte UPDATE_RATE_250 = 0x6; // 250 Hz
  48.     static const byte UPDATE_RATE_500 = 0x7; // 500 Hz
  49.  
  50.     //operating mode options
  51.     //MD1 MD0
  52.     static const byte MODE_NORMAL = 0x0; //normal mode
  53.     static const byte MODE_SELF_CAL = 0x1; //self-calibration
  54.     static const byte MODE_ZERO_SCALE_CAL = 0x2; //zero-scale system calibration, POR 0x1F4000, set FSYNC high before calibration, FSYNC low after calibration
  55.     static const byte MODE_FULL_SCALE_CAL = 0x3; //full-scale system calibration, POR 0x5761AB, set FSYNC high before calibration, FSYNC low after calibration
  56.  
  57.     //gain setting
  58.     static const byte GAIN_1 = 0x0;
  59.     static const byte GAIN_2 = 0x1;
  60.     static const byte GAIN_4 = 0x2;
  61.     static const byte GAIN_8 = 0x3;
  62.     static const byte GAIN_16 = 0x4;
  63.     static const byte GAIN_32 = 0x5;
  64.     static const byte GAIN_64 = 0x6;
  65.     static const byte GAIN_128 = 0x7;
  66.  
  67.     static const byte UNIPOLAR = 0x0;
  68.     static const byte BIPOLAR = 0x1;
  69.  
  70.     static const byte CLK_DIV_1 = 0x1;
  71.     static const byte CLK_DIV_2 = 0x2;
  72.  
  73.     SPIClass vspi;
  74.  
  75.  
  76.     byte spiTransfer(volatile byte data) {
  77.        
  78.         vspi.beginTransaction(SPISettings(SPI_CLK, MSBFIRST, SPI_MODE0));
  79.         digitalWrite(pinCS, LOW);
  80.         vspi.transfer(data);
  81.         digitalWrite(pinCS, HIGH);
  82.         vspi.endTransaction();
  83.  
  84.  
  85.         return data;
  86.     };
  87.  
  88.     AD770X(double vref);
  89.     void setNextOperation(byte reg, byte channel, byte readWrite);
  90.     void writeClockRegister(byte CLKDIS, byte CLKDIV, byte outputUpdateRate);
  91.     void writeSetupRegister(byte operationMode, byte gain, byte unipolar, byte buffered, byte fsync);
  92.     unsigned int readADResultRaw(byte channel);
  93.     double readADResult(byte channel, float refOffset = 0.0);
  94.     void reset();
  95.     bool dataReady(byte channel);
  96.     void init(byte channel);
  97.     void init(byte channel, byte clkDivider, byte polarity, byte gain, byte updRate);
  98. private:
  99.     static const int pinMOSI = 23; //MOSI
  100.     static const int pinMISO = 19; //MISO
  101.     static const int pinSPIClock = 18; //SCK
  102.     static const int pinCS = 5; //CS
  103.     double VRef;
  104.     unsigned int readADResult();
  105. };
  106. #endif

User avatar
jgustavoam
Posts: 134
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: ESP32 Arduino AD7705 SPI Library

Postby jgustavoam » Sun Jul 31, 2022 2:20 pm

Hi,
Let's do it by steps...
https://github.com/espressif/arduino-esp32

Important References:
https://docs.espressif.com/projects/esp ... aster.html (ESP-IDF only)
https://docs.espressif.com/projects/ard ... arted.html

See how to build an ESP32 Arduino library. I'm not an experienced programmer.
https://github.com/espressif/esp32-arduino-lib-builder

Searching for a ready-made library.
https://github.com/search?p=2&q=esp32+a ... positories

Ready-made Library BME280 (SPI), for example:
https://github.com/mgo-tec/ESP32_BME280_SPI
https://www.bosch-sensortec.com/media/b ... -ds002.pdf

https://github.com/espressif/arduino-es ... _Buses.ino

Code: Select all

/* The ESP32 has four SPi buses, however as of right now only two of
 * them are available to use, HSPI and VSPI. Simply using the SPI API 
 * as illustrated in Arduino examples will use VSPI, leaving HSPI unused.
 * 
 * However if we simply intialise two instance of the SPI class for both
 * of these buses both can be used. However when just using these the Arduino
 * way only will actually be outputting at a time.

----------------------------------------------------------------------

Do you know the Golang language? Very simple.
https://tinygo.org/docs/reference/micro ... eboard-v2/
https://github.com/Gustavomurta/tinyGo_ ... 04/main.go
Retired IBM Brasil
Electronic hobbyist since 1976.

Who is online

Users browsing this forum: No registered users and 61 guests