Skip to content

Commit

Permalink
support ADDR and REFSEL (#2)
Browse files Browse the repository at this point in the history
- add **getLastADCA()** and **getLastADCB()**
- obsolete **getValue()**
- updated examples (with above change).
- add support for **ADDR** pin
- add support for **REFSEL** pin
- add support for **RANGE** pins
- add examples 
- update readme.md
  • Loading branch information
RobTillaart authored Feb 23, 2025
1 parent 60994dc commit 2e45a6d
Show file tree
Hide file tree
Showing 15 changed files with 529 additions and 52 deletions.
127 changes: 113 additions & 14 deletions AD7367.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//
// FILE: AD7367.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// DATE: 2025-01-10
// PURPOSE: Arduino library for the AD7367, 2 channel simultaneous sampling 14 bit ADC.
// URL: https://github.com/RobTillaart/AD7367


#include "AD7367.h"
Expand Down Expand Up @@ -36,13 +37,21 @@ void AD7367::begin()
digitalWrite(_convert, HIGH);
}


uint8_t AD7367::getType()
{
return _type;
}

int AD7367::getBits()
{
return _bits;
}


//////////////////////////////////////////////////////////////////
//
// READ
//
int AD7367::read()
{
// simulation
Expand All @@ -57,7 +66,6 @@ int AD7367::read()
return readAsync();
}


void AD7367::triggerConversion()
{
// Trigger conversion by 10 ns pulse.
Expand All @@ -69,19 +77,16 @@ void AD7367::triggerConversion()
digitalWrite(_convert, HIGH);
}


bool AD7367::conversionBusy()
{
return digitalRead(_busy) == HIGH;
}


bool AD7367::conversionReady()
{
return digitalRead(_busy) == LOW;
}


int AD7367::readAsync()
{
digitalWrite(_select, LOW);
Expand All @@ -93,28 +98,122 @@ int AD7367::readAsync()
{
digitalWrite(_clock, LOW);
digitalWrite(_clock, HIGH);
d0 <<= 1;
d0 |= digitalRead(_data0);
d1 <<= 1;
d1 |= digitalRead(_data1);
d0 = (d0 << 1) | digitalRead(_data0);
d1 = (d1 << 1) | digitalRead(_data1);
}
digitalWrite(_select, HIGH);

_value[0] = d0;
_value[1] = d1;
return 0;
}

int AD7367::getLastADCA()
{
return _value[0];
}

int AD7367::getValue(uint8_t channel)
int AD7367::getLastADCB()
{
return _value[channel];
return _value[1];
}


int AD7367::getBits()
//////////////////////////////////////////////////////////////////
//
// FASTREAD
//
int AD7367::fastRead(int &a, int &b)
{
return _bits;
// Trigger conversion by 10 ns pulse. (see above)
digitalWrite(_convert, LOW);
// delayMicroseconds(1);
digitalWrite(_convert, HIGH);
// wait for ready (blocking)
while(digitalRead(_busy) == HIGH);
// fetch data
digitalWrite(_select, LOW);
// reset data received.
a = 0;
b = 0;
// clock in the bits
for (int i = 0; i < _bits; i++)
{
digitalWrite(_clock, LOW);
digitalWrite(_clock, HIGH);
a = (a << 1) | digitalRead(_data0);
b = (b << 1) | digitalRead(_data1);
}
digitalWrite(_select, HIGH);

_value[0] = a;
_value[1] = b;
return 0;
}


//////////////////////////////////////////////////////////////////
//
// ADDR + REFSEL
//
void AD7367::setADDRpin(uint8_t pin)
{
_addr = pin;
pinMode(_addr, OUTPUT);
digitalWrite(_addr, LOW);
}

void AD7367::setREFSELpin(uint8_t pin)
{
_refsel = pin;
pinMode(_refsel, OUTPUT);
digitalWrite(_refsel, HIGH);
}


//////////////////////////////////////////////////////////////////
//
// RANGE
//
void AD7367::setRangePin(uint8_t range0, uint8_t range1)
{
_range0 = range0;
_range1 = range1;
pinMode(_range0, OUTPUT);
pinMode(_range1, OUTPUT);
digitalWrite(_range0, LOW);
digitalWrite(_range1, LOW);
}

int AD7367::setRange(uint8_t range)
{
if (range > 2) return -1;
if (conversionBusy()) return -2;
digitalWrite(_range0, range & 0x01);
digitalWrite(_range1, (range >> 1) & 0x01);
return 0;
}

uint8_t AD7367::getRange()
{
if ((_range0 == 255) || (_range1 == 255)) return 255;
uint8_t range = digitalRead(_range0) == LOW ? 0 : 1;
range += digitalRead(_range1) == LOW ? 0 : 2;
return range;
}



//////////////////////////////////////////////////////////////////
//
// OBSOLETE
//
int AD7367::getValue(uint8_t channel)
{
return _value[channel];
}



//////////////////////////////////////////////////////////////////
//
Expand Down
59 changes: 53 additions & 6 deletions AD7367.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
//
// FILE: AD7367.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// DATE: 2025-01-10
// PURPOSE: Arduino library for the AD7367, 2 channel simultaneous sampling 14 bit ADC.
// Also AD7366 == 12 bits.
// URL: https://github.com/RobTillaart/AD7367


#include "Arduino.h"

#define AD7367_LIB_VERSION (F("0.1.0"))
#define AD7367_LIB_VERSION (F("0.1.1"))


class AD7367
Expand All @@ -25,14 +26,55 @@ class AD7367
int getBits(); // returns 12 or 14

// READ
int read();
// READ ASYNC
// note the ADDR line below determines which pair is read (a1, b1) or (a2, b2)
int read(); // reads ADC-A and ADC-B into an internal buffer.

// READ ASYNCHRONOUS
void triggerConversion();
bool conversionBusy();
bool conversionReady();
int readAsync();
// GET VALUE
int getValue(uint8_t channel); // channel = 0 or 1
// GET VALUES FROM ADC's
int getLastADCA(); // returns last read value from ADC-A
int getLastADCB(); // returns last read value from ADC-B

// FASTREAD
// read and return 2 measurements by reference. (arrays)
// a == ADC-A, b == ADC-B
int fastRead(int &a, int &b);


// ADDR
// one could hard connect the ADDR pin,
// so these functions are not needed
// pin is set default LOW
void setADDRpin(uint8_t pin);
// LOW = (Va1, Vb1) or HIGH = (Va2, Vb2)
inline void ADDRwrite(uint8_t mode) { digitalWrite(_addr, mode); };


// REFSEL
// one could hard connect the REFSEL pin,
// so these functions are not needed
// pin is set default HIGH = internal.
void setREFSELpin(uint8_t pin);
// LOW = external voltage or LOW = internal 2.5 Volt.
inline void REFSELwrite(uint8_t mode) { digitalWrite(_refsel, mode); };


// RANGE
void setRangePin(uint8_t range0, uint8_t range1);
// page 16/17, table 8
// 0 = ±10 V
// 1 = ±5 V
// 2 = 0 V to 10 V
// other values = fail
// returns 0 on success, -1 or -2 on failure.
int setRange(uint8_t range);
uint8_t getRange(); // returns 0, 1, 2 (255 if pins are not set)

// OBSOLETE
int getValue(uint8_t channel); // 0 = ADC-A, 1 = ADC-B


protected:
Expand All @@ -45,6 +87,11 @@ class AD7367
uint8_t _data0;
uint8_t _data1;
int16_t _value[2] = {0, 0};

uint8_t _addr = 255;
uint8_t _refsel = 255;
uint8_t _range0 = 255;
uint8_t _range1 = 255;
};


Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.1.1] - 2025-02-04
- add **getLastADCA()** and **getLastADCB()**
- obsolete **getValue()**
- updated examples (with above change).
- add support for **ADDR** pin
- add support for **REFSEL** pin
- add support for **RANGE** pins
- add examples
- update readme.md


## [0.1.0] - 2025-01-10
- initial version

Loading

0 comments on commit 2e45a6d

Please sign in to comment.