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 "Arduino.h"
|
||||||
|
|
||||||
#include "Streaming.h"
|
#include "Streaming.h"
|
||||||
|
#include "Metro.h"
|
||||||
|
|
||||||
#include "ads1210.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;
|
ADS1210 ads1210;
|
||||||
|
LED led;
|
||||||
|
Metro secondTick = Metro(1000);
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
pinMode(8, OUTPUT);
|
led(LED_PIN);
|
||||||
|
ads1210.begin(ADC_CS_PIN, ADC_RDY_PIN);
|
||||||
ads1210.begin(9, 7);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
|
ads1210.exec();
|
||||||
delay(1000); // wait for a second
|
|
||||||
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
|
if (secondTick.check() == 1) {
|
||||||
delay(1000); // wait for a second
|
led.toggle();
|
||||||
|
|
||||||
uint32_t adcValue = ads1210.get();
|
Serial << "AdcValue: " << adc1210.value << endl;
|
||||||
Serial << "AdcValue: " << adcValue << endl;
|
}
|
||||||
}
|
}
|
||||||
|
50
ads1210.cpp
50
ads1210.cpp
@ -13,8 +13,6 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
ADS1210::ADS1210() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void ADS1210::enableCS() const {
|
void ADS1210::enableCS() const {
|
||||||
digitalWrite(m_csPin, LOW);
|
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 {
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t ADS1210::readDataOutRegister() const {
|
void ADS1210::exec() {
|
||||||
union {
|
if (0 == digitalRead(m_drdyPin)) {
|
||||||
uint8_t in[4];
|
union {
|
||||||
uint32_t out;
|
uint8_t in[4];
|
||||||
} res;
|
uint32_t out;
|
||||||
|
} res;
|
||||||
|
|
||||||
waitForDRdy();
|
enableCS();
|
||||||
enableCS();
|
SPI.transfer(ADDR_DOR2 | INSR_MB1 | INSR_RW);
|
||||||
SPI.transfer(ADDR_DOR2 | INSR_MB1 | INSR_RW);
|
res.in[2] = SPI.transfer(0);
|
||||||
res.in[2] = SPI.transfer(0);
|
res.in[1] = SPI.transfer(0);
|
||||||
res.in[1] = SPI.transfer(0);
|
res.in[0] = SPI.transfer(0);
|
||||||
res.in[0] = SPI.transfer(0);
|
res.in[3] = 0;
|
||||||
res.in[3] = 0;
|
disableCS();
|
||||||
disableCS();
|
|
||||||
|
|
||||||
Serial << "DOR4x8: " << _HEX(res.in[3]) << " " << _HEX(res.in[2]) << " " << _HEX(res.in[1]) << " " << _HEX(res.in[0]) << endl;
|
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;
|
Serial << "DOR1x32: " << _HEX(res.out) << endl;
|
||||||
|
|
||||||
return res.out;
|
value = res.out;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -129,14 +124,15 @@ void ADS1210::begin(uint8_t csPin, uint8_t drdyPin) {
|
|||||||
pinMode(m_drdyPin, INPUT);
|
pinMode(m_drdyPin, INPUT);
|
||||||
writeRegister(ADDR_CMR3, CMR_SDL | CMR_UB | CMR_REFO);
|
writeRegister(ADDR_CMR3, CMR_SDL | CMR_UB | CMR_REFO);
|
||||||
Serial << "done." << endl;
|
Serial << "done." << endl;
|
||||||
|
|
||||||
|
Serial << "Set gain ... ";
|
||||||
|
setGain(CMR_Gain_2);
|
||||||
|
Serial << "done." << endl;
|
||||||
|
|
||||||
Serial << "SelfCalibration ... ";
|
Serial << "SelfCalibration ... ";
|
||||||
setMode(CMR_MD_SelfCalibration);
|
setMode(CMR_MD_SelfCalibration);
|
||||||
waitForDRdyWOTimeout();
|
waitForDRdy();
|
||||||
Serial << "done." << endl;
|
Serial << "done." << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint32_t ADS1210::get() const {
|
|
||||||
return readDataOutRegister();
|
|
||||||
}
|
|
||||||
|
11
ads1210.h
11
ads1210.h
@ -10,10 +10,11 @@
|
|||||||
|
|
||||||
class ADS1210 {
|
class ADS1210 {
|
||||||
public:
|
public:
|
||||||
ADS1210();
|
ADS1210() : value(0);
|
||||||
void begin(uint8_t csPin, uint8_t drdyPin);
|
void begin(uint8_t csPin, uint8_t drdyPin);
|
||||||
uint32_t get() const;
|
void exec();
|
||||||
|
|
||||||
|
uint32_t value;
|
||||||
private:
|
private:
|
||||||
// register addresses
|
// register addresses
|
||||||
const uint8_t ADDR_DOR2 = 0x00;
|
const uint8_t ADDR_DOR2 = 0x00;
|
||||||
@ -69,17 +70,13 @@ private:
|
|||||||
|
|
||||||
uint8_t m_csPin;
|
uint8_t m_csPin;
|
||||||
uint8_t m_drdyPin;
|
uint8_t m_drdyPin;
|
||||||
|
|
||||||
|
|
||||||
void enableCS() const;
|
void enableCS() const;
|
||||||
void disableCS() const;
|
void disableCS() const;
|
||||||
void writeRegister(const uint8_t regAddr, const uint8_t value) const;
|
void writeRegister(const uint8_t regAddr, const uint8_t value) const;
|
||||||
uint8_t readRegister(const uint8_t regAddr) const;
|
uint8_t readRegister(const uint8_t regAddr) const;
|
||||||
uint32_t readDataOutRegister() const;
|
|
||||||
void waitForDRdy() const;
|
void waitForDRdy() const;
|
||||||
void waitForDRdyWOTimeout() const;
|
|
||||||
void disableRefO() const;
|
|
||||||
void enableRefO() const;
|
|
||||||
void setMode(uint8_t mode) const;
|
void setMode(uint8_t mode) const;
|
||||||
void setGain(uint8_t gain) 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