diff --git a/ModbusThermometer.cpp b/ModbusThermometer.cpp index ba5ce46..d840682 100644 --- a/ModbusThermometer.cpp +++ b/ModbusThermometer.cpp @@ -1,6 +1,6 @@ #include "Arduino.h" -#include +#include "Streaming.h" #include "ads1210.h" @@ -15,7 +15,7 @@ void setup() { pinMode(13, OUTPUT); - ads1210.begin(10); + ads1210.begin(9); } @@ -26,4 +26,6 @@ void loop() { delay(1000); // wait for a second Serial << "Tick" << endl; + ads1210.begin(9); + } diff --git a/Streaming.h b/Streaming.h new file mode 100644 index 0000000..9e54ae9 --- /dev/null +++ b/Streaming.h @@ -0,0 +1,105 @@ +/* +Streaming.h - Arduino library for supporting the << streaming operator +Copyright (c) 2010-2012 Mikal Hart. All rights reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef ARDUINO_STREAMING +#define ARDUINO_STREAMING + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#define STREAMING_LIBRARY_VERSION 5 + +// Generic template +template +inline Print &operator <<(Print &stream, T arg) +{ stream.print(arg); return stream; } + +struct _BASED +{ + long val; + int base; + _BASED(long v, int b): val(v), base(b) + {} +}; + +#if ARDUINO >= 100 + +struct _BYTE_CODE +{ + byte val; + _BYTE_CODE(byte v) : val(v) + {} +}; +#define _BYTE(a) _BYTE_CODE(a) + +inline Print &operator <<(Print &obj, const _BYTE_CODE &arg) +{ obj.write(arg.val); return obj; } + +#else + +#define _BYTE(a) _BASED(a, BYTE) + +#endif + +#define _HEX(a) _BASED(a, HEX) +#define _DEC(a) _BASED(a, DEC) +#define _OCT(a) _BASED(a, OCT) +#define _BIN(a) _BASED(a, BIN) + +// Specialization for class _BASED +// Thanks to Arduino forum user Ben Combee who suggested this +// clever technique to allow for expressions like +// Serial << _HEX(a); + +inline Print &operator <<(Print &obj, const _BASED &arg) +{ obj.print(arg.val, arg.base); return obj; } + +#if ARDUINO >= 18 +// Specialization for class _FLOAT +// Thanks to Michael Margolis for suggesting a way +// to accommodate Arduino 0018's floating point precision +// feature like this: +// Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision + +struct _FLOAT +{ + float val; + int digits; + _FLOAT(double v, int d): val(v), digits(d) + {} +}; + +inline Print &operator <<(Print &obj, const _FLOAT &arg) +{ obj.print(arg.val, arg.digits); return obj; } +#endif + +// Specialization for enum _EndLineCode +// Thanks to Arduino forum user Paul V. who suggested this +// clever technique to allow for expressions like +// Serial << "Hello!" << endl; + +enum _EndLineCode { endl }; + +inline Print &operator <<(Print &obj, _EndLineCode arg) +{ obj.println(); return obj; } + +#endif diff --git a/ads1210.cpp b/ads1210.cpp index fc2441e..0cc0120 100644 --- a/ads1210.cpp +++ b/ads1210.cpp @@ -7,7 +7,7 @@ // #include #include -#include +#include "Streaming.h" #include "ads1210.h" ADS1210::ADS1210() { @@ -24,10 +24,10 @@ void ADS1210::writeCMR () const { uint8_t instr = INSR_MB1 | INSR_MB0 | ADDR_CMR3; enableCS(); SPI.transfer(instr); - SPI.transfer(m_cmrShadow[3]); - SPI.transfer(m_cmrShadow[2]); - SPI.transfer(m_cmrShadow[1]); - SPI.transfer(m_cmrShadow[0]); + SPI.transfer(m_cmrWriteShadow[3]); + SPI.transfer(m_cmrWriteShadow[2]); + SPI.transfer(m_cmrWriteShadow[1]); + SPI.transfer(m_cmrWriteShadow[0]); disableCS(); } @@ -35,29 +35,53 @@ void ADS1210::readCMR () { uint8_t instr = INSR_MB1 | INSR_MB0 | ADDR_CMR3 | INSR_RW; enableCS(); SPI.transfer(instr); - m_cmrShadow[3] = SPI.transfer(0); - m_cmrShadow[2] = SPI.transfer(0); - m_cmrShadow[1] = SPI.transfer(0); - m_cmrShadow[0] = SPI.transfer(0); + 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 ADS1210::begin(uint8_t csPin) { - m_csPin = csPin; - + static bool onlyOnce = false; + + m_csPin = csPin; + // initialization of SPI pinMode(m_csPin, OUTPUT); digitalWrite(m_csPin, HIGH); SPI.begin(); SPI.setBitOrder(MSBFIRST); - SPI.setClockDivider(SPI_CLOCK_DIV2); - SPI.setDataMode(SPI_MODE0); + SPI.setClockDivider(SPI_CLOCK_DIV16); + SPI.setDataMode(SPI_MODE1); - // initialization of the ADS1210 - readCMR(); - - Serial << "CMR3: " << _HEX(m_cmrShadow[3]) << ", CMR2: " << _HEX(m_cmrShadow[2]) << ", CMR1: " << _HEX(m_cmrShadow[1]) << ", CMR0: " << _HEX(m_cmrShadow[0]) << 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; } diff --git a/ads1210.h b/ads1210.h index 73b9d13..070800c 100644 --- a/ads1210.h +++ b/ads1210.h @@ -68,7 +68,8 @@ private: uint8_t m_csPin; - uint8_t m_cmrShadow[4]; + uint8_t m_cmrReadShadow[4]; + uint8_t m_cmrWriteShadow[4]; void enableCS() const; void disableCS() const;