diff --git a/Metro.cpp b/Metro.cpp new file mode 100644 index 0000000..104739f --- /dev/null +++ b/Metro.cpp @@ -0,0 +1,61 @@ + + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif +#include "Metro.h" + +Metro::Metro() +{ + + this->interval_millis = 1000; + +} + + +Metro::Metro(unsigned long interval_millis) +{ + + this->interval_millis = interval_millis; + +} + + +void Metro::interval(unsigned long interval_millis) +{ + this->interval_millis = interval_millis; +} + +uint8_t Metro::check() +{ + + unsigned long now = millis(); + + if ( interval_millis == 0 ){ + previous_millis = now; + return 1; + } + + if ( (now - previous_millis) >= interval_millis) { + #ifdef NOCATCH-UP + previous_millis = now ; + #else + previous_millis += interval_millis ; + #endif + return 1; + } + + return 0; + +} + +void Metro::reset() +{ + + this->previous_millis = millis(); + +} + + diff --git a/Metro.h b/Metro.h new file mode 100644 index 0000000..ec09aad --- /dev/null +++ b/Metro.h @@ -0,0 +1,49 @@ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + + +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * + Main code by Thomas O Fredericks (tof@t-o-f.info) + Contributions by Paul Bouchier and Benjamin.soelberg +* * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef Metro_h +#define Metro_h + +#include + + +class Metro +{ + +public: + Metro(); + Metro(unsigned long interval_millis); + void interval(unsigned long interval_millis); + uint8_t check(); + void reset(); + +private: + unsigned long previous_millis, interval_millis; + +}; + +#endif + + diff --git a/ModbusThermometer.cpp b/ModbusThermometer.cpp index be609f2..aa12ab3 100644 --- a/ModbusThermometer.cpp +++ b/ModbusThermometer.cpp @@ -1,30 +1,36 @@ #include "Arduino.h" #include "Streaming.h" +#include "Metro.h" #include "ads1210.h" +#include "led.h" + + +const uint8_t LED_PIN = 8; +const uint8_t ADC_CS_PIN = 9; +const uint8_t ADC_RDY_PIN = 7; ADS1210 ads1210; - +LED led; +Metro secondTick = Metro(1000); void setup() { Serial.begin(9600); delay(1000); - pinMode(8, OUTPUT); - - ads1210.begin(9, 7); - + led(LED_PIN); + ads1210.begin(ADC_CS_PIN, ADC_RDY_PIN); } void loop() { - digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level) - delay(1000); // wait for a second - digitalWrite(8, LOW); // turn the LED off by making the voltage LOW - delay(1000); // wait for a second + ads1210.exec(); + + if (secondTick.check() == 1) { + led.toggle(); - uint32_t adcValue = ads1210.get(); - Serial << "AdcValue: " << adcValue << endl; + Serial << "AdcValue: " << adc1210.value << endl; + } } diff --git a/ads1210.cpp b/ads1210.cpp index c71ec12..22c705e 100644 --- a/ads1210.cpp +++ b/ads1210.cpp @@ -13,8 +13,6 @@ -ADS1210::ADS1210() { -} void ADS1210::enableCS() const { digitalWrite(m_csPin, LOW); @@ -33,10 +31,6 @@ void ADS1210::waitForDRdy() const { } } -void ADS1210::waitForDRdyWOTimeout() const { - while (0 != digitalRead(m_drdyPin)) { - } -} void ADS1210::writeRegister(const uint8_t regAddr, const uint8_t value) const { @@ -60,25 +54,26 @@ uint8_t ADS1210::readRegister(const uint8_t regAddr) const { return res; } -uint32_t ADS1210::readDataOutRegister() const { - union { - uint8_t in[4]; - uint32_t out; - } res; +void ADS1210::exec() { + if (0 == digitalRead(m_drdyPin)) { + union { + uint8_t in[4]; + uint32_t out; + } res; - waitForDRdy(); - 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(); + 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; - - return res.out; + 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; + + value = res.out; + } } @@ -129,14 +124,15 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin) { pinMode(m_drdyPin, INPUT); writeRegister(ADDR_CMR3, CMR_SDL | CMR_UB | CMR_REFO); Serial << "done." << endl; + + Serial << "Set gain ... "; + setGain(CMR_Gain_2); + Serial << "done." << endl; Serial << "SelfCalibration ... "; setMode(CMR_MD_SelfCalibration); - waitForDRdyWOTimeout(); + waitForDRdy(); Serial << "done." << endl; } -uint32_t ADS1210::get() const { - return readDataOutRegister(); -} diff --git a/ads1210.h b/ads1210.h index 3f771b1..f3b4da1 100644 --- a/ads1210.h +++ b/ads1210.h @@ -10,10 +10,11 @@ class ADS1210 { public: - ADS1210(); + ADS1210() : value(0); void begin(uint8_t csPin, uint8_t drdyPin); - uint32_t get() const; + void exec(); + uint32_t value; private: // register addresses const uint8_t ADDR_DOR2 = 0x00; @@ -69,17 +70,13 @@ private: uint8_t m_csPin; uint8_t m_drdyPin; - + void enableCS() const; void disableCS() const; 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; }; diff --git a/led.h b/led.h new file mode 100644 index 0000000..1e42a52 --- /dev/null +++ b/led.h @@ -0,0 +1,32 @@ +#ifndef _LED_H_ +#define _LED_H_ + +#include +#include + +class LED { +public: + LED() : m_state(0) {}; + void begin(const uint8_t pin) { + m_pin = pin; + pinMode(m_pin, OUTPUT); + digitalWrite(m_pin, m_state); + }; + void on() { + m_state = 1; + digitalWrite(m_pin, m_state); + } + void off() { + m_state = 0; + digitalWrite(m_pin, m_state); + } + void toggle() { + m_state ^= 1; + digitalWrite(m_pin, m_state); + } +private: + uint8_t m_state; + uint8_t m_pin; +} + +#endif // _LED_H_