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

View File

@@ -17,28 +17,55 @@
#define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value) #define convertSigned24BitToLong(value) ((value) & (1l << 23) ? (value) - 0x1000000 : value)
void drdyCallback(void *arg)
{
auto cls = (ADS1256 *)arg;
if (!arg)
return;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (digitalRead(cls->getDRDYpin())) // impose wait on low
{
xSemaphoreTakeFromISR(cls->getDRDYsemaphoreLow(), &xHigherPriorityTaskWoken);
xSemaphoreGiveFromISR(cls->getDRDYsemaphoreHigh(), &xHigherPriorityTaskWoken);
}
else // impose wait on high
{
xSemaphoreTakeFromISR(cls->getDRDYsemaphoreHigh(), &xHigherPriorityTaskWoken);
xSemaphoreGiveFromISR(cls->getDRDYsemaphoreLow(), &xHigherPriorityTaskWoken);
}
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
// Constructor // Constructor
ADS1256::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(spi), ADS1256::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(spi),
_DRDY_pin(DRDY_pin), _RESET_pin(RESET_pin), _SYNC_pin(SYNC_pin), _CS_pin(CS_pin), _VREF(VREF), _PGA(0) m_DRDY_pin(DRDY_pin), m_RESET_pin(RESET_pin), m_SYNC_pin(SYNC_pin), m_CS_pin(CS_pin), m_VREF(VREF), m_PGA(0)
{ {
pinMode(_DRDY_pin, INPUT); pinMode(m_DRDY_pin, INPUT);
if (RESET_pin != PIN_UNUSED) if (RESET_pin != PIN_UNUSED)
{ {
pinMode(_RESET_pin, OUTPUT); pinMode(m_RESET_pin, OUTPUT);
} }
if (SYNC_pin != PIN_UNUSED) if (SYNC_pin != PIN_UNUSED)
{ {
pinMode(_SYNC_pin, OUTPUT); pinMode(m_SYNC_pin, OUTPUT);
} }
if (CS_pin != PIN_UNUSED) if (CS_pin != PIN_UNUSED)
{ {
pinMode(_CS_pin, OUTPUT); pinMode(m_CS_pin, OUTPUT);
} }
updateConversionParameter(); updateConversionParameter();
m_drdyHigh = xSemaphoreCreateBinary();
m_drdyLow = xSemaphoreCreateBinary();
xSemaphoreGive(m_drdyHigh);
xSemaphoreGive(m_drdyLow);
attachInterruptArg(DRDY_pin, drdyCallback, (void *)this, CHANGE);
} }
// Initialization // Initialization
@@ -48,18 +75,18 @@ void ADS1256::InitializeADC()
CS_LOW(); CS_LOW();
// We do a manual chip reset on the ADS1256 - Datasheet Page 27/ RESET // We do a manual chip reset on the ADS1256 - Datasheet Page 27/ RESET
if(_RESET_pin != PIN_UNUSED) if (m_RESET_pin != PIN_UNUSED)
{ {
digitalWrite(_RESET_pin, LOW); digitalWrite(m_RESET_pin, LOW);
delay(200); delay(200);
digitalWrite(_RESET_pin, HIGH); //RESET is set to high digitalWrite(m_RESET_pin, HIGH); // RESET is set to high
delay(1000); delay(1000);
} }
// Sync pin is also treated if it is defined // Sync pin is also treated if it is defined
if(_SYNC_pin != PIN_UNUSED) if (m_SYNC_pin != PIN_UNUSED)
{ {
digitalWrite(_SYNC_pin, HIGH); //RESET is set to high digitalWrite(m_SYNC_pin, HIGH); // RESET is set to high
} }
#ifndef ADS1256_SPI_ALREADY_STARTED // Guard macro to allow external initialization of the SPI #ifndef ADS1256_SPI_ALREADY_STARTED // Guard macro to allow external initialization of the SPI
@@ -70,40 +97,40 @@ void ADS1256::InitializeADC()
// We both pass values to the variables and then send those values to the corresponding registers // We both pass values to the variables and then send those values to the corresponding registers
delay(200); delay(200);
_STATUS = 0b00110110; //BUFEN and ACAL enabled, Order is MSB, rest is read only m_STATUS = 0b00110110; // BUFEN and ACAL enabled, Order is MSB, rest is read only
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, m_STATUS);
delay(200); delay(200);
_MUX = DIFF_0_1; //MUX AIN0+AIN1 m_MUX = DIFF_0_1; // MUX AIN0+AIN1
writeRegister(MUX_REG, _MUX); writeRegister(MUX_REG, m_MUX);
delay(200); delay(200);
_ADCON = WAKEUP; //ADCON - CLK: OFF, SDCS: OFF, PGA = 0 (+/- 5 V) m_ADCON = WAKEUP; // ADCON - CLK: OFF, SDCS: OFF, PGA = 0 (+/- 5 V)
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, m_ADCON);
delay(200); delay(200);
updateConversionParameter(); updateConversionParameter();
_DRATE = DRATE_100SPS; //100SPS m_DRATE = DRATE_100SPS; // 100SPS
writeRegister(DRATE_REG, _DRATE); writeRegister(DRATE_REG, m_DRATE);
delay(200); delay(200);
sendDirectCommand(SELFCAL); // Offset and self-gain calibration sendDirectCommand(SELFCAL); // Offset and self-gain calibration
delay(200); delay(200);
_isAcquisitionRunning = false; //MCU will be waiting to start a continuous acquisition m_isAcquisitionRunning = false; // MCU will be waiting to start a continuous acquisition
} }
void ADS1256::waitForLowDRDY() void ADS1256::waitForLowDRDY()
{ {
while (digitalRead(_DRDY_pin) == HIGH) {} xSemaphoreTake(m_drdyLow, pdMS_TO_TICKS(10));
xSemaphoreGive(m_drdyLow);
} }
void ADS1256::waitForHighDRDY() void ADS1256::waitForHighDRDY()
{ {
#if F_CPU >= 48000000 //Fast MCUs need this protection to wait until DRDY goes high after a conversion xSemaphoreTake(m_drdyHigh, pdMS_TO_TICKS(10));
while (digitalRead(_DRDY_pin) == LOW) {} xSemaphoreGive(m_drdyHigh);
#endif
} }
void ADS1256::stopConversion() // Sending SDATAC to stop the continuous conversion void ADS1256::stopConversion() // Sending SDATAC to stop the continuous conversion
@@ -113,31 +140,31 @@ void ADS1256::stopConversion() //Sending SDATAC to stop the continuous conversio
CS_HIGH(); // We finished the command sequence, so we switch it back to HIGH CS_HIGH(); // We finished the command sequence, so we switch it back to HIGH
_spi->endTransaction(); _spi->endTransaction();
_isAcquisitionRunning = false; //Reset to false, so the MCU will be able to start a new conversion m_isAcquisitionRunning = false; // Reset to false, so the MCU will be able to start a new conversion
} }
void ADS1256::setDRATE(uint8_t drate) // Setting DRATE (sampling frequency) void ADS1256::setDRATE(uint8_t drate) // Setting DRATE (sampling frequency)
{ {
writeRegister(DRATE_REG, drate); writeRegister(DRATE_REG, drate);
_DRATE = drate; m_DRATE = drate;
delay(200); delay(200);
} }
void ADS1256::setMUX(uint8_t mux) // Setting MUX (input channel) void ADS1256::setMUX(uint8_t mux) // Setting MUX (input channel)
{ {
writeRegister(MUX_REG, mux); writeRegister(MUX_REG, mux);
_MUX = mux; m_MUX = mux;
delay(200); delay(200);
} }
void ADS1256::setPGA(uint8_t pga) // Setting PGA (input voltage range) void ADS1256::setPGA(uint8_t pga) // Setting PGA (input voltage range)
{ {
_PGA = pga; m_PGA = pga;
_ADCON = readRegister(ADCON_REG); //Read the most recent value of the register m_ADCON = readRegister(ADCON_REG); // Read the most recent value of the register
_ADCON = (_ADCON & 0b11111000) | (_PGA & 0b00000111); // Clearing and then setting bits 2-0 based on pga m_ADCON = (m_ADCON & 0b11111000) | (m_PGA & 0b00000111); // Clearing and then setting bits 2-0 based on pga
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, m_ADCON);
delay(200); delay(200);
updateConversionParameter(); // Update the multiplier according top the new PGA value updateConversionParameter(); // Update the multiplier according top the new PGA value
@@ -153,95 +180,101 @@ uint8_t ADS1256::getPGA() //Reading PGA from the ADCON register
void ADS1256::setCLKOUT(uint8_t clkout) // Setting CLKOUT void ADS1256::setCLKOUT(uint8_t clkout) // Setting CLKOUT
{ {
_ADCON = readRegister(ADCON_REG); //Read the most recent value of the register m_ADCON = readRegister(ADCON_REG); // Read the most recent value of the register
// Values: 0, 1, 2, 3 // Values: 0, 1, 2, 3
if (clkout == 0) if (clkout == 0)
{ {
// 00 // 00
bitWrite(_ADCON, 6, 0); bitWrite(m_ADCON, 6, 0);
bitWrite(_ADCON, 5, 0); bitWrite(m_ADCON, 5, 0);
} }
else if (clkout == 1) else if (clkout == 1)
{ {
// 01 (default) // 01 (default)
bitWrite(_ADCON, 6, 0); bitWrite(m_ADCON, 6, 0);
bitWrite(_ADCON, 5, 1); bitWrite(m_ADCON, 5, 1);
} }
else if (clkout == 2) else if (clkout == 2)
{ {
// 10 // 10
bitWrite(_ADCON, 6, 1); bitWrite(m_ADCON, 6, 1);
bitWrite(_ADCON, 5, 0); bitWrite(m_ADCON, 5, 0);
} }
else if (clkout == 3) else if (clkout == 3)
{ {
// 11 // 11
bitWrite(_ADCON, 6, 1); bitWrite(m_ADCON, 6, 1);
bitWrite(_ADCON, 5, 1); bitWrite(m_ADCON, 5, 1);
}
else
{
} }
else{}
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, m_ADCON);
delay(100); delay(100);
} }
void ADS1256::setSDCS(uint8_t sdcs) // Setting SDCS void ADS1256::setSDCS(uint8_t sdcs) // Setting SDCS
{ {
_ADCON = readRegister(ADCON_REG); //Read the most recent value of the register m_ADCON = readRegister(ADCON_REG); // Read the most recent value of the register
// Values: 0, 1, 2, 3 // Values: 0, 1, 2, 3
if (sdcs == 0) if (sdcs == 0)
{ {
// 00 (default) // 00 (default)
bitWrite(_ADCON, 4, 0); bitWrite(m_ADCON, 4, 0);
bitWrite(_ADCON, 3, 0); bitWrite(m_ADCON, 3, 0);
} }
else if (sdcs == 1) else if (sdcs == 1)
{ {
// 01 // 01
bitWrite(_ADCON, 4, 0); bitWrite(m_ADCON, 4, 0);
bitWrite(_ADCON, 3, 1); bitWrite(m_ADCON, 3, 1);
} }
else if (sdcs == 2) else if (sdcs == 2)
{ {
// 10 // 10
bitWrite(_ADCON, 4, 1); bitWrite(m_ADCON, 4, 1);
bitWrite(_ADCON, 3, 0); bitWrite(m_ADCON, 3, 0);
} }
else if (sdcs == 3) else if (sdcs == 3)
{ {
// 11 // 11
bitWrite(_ADCON, 4, 1); bitWrite(m_ADCON, 4, 1);
bitWrite(_ADCON, 3, 1); bitWrite(m_ADCON, 3, 1);
}
else
{
} }
else{}
writeRegister(ADCON_REG, _ADCON); writeRegister(ADCON_REG, m_ADCON);
delay(100); delay(100);
} }
void ADS1256::setByteOrder(uint8_t byteOrder) // Setting byte order (MSB/LSB) void ADS1256::setByteOrder(uint8_t byteOrder) // Setting byte order (MSB/LSB)
{ {
_STATUS = readRegister(STATUS_REG); //Read the most recent value of the register m_STATUS = readRegister(STATUS_REG); // Read the most recent value of the register
if (byteOrder == 0) if (byteOrder == 0)
{ {
// Byte order is MSB (default) // Byte order is MSB (default)
bitWrite(_STATUS, 3, 0); bitWrite(m_STATUS, 3, 0);
// Set value of _STATUS at the third bit to 0 // Set value of _STATUS at the third bit to 0
} }
else if (byteOrder == 1) else if (byteOrder == 1)
{ {
// Byte order is LSB // Byte order is LSB
bitWrite(_STATUS, 3, 1); bitWrite(m_STATUS, 3, 1);
// Set value of _STATUS at the third bit to 1 // Set value of _STATUS at the third bit to 1
} }
else{} else
{
}
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, m_STATUS);
delay(100); delay(100);
} }
@@ -254,23 +287,25 @@ uint8_t ADS1256::getByteOrder() //Getting byte order (MSB/LSB)
void ADS1256::setAutoCal(uint8_t acal) // Setting ACAL (Automatic SYSCAL) void ADS1256::setAutoCal(uint8_t acal) // Setting ACAL (Automatic SYSCAL)
{ {
_STATUS = readRegister(STATUS_REG); //Read the most recent value of the register m_STATUS = readRegister(STATUS_REG); // Read the most recent value of the register
if (acal == 0) if (acal == 0)
{ {
// Auto-calibration is disabled (default) // Auto-calibration is disabled (default)
bitWrite(_STATUS, 2, 0); bitWrite(m_STATUS, 2, 0);
//_STATUS |= B00000000; //_STATUS |= B00000000;
} }
else if (acal == 1) else if (acal == 1)
{ {
// Auto-calibration is enabled // Auto-calibration is enabled
bitWrite(_STATUS, 2, 1); bitWrite(m_STATUS, 2, 1);
//_STATUS |= B00000100; //_STATUS |= B00000100;
} }
else{} else
{
}
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, m_STATUS);
delay(100); delay(100);
} }
@@ -283,23 +318,25 @@ uint8_t ADS1256::getAutoCal() //Getting ACAL (Automatic SYSCAL)
void ADS1256::setBuffer(uint8_t bufen) // Setting input buffer (Input impedance) void ADS1256::setBuffer(uint8_t bufen) // Setting input buffer (Input impedance)
{ {
_STATUS = readRegister(STATUS_REG); //Read the most recent value of the register m_STATUS = readRegister(STATUS_REG); // Read the most recent value of the register
if (bufen == 0) if (bufen == 0)
{ {
// Analog input buffer is disabled (default) // Analog input buffer is disabled (default)
//_STATUS |= B00000000; //_STATUS |= B00000000;
bitWrite(_STATUS, 1, 0); bitWrite(m_STATUS, 1, 0);
} }
else if (bufen == 1) else if (bufen == 1)
{ {
// Analog input buffer is enabled (recommended) // Analog input buffer is enabled (recommended)
//_STATUS |= B00000010; //_STATUS |= B00000010;
bitWrite(_STATUS, 1, 1); bitWrite(m_STATUS, 1, 1);
}
else
{
} }
else{}
writeRegister(STATUS_REG, _STATUS); writeRegister(STATUS_REG, m_STATUS);
delay(100); delay(100);
} }
@@ -312,7 +349,7 @@ uint8_t ADS1256::getBuffer() //Getting input buffer (Input impedance)
void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) // Setting GPIO void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) // Setting GPIO
{ {
_GPIO = readRegister(IO_REG); //Read the most recent value of the register m_GPIO = readRegister(IO_REG); // Read the most recent value of the register
// Default: 11100000 - DEC: 224 - Ref: p32 I/O section // Default: 11100000 - DEC: 224 - Ref: p32 I/O section
// Sets D3-D0 as input or output // Sets D3-D0 as input or output
@@ -327,7 +364,7 @@ void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //
{ {
GPIO_bit7 = 0; // D3 is output GPIO_bit7 = 0; // D3 is output
} }
bitWrite(_GPIO, 7, GPIO_bit7); bitWrite(m_GPIO, 7, GPIO_bit7);
//----------------------------------------------------- //-----------------------------------------------------
// Bit6: DIR2 // Bit6: DIR2
if (dir2 == 1) if (dir2 == 1)
@@ -338,7 +375,7 @@ void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //
{ {
GPIO_bit6 = 0; // D2 is output GPIO_bit6 = 0; // D2 is output
} }
bitWrite(_GPIO, 6, GPIO_bit6); bitWrite(m_GPIO, 6, GPIO_bit6);
//----------------------------------------------------- //-----------------------------------------------------
// Bit5: DIR1 // Bit5: DIR1
if (dir1 == 1) if (dir1 == 1)
@@ -349,7 +386,7 @@ void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //
{ {
GPIO_bit5 = 0; // D1 is output GPIO_bit5 = 0; // D1 is output
} }
bitWrite(_GPIO, 5, GPIO_bit5); bitWrite(m_GPIO, 5, GPIO_bit5);
//----------------------------------------------------- //-----------------------------------------------------
// Bit4: DIR0 // Bit4: DIR0
if (dir0 == 1) if (dir0 == 1)
@@ -360,16 +397,16 @@ void ADS1256::setGPIO(uint8_t dir0, uint8_t dir1, uint8_t dir2, uint8_t dir3) //
{ {
GPIO_bit4 = 0; // D0 is output (default) GPIO_bit4 = 0; // D0 is output (default)
} }
bitWrite(_GPIO, 4, GPIO_bit4); bitWrite(m_GPIO, 4, GPIO_bit4);
//----------------------------------------------------- //-----------------------------------------------------
writeRegister(IO_REG, _GPIO); writeRegister(IO_REG, m_GPIO);
delay(100); delay(100);
} }
void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value) // Writing GPIO void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value, uint8_t dir3value) // Writing GPIO
{ {
_GPIO = readRegister(IO_REG); m_GPIO = readRegister(IO_REG);
// Sets D3-D0 output values // Sets D3-D0 output values
// It is important that first one must use setGPIO, then writeGPIO // It is important that first one must use setGPIO, then writeGPIO
@@ -385,7 +422,7 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
{ {
GPIO_bit3 = 0; GPIO_bit3 = 0;
} }
bitWrite(_GPIO, 3, GPIO_bit3); bitWrite(m_GPIO, 3, GPIO_bit3);
//----------------------------------------------------- //-----------------------------------------------------
// Bit2: DIR2 // Bit2: DIR2
if (dir2value == 1) if (dir2value == 1)
@@ -396,7 +433,7 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
{ {
GPIO_bit2 = 0; GPIO_bit2 = 0;
} }
bitWrite(_GPIO, 2, GPIO_bit2); bitWrite(m_GPIO, 2, GPIO_bit2);
//----------------------------------------------------- //-----------------------------------------------------
// Bit1: DIR1 // Bit1: DIR1
if (dir1value == 1) if (dir1value == 1)
@@ -407,7 +444,7 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
{ {
GPIO_bit1 = 0; GPIO_bit1 = 0;
} }
bitWrite(_GPIO, 1, GPIO_bit1); bitWrite(m_GPIO, 1, GPIO_bit1);
//----------------------------------------------------- //-----------------------------------------------------
// Bit0: DIR0 // Bit0: DIR0
if (dir0value == 1) if (dir0value == 1)
@@ -418,10 +455,10 @@ void ADS1256::writeGPIO(uint8_t dir0value, uint8_t dir1value, uint8_t dir2value,
{ {
GPIO_bit0 = 0; GPIO_bit0 = 0;
} }
bitWrite(_GPIO, 0, GPIO_bit0); bitWrite(m_GPIO, 0, GPIO_bit0);
//----------------------------------------------------- //-----------------------------------------------------
writeRegister(IO_REG, _GPIO); writeRegister(IO_REG, m_GPIO);
delay(100); delay(100);
} }
@@ -429,13 +466,13 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) //Reading GPIO
{ {
uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0, GPIO_return; uint8_t GPIO_bit3, GPIO_bit2, GPIO_bit1, GPIO_bit0, GPIO_return;
_GPIO = readRegister(IO_REG); //Read the GPIO register m_GPIO = readRegister(IO_REG); // Read the GPIO register
// Save each bit values in a variable // Save each bit values in a variable
GPIO_bit3 = bitRead(_GPIO, 3); GPIO_bit3 = bitRead(m_GPIO, 3);
GPIO_bit2 = bitRead(_GPIO, 2); GPIO_bit2 = bitRead(m_GPIO, 2);
GPIO_bit1 = bitRead(_GPIO, 1); GPIO_bit1 = bitRead(m_GPIO, 1);
GPIO_bit0 = bitRead(_GPIO, 0); GPIO_bit0 = bitRead(m_GPIO, 0);
delay(100); delay(100);
@@ -459,7 +496,6 @@ uint8_t ADS1256::readGPIO(uint8_t gpioPin) //Reading GPIO
} }
return GPIO_return; return GPIO_return;
} }
void ADS1256::sendDirectCommand(uint8_t directCommand) void ADS1256::sendDirectCommand(uint8_t directCommand)
@@ -476,10 +512,9 @@ void ADS1256::sendDirectCommand(uint8_t directCommand)
_spi->endTransaction(); _spi->endTransaction();
} }
float ADS1256::convertToVoltage(int32_t rawData) // Converting the 24-bit data into a voltage value float ADS1256::convertToVoltage(int32_t rawData) // Converting the 24-bit data into a voltage value
{ {
return(conversionParameter * rawData); return (m_conversionParameter * rawData);
} }
void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite) void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrite)
@@ -502,7 +537,6 @@ void ADS1256::writeRegister(uint8_t registerAddress, uint8_t registerValueToWrit
CS_HIGH(); CS_HIGH();
_spi->endTransaction(); _spi->endTransaction();
delay(100); delay(100);
} }
long ADS1256::readRegister(uint8_t registerAddress) // Reading a register long ADS1256::readRegister(uint8_t registerAddress) // Reading a register
@@ -528,7 +562,6 @@ long ADS1256::readRegister(uint8_t registerAddress) //Reading a register
return regValue; return regValue;
} }
long ADS1256::readSingle() // Reading a single value ONCE using the RDATA command long ADS1256::readSingle() // Reading a single value ONCE using the RDATA command
{ {
_spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1));
@@ -537,25 +570,25 @@ long ADS1256::readSingle() //Reading a single value ONCE using the RDATA command
_spi->transfer(RDATA); // Issue RDATA (0000 0001) command _spi->transfer(RDATA); // Issue RDATA (0000 0001) command
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB m_outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte m_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB m_outputBuffer[2] = _spi->transfer(0); // LSB
// Shifting and combining the above three items into a single, 24-bit number // Shifting and combining the above three items into a single, 24-bit number
_outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]); m_outputValue = ((long)m_outputBuffer[0] << 16) | ((long)m_outputBuffer[1] << 8) | (m_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); m_outputValue = convertSigned24BitToLong(m_outputValue);
CS_HIGH(); // We finished the command sequence, so we set CS to HIGH CS_HIGH(); // We finished the command sequence, so we set CS to HIGH
_spi->endTransaction(); _spi->endTransaction();
return(_outputValue); return (m_outputValue);
} }
long ADS1256::readSingleContinuous() // Reads the recently selected input channel using RDATAC long ADS1256::readSingleContinuous() // Reads the recently selected input channel using RDATAC
{ {
if(_isAcquisitionRunning == false) if (m_isAcquisitionRunning == false)
{ {
_isAcquisitionRunning = true; m_isAcquisitionRunning = true;
_spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1));
CS_LOW(); // REF: P34: "CS must stay low during the entire command sequence" CS_LOW(); // REF: P34: "CS must stay low during the entire command sequence"
waitForLowDRDY(); waitForLowDRDY();
@@ -567,24 +600,24 @@ long ADS1256::readSingleContinuous() //Reads the recently selected input channel
waitForLowDRDY(); waitForLowDRDY();
} }
_outputBuffer[0] = _spi->transfer(0); // MSB m_outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte m_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB m_outputBuffer[2] = _spi->transfer(0); // LSB
_outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]); m_outputValue = ((long)m_outputBuffer[0] << 16) | ((long)m_outputBuffer[1] << 8) | (m_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); m_outputValue = convertSigned24BitToLong(m_outputValue);
waitForHighDRDY(); waitForHighDRDY();
return _outputValue; return m_outputValue;
} }
long ADS1256::cycleSingle() long ADS1256::cycleSingle()
{ {
if(_isAcquisitionRunning == false) if (m_isAcquisitionRunning == false)
{ {
_isAcquisitionRunning = true; m_isAcquisitionRunning = true;
_cycle = 0; m_cycle = 0;
_spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1));
CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24] CS_LOW(); // CS must stay LOW during the entire sequence [Ref: P34, T24]
_spi->transfer(WREG | MUX_REG); // 0x50 = WREG //1 = MUX _spi->transfer(WREG | MUX_REG); // 0x50 = WREG //1 = MUX
@@ -593,14 +626,15 @@ long ADS1256::cycleSingle()
delayMicroseconds(250); delayMicroseconds(250);
} }
else else
{}
if(_cycle < 8)
{ {
_outputValue = 0; }
if (m_cycle < 8)
{
m_outputValue = 0;
waitForLowDRDY(); waitForLowDRDY();
// Step 1. - Updating MUX // Step 1. - Updating MUX
switch (_cycle) switch (m_cycle)
{ {
// Channels are written manually // Channels are written manually
case 0: // Channel 2 case 0: // Channel 2
@@ -645,29 +679,29 @@ long ADS1256::cycleSingle()
_spi->transfer(RDATA); _spi->transfer(RDATA);
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB m_outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte m_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB m_outputBuffer[2] = _spi->transfer(0); // LSB
_outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]); m_outputValue = ((long)m_outputBuffer[0] << 16) | ((long)m_outputBuffer[1] << 8) | (m_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); m_outputValue = convertSigned24BitToLong(m_outputValue);
_cycle++; //Increase cycle - This will move to the next MUX input channel m_cycle++; // Increase cycle - This will move to the next MUX input channel
if(_cycle == 8) if (m_cycle == 8)
{ {
_cycle = 0; //Reset to 0 - Restart conversion from the 1st input channel m_cycle = 0; // Reset to 0 - Restart conversion from the 1st input channel
} }
} }
return _outputValue; return m_outputValue;
} }
long ADS1256::cycleDifferential() long ADS1256::cycleDifferential()
{ {
if(_isAcquisitionRunning == false) if (m_isAcquisitionRunning == false)
{ {
_cycle = 0; m_cycle = 0;
_isAcquisitionRunning = true; m_isAcquisitionRunning = true;
_spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1)); _spi->beginTransaction(SPISettings(SPI_FREQ, MSBFIRST, SPI_MODE1));
// Set the AIN0+AIN1 as inputs manually // Set the AIN0+AIN1 as inputs manually
@@ -678,16 +712,17 @@ long ADS1256::cycleDifferential()
delayMicroseconds(250); delayMicroseconds(250);
} }
else else
{}
if(_cycle < 4)
{ {
_outputValue = 0; }
if (m_cycle < 4)
{
m_outputValue = 0;
// DRDY has to go low // DRDY has to go low
waitForLowDRDY(); waitForLowDRDY();
// Step 1. - Updating MUX // Step 1. - Updating MUX
switch (_cycle) switch (m_cycle)
{ {
case 0: // Channel 2 case 0: // Channel 2
updateMUX(DIFF_2_3); // AIN2+AIN3 updateMUX(DIFF_2_3); // AIN2+AIN3
@@ -714,27 +749,27 @@ long ADS1256::cycleDifferential()
_spi->transfer(RDATA); // Issue RDATA (0000 0001) command _spi->transfer(RDATA); // Issue RDATA (0000 0001) command
delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30. delayMicroseconds(7); // Wait t6 time (~6.51 us) REF: P34, FIG:30.
_outputBuffer[0] = _spi->transfer(0); // MSB m_outputBuffer[0] = _spi->transfer(0); // MSB
_outputBuffer[1] = _spi->transfer(0); // Mid-byte m_outputBuffer[1] = _spi->transfer(0); // Mid-byte
_outputBuffer[2] = _spi->transfer(0); // LSB m_outputBuffer[2] = _spi->transfer(0); // LSB
_outputValue = ((long)_outputBuffer[0]<<16) | ((long)_outputBuffer[1]<<8) | (_outputBuffer[2]); m_outputValue = ((long)m_outputBuffer[0] << 16) | ((long)m_outputBuffer[1] << 8) | (m_outputBuffer[2]);
_outputValue = convertSigned24BitToLong(_outputValue); m_outputValue = convertSigned24BitToLong(m_outputValue);
_cycle++; m_cycle++;
if(_cycle == 4) if (m_cycle == 4)
{ {
_cycle = 0; m_cycle = 0;
// After the 4th cycle, we reset to zero so the next iteration reads the 1st MUX again // After the 4th cycle, we reset to zero so the next iteration reads the 1st MUX again
} }
} }
return _outputValue; return m_outputValue;
} }
void ADS1256::updateConversionParameter() void ADS1256::updateConversionParameter()
{ {
conversionParameter = ((2.0 * _VREF) / 8388608.0) / (pow(2, _PGA)); //Calculate the "bit to Volts" multiplier m_conversionParameter = ((2.0 * m_VREF) / 8388608.0) / (pow(2, m_PGA)); // Calculate the "bit to Volts" multiplier
// 8388608 = 2^{23} - 1, REF: p23, Table 16. // 8388608 = 2^{23} - 1, REF: p23, Table 16.
} }
@@ -747,16 +782,16 @@ void ADS1256::updateMUX(uint8_t muxValue)
inline void ADS1256::CS_LOW() inline void ADS1256::CS_LOW()
{ {
if (_CS_pin != PIN_UNUSED) //Sets CS LOW if it is not an unused pin if (m_CS_pin != PIN_UNUSED) // Sets CS LOW if it is not an unused pin
{ {
digitalWrite(_CS_pin, LOW); digitalWrite(m_CS_pin, LOW);
} }
} }
inline void ADS1256::CS_HIGH() inline void ADS1256::CS_HIGH()
{ {
if (_CS_pin != PIN_UNUSED) //Sets CS HIGH if it is not an unused pin if (m_CS_pin != PIN_UNUSED) // Sets CS HIGH if it is not an unused pin
{ {
digitalWrite(_CS_pin, HIGH); digitalWrite(m_CS_pin, HIGH);
} }
} }

View File

@@ -14,6 +14,7 @@
#define _ADS1256_h #define _ADS1256_h
#include <SPI.h> #include <SPI.h>
#include <Arduino.h>
// SPI Frequency // SPI Frequency
#define SPI_FREQ 1920000 #define SPI_FREQ 1920000
@@ -99,7 +100,6 @@
#define RESET 0b11111110 #define RESET 0b11111110
//---------------------------------------------------------------- //----------------------------------------------------------------
class ADS1256 class ADS1256
{ {
public: public:
@@ -107,6 +107,11 @@ static constexpr int8_t PIN_UNUSED = -1;
// Constructor // 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(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(); void InitializeADC();
@@ -154,8 +159,23 @@ static constexpr int8_t PIN_UNUSED = -1;
// Stop AD // Stop AD
void stopConversion(); void stopConversion();
private: // 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 waitForLowDRDY(); // Block until DRDY is low
@@ -166,27 +186,30 @@ 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 _VREF = 0; //Value of the reference voltage float m_VREF = 0; // Value of the reference voltage
float conversionParameter = 0; //PGA-dependent multiplier float m_conversionParameter = 0; // PGA-dependent multiplier
// Pins // Pins
int8_t _DRDY_pin; //Pin assigned for DRDY int8_t m_DRDY_pin; // Pin assigned for DRDY
int8_t _RESET_pin; //Pin assigned for RESET int8_t m_RESET_pin; // Pin assigned for RESET
int8_t _SYNC_pin; //Pin assigned for SYNC int8_t m_SYNC_pin; // Pin assigned for SYNC
int8_t _CS_pin; //Pin assigned for CS int8_t m_CS_pin; // Pin assigned for CS
// Register values // Register values
byte _DRATE; //Value of the DRATE register uint8_t m_DRATE; // Value of the DRATE register
byte _ADCON; //Value of the ADCON register uint8_t m_ADCON; // Value of the ADCON register
byte _MUX; //Value of the MUX register uint8_t m_MUX; // Value of the MUX register
byte _PGA; //Value of the PGA (within ADCON) uint8_t m_PGA; // Value of the PGA (within ADCON)
byte _GPIO; //Value of the GPIO register uint8_t m_GPIO; // Value of the GPIO register
byte _STATUS; //Value of the status register uint8_t m_STATUS; // Value of the status register
byte _GPIOvalue; //GPIO value uint8_t m_GPIOvalue; // GPIO value
byte _ByteOrder; //Byte order uint8_t m_ByteOrder; // Byte order
byte _outputBuffer[3]; //3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous uint8_t m_outputBuffer[3]; // 3-byte (24-bit) buffer for the fast acquisition - Single-channel, continuous
long _outputValue; //Combined value of the _outputBuffer[3] int32_t m_outputValue; // Combined value of the m_outputBuffer[3]
bool _isAcquisitionRunning; //bool that keeps track of the acquisition (running or not) bool m_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 uint8_t m_cycle; // Tracks the cycles as the MUX is cycling through the input channels
SemaphoreHandle_t m_drdyHigh;
SemaphoreHandle_t m_drdyLow;
}; };
#endif #endif

View File

@@ -131,35 +131,6 @@ void loop()
#endif #endif
LOG_DEBUG("Init SPI OK"); 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 //////// //////// INIT I2C INTERFACES ////////
LOG_DEBUG("Init I2C Interfaces"); LOG_DEBUG("Init I2C Interfaces");
bool i2c_ok = true; bool i2c_ok = true;