162 lines
3.5 KiB
C++
162 lines
3.5 KiB
C++
/*
|
|
* ads1210.cpp
|
|
*
|
|
* Created on: Nov 2, 2014
|
|
* Author: wn
|
|
*/
|
|
|
|
// #include <Arduino.h>
|
|
#include <SPI.h>
|
|
// #include "Streaming.h"
|
|
#include "ads1210.h"
|
|
#include "fatal.h"
|
|
#include "Config.h"
|
|
|
|
|
|
|
|
const uint8_t CONFIG_CAL_OFFSET = 0;
|
|
const uint8_t CONFIG_CAL_FACTOR = 4;
|
|
|
|
|
|
void ADS1210::enableCS() const {
|
|
digitalWrite(m_csPin, LOW);
|
|
}
|
|
void ADS1210::disableCS() const {
|
|
digitalWrite(m_csPin, HIGH);
|
|
}
|
|
|
|
void ADS1210::waitForDRdy() const {
|
|
uint32_t timeOut = 1000000;
|
|
while ((0 != digitalRead(m_drdyPin)) && timeOut) {
|
|
timeOut--;
|
|
}
|
|
if (0 == timeOut) {
|
|
fatal(1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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::setCalFactor(float calFactor) {
|
|
m_calFactor = calFactor;
|
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, m_calFactor);
|
|
}
|
|
|
|
void ADS1210::setCalOffset(float calOffset) {
|
|
m_calOffset = calOffset;
|
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, m_calOffset);
|
|
}
|
|
|
|
void ADS1210::exec() {
|
|
if (0 == digitalRead(m_drdyPin)) {
|
|
union {
|
|
uint8_t in[4];
|
|
uint32_t out;
|
|
} res;
|
|
|
|
writeRegister(ADDR_CMR3, CMR_SDL | CMR_UB | CMR_REFO);
|
|
|
|
enableCS();
|
|
SPI.transfer(ADDR_DOR2 | INSR_MB1 | INSR_RW);
|
|
res.in[2] = SPI.transfer(0);
|
|
res.in[1] = SPI.transfer(0);
|
|
res.in[0] = SPI.transfer(0);
|
|
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;
|
|
|
|
m_value = (res.out >> SKIPPED_BITS);
|
|
|
|
uint32_t vMax = (V_MAX >> SKIPPED_BITS);
|
|
|
|
m_u = (((float)m_value) / ((float)vMax)) * U_REF;
|
|
m_rRaw = ((((float)vMax) / ((float)m_value)) - 1.0) * R_REF;
|
|
m_r = (m_rRaw - m_calOffset) * m_calFactor;
|
|
}
|
|
}
|
|
|
|
|
|
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, bool initializeConfig, int eepromAddr) {
|
|
static bool onlyOnce = false;
|
|
|
|
m_csPin = csPin;
|
|
m_drdyPin = drdyPin;
|
|
m_eepromAddr = eepromAddr;
|
|
|
|
if (initializeConfig) {
|
|
// set default values
|
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_OFFSET, 0.0);
|
|
Config::setFloat(m_eepromAddr + CONFIG_CAL_FACTOR, 1.0);
|
|
}
|
|
|
|
// initialization of SPI
|
|
// Serial << "Start SPI initialization ... ";
|
|
pinMode(m_csPin, OUTPUT);
|
|
digitalWrite(m_csPin, HIGH);
|
|
SPI.begin();
|
|
SPI.setBitOrder(MSBFIRST);
|
|
SPI.setClockDivider(SPI_CLOCK_DIV128);
|
|
SPI.setDataMode(SPI_MODE1);
|
|
// Serial << "done." << endl;
|
|
|
|
// initialization of ADS1210
|
|
// Serial << "Start ADS1210 initialization ... ";
|
|
pinMode(m_drdyPin, INPUT);
|
|
delay(10);
|
|
writeRegister(ADDR_CMR3, CMR_SDL | CMR_UB | CMR_REFO);
|
|
writeRegister(ADDR_CMR1, 0x1b); // data rate
|
|
writeRegister(ADDR_CMR0, 0x58);
|
|
// Serial << "done." << endl;
|
|
|
|
// Serial << "Set gain ... ";
|
|
// setGain(CMR_Gain_2);
|
|
// Serial << "done." << endl;
|
|
|
|
// Serial << "SelfCalibration ... ";
|
|
setMode(CMR_MD_SelfCalibration);
|
|
waitForDRdy();
|
|
// Serial << "done." << endl;
|
|
|
|
m_calOffset = Config::getFloat(m_eepromAddr + CONFIG_CAL_OFFSET);
|
|
m_calFactor = Config::getFloat(m_eepromAddr + CONFIG_CAL_FACTOR);
|
|
}
|
|
|
|
|