have two tm7705 boards from ebay, im trying to read adc chanel 1 with a library made by Kerry D.Wong but its not working, when conecting 4.6v supply to board input pins then measured voltage with voltmeter across -ain +ain of adc chip is 2.3v thats half the voltage of the supply meaning that input circuitry is dividing the input voltage, and the value showing in serial monitor is 1.33 even if i change the input voltage, i noticed that if i change GAIN_1 to GAIN_128 the value changes to 2.42 but stays there even when changing input voltage,
checked the datasheet but seems that the library is made correct i don`t know whats wrong the library and code is as below:
#ifndef AD770X_H
#define AD770X_H
#include <Arduino.h>
/*
- AD7705/AD7706 Library
- Kerry D. Wong
- http://www.kerrywong.com
- Initial version 1.0 3/2011
- Updated 1.1 4/2012
*/
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;
byte spiTransfer(volatile byte data) {
SPDR = data;
while (!(SPSR & _BV(SPIF)));
return SPDR;
};
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);
double readADResult(byte channel, float refOffset = 0.0);
void reset();
bool dataReady(byte channel);
int comRegRead(byte channel);
void init(byte channel);
void init(byte channel, byte clkDivider, byte polarity, byte gain, byte updRate);
private:
static const int pinMOSI = 11; //MOSI
static const int pinMISO = 12; //MISO
static const int pinSPIClock = 13; //SCK
static const int pinCS = 10; //CS
double VRef;
unsigned int readADResult();
};
#endif
/*
- AD7705/AD7706 Library
- Kerry D. Wong
- http://www.kerrywong.com
- Initial version 1.0 3/2011
- Updated 1.1 4/2012
*/
#include "AD770X.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)
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 = 0x01 << 2;
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(0);
byte b2 = spiTransfer(0);
digitalWrite(pinCS, HIGH);
unsigned int r = b1 << 8 | b2;
return r;
}
double AD770X::readADResult(byte channel, float refOffset) {
while (!dataReady(channel)) {
};
setNextOperation(REG_DATA, channel, 1);
return readADResult() * 1.0 / 65536.0 * VRef - refOffset;
}
bool AD770X::dataReady(byte channel) {
setNextOperation(REG_CMM, channel, 1);
digitalWrite(pinCS, LOW);
byte b1 = spiTransfer(0);
digitalWrite(pinCS, HIGH);
return (b1 & 0xF0) == 0x0;
}
void AD770X::reset() {
digitalWrite(pinCS, LOW);
for (int i = 0; i < 100; i++)
spiTransfer(0xff);
digitalWrite(pinCS, HIGH);
}
AD770X::AD770X(double vref) {
VRef = vref;
pinMode(pinMOSI, OUTPUT);
pinMode(pinMISO, INPUT);
pinMode(pinSPIClock, OUTPUT);
pinMode(pinCS, OUTPUT);
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(1, 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_128, UPDATE_RATE_500);
}
/*
- AD770X Library
- sample code (AD7706)
- Kerry D. Wong
- http://www.kerrywong.com
- 3/2011
*/
#include <AD770X.h>
#include <SPI.h>
AD770X ad7706(2.5);
double v;
void setup()
{
pinMode(SS, OUTPUT);
digitalWrite(SS,HIGH); //disable device
digitalWrite(10, HIGH);
Serial.begin(115200);
ad7706.reset();
ad7706.init(AD770X::CHN_AIN1);
}
void loop()
{
v = ad7706.readADResult(AD770X::CHN_AIN1, 0.0);
Serial.println(v);
}