ESP32 Arduino AD7705 SPI Library
ESP32 Arduino AD7705 SPI Library
Does anyone know of a library that I can use with an AD7705, communicating using SPI, to and ESP32 using the Arduino IDE?
- jgustavoam
- Posts: 173
- Joined: Thu Feb 01, 2018 2:43 pm
- Location: Belo Horizonte , Brazil
- Contact:
Re: ESP32 Arduino AD7705 SPI Library
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
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.
Electronic hobbyist since 1976.
Re: ESP32 Arduino AD7705 SPI Library
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
.h file:
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
Code: AD7705.cpp Select all
/*
* AD7705/AD7706 Library
* Kerry D. Wong
* http://www.kerrywong.com
* Initial version 1.0 3/2011
* Updated 1.1 4/2012
*/
#include "AD7705.h"
//write communication register
// 7 6 5 4 3 2 1 0
//0/DRDY(0) RS2(0) RS1(0) RS0(0) R/W(0) STBY(0) CH1(0) CH0(0)
SPIClass vspi(VSPI);
void AD770X::setNextOperation(byte reg, byte channel, byte readWrite) {
byte r = 0;
r = reg << 4 | readWrite << 3 | channel;
//digitalWrite(pinCS, LOW);
spiTransfer(r);
//digitalWrite(pinCS, HIGH);
}
//Clock Register
// 7 6 5 4 3 2 1 0
//ZERO(0) ZERO(0) ZERO(0) CLKDIS(0) CLKDIV(0) CLK(1) FS1(0) FS0(1)
//
//CLKDIS: master clock disable bit
//CLKDIV: clock divider bit
void AD770X::writeClockRegister(byte CLKDIS, byte CLKDIV, byte outputUpdateRate) {
byte r = CLKDIS << 4 | CLKDIV << 3 | outputUpdateRate;
r &= ~(1 << 2); // clear CLK
//digitalWrite(pinCS, LOW);
spiTransfer(r);
//digitalWrite(pinCS, HIGH);
}
//Setup Register
// 7 6 5 4 3 2 1 0
//MD10) MD0(0) G2(0) G1(0) G0(0) B/U(0) BUF(0) FSYNC(1)
void AD770X::writeSetupRegister(byte operationMode, byte gain, byte unipolar, byte buffered, byte fsync) {
byte r = operationMode << 6 | gain << 3 | unipolar << 2 | buffered << 1 | fsync;
//digitalWrite(pinCS, LOW);
spiTransfer(r);
//digitalWrite(pinCS, HIGH);
}
unsigned int AD770X::readADResult() {
//digitalWrite(pinCS, LOW);
byte b1 = spiTransfer(0x0);
byte b2 = spiTransfer(0x0);
//digitalWrite(pinCS, HIGH);
unsigned int r = b1 << 8 | b2;
return r;
}
unsigned int AD770X::readADResultRaw(byte channel) {
while (!dataReady(channel)) {
};
setNextOperation(REG_DATA, channel, 1);
return readADResult();
}
double AD770X::readADResult(byte channel, float refOffset) {
return readADResultRaw(channel) * 1.0 / 65536.0 * VRef - refOffset;
}
bool AD770X::dataReady(byte channel) {
setNextOperation(REG_CMM, channel, 1);
//digitalWrite(pinCS, LOW);
byte b1 = spiTransfer(0x0);
//digitalWrite(pinCS, HIGH);
return (b1 & 0x80) == 0x0;
}
void AD770X::reset() {
//digitalWrite(pinCS, LOW);
/*for (int i = 0; i < 100; i++)
spiTransfer(0xff);*/
digitalWrite(0, LOW);
delay(100);
digitalWrite(0, HIGH);
//digitalWrite(pinCS, HIGH);
}
AD770X::AD770X(double vref) {
VRef = vref;
pinMode(pinMOSI, OUTPUT);
pinMode(pinMISO, INPUT);
pinMode(pinSPIClock, OUTPUT);
pinMode(pinCS, OUTPUT);
pinMode(0, OUTPUT); // RST
vspi.begin();//pinSPIClock, pinMISO, pinMOSI, pinCS);
digitalWrite(pinCS, HIGH);
//SPCR = _BV(SPE) | _BV(MSTR) | _BV(CPOL) | _BV(CPHA) | _BV(SPI2X) | _BV(SPR1) | _BV(SPR0);
}
void AD770X::init(byte channel, byte clkDivider, byte polarity, byte gain, byte updRate) {
setNextOperation(REG_CLOCK, channel, 0);
writeClockRegister(0, clkDivider, updRate);
setNextOperation(REG_SETUP, channel, 0);
writeSetupRegister(MODE_SELF_CAL, gain, polarity, 0, 0);
while (!dataReady(channel)) {
};
}
void AD770X::init(byte channel) {
init(channel, CLK_DIV_1, BIPOLAR, GAIN_1, UPDATE_RATE_25);
}.h file:
Code: AD7705.h Select all
#ifndef AD770X_H
#define AD770X_H
#include <Arduino.h>
#include <SPI.h>
/*
* AD7705/AD7706 Library
* Kerry D. Wong
* http://www.kerrywong.com
* Initial version 1.0 3/2011
* Updated 1.1 4/2012
*/
//#define SPI_CLK 16000000UL // 16 MHz
#define SPI_CLK 400000UL // 400 kHz
class AD770X {
public:
//register selection
//RS2 RS1 RS0
static const byte REG_CMM = 0x0; //communication register 8 bit
static const byte REG_SETUP = 0x1; //setup register 8 bit
static const byte REG_CLOCK = 0x2; //clock register 8 bit
static const byte REG_DATA = 0x3; //data register 16 bit, contains conversion result
static const byte REG_TEST = 0x4; //test register 8 bit, POR 0x0
static const byte REG_NOP = 0x5; //no operation
static const byte REG_OFFSET = 0x6; //offset register 24 bit
static const byte REG_GAIN = 0x7; // gain register 24 bit
//channel selection for AD7706 (for AD7705 use the first two channel definitions)
//CH1 CH0
static const byte CHN_AIN1 = 0x0; //AIN1; calibration register pair 0
static const byte CHN_AIN2 = 0x1; //AIN2; calibration register pair 1
static const byte CHN_COMM = 0x2; //common; calibration register pair 0
static const byte CHN_AIN3 = 0x3; //AIN3; calibration register pair 2
//output update rate
//CLK FS1 FS0
static const byte UPDATE_RATE_20 = 0x0; // 20 Hz
static const byte UPDATE_RATE_25 = 0x1; // 25 Hz
static const byte UPDATE_RATE_100 = 0x2; // 100 Hz
static const byte UPDATE_RATE_200 = 0x3; // 200 Hz
static const byte UPDATE_RATE_50 = 0x4; // 50 Hz
static const byte UPDATE_RATE_60 = 0x5; // 60 Hz
static const byte UPDATE_RATE_250 = 0x6; // 250 Hz
static const byte UPDATE_RATE_500 = 0x7; // 500 Hz
//operating mode options
//MD1 MD0
static const byte MODE_NORMAL = 0x0; //normal mode
static const byte MODE_SELF_CAL = 0x1; //self-calibration
static const byte MODE_ZERO_SCALE_CAL = 0x2; //zero-scale system calibration, POR 0x1F4000, set FSYNC high before calibration, FSYNC low after calibration
static const byte MODE_FULL_SCALE_CAL = 0x3; //full-scale system calibration, POR 0x5761AB, set FSYNC high before calibration, FSYNC low after calibration
//gain setting
static const byte GAIN_1 = 0x0;
static const byte GAIN_2 = 0x1;
static const byte GAIN_4 = 0x2;
static const byte GAIN_8 = 0x3;
static const byte GAIN_16 = 0x4;
static const byte GAIN_32 = 0x5;
static const byte GAIN_64 = 0x6;
static const byte GAIN_128 = 0x7;
static const byte UNIPOLAR = 0x0;
static const byte BIPOLAR = 0x1;
static const byte CLK_DIV_1 = 0x1;
static const byte CLK_DIV_2 = 0x2;
SPIClass vspi;
byte spiTransfer(volatile byte data) {
vspi.beginTransaction(SPISettings(SPI_CLK, MSBFIRST, SPI_MODE0));
digitalWrite(pinCS, LOW);
vspi.transfer(data);
digitalWrite(pinCS, HIGH);
vspi.endTransaction();
return data;
};
AD770X(double vref);
void setNextOperation(byte reg, byte channel, byte readWrite);
void writeClockRegister(byte CLKDIS, byte CLKDIV, byte outputUpdateRate);
void writeSetupRegister(byte operationMode, byte gain, byte unipolar, byte buffered, byte fsync);
unsigned int readADResultRaw(byte channel);
double readADResult(byte channel, float refOffset = 0.0);
void reset();
bool dataReady(byte channel);
void init(byte channel);
void init(byte channel, byte clkDivider, byte polarity, byte gain, byte updRate);
private:
static const int pinMOSI = 23; //MOSI
static const int pinMISO = 19; //MISO
static const int pinSPIClock = 18; //SCK
static const int pinCS = 5; //CS
double VRef;
unsigned int readADResult();
};
#endif- jgustavoam
- Posts: 173
- Joined: Thu Feb 01, 2018 2:43 pm
- Location: Belo Horizonte , Brazil
- Contact:
Re: ESP32 Arduino AD7705 SPI Library
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
----------------------------------------------------------------------
Do you know the Golang language? Very simple.
https://tinygo.org/docs/reference/micro ... eboard-v2/
https://github.com/Gustavomurta/tinyGo_ ... 04/main.go
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.
Electronic hobbyist since 1976.
Who is online
Users browsing this forum: ChatGPT-User and 2 guests