add gain=2, non-blocking operation, led abstracted
This commit is contained in:
61
Metro.cpp
Normal file
61
Metro.cpp
Normal file
@ -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();
|
||||
|
||||
}
|
||||
|
||||
|
49
Metro.h
Normal file
49
Metro.h
Normal file
@ -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 <inttypes.h>
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
@ -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();
|
||||
|
||||
uint32_t adcValue = ads1210.get();
|
||||
Serial << "AdcValue: " << adcValue << endl;
|
||||
if (secondTick.check() == 1) {
|
||||
led.toggle();
|
||||
|
||||
Serial << "AdcValue: " << adc1210.value << endl;
|
||||
}
|
||||
}
|
||||
|
22
ads1210.cpp
22
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,13 +54,13 @@ uint8_t ADS1210::readRegister(const uint8_t regAddr) const {
|
||||
return res;
|
||||
}
|
||||
|
||||
uint32_t ADS1210::readDataOutRegister() const {
|
||||
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);
|
||||
@ -78,7 +72,8 @@ uint32_t ADS1210::readDataOutRegister() const {
|
||||
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;
|
||||
value = res.out;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -130,13 +125,14 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin) {
|
||||
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();
|
||||
}
|
||||
|
@ -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;
|
||||
@ -75,11 +76,7 @@ private:
|
||||
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;
|
||||
};
|
||||
|
32
led.h
Normal file
32
led.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef _LED_H_
|
||||
#define _LED_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <stdint.h>
|
||||
|
||||
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_
|
Reference in New Issue
Block a user