some more adc code
This commit is contained in:
@ -15,7 +15,7 @@ void setup() {
|
||||
|
||||
pinMode(13, OUTPUT);
|
||||
|
||||
ads1210.begin(9);
|
||||
ads1210.begin(9, 8);
|
||||
|
||||
}
|
||||
|
||||
@ -24,8 +24,7 @@ void loop() {
|
||||
delay(1000); // wait for a second
|
||||
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
|
||||
delay(1000); // wait for a second
|
||||
|
||||
Serial << "Tick" << endl;
|
||||
ads1210.begin(9);
|
||||
|
||||
uint32_t adcValue = ads1210.get();
|
||||
Serial << "AdcValue: " << adcValue << endl;
|
||||
}
|
||||
|
144
ads1210.cpp
144
ads1210.cpp
@ -9,6 +9,9 @@
|
||||
#include <SPI.h>
|
||||
#include "Streaming.h"
|
||||
#include "ads1210.h"
|
||||
#include "fatal.h"
|
||||
|
||||
|
||||
|
||||
ADS1210::ADS1210() {
|
||||
}
|
||||
@ -20,68 +23,119 @@ void ADS1210::disableCS() const {
|
||||
digitalWrite(m_csPin, HIGH);
|
||||
}
|
||||
|
||||
void ADS1210::writeCMR () const {
|
||||
uint8_t instr = INSR_MB1 | INSR_MB0 | ADDR_CMR3;
|
||||
enableCS();
|
||||
SPI.transfer(instr);
|
||||
SPI.transfer(m_cmrWriteShadow[3]);
|
||||
SPI.transfer(m_cmrWriteShadow[2]);
|
||||
SPI.transfer(m_cmrWriteShadow[1]);
|
||||
SPI.transfer(m_cmrWriteShadow[0]);
|
||||
disableCS();
|
||||
void ADS1210::waitForDRdy() const {
|
||||
uint32_t timeOut = 100000;
|
||||
while ((0 != digitalRead(m_drdyPin)) && timeOut--) {
|
||||
}
|
||||
if (0 == timeOut) {
|
||||
fatal(1);
|
||||
}
|
||||
}
|
||||
|
||||
void ADS1210::readCMR () {
|
||||
uint8_t instr = INSR_MB1 | INSR_MB0 | ADDR_CMR3 | INSR_RW;
|
||||
enableCS();
|
||||
SPI.transfer(instr);
|
||||
m_cmrReadShadow[3] = SPI.transfer(0);
|
||||
m_cmrReadShadow[2] = SPI.transfer(0);
|
||||
m_cmrReadShadow[1] = SPI.transfer(0);
|
||||
m_cmrReadShadow[0] = SPI.transfer(0);
|
||||
disableCS();
|
||||
void waitForDRdyWOTimeout() const {
|
||||
while (0 != digitalRead(m_drdyPin)) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ADS1210::writeRegister(const uint8_t regAddr, const uint8_t value) const {
|
||||
waitForDRdy();
|
||||
enableCS();
|
||||
SPI.transfer(regAddr);
|
||||
SPI.transfer(value);
|
||||
disableCS();
|
||||
}
|
||||
|
||||
|
||||
uint8_t ADS1210::readRegister(const uint8_t regAddr) const {
|
||||
uint8_t res;
|
||||
|
||||
waitForDRdy();
|
||||
enableCS();
|
||||
SPI.transfer(regAddr | INSR_RW);
|
||||
res = SPI.transfer(0);
|
||||
disableCS();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void ADS1210::begin(uint8_t csPin) {
|
||||
uint32_t ADS1210::readDataOutRegister() const {
|
||||
union {
|
||||
uint8_t in[4];
|
||||
uint32_t out;
|
||||
} res;
|
||||
|
||||
waitForDRdy();
|
||||
enableCS();
|
||||
SPI.transfer(ADDR_DOR2 | INSR_MB1 | INSR_RW);
|
||||
res.in[2] = SPI.transfer();
|
||||
res.in[1] = SPI.transfer();
|
||||
res.in[0] = SPI.transfer();
|
||||
res.in[3] = 0;
|
||||
disableCS();
|
||||
|
||||
Serial << "DOR4x8: " << _HEX(res.in[3]) << " " << _HEX(res.in[2]) << " " << _HEX(res.in[1]) << " " << _HEX(res.in[0]) << endl;
|
||||
Serial << "DOR1x32: " << _HEX(res.out) << endl;
|
||||
|
||||
return res.out;
|
||||
}
|
||||
|
||||
|
||||
void ADS1210::disableRefO() const {
|
||||
uint8_t cmr = readRegister(ADDR_CMR3);
|
||||
cmr &= ~CMR_REFO;
|
||||
writeRegister(ADDR_CMR3, cmr);
|
||||
}
|
||||
|
||||
void ADS1210::enableRefO() const {
|
||||
uint8_t cmr = readRegister(ADDR_CMR3);
|
||||
cmr |= CMR_REFO;
|
||||
writeRegister(ADDR_CMR3, cmr);
|
||||
}
|
||||
|
||||
void ADS1210::setMode(uint8_t mode) const {
|
||||
uint8_t cmr = readRegister(ADDR_CMR2);
|
||||
cmr &= ~CMR_MD_Mask;
|
||||
cmr |= mode;
|
||||
writeRegister(ADDR_CMR2, cmr);
|
||||
}
|
||||
|
||||
void ADS1210::setGain(uint8_t gain) const {
|
||||
uint8_t cmr = readRegister(ADDR_CMR2);
|
||||
cmr &= ~CMR_Gain_Mask;
|
||||
cmr |= gain;
|
||||
writeRegister(ADDR_CMR2, cmr);
|
||||
}
|
||||
|
||||
void ADS1210::begin(uint8_t csPin, uint8_t drdyPin) {
|
||||
static bool onlyOnce = false;
|
||||
|
||||
m_csPin = csPin;
|
||||
m_drdyPin = drdyPin;
|
||||
|
||||
// initialization of SPI
|
||||
Serial << "Start SPI initialization ... ";
|
||||
pinMode(m_csPin, OUTPUT);
|
||||
digitalWrite(m_csPin, HIGH);
|
||||
SPI.begin();
|
||||
SPI.setBitOrder(MSBFIRST);
|
||||
SPI.setClockDivider(SPI_CLOCK_DIV16);
|
||||
SPI.setDataMode(SPI_MODE1);
|
||||
Serial << "done." << endl;
|
||||
|
||||
if (! onlyOnce) {
|
||||
// initialization of the ADS1210
|
||||
m_cmrWriteShadow[3] |= CMR_SDL;
|
||||
m_cmrWriteShadow[2] = 0;
|
||||
m_cmrWriteShadow[1] = 0;
|
||||
m_cmrWriteShadow[0] = 0;
|
||||
writeCMR();
|
||||
// onlyOnce = true;
|
||||
}
|
||||
|
||||
// static bool toggleRefO = false;
|
||||
// if (toggleRefO) {
|
||||
// toggleRefO = false;
|
||||
// m_cmrWriteShadow[3] |= CMR_REFO;
|
||||
// } else {
|
||||
// toggleRefO = true;
|
||||
// m_cmrWriteShadow[3] &= ~CMR_REFO;
|
||||
// }
|
||||
// writeCMR();
|
||||
|
||||
readCMR();
|
||||
|
||||
Serial << "CMR3: " << _HEX(m_cmrReadShadow[3]) <<
|
||||
", CMR2: " << _HEX(m_cmrReadShadow[2]) <<
|
||||
", CMR1: " << _HEX(m_cmrReadShadow[1]) <<
|
||||
", CMR0: " << _HEX(m_cmrReadShadow[0]) << endl;
|
||||
// initialization of ADS1210
|
||||
Serial << "Start ADS1210 initialization ... ";
|
||||
pinMode(m_drdyPin, INPUT);
|
||||
writeRegister(ADDR_CMR3, CMR_SDL | CMR_UB | CMR_REFO);
|
||||
Serial << "done." << endl;
|
||||
|
||||
Serial << "SelfCalibration ... ";
|
||||
setMode(CMR_MD_SelfCalibration);
|
||||
waitForDRdyWOTimeout();
|
||||
Serial << "done." << endl;
|
||||
}
|
||||
|
||||
|
||||
uint32_t ADS1210::get() const {
|
||||
return readDataOutRegister();
|
||||
}
|
||||
|
19
ads1210.h
19
ads1210.h
@ -11,8 +11,9 @@
|
||||
class ADS1210 {
|
||||
public:
|
||||
ADS1210();
|
||||
void begin(uint8_t csPin);
|
||||
uint32_t get();
|
||||
void begin(uint8_t csPin, uint8_t drdyPin);
|
||||
uint32_t get() const;
|
||||
|
||||
private:
|
||||
// register addresses
|
||||
const uint8_t ADDR_DOR2 = 0x00;
|
||||
@ -67,14 +68,20 @@ private:
|
||||
const uint8_t INSR_ADDR = 0x0f;
|
||||
|
||||
uint8_t m_csPin;
|
||||
uint8_t m_drdyPin;
|
||||
|
||||
uint8_t m_cmrReadShadow[4];
|
||||
uint8_t m_cmrWriteShadow[4];
|
||||
|
||||
void enableCS() const;
|
||||
void disableCS() const;
|
||||
void writeCMR() const;
|
||||
void readCMR();
|
||||
void writeRegister(const uint8_t regAddr, const uint8_t value) const;
|
||||
uint8_t readRegister(const uint8_t regAddr) const;
|
||||
uint32_t readDataOutRegister() const;
|
||||
void waitForDRdy() const;
|
||||
void waitForDRdyWOTimeout() const;
|
||||
void disableRefO() const;
|
||||
void enableRefO() const;
|
||||
void setMode(uint8_t mode) const;
|
||||
void setGain(uint8_t gain) const;
|
||||
};
|
||||
|
||||
|
||||
|
9
fatal.cpp
Normal file
9
fatal.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "Streaming.h"
|
||||
#include "fatal.h"
|
||||
|
||||
|
||||
void fatal(uint8_t code) {
|
||||
Serial << "Fatal: " << code << endl;
|
||||
delay(1000);
|
||||
while (true) ;
|
||||
}
|
Reference in New Issue
Block a user