LM74 temperature sensor Library on SPI I/F

TmpLM74.cpp

Committer:
atpolitis
Date:
2011-03-10
Revision:
0:9293a89e30e2
Child:
1:a29c5af6c1f0

File content as of revision 0:9293a89e30e2:


#include "mbed.h"
#include "TmpLM74.h"


TmpLM74::TmpLM74(PinName mosi, PinName miso, PinName sclk, PinName csLM74):
        SPI(mosi, miso, sclk), _csLM74(csLM74) {

       _csLM74 = 1;

        format(8,3);            // initialize SPI peripheral
        frequency(1000000);     // initialize SPI peripheral

        startLM74();
    }
    
float TmpLM74::readTemp(void){

    float realTemp;
    int16_t binTemp;
    uint16_t tempRegister;
    
    _csLM74 = 0;
    
    tempRegister = ((uint16_t) write(0xFF)) << 8;
    tempRegister += write(0xFF);
                    
    _csLM74 = 1;
    
    binTemp = tempRegister >> 3;
    
    if((tempRegister & 0xFFFC) == 0x8000) {       // Manufacturer's Device ID Register (power down)
        startLM74();
        binTemp = INVALID_LM74_TEMP;
    } else if ((tempRegister & 0x0004) == 0) {    // temp reading NOT ready
        binTemp = INVALID_LM74_TEMP;
    } else if (binTemp > 0x0FFF) {                // negative temperature
        binTemp &= 0xFFF;
        binTemp = ~binTemp;
    }
    realTemp = ((float) binTemp) * 0.0625;
    
    return realTemp;
}
//      ******************************************************************

void TmpLM74::startLM74(void){

    _csLM74 = 0;
    
    uint8_t dummy = write(0x00);
    dummy = write(0x00);
    dummy = write(0x00);
    dummy = write(0x00);            // this byte sets LM74 to continuous conversion
    
    _csLM74 = 1;
}
//      ******************************************************************

void TmpLM74::shutLM74down(void){

    _csLM74 = 0;
    
    uint8_t dummy = write(0xFF);
    dummy = write(0xFF);
    dummy = write(0xFF);        // these 2 bytes set LM74 to power down
    dummy = write(0xFF);        // these 2 bytes set LM74 to power down
    
    _csLM74 = 1;
}
//      ******************************************************************