Commit eff5560d authored by Alisson Claudino's avatar Alisson Claudino

ADD: ThermoCouple library class

parent df244490
......@@ -6,3 +6,5 @@ Circuits/*/.kicad_pcb-bak
Circuits/*/rescue-backup
INTFP(I)
Interface/__pycache__
Firmware/.*
.idea
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads0(0x48);
Adafruit_ADS1115 ads1(0x49);
int16_t adc0, adc1, adc2, adc3, adc4, adc5, adc6, adc7;
void setup(void)
{
Serial.begin(115200);
//Serial.println("Hello!");
//Serial.println("Getting single-ended readings from AIN0..3");
//Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
ads0.begin();
ads1.begin();
}
void loop(void)
{
adc0 = ads0.readADC_SingleEnded(0);
adc1 = ads0.readADC_SingleEnded(1);
/*adc2 = ads0.readADC_SingleEnded(2);
adc3 = ads0.readADC_SingleEnded(3);
adc4 = ads1.readADC_SingleEnded(0);
adc5 = ads1.readADC_SingleEnded(1);
adc6 = ads1.readADC_SingleEnded(2);
adc7 = ads1.readADC_SingleEnded(3);*/
Serial.print(adc0);
Serial.print(" ");
Serial.print(adc1);
/*Serial.print(" ");
Serial.print(adc2);
Serial.print(" ");
Serial.print(adc3);
Serial.print(" ");
Serial.print(adc4);
Serial.print(" ");
Serial.print(adc5);
Serial.print(" ");
Serial.print(adc6);
Serial.print(" ");
Serial.print(adc7);
Serial.print(" ");
Serial.print(millis());*/
Serial.print("\n");
while(millis()%100!=0);
}
/**************************************************************************/
/*!
@file Adafruit_ADS1015.cpp
@author K.Townsend (Adafruit Industries)
@license BSD (see license.txt)
@file ADS1115.cpp
@author Alisson Claudino
@based on Adafruit_ADS1015 library K. Townsend (Adafruit Industries)
@license GPLv3
Driver for the ADS1015/ADS1115 ADC
This is a library for the Adafruit MPL115A2 breakout
----> https://www.adafruit.com/products/???
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
......@@ -26,7 +17,7 @@
#include <Wire.h>
#include "Adafruit_ADS1015.h"
#include "ADS1115.h"
/**************************************************************************/
/*!
......@@ -77,7 +68,7 @@ static uint16_t readRegister(uint8_t i2cAddress, uint8_t reg) {
i2cwrite(ADS1015_REG_POINTER_CONVERT);
Wire.endTransmission();
Wire.requestFrom(i2cAddress, (uint8_t)2);
return ((i2cread() << 8) | i2cread());
return ((i2cread() << 8) | i2cread());
}
/**************************************************************************/
......@@ -85,7 +76,7 @@ static uint16_t readRegister(uint8_t i2cAddress, uint8_t reg) {
@brief Instantiates a new ADS1015 class w/appropriate properties
*/
/**************************************************************************/
Adafruit_ADS1015::Adafruit_ADS1015(uint8_t i2cAddress)
ADS1015::ADS1015(uint8_t i2cAddress)
{
m_i2cAddress = i2cAddress;
m_conversionDelay = ADS1015_CONVERSIONDELAY;
......@@ -98,7 +89,7 @@ Adafruit_ADS1015::Adafruit_ADS1015(uint8_t i2cAddress)
@brief Instantiates a new ADS1115 class w/appropriate properties
*/
/**************************************************************************/
Adafruit_ADS1115::Adafruit_ADS1115(uint8_t i2cAddress)
ADS1115::ADS1115(uint8_t i2cAddress)
{
m_i2cAddress = i2cAddress;
m_conversionDelay = ADS1115_CONVERSIONDELAY;
......@@ -111,7 +102,7 @@ Adafruit_ADS1115::Adafruit_ADS1115(uint8_t i2cAddress)
@brief Sets up the HW (reads coefficients values, etc.)
*/
/**************************************************************************/
void Adafruit_ADS1015::begin() {
void ADS1015::begin() {
Wire.begin();
}
......@@ -120,7 +111,7 @@ void Adafruit_ADS1015::begin() {
@brief Sets the gain and input voltage range
*/
/**************************************************************************/
void Adafruit_ADS1015::setGain(adsGain_t gain)
void ADS1015::setGain(adsGain_t gain)
{
m_gain = gain;
}
......@@ -130,7 +121,7 @@ void Adafruit_ADS1015::setGain(adsGain_t gain)
@brief Gets a gain and input voltage range
*/
/**************************************************************************/
adsGain_t Adafruit_ADS1015::getGain()
adsGain_t ADS1015::getGain()
{
return m_gain;
}
......@@ -140,12 +131,12 @@ adsGain_t Adafruit_ADS1015::getGain()
@brief Gets a single-ended ADC reading from the specified channel
*/
/**************************************************************************/
uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) {
uint16_t ADS1015::readADC_SingleEnded(uint8_t channel) {
if (channel > 3)
{
return 0;
}
// Start with default values
uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
......@@ -185,18 +176,18 @@ uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) {
// Read the conversion results
// Shift 12-bit results right 4 bits for the ADS1015
return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
}
/**************************************************************************/
/*!
/*!
@brief Reads the conversion results, measuring the voltage
difference between the P (AIN0) and N (AIN1) input. Generates
a signed value since the difference can be either
positive or negative.
*/
/**************************************************************************/
int16_t Adafruit_ADS1015::readADC_Differential_0_1() {
int16_t ADS1015::readADC_Differential_0_1() {
// Start with default values
uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
......@@ -207,7 +198,7 @@ int16_t Adafruit_ADS1015::readADC_Differential_0_1() {
// Set PGA/voltage range
config |= m_gain;
// Set channels
config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
......@@ -240,14 +231,14 @@ int16_t Adafruit_ADS1015::readADC_Differential_0_1() {
}
/**************************************************************************/
/*!
/*!
@brief Reads the conversion results, measuring the voltage
difference between the P (AIN2) and N (AIN3) input. Generates
a signed value since the difference can be either
positive or negative.
*/
/**************************************************************************/
int16_t Adafruit_ADS1015::readADC_Differential_2_3() {
int16_t ADS1015::readADC_Differential_2_3() {
// Start with default values
uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
......@@ -299,7 +290,7 @@ int16_t Adafruit_ADS1015::readADC_Differential_2_3() {
This will also set the ADC in continuous conversion mode.
*/
/**************************************************************************/
void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
void ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
{
// Start with default values
uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
......@@ -312,7 +303,7 @@ void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t thre
// Set PGA/voltage range
config |= m_gain;
// Set single-ended input channel
switch (channel)
{
......@@ -345,7 +336,7 @@ void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t thre
results without changing the config value.
*/
/**************************************************************************/
int16_t Adafruit_ADS1015::getLastConversionResults()
int16_t ADS1015::getLastConversionResults()
{
// Wait for the conversion to complete
delay(m_conversionDelay);
......@@ -368,4 +359,3 @@ int16_t Adafruit_ADS1015::getLastConversionResults()
return (int16_t)res;
}
}
/**************************************************************************/
/*!
@file Adafruit_ADS1015.h
@author K. Townsend (Adafruit Industries)
@license BSD (see license.txt)
@file ADS1115.h
@author Alisson Claudino
@based on Adafruit_ADS1015 library K. Townsend (Adafruit Industries)
@license GPLv3
This is a library for the Adafruit ADS1015 breakout board
----> https://www.adafruit.com/products/???
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
v1.1 - Added ADS1115 support - W. Earl
*/
/**************************************************************************/
......@@ -79,7 +69,7 @@
#define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
#define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)
#define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
#define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
......@@ -117,7 +107,7 @@ typedef enum
GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V
} adsGain_t;
class Adafruit_ADS1015
class ADS1015
{
protected:
// Instance-specific properties
......@@ -127,7 +117,7 @@ protected:
adsGain_t m_gain;
public:
Adafruit_ADS1015(uint8_t i2cAddress = ADS1015_ADDRESS);
ADS1015(uint8_t i2cAddress = ADS1015_ADDRESS);
void begin(void);
uint16_t readADC_SingleEnded(uint8_t channel);
int16_t readADC_Differential_0_1(void);
......@@ -141,10 +131,10 @@ protected:
};
// Derive from ADS1105 & override construction to set properties
class Adafruit_ADS1115 : public Adafruit_ADS1015
class ADS1115 : public ADS1015
{
public:
Adafruit_ADS1115(uint8_t i2cAddress = ADS1015_ADDRESS);
ADS1115(uint8_t i2cAddress = ADS1015_ADDRESS);
private:
};
/**
* @file ThermoCouple.cpp
* This is the library to access the Termopar Nanoshield version 2
*
* Copyright (c) 2015 Circuitar
* This software is released under the MIT license. See the attached LICENSE file for details.
*/
#include "ThermoCouple.h"
// MAX31856 registers
#define MAX31856_REG_CR0 0x00
#define MAX31856_REG_CR1 0x01
#define MAX31856_REG_MASK 0x02
#define MAX31856_REG_CJHF 0x03
#define MAX31856_REG_CJLF 0x04
#define MAX31856_REG_LTHFTH 0x05
#define MAX31856_REG_LTHFTL 0x06
#define MAX31856_REG_LTLFTH 0x07
#define MAX31856_REG_LTLFTL 0x08
#define MAX31856_REG_CJTO 0x09
#define MAX31856_REG_CJTH 0x0A
#define MAX31856_REG_CJTL 0x0B
#define MAX31856_REG_LTCBH 0x0C
#define MAX31856_REG_LTCBM 0x0D
#define MAX31856_REG_LTCBL 0x0E
#define MAX31856_REG_SR 0x0F
// MAX31856 register read/write masks
#define MAX31856_REG_READ 0x00
#define MAX31856_REG_WRITE 0x80
SPISettings ThermoCouple::spiSettings = SPISettings(1000000, MSBFIRST, SPI_MODE1);
ThermoCouple::ThermoCouple(uint8_t cs, TcType type, TcAveraging avg, TcOcd ocd) {
this->cs = cs;
this->type = type;
this->avg = avg;
this->ocd = ocd;
this->internal = 0;
this->external = 0;
this->fault = 0;
}
void ThermoCouple::begin() {
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
SPI.begin();
// Initialize MAX31856
SPI.beginTransaction(spiSettings);
digitalWrite(cs, LOW);
SPI.transfer(MAX31856_REG_CR0 | MAX31856_REG_WRITE);
SPI.transfer(0x82 | ((uint8_t)ocd << 4)); // Setup CR0 register:
// Automatic conversion mode
// Set open circuit fault detection mode
// Cold juntion sensor enabled
// Fault detection in comparator mode
// Clear fault status
// Noise rejection = 60Hz
SPI.transfer(((uint8_t)avg << 4) | ((uint8_t)type & 0x0F)); // Setup CR1 register:
// Setup selected averaging mode
// Setup selected thermocouple type
digitalWrite(cs, HIGH);
SPI.endTransaction();
}
void ThermoCouple::read() {
uint16_t cj = 0;
uint32_t ltc = 0;
SPI.beginTransaction(spiSettings);
digitalWrite(cs, LOW);
SPI.transfer(MAX31856_REG_CJTH | MAX31856_REG_READ);
cj |= (uint16_t)SPI.transfer(0) << 8;
cj |= SPI.transfer(0);
ltc |= (uint32_t)SPI.transfer(0) << 16;
ltc |= (uint32_t)SPI.transfer(0) << 8;
ltc |= SPI.transfer(0);
fault = SPI.transfer(0);
digitalWrite(cs, HIGH);
SPI.endTransaction();
cj >>= 2;
internal = (cj >= (1U << 13) ? cj - (1UL << 14) : cj) * 0.015625;
int32_t signed_ltc = (int32_t)(ltc >= (1UL << 23) ? ltc - (1UL << 24) : ltc) / 32;
switch (type) {
case TC_TYPE_VOLTAGE_GAIN_8:
external = signed_ltc / (8 * 1.6 * (1UL << 17));
break;
case TC_TYPE_VOLTAGE_GAIN_32:
external = signed_ltc / (32 * 1.6 * (1UL << 17));
break;
default:
external = signed_ltc * 0.0078125;
}
}
double ThermoCouple::getInternal() {
return internal;
}
double ThermoCouple::getExternal() {
return external;
}
bool ThermoCouple::isExternalOutOfRange() {
return (fault & 0x40) != 0;
}
bool ThermoCouple::isInternalOutOfRange() {
return (fault & 0x80) != 0;
}
bool ThermoCouple::isOverUnderVoltage() {
return (fault & 0x02) != 0;
}
bool ThermoCouple::isOpen() {
return (fault & 0x01) != 0;
}
bool ThermoCouple::hasError() {
return (fault & 0xC3) != 0;
}
/**
* @file Nanoshield_Termopar.h
* This is the library to access the Termopar Nanoshield version 2
*
* Copyright (c) 2015 Circuitar
* This software is released under the MIT license. See the attached LICENSE file for details.
*/
#ifndef NANOSHIELD_TERMOPAR_h
#define NANOSHIELD_TERMOPAR_h
#include "Arduino.h"
#include <SPI.h>
enum TcType {
TC_TYPE_B,
TC_TYPE_E,
TC_TYPE_J,
TC_TYPE_K,
TC_TYPE_N,
TC_TYPE_R,
TC_TYPE_S,
TC_TYPE_T,
TC_TYPE_VOLTAGE_GAIN_8 = 0x8,
TC_TYPE_VOLTAGE_GAIN_32 = 0xC
};
enum TcAveraging {
TC_AVG_OFF,
TC_AVG_2_SAMPLES,
TC_AVG_4_SAMPLES,
TC_AVG_8_SAMPLES,
TC_AVG_16_SAMPLES
};
enum TcOcd {
TC_OCD_OFF,
TC_OCD_15_MS,
TC_OCD_37_MS,
TC_OCD_125_MS
};
class Thermocouple {
public:
/**
* @brief Constructor.
*
* Creates an object to access one Termopar Nanoshield.
*
* @param cs Chip select pin to access Termopar Nanoshield.
* @param type Thermocouple type.
* @param avg Averaging mode.
* @param ocd Open circuit detection mode.
*/
Thermocouple(uint8_t cs = 8, TcType type = TC_TYPE_K, TcAveraging avg = TC_AVG_OFF, TcOcd ocd = TC_OCD_15_MS);
/**
* @brief Initializes the module.
*
* Initializes SPI and CS pin.
*/
void begin();
/**
* @brief Reads all temperatures.
*
* @see getInternal()
* @see getExternal()
* @see hasError()
*/
void read();
/**
* @brief Gets the last external temperature reading (hot junction).
*
* @return The last external temperature reading.
* @see read()
*/
double getExternal();
/**
* @brief Gets the last internal temperature reading (cold junction).
*
* @return The last internal temperature reading.
* @see read()
*/
double getInternal();
/**
* @brief Checks if external temperature is out of range.
*
* @return True if external temperature (hot junction) is out of range.
*/
bool isExternalOutOfRange();
/**
* @brief Checks if internal temperature is out of range.
*
* @return True if internal temperature (cold junction) is out of range.
*/
bool isInternalOutOfRange();
/**
* @brief Checks for overvoltage or undervoltage.
*
* @return True if there is overvoltage or undervoltage on the thermocouple inputs.
*/
bool isOverUnderVoltage();
/**
* @brief Checks if thermocouple circuit is open.
*
* @return True if thermocouple circuit is open.
*/
bool isOpen();
/**
* @brief Checks if there are errors.
*
* @return True if any of the following errors is detected: open circuit, overvoltage, undervoltage,
* internal temperature out of range or external temperatur out of range.
* @see isExternalOutOfRange()
* @see isInternalOutOfRange()
* @see isOverUnderVoltage()
* @see isOpen()
*/
bool hasError();
private:
static SPISettings spiSettings;
uint8_t cs;
TcType type;
TcAveraging avg;
TcOcd ocd;
double internal;
double external;
uint8_t fault;
};
#endif
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#include <ADS1115.h>
Adafruit_ADS1115 ads0(0x48);
Adafruit_ADS1115 ads1(0x49);
ADS1115 ads0(0x48);
ADS1115 ads1(0x49);
Thermocouple term1;
int16_t adc0, adc1, adc2, adc3, adc4, adc5, adc6, adc7;
void setup(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment