ADC ok with interrupt or drdy

This commit is contained in:
Emanuele Trabattoni
2026-04-17 12:21:35 +02:00
parent 1b8ba88b05
commit bea29dc8f5
3 changed files with 652 additions and 623 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
//ADS1256 header file
// ADS1256 header file
/*
Name: ADS1256.h
Created: 2022/07/14
@@ -14,54 +14,55 @@
#define _ADS1256_h
#include <SPI.h>
#include <Arduino.h>
// SPI Frequency
#define SPI_FREQ 1920000
//Differential inputs
#define DIFF_0_1 0b00000001 //A0 + A1 as differential input
#define DIFF_2_3 0b00100011 //A2 + A3 as differential input
#define DIFF_4_5 0b01000101 //A4 + A5 as differential input
#define DIFF_6_7 0b01100111 //A6 + A7 as differential input
// Differential inputs
#define DIFF_0_1 0b00000001 // A0 + A1 as differential input
#define DIFF_2_3 0b00100011 // A2 + A3 as differential input
#define DIFF_4_5 0b01000101 // A4 + A5 as differential input
#define DIFF_6_7 0b01100111 // A6 + A7 as differential input
//Single-ended inputs
#define SING_0 0b00001111 //A0 + GND (common) as single-ended input
#define SING_1 0b00011111 //A1 + GND (common) as single-ended input
#define SING_2 0b00101111 //A2 + GND (common) as single-ended input
#define SING_3 0b00111111 //A3 + GND (common) as single-ended input
#define SING_4 0b01001111 //A4 + GND (common) as single-ended input
#define SING_5 0b01011111 //A5 + GND (common) as single-ended input
#define SING_6 0b01101111 //A6 + GND (common) as single-ended input
#define SING_7 0b01111111 //A7 + GND (common) as single-ended input
// Single-ended inputs
#define SING_0 0b00001111 // A0 + GND (common) as single-ended input
#define SING_1 0b00011111 // A1 + GND (common) as single-ended input
#define SING_2 0b00101111 // A2 + GND (common) as single-ended input
#define SING_3 0b00111111 // A3 + GND (common) as single-ended input
#define SING_4 0b01001111 // A4 + GND (common) as single-ended input
#define SING_5 0b01011111 // A5 + GND (common) as single-ended input
#define SING_6 0b01101111 // A6 + GND (common) as single-ended input
#define SING_7 0b01111111 // A7 + GND (common) as single-ended input
//PGA settings //Input voltage range
#define PGA_1 0b00000000 //± 5 V
#define PGA_2 0b00000001 //± 2.5 V
#define PGA_4 0b00000010 //± 1.25 V
#define PGA_8 0b00000011 //± 625 mV
#define PGA_16 0b00000100 //± 312.5 mV
// PGA settings //Input voltage range
#define PGA_1 0b00000000 // ± 5 V
#define PGA_2 0b00000001 // ± 2.5 V
#define PGA_4 0b00000010 // ± 1.25 V
#define PGA_8 0b00000011 // ± 625 mV
#define PGA_16 0b00000100 // ± 312.5 mV
#define PGA_32 0b00000101 //+ 156.25 mV
#define PGA_64 0b00000110 //± 78.125 mV
#define PGA_64 0b00000110 // ± 78.125 mV
//Datarate //DEC
#define DRATE_30000SPS 0b11110000 //240
#define DRATE_15000SPS 0b11100000 //224
#define DRATE_7500SPS 0b11010000 //208
#define DRATE_3750SPS 0b11000000 //192
#define DRATE_2000SPS 0b10110000 //176
#define DRATE_1000SPS 0b10100001 //161
#define DRATE_500SPS 0b10010010 //146
#define DRATE_100SPS 0b10000010 //130
#define DRATE_60SPS 0b01110010 //114
#define DRATE_50SPS 0b01100011 //99
#define DRATE_30SPS 0b01010011 //83
#define DRATE_25SPS 0b01000011 //67
#define DRATE_15SPS 0b00110011 //51
#define DRATE_10SPS 0b00100011 //35
#define DRATE_5SPS 0b00010011 //19
#define DRATE_2SPS 0b00000011 //3
// Datarate //DEC
#define DRATE_30000SPS 0b11110000 // 240
#define DRATE_15000SPS 0b11100000 // 224
#define DRATE_7500SPS 0b11010000 // 208
#define DRATE_3750SPS 0b11000000 // 192
#define DRATE_2000SPS 0b10110000 // 176
#define DRATE_1000SPS 0b10100001 // 161
#define DRATE_500SPS 0b10010010 // 146
#define DRATE_100SPS 0b10000010 // 130
#define DRATE_60SPS 0b01110010 // 114
#define DRATE_50SPS 0b01100011 // 99
#define DRATE_30SPS 0b01010011 // 83
#define DRATE_25SPS 0b01000011 // 67
#define DRATE_15SPS 0b00110011 // 51
#define DRATE_10SPS 0b00100011 // 35
#define DRATE_5SPS 0b00010011 // 19
#define DRATE_2SPS 0b00000011 // 3
//Status register
// Status register
#define BITORDER_MSB 0
#define BITORDER_LSB 1
#define ACAL_DISABLED 0
@@ -69,7 +70,7 @@
#define BUFFER_DISABLED 0
#define BUFFER_ENABLED 1
//Register addresses
// Register addresses
#define STATUS_REG 0x00
#define MUX_REG 0x01
#define ADCON_REG 0x02
@@ -82,7 +83,7 @@
#define FSC1_REG 0x09
#define FSC2_REG 0x0A
//Command definitions
// Command definitions
#define WAKEUP 0b00000000
#define RDATA 0b00000001
#define RDATAC 0b00000011
@@ -99,26 +100,30 @@
#define RESET 0b11111110
//----------------------------------------------------------------
class ADS1256
{
public:
static constexpr int8_t PIN_UNUSED = -1;
static constexpr int8_t PIN_UNUSED = -1;
//Constructor
ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYNC_pin, const int8_t CS_pin, float VREF, SPIClass* spi = &SPI);
// Constructor
ADS1256(const int8_t DRDY_pin, const int8_t RESET_pin, const int8_t SYNC_pin, const int8_t CS_pin, float VREF, SPIClass *spi = &SPI);
~ADS1256()
{
vSemaphoreDelete(m_drdyHigh);
vSemaphoreDelete(m_drdyLow);
}
//Initializing function
// Initializing function
void InitializeADC();
//ADS1256(int drate, int pga, int byteOrder, bool bufen);
// ADS1256(int drate, int pga, int byteOrder, bool bufen);
//Read a register
// Read a register
long readRegister(uint8_t registerAddress);
//Write a register
// Write a register
void writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite);
//Individual methods
// Individual methods
void setDRATE(uint8_t drate);
void setPGA(uint8_t pga);
uint8_t getPGA();
@@ -136,57 +141,75 @@ static constexpr int8_t PIN_UNUSED = -1;
void setSDCS(uint8_t sdcs);
void sendDirectCommand(uint8_t directCommand);
//Get a single conversion
// Get a single conversion
long readSingle();
//Single input continuous reading
// Single input continuous reading
long readSingleContinuous();
//Cycling through the single-ended inputs
long cycleSingle(); //Ax + COM
// Cycling through the single-ended inputs
long cycleSingle(); // Ax + COM
//Cycling through the differential inputs
long cycleDifferential(); //Ax + Ay
// Cycling through the differential inputs
long cycleDifferential(); // Ax + Ay
//Converts the reading into a voltage value
// Converts the reading into a voltage value
float convertToVoltage(int32_t rawData);
//Stop AD
// Stop AD
void stopConversion();
// functions for callback
inline uint8_t getDRDYpin()
{
return m_DRDY_pin;
}
SemaphoreHandle_t getDRDYsemaphoreHigh()
{
return m_drdyHigh;
}
SemaphoreHandle_t getDRDYsemaphoreLow()
{
return m_drdyLow;
}
private:
SPIClass *_spi; // Pointer to an SPIClass object
SPIClass* _spi; //Pointer to an SPIClass object
void waitForLowDRDY(); // Block until DRDY is low
void waitForHighDRDY(); // Block until DRDY is high
void updateMUX(uint8_t muxValue);
inline void CS_LOW();
inline void CS_HIGH();
void waitForLowDRDY(); // Block until DRDY is low
void waitForHighDRDY(); // Block until DRDY is high
void updateMUX(uint8_t muxValue);
inline void CS_LOW();
inline void CS_HIGH();
void updateConversionParameter(); // Refresh the conversion parameter based on the PGA
void updateConversionParameter(); //Refresh the conversion parameter based on the PGA
float m_VREF = 0; // Value of the reference voltage
float m_conversionParameter = 0; // PGA-dependent multiplier
// Pins
int8_t m_DRDY_pin; // Pin assigned for DRDY
int8_t m_RESET_pin; // Pin assigned for RESET
int8_t m_SYNC_pin; // Pin assigned for SYNC
int8_t m_CS_pin; // Pin assigned for CS
float _VREF = 0; //Value of the reference voltage
float conversionParameter = 0; //PGA-dependent multiplier
//Pins
int8_t _DRDY_pin; //Pin assigned for DRDY
int8_t _RESET_pin; //Pin assigned for RESET
int8_t _SYNC_pin; //Pin assigned for SYNC
int8_t _CS_pin; //Pin assigned for CS
// Register values
uint8_t m_DRATE; // Value of the DRATE register
uint8_t m_ADCON; // Value of the ADCON register
uint8_t m_MUX; // Value of the MUX register
uint8_t m_PGA; // Value of the PGA (within ADCON)
uint8_t m_GPIO; // Value of the GPIO register
uint8_t m_STATUS; // Value of the status register
uint8_t m_GPIOvalue; // GPIO value
uint8_t m_ByteOrder; // Byte order
//Register values
byte _DRATE; //Value of the DRATE register
byte _ADCON; //Value of the ADCON register
byte _MUX; //Value of the MUX register
byte _PGA; //Value of the PGA (within ADCON)
byte _GPIO; //Value of the GPIO register
byte _STATUS; //Value of the status register
byte _GPIOvalue; //GPIO value
byte _ByteOrder; //Byte order
uint8_t m_outputBuffer[3]; // 3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
int32_t m_outputValue; // Combined value of the m_outputBuffer[3]
bool m_isAcquisitionRunning; // bool that keeps track of the acquisition (running or not)
uint8_t m_cycle; // Tracks the cycles as the MUX is cycling through the input channels
byte _outputBuffer[3]; //3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
long _outputValue; //Combined value of the _outputBuffer[3]
bool _isAcquisitionRunning; //bool that keeps track of the acquisition (running or not)
uint8_t _cycle; //Tracks the cycles as the MUX is cycling through the input channels
SemaphoreHandle_t m_drdyHigh;
SemaphoreHandle_t m_drdyLow;
};
#endif

View File

@@ -131,35 +131,6 @@ void loop()
#endif
LOG_DEBUG("Init SPI OK");
uint8_t chs[8] = {
SING_0, SING_1, SING_2, SING_3, SING_4, SING_5, SING_6, SING_7};
float res[8];
auto timeout = Serial.getTimeout();
Serial.setTimeout(0);
uint64_t count = 0;
while (Serial.read() != 's') // The conversion is stopped by a character received from the serial port
{
auto start = esp_timer_get_time();
for (int i = 0; i < 8; i++)
{
// dev->m_adc_a->setMUX(chs[i]);
res[i] += 0.1f * (dev->m_adc_a->convertToVoltage(dev->m_adc_a->cycleSingle()) - res[i]);
}
auto stop = esp_timer_get_time();
if (count++ % 25 == 0)
{
clearScreen();
for (int j = 0; j < 8; j++)
{
Serial.printf("ADC_A SING_%d: %5.4f\n", j, res[j]);
}
Serial.printf("ADC Time: %5.3f ms\n", (float)((stop - start) / 1000.0f));
}
}
Serial.setTimeout(timeout);
dev->m_adc_a->stopConversion();
//////// INIT I2C INTERFACES ////////
LOG_DEBUG("Init I2C Interfaces");
bool i2c_ok = true;