diff --git a/CHANGELOG.md b/CHANGELOG.md index 0375c8e..52dcfa1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# Change Log MAX44009 +# Change Log MAX31850 All notable changes to this project will be documented in this file. @@ -6,13 +6,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.1.2] - 2023-01-23 +- update GitHub actions +- update license 2023 +- add setTypeTC() + getTypeTC() +- update keywords.txt +- update readme.md +- add retries parameter to **begin()** +- minor edits. + + ## [0.1.1] - 2022-11-16 - add RP2040 in build-CI - add changelog.md - major rewrite based on reread of datasheet. - initial release(?) - ## [0.1.0] - 2021-06-03 - initial version (not released) diff --git a/LICENSE b/LICENSE index a8b455d..088bbaf 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021-2021 Rob Tillaart +Copyright (c) 2021-2023 Rob Tillaart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/MAX31850.cpp b/MAX31850.cpp index bb0b3d4..1330878 100644 --- a/MAX31850.cpp +++ b/MAX31850.cpp @@ -1,20 +1,18 @@ // // FILE: MAX31850.cpp // AUTHOR: Rob.Tillaart@gmail.com -// VERSION: 0.1.1 +// VERSION: 0.1.2 // DATE: 2021-06-03 // PUPROSE: Arduino library for the MAX31850 thermocouple temperature sensor. -// -// HISTORY: see changelog.md #include "MAX31850.h" // OneWire commands -#define STARTCONVO 0x44 -#define READSCRATCH 0xBE -#define WRITESCRATCH 0x4E +#define STARTCONVO 0x44 +#define READSCRATCH 0xBE +#define WRITESCRATCH 0x4E // SCRATCHPAD registers @@ -27,7 +25,7 @@ #define INTERN_TEMP_MSB 3 // 4 address bits 0..15 -#define CONFIGURATION 4 +#define CONFIGURATION 4 #define RESERVED_1 5 #define RESERVED_2 6 #define RESERVED_3 7 @@ -42,6 +40,7 @@ MAX31850::MAX31850(OneWire* oneWire) { _oneWire = oneWire; _addresFound = false; + _typeTC = 'K'; // most used. _TCTemp = 0; _internalTemp = 0; _errorBits = 0; @@ -50,10 +49,10 @@ MAX31850::MAX31850(OneWire* oneWire) } -bool MAX31850::begin(void) +bool MAX31850::begin(uint8_t retries) { _addresFound = false; - for (uint8_t retries = 3; (retries > 0) && (_addresFound == false); retries--) + for (uint8_t r = retries; (r > 0) && (_addresFound == false); r--) { _oneWire->reset(); _oneWire->reset_search(); @@ -139,31 +138,30 @@ uint8_t MAX31850::getAddressPins() }; -// bool MAX31850::setType(char typeTC) -// { -// switch(typeTC) -// { -// case 'E': -// case 'J': -// case 'K': -// case 'N': -// case 'R': -// case 'S': -// case 'T': -// _typeTC = typeTC; -// return true; -// } -// return false; -// } -// -// -// char MAX31850::getType() -// { -// return _typeTC; -// } - - - +bool MAX31850::setTypeTC(char typeTC) +{ + switch(toupper(typeTC)) + { + case 'E': + case 'J': + case 'K': + case 'N': + case 'R': + case 'S': + case 'T': + _typeTC = toupper(typeTC); + return true; + } + return false; +} + + +char MAX31850::getTypeTC() +{ + return _typeTC; +} + + ///////////////////////////////////////////////////////////////////////////// // // PROTECTED @@ -191,4 +189,6 @@ MAX31851::MAX31851(OneWire * onewire) : MAX31850(onewire) { } + // -- END OF FILE -- + diff --git a/MAX31850.h b/MAX31850.h index d4c440c..8498b41 100644 --- a/MAX31850.h +++ b/MAX31850.h @@ -2,13 +2,12 @@ // // FILE: MAX31850.h // AUTHOR: Rob.Tillaart@gmail.com -// VERSION: 0.1.1 +// VERSION: 0.1.2 // DATE: 2021-06-03 // PUPROSE: Arduino library for the MAX31850 thermocouple temperature sensor. -// -#define MAX31850_LIB_VERSION (F("0.1.1")) +#define MAX31850_LIB_VERSION (F("0.1.2")) #include "Arduino.h" #include "OneWire.h" @@ -30,8 +29,8 @@ class MAX31850 { public: explicit MAX31850(OneWire * oneWire); - bool begin(void); - bool getAddress(uint8_t* buffer); + bool begin(uint8_t retries = 3); + bool getAddress(uint8_t * buffer); void requestTemperatures(void); bool isConversionComplete(void); @@ -46,6 +45,10 @@ class MAX31850 // TODO uint8_t getAddressPins(); + // type is a char from E J K N R S T (lowercase will be converted) + bool setTypeTC(char typeTC = 'K'); // K is most used + char getTypeTC(); + protected: void readScratchPad(uint8_t *, uint8_t); diff --git a/README.md b/README.md index cc16dbe..44c4dde 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,12 @@ [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](/~https://github.com/RobTillaart/MAX31850/blob/master/LICENSE) [![GitHub release](https://img.shields.io/github/release/RobTillaart/MAX31850.svg?maxAge=3600)](/~https://github.com/RobTillaart/MAX31850/releases) + # MAX31850 Arduino library for the MAX31850 / MAX31851 thermocouple sensor. + ## Description **WARNING EXPERIMENTAL** needs more testing (no hardware yet). @@ -23,13 +25,15 @@ The MAX31850/1 supports K-, J-, N-, T-, S-, R-, and E-type thermocouples. (to be The library supports both the MAX31850 and MAX31851 but is limited to one sensor per pin. The only feature the class supports is the asynchronous reading of the temperature. -This allowed the class to be both minimal in size and non-blocking. -In fact the class has no support for a synchronous read in one call. +This allowed the class to be both minimal in size and non-blocking. +In fact the class has no support for a synchronous read in one call. This choice will teach people how to work in a non-blocking way from the start. -Finally this library will probably make it easier to use a MAX31850 with processing +Finally this library will probably make it easier to use a MAX31850 with processing boards or IC's with small memory footprint. +The MAX31851 is (for now) functional identical to MAX31850. + ## Hardware connection @@ -45,52 +49,65 @@ boards or IC's with small memory footprint. // 4 address pins. ``` -This library supports only one MAX31850 per Arduino/ MCU pin. +This library supports only one MAX31850 per Arduino/ MCU IO pin. -### Pull up resistor +#### Pull up resistor -Connect a pull-up resistor 4.7 KOhm between pin3 and pin2. -When the wires are longer this resistor needs to be smaller. +Connect a pull-up resistor 4.7 KOhm between DATA (pin 3) and VCC (pin 2). +When the wires are longer this resistor needs to be smaller. An **indicative** table for pull up resistors, (E12 series), to get started. -Note: thicker wires require smaller resistors (typically 1 step in E12 series) +Note: thicker wires require smaller resistors (typically 1 step in E12 series) -| Length | - 5.0 Volt | - 3.3 Volt | -|--------------:|------------:|----------:| -| 10cm (4") | 10K0 | 6K8 | -| 20cm (8") | 8K2 | 4K7 | -| 50cm (20") | 4K7 | 3K3 | -| 100cm (3'4") | 3K3 | 2K2 | -| 200cm (6'8") | 2K2 | 1K0 | -| 500cm (16'8") | 1K0 | \* | -| longer | * | \* | +| Length | 5.0 Volt | 3.3 Volt | +|----------------:|-----------:|-----------:| +| 10cm (4") | 10K0 | 6K8 | +| 20cm (8") | 8K2 | 4K7 | +| 50cm (20") | 4K7 | 3K3 | +| 100cm (3'4") | 3K3 | 2K2 | +| 200cm (6'8") | 2K2 | 1K0 | +| 500cm (16'8") | 1K0 | \* | +| longer | \* | \* | -\* = no info, smaller +\* = no info, smaller ## Interface +```cpp +#include "MAX31850.h" +``` + + #### Constructor - **explicit MAX31850(OneWire \* oneWire)** constructor. -- **bool begin(void)** initialize the library. +- **bool begin(uint8_t retries = 3)** initialize the library. Returns true if addresses are found. +Nr of retries can be adjusted if needed, default 3. - **bool getAddress(uint8_t \* buffer)** get the address if found. + #### Read the sensor -- **void requestTemperatures(void)** idem. -- **bool isConversionComplete(void)** idem. + +- **void requestTemperatures(void)** Asynchronous request to start conversion. +- **bool isConversionComplete(void)** CHeck if requested conversion is done. - **float read(void)** read the data from the sensor. Returns the temperature of the thermoCouple as this is most often needed. -- **float getTempTC(void)** returns temperature of the ThermoCouple. -One must call **read()** to get new measurements. -- **float getTempInternal(void)** returns internal temperature. +- **float getTempTC(void)** returns temperature of the ThermoCouple from cache. +One must call **read()** again to get new measurements. +- **float getTempInternal(void)** returns internal temperature from cache. +One must call **read()** again to get new measurements. -#### other -- **uint8_t getAddressPins()** ?? -- **uint8_t getErrorCode()** + +#### Other + +- **uint8_t getAddressPins()** binary form of the 4 address bits. 0x00..0x0F. +- **uint8_t getErrorCode()** get the internal error code. See below. +- **bool setTypeTC(char typeTC)** typeTC is one char from E J K N R S T +- **uint8_t getTypeTC()** returns the current TC type. ## Types of thermocouples @@ -98,15 +115,15 @@ One must call **read()** to get new measurements. The MAX31850 comes in MAX31850E.. MAX31850T types reflecting the version of TC to use. -| Sensor type | SC in µV/°C | Temp Range in °C | Material | notes | -|:-----------:|:------------|:-----------------|:--------------------------|:-----------| -| E_TC | 76.373 | -270 to +1000 | Constantan Chromel | -| J_TC | 57.953 | -210 to +1200 | Constantan Iron | -| K_TC | 41.276 | -270 to +1372 | Alumel Chromel | most used | -| N_TC | 36.256 | -270 to +1300 | Nisil Nicrosil | -| R_TC | 10.506 | -50 to +1768 | Platinum Platinum/Rhodium | -| S_TC | 9.587 | +50 to +1768 | Platinum Platinum/Rhodium | -| T_TC | 52.18 | -270 to +400 | Constantan Copper | +| Sensor type | SC in µV/°C | Temp Range in °C | Material | notes | +|:-------------:|:--------------|:-------------------|:----------------------------|:------------| +| E_TC | 76.373 | -270 to +1000 | Constantan Chromel | +| J_TC | 57.953 | -210 to +1200 | Constantan Iron | +| K_TC | 41.276 | -270 to +1372 | Alumel Chromel | most used | +| N_TC | 36.256 | -270 to +1300 | Nisil Nicrosil | +| R_TC | 10.506 | -50 to +1768 | Platinum Platinum/Rhodium | +| S_TC | 9.587 | +50 to +1768 | Platinum Platinum/Rhodium | +| T_TC | 52.18 | -270 to +400 | Constantan Copper | (MAX31851 idem) @@ -115,27 +132,40 @@ The MAX31850 comes in MAX31850E.. MAX31850T types reflecting the version of TC t | name | value | |:--------------------------|:-------:| -| MAX31850_OK | 0 | -| MAX31850_ERR_SHORT_OPEN | 1 | -| MAX31850_ERR_SHORT_GND | 2 | -| MAX31850_ERR_SHORT_VDD | 4 | +| MAX31850_OK | 0 | +| MAX31850_ERR_SHORT_OPEN | 1 | +| MAX31850_ERR_SHORT_GND | 2 | +| MAX31850_ERR_SHORT_VDD | 4 | ## Future -#### must +#### Must + - improve documentation - get hardware to test (sponsors welcome) - test on different platforms -#### should -- investigate different thermocouples +#### Should + +- investigate different thermocouples - test with different platforms +- has the 31851 special features to implement? #### could -- expand to multi sensor per pin. + +- extend error handling +- expand to multi sensor per pin. - first get one sensor working +- investigate range checking on type TC + - need setType() and more. + - need lower upper range = int16_t will do. +- add CRC check + - + error code + + +#### Wont diff --git a/examples/MAX31850_getAddress/MAX31850_getAddress.ino b/examples/MAX31850_getAddress/MAX31850_getAddress.ino index b7692ce..797c7a2 100644 --- a/examples/MAX31850_getAddress/MAX31850_getAddress.ino +++ b/examples/MAX31850_getAddress/MAX31850_getAddress.ino @@ -4,7 +4,6 @@ // PURPOSE: MAX31850 lib getAddress demo - #include "OneWire.h" #include "MAX31850.h" @@ -43,8 +42,8 @@ void setup() Serial.print("Address: "); for (uint8_t i = 0; i < 8; i++) { - if (da[i] < 0x10) Serial.print('0'); - Serial.print(da[i], HEX); + if (da[i] < 0x10) Serial.print('0'); + Serial.print(da[i], HEX); } Serial.println(); } @@ -52,7 +51,6 @@ void setup() void loop() { - } diff --git a/examples/MAX31850_test_disconnect/MAX31850_test_disconnect.ino b/examples/MAX31850_test_disconnect/MAX31850_test_disconnect.ino index ec0623d..c32bf42 100644 --- a/examples/MAX31850_test_disconnect/MAX31850_test_disconnect.ino +++ b/examples/MAX31850_test_disconnect/MAX31850_test_disconnect.ino @@ -4,7 +4,6 @@ // PURPOSE: Minimal MAX31850 lib with async support. - #include "OneWire.h" #include "MAX31850.h" diff --git a/keywords.txt b/keywords.txt index a7c77af..4462a1f 100644 --- a/keywords.txt +++ b/keywords.txt @@ -14,18 +14,22 @@ getAddress KEYWORD2 requestTemperatures KEYWORD2 isConversionComplete KEYWORD2 + read KEYWORD2 getTempTC KEYWORD2 getTempInternal KEYWORD2 +getErrorCode KEYWORD2 getAddressPins KEYWORD2 -setType KEYWORD2 -getType KEYWORD2 +setTypeTC KEYWORD2 +getTypeTC KEYWORD2 # Constants (LITERAL1) -DS18B20_LIB_VERSION LITERAL1 -DEVICE_DISCONNECTED LITERAL1 -DEVICE_CRC_ERROR LITERAL1 -MAX31850_CLEAR LITERAL1 -MAX31850_CRC LITERAL1 +MAX31850_LIB_VERSION LITERAL1 + +MAX31850_OK LITERAL1 +MAX31850_ERR_SHORT_OPEN LITERAL1 +MAX31850_ERR_SHORT_GND LITERAL1 +MAX31850_ERR_SHORT_VDD LITERAL1 + diff --git a/library.json b/library.json index 3ac3509..0a5bd81 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "/~https://github.com/RobTillaart/MAX31850.git" }, - "version": "0.1.1", + "version": "0.1.2", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/library.properties b/library.properties index 0d19d8b..5e32617 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=MAX31850 -version=0.1.1 +version=0.1.2 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for the MAX31850 thermocouple temperature sensor. diff --git a/test/unit_test_001.cpp_avr_specific b/test/unit_test_001.cpp_avr_specific index df87ac4..dc30513 100644 --- a/test/unit_test_001.cpp_avr_specific +++ b/test/unit_test_001.cpp_avr_specific @@ -40,39 +40,71 @@ unittest_teardown() } +unittest(test_constants) +{ + assertEqual(0, MAX31850_OK); + assertEqual(1, MAX31850_ERR_SHORT_OPEN); + assertEqual(2, MAX31850_ERR_SHORT_GND); + assertEqual(4, MAX31850_ERR_SHORT_VDD); +} + + unittest(test_constructor) { OneWire oneWire(4); MAX31850 sensor(&oneWire); sensor.begin(); - - assertEqual(MAX31850_CLEAR, sensor.getConfig()); - - sensor.setConfig(MAX31850_CRC); - assertEqual(MAX31850_CRC, sensor.getConfig()); - - sensor.setConfig(MAX31850_CLEAR); - assertEqual(MAX31850_CLEAR, sensor.getConfig()); + assertEqual(1,1); + } -unittest(test_try_read) + +unittest(test_setTypeTC) { OneWire oneWire(4); MAX31850 sensor(&oneWire); sensor.begin(); - for (int res = 9; res < 13; res++) - { - sensor.setResolution(res); - sensor.requestTemperatures(); - delay(750); - assertFalse(sensor.isConversionComplete()); - assertEqual(DEVICE_DISCONNECTED, sensor.getTempC()); - } + + // default = K + assertEqual('K', sensor.getTypeTC()); + + assertTrue(sensor.setTypeTC('E')); + assertEqual('E', sensor.getTypeTC()); + assertTrue(sensor.setTypeTC('J')); + assertEqual('J', sensor.getTypeTC()); + + assertTrue(sensor.setTypeTC('K')); + assertEqual('K', sensor.getTypeTC()); + + assertTrue(sensor.setTypeTC('N')); + assertEqual('N', sensor.getTypeTC()); + + assertTrue(sensor.setTypeTC('R')); + assertEqual('R', sensor.getTypeTC()); + + assertTrue(sensor.setTypeTC('S')); + assertEqual('S', sensor.getTypeTC()); + + assertTrue(sensor.setTypeTC('T')); + assertEqual('T', sensor.getTypeTC()); + + // set default + assertTrue(sensor.setTypeTC()); + assertEqual('K', sensor.getTypeTC()); + + // lower case ? + + // failing chars. + assertFalse(sensor.setTypeTC('a')); + } + unittest_main() -// -------- + +// -- END OF FILE -- +