LM74 temperature sensor Library on SPI I/F

Committer:
atpolitis
Date:
Fri Mar 11 09:55:23 2011 +0000
Revision:
1:a29c5af6c1f0
Parent:
0:9293a89e30e2
correction for negative temperarures

Who changed what in which revision?

UserRevisionLine numberNew contents of line
atpolitis 0:9293a89e30e2 1 /* mbed tmpLM74 Library, for a LM74 based digital thermometer
atpolitis 0:9293a89e30e2 2 * Copyright (c) 2011, atpolitis, http://mbed.org
atpolitis 0:9293a89e30e2 3 *
atpolitis 0:9293a89e30e2 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
atpolitis 0:9293a89e30e2 5 * of this software and associated documentation files (the "Software"), to deal
atpolitis 0:9293a89e30e2 6 * in the Software without restriction, including without limitation the rights
atpolitis 0:9293a89e30e2 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
atpolitis 0:9293a89e30e2 8 * copies of the Software, and to permit persons to whom the Software is
atpolitis 0:9293a89e30e2 9 * furnished to do so, subject to the following conditions:
atpolitis 0:9293a89e30e2 10 *
atpolitis 0:9293a89e30e2 11 * The above copyright notice and this permission notice shall be included in
atpolitis 0:9293a89e30e2 12 * all copies or substantial portions of the Software.
atpolitis 0:9293a89e30e2 13 *
atpolitis 0:9293a89e30e2 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
atpolitis 0:9293a89e30e2 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
atpolitis 0:9293a89e30e2 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
atpolitis 0:9293a89e30e2 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
atpolitis 0:9293a89e30e2 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
atpolitis 0:9293a89e30e2 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
atpolitis 0:9293a89e30e2 20 * THE SOFTWARE.
atpolitis 0:9293a89e30e2 21 */
atpolitis 0:9293a89e30e2 22
atpolitis 0:9293a89e30e2 23 #ifndef MBED_TMPLM74_H
atpolitis 0:9293a89e30e2 24 #define MBED_TMPLM74_H
atpolitis 0:9293a89e30e2 25
atpolitis 0:9293a89e30e2 26 #include "mbed.h"
atpolitis 0:9293a89e30e2 27
atpolitis 0:9293a89e30e2 28 #define INVALID_LM74_TEMP 0X961
atpolitis 0:9293a89e30e2 29 #define MAX_LM74_TEMP (INVALID_LM74_TEMP-1)*0.0625
atpolitis 0:9293a89e30e2 30
atpolitis 0:9293a89e30e2 31 /** An LM74 based digital thermometer
atpolitis 0:9293a89e30e2 32 *
atpolitis 0:9293a89e30e2 33 * Currently supports SPI peripheral interface
atpolitis 0:9293a89e30e2 34 *
atpolitis 0:9293a89e30e2 35 * @code
atpolitis 0:9293a89e30e2 36 * #include "mbed.h"
atpolitis 0:9293a89e30e2 37 * #include "tmpLM74.h"
atpolitis 0:9293a89e30e2 38 *
atpolitis 0:9293a89e30e2 39 * Serial pc(USBTX, USBRX); // tx, rx
atpolitis 0:9293a89e30e2 40 * DigitalOut myled(LED1);
atpolitis 0:9293a89e30e2 41 *
atpolitis 0:9293a89e30e2 42 * TmpLM74 Temp74(p5, p6, p7, p8);
atpolitis 0:9293a89e30e2 43 *
atpolitis 0:9293a89e30e2 44 * // mbed connections:
atpolitis 0:9293a89e30e2 45 * // mosi(p5) to LM74 pin 1, through 10k
atpolitis 0:9293a89e30e2 46 * // miso(p6) to LM74 pin 1
atpolitis 0:9293a89e30e2 47 * // sclk(p7) to LM74 pin 2
atpolitis 0:9293a89e30e2 48 * // miso(p8) to LM74 pin 7
atpolitis 0:9293a89e30e2 49 *
atpolitis 0:9293a89e30e2 50 * //TmpLM74.startLM74(); // CAN be used, but NOT necessary (invoked within readTemp(), if needed)
atpolitis 0:9293a89e30e2 51 *
atpolitis 0:9293a89e30e2 52 * int main() {
atpolitis 0:9293a89e30e2 53 *
atpolitis 0:9293a89e30e2 54 * float TempC;
atpolitis 0:9293a89e30e2 55 *
atpolitis 0:9293a89e30e2 56 * pc.printf("HELLO!, testing LM74 temperature sensor ...\n\r");
atpolitis 0:9293a89e30e2 57 *
atpolitis 0:9293a89e30e2 58 * for (int i = 0; i < 10; i++){
atpolitis 0:9293a89e30e2 59 * wait(2);
atpolitis 0:9293a89e30e2 60 * TempC = Temp74.readTemp();
atpolitis 0:9293a89e30e2 61 * pc.printf("Temperature = ");
atpolitis 0:9293a89e30e2 62 * if(TempC > MAX_LM74_TEMP) { // check whether temp is valid
atpolitis 0:9293a89e30e2 63 * pc.printf(" ? *C\n", TempC);
atpolitis 0:9293a89e30e2 64 * } else {
atpolitis 0:9293a89e30e2 65 * pc.printf("%3.1f *C\n", TempC);
atpolitis 0:9293a89e30e2 66 * }
atpolitis 0:9293a89e30e2 67 * myled = !myled;
atpolitis 0:9293a89e30e2 68 * }
atpolitis 0:9293a89e30e2 69 * Temp74.shutLM74down();
atpolitis 0:9293a89e30e2 70 * while(1){
atpolitis 0:9293a89e30e2 71 * wait(1);
atpolitis 0:9293a89e30e2 72 * myled = !myled;
atpolitis 0:9293a89e30e2 73 * }
atpolitis 0:9293a89e30e2 74 * }
atpolitis 0:9293a89e30e2 75 * @endcode
atpolitis 0:9293a89e30e2 76 */
atpolitis 0:9293a89e30e2 77 // ********************************************************************
atpolitis 0:9293a89e30e2 78 // in this version, class TmpLM74 is derived from class SPI
atpolitis 0:9293a89e30e2 79 // and inherits its members
atpolitis 0:9293a89e30e2 80 //
atpolitis 0:9293a89e30e2 81 // ********************************************************************
atpolitis 0:9293a89e30e2 82
atpolitis 0:9293a89e30e2 83 class TmpLM74: public SPI {
atpolitis 0:9293a89e30e2 84
atpolitis 0:9293a89e30e2 85 public:
atpolitis 0:9293a89e30e2 86
atpolitis 0:9293a89e30e2 87 TmpLM74(PinName mosi, PinName miso, PinName sclk, PinName csLM74);
atpolitis 0:9293a89e30e2 88
atpolitis 0:9293a89e30e2 89 float readTemp(void);
atpolitis 0:9293a89e30e2 90 void startLM74(void);
atpolitis 0:9293a89e30e2 91 void shutLM74down(void);
atpolitis 0:9293a89e30e2 92
atpolitis 0:9293a89e30e2 93 protected:
atpolitis 0:9293a89e30e2 94 DigitalOut _csLM74;
atpolitis 0:9293a89e30e2 95 };
atpolitis 0:9293a89e30e2 96
atpolitis 0:9293a89e30e2 97 #endif