MAX31855 library
MAX31855.cpp@0:5d56064bc4c3, 2015-02-09 (annotated)
- Committer:
- yihui
- Date:
- Mon Feb 09 05:56:04 2015 +0000
- Revision:
- 0:5d56064bc4c3
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:5d56064bc4c3 | 1 | /* |
yihui | 0:5d56064bc4c3 | 2 | * A library for MAX31855 |
yihui | 0:5d56064bc4c3 | 3 | * |
yihui | 0:5d56064bc4c3 | 4 | * Copyright (c) 2015 Seeed Technology Limited. |
yihui | 0:5d56064bc4c3 | 5 | * Author : Yihui Xiong |
yihui | 0:5d56064bc4c3 | 6 | * |
yihui | 0:5d56064bc4c3 | 7 | * The MIT License (MIT) |
yihui | 0:5d56064bc4c3 | 8 | * |
yihui | 0:5d56064bc4c3 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
yihui | 0:5d56064bc4c3 | 10 | * of this software and associated documentation files (the "Software"), to deal |
yihui | 0:5d56064bc4c3 | 11 | * in the Software without restriction, including without limitation the rights |
yihui | 0:5d56064bc4c3 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yihui | 0:5d56064bc4c3 | 13 | * copies of the Software, and to permit persons to whom the Software is |
yihui | 0:5d56064bc4c3 | 14 | * furnished to do so, subject to the following conditions: |
yihui | 0:5d56064bc4c3 | 15 | * |
yihui | 0:5d56064bc4c3 | 16 | * The above copyright notice and this permission notice shall be included in |
yihui | 0:5d56064bc4c3 | 17 | * all copies or substantial portions of the Software. |
yihui | 0:5d56064bc4c3 | 18 | * |
yihui | 0:5d56064bc4c3 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yihui | 0:5d56064bc4c3 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yihui | 0:5d56064bc4c3 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yihui | 0:5d56064bc4c3 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yihui | 0:5d56064bc4c3 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yihui | 0:5d56064bc4c3 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yihui | 0:5d56064bc4c3 | 25 | * THE SOFTWARE. |
yihui | 0:5d56064bc4c3 | 26 | */ |
yihui | 0:5d56064bc4c3 | 27 | |
yihui | 0:5d56064bc4c3 | 28 | |
yihui | 0:5d56064bc4c3 | 29 | #include "max31855.h" |
yihui | 0:5d56064bc4c3 | 30 | |
yihui | 0:5d56064bc4c3 | 31 | #define LOG(args...) printf(args) |
yihui | 0:5d56064bc4c3 | 32 | |
yihui | 0:5d56064bc4c3 | 33 | MAX31855::MAX31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) |
yihui | 0:5d56064bc4c3 | 34 | { |
yihui | 0:5d56064bc4c3 | 35 | thermocoupleT = 0.0 / 0.0; |
yihui | 0:5d56064bc4c3 | 36 | internalT = 0.0 / 0.0; |
yihui | 0:5d56064bc4c3 | 37 | lastReadTime = 0; |
yihui | 0:5d56064bc4c3 | 38 | } |
yihui | 0:5d56064bc4c3 | 39 | |
yihui | 0:5d56064bc4c3 | 40 | float MAX31855::read(temperature_type_t type) { |
yihui | 0:5d56064bc4c3 | 41 | uint8_t buf[4]; |
yihui | 0:5d56064bc4c3 | 42 | |
yihui | 0:5d56064bc4c3 | 43 | uint32_t time = us_ticker_read(); |
yihui | 0:5d56064bc4c3 | 44 | uint32_t duration = time - lastReadTime; |
yihui | 0:5d56064bc4c3 | 45 | if (duration > 250000) { // more than 250ms |
yihui | 0:5d56064bc4c3 | 46 | //Set CS low to start transmission (interrupts conversion) |
yihui | 0:5d56064bc4c3 | 47 | ncs = 0; |
yihui | 0:5d56064bc4c3 | 48 | |
yihui | 0:5d56064bc4c3 | 49 | //Read in Probe tempeature |
yihui | 0:5d56064bc4c3 | 50 | for (int i = 0; i < 4; i++) { |
yihui | 0:5d56064bc4c3 | 51 | buf[i] = spi.write(0); |
yihui | 0:5d56064bc4c3 | 52 | } |
yihui | 0:5d56064bc4c3 | 53 | |
yihui | 0:5d56064bc4c3 | 54 | //Set CS high to stop transmission (restarts conversion) |
yihui | 0:5d56064bc4c3 | 55 | ncs = 1; |
yihui | 0:5d56064bc4c3 | 56 | |
yihui | 0:5d56064bc4c3 | 57 | if (buf[1] & 1) { |
yihui | 0:5d56064bc4c3 | 58 | uint8_t fault = buf[3] & 0x7; |
yihui | 0:5d56064bc4c3 | 59 | if (fault & 0x1) { |
yihui | 0:5d56064bc4c3 | 60 | LOG("The thermocouple is open (no connections).\r\n"); |
yihui | 0:5d56064bc4c3 | 61 | } |
yihui | 0:5d56064bc4c3 | 62 | if (fault & 0x2) { |
yihui | 0:5d56064bc4c3 | 63 | LOG("The thermocouple is short-circuited to GND.\r\n"); |
yihui | 0:5d56064bc4c3 | 64 | } |
yihui | 0:5d56064bc4c3 | 65 | if (fault & 0x4) { |
yihui | 0:5d56064bc4c3 | 66 | LOG("The thermocouple is short-circuited to VCC.\r\n"); |
yihui | 0:5d56064bc4c3 | 67 | } |
yihui | 0:5d56064bc4c3 | 68 | return 0.0 / 0.0; |
yihui | 0:5d56064bc4c3 | 69 | } |
yihui | 0:5d56064bc4c3 | 70 | |
yihui | 0:5d56064bc4c3 | 71 | int t1 = ((buf[0] & 0x7F) << 6) + (buf[1] >> 2); |
yihui | 0:5d56064bc4c3 | 72 | if (buf[0] & 0x80) { // sign bit |
yihui | 0:5d56064bc4c3 | 73 | t1 = -t1; |
yihui | 0:5d56064bc4c3 | 74 | } |
yihui | 0:5d56064bc4c3 | 75 | thermocoupleT = t1 * 0.25; |
yihui | 0:5d56064bc4c3 | 76 | |
yihui | 0:5d56064bc4c3 | 77 | int t2 = ((buf[2] & 0x7F) << 4) + (buf[3] >> 4); |
yihui | 0:5d56064bc4c3 | 78 | if (buf[2] & 0x80) { |
yihui | 0:5d56064bc4c3 | 79 | t2 = -t2; |
yihui | 0:5d56064bc4c3 | 80 | } |
yihui | 0:5d56064bc4c3 | 81 | internalT = t2 * 0.0625; |
yihui | 0:5d56064bc4c3 | 82 | } |
yihui | 0:5d56064bc4c3 | 83 | |
yihui | 0:5d56064bc4c3 | 84 | if (THERMOCOUPLE_T == type) { |
yihui | 0:5d56064bc4c3 | 85 | return thermocoupleT; |
yihui | 0:5d56064bc4c3 | 86 | } else { |
yihui | 0:5d56064bc4c3 | 87 | return internalT; |
yihui | 0:5d56064bc4c3 | 88 | } |
yihui | 0:5d56064bc4c3 | 89 | } |
yihui | 0:5d56064bc4c3 | 90 | |
yihui | 0:5d56064bc4c3 | 91 |