A feature complete driver for the LM75B temperature sensor from NXP.

Dependents:   app-board-TempAlarm LM75B IoTWorkshopSensors EduRobot ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LM75B.cpp Source File

LM75B.cpp

00001 /* LM75B Driver Library
00002  * Copyright (c) 2013 Neil Thiessen
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "LM75B.h"
00018 
00019 LM75B::LM75B(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
00020 {
00021     //Set the I2C bus frequency
00022     m_I2C.frequency(hz);
00023 }
00024 
00025 bool LM75B::open()
00026 {
00027     //Probe for the LM75B using a Zero Length Transfer
00028     if (!m_I2C.write(m_ADDR, NULL, 0)) {
00029         //Return success
00030         return true;
00031     } else {
00032         //Return failure
00033         return false;
00034     }
00035 }
00036 
00037 LM75B::PowerMode LM75B::powerMode()
00038 {
00039     //Read the 8-bit register value
00040     char value = read8(REG_CONF);
00041 
00042     //Return the status of the SHUTDOWN bit
00043     if (value & (1 << 0))
00044         return POWER_SHUTDOWN;
00045     else
00046         return POWER_NORMAL;
00047 }
00048 
00049 void LM75B::powerMode(PowerMode mode)
00050 {
00051     //Read the current 8-bit register value
00052     char value = read8(REG_CONF);
00053 
00054     //Set or clear the SHUTDOWN bit
00055     if (mode == POWER_SHUTDOWN)
00056         value |= (1 << 0);
00057     else
00058         value &= ~(1 << 0);
00059 
00060     //Write the value back out
00061     write8(REG_CONF, value);
00062 }
00063 
00064 LM75B::OSMode LM75B::osMode()
00065 {
00066     //Read the 8-bit register value
00067     char value = read8(REG_CONF);
00068 
00069     //Return the status of the OS_COMP_INT bit
00070     if (value & (1 << 1))
00071         return OS_INTERRUPT;
00072     else
00073         return OS_COMPARATOR;
00074 }
00075 
00076 void LM75B::osMode(OSMode mode)
00077 {
00078     //Read the current 8-bit register value
00079     char value = read8(REG_CONF);
00080 
00081     //Set or clear the OS_COMP_INT bit
00082     if (mode == OS_INTERRUPT)
00083         value |= (1 << 1);
00084     else
00085         value &= ~(1 << 1);
00086 
00087     //Write the value back out
00088     write8(REG_CONF, value);
00089 }
00090 
00091 LM75B::OSPolarity LM75B::osPolarity()
00092 {
00093     //Read the 8-bit register value
00094     char value = read8(REG_CONF);
00095 
00096     //Return the status of the OS_POL bit
00097     if (value & (1 << 2))
00098         return OS_ACTIVE_HIGH;
00099     else
00100         return OS_ACTIVE_LOW;
00101 }
00102 
00103 void LM75B::osPolarity(OSPolarity polarity)
00104 {
00105     //Read the current 8-bit register value
00106     char value = read8(REG_CONF);
00107 
00108     //Set or clear the OS_POL bit
00109     if (polarity == OS_ACTIVE_HIGH)
00110         value |= (1 << 2);
00111     else
00112         value &= ~(1 << 2);
00113 
00114     //Write the value back out
00115     write8(REG_CONF, value);
00116 }
00117 
00118 LM75B::OSFaultQueue LM75B::osFaultQueue()
00119 {
00120     //Read the 8-bit register value
00121     char value = read8(REG_CONF);
00122 
00123     //Return the status of the OS_F_QUE bits
00124     if ((value & (1 << 3)) && (value & (1 << 4)))
00125         return OS_FAULT_QUEUE_6;
00126     else if (!(value & (1 << 3)) && (value & (1 << 4)))
00127         return OS_FAULT_QUEUE_4;
00128     else if ((value & (1 << 3)) && !(value & (1 << 4)))
00129         return OS_FAULT_QUEUE_2;
00130     else
00131         return OS_FAULT_QUEUE_1;
00132 }
00133 
00134 void LM75B::osFaultQueue(OSFaultQueue queue)
00135 {
00136     //Read the current 8-bit register value
00137     char value = read8(REG_CONF);
00138 
00139     //Clear the old OS_F_QUE bits
00140     value &= ~(3 << 3);
00141 
00142     //Set the new OS_F_QUE bits
00143     if (queue == OS_FAULT_QUEUE_2)
00144         value |= (1 << 3);
00145     else if (queue == OS_FAULT_QUEUE_4)
00146         value |= (2 << 3);
00147     else if (queue == OS_FAULT_QUEUE_6)
00148         value |= (3 << 3);
00149 
00150     //Write the value back out
00151     write8(REG_CONF, value);
00152 }
00153 
00154 float LM75B::alertTemp()
00155 {
00156     //Use the 9-bit helper to read the TOS register
00157     return readAlertTempHelper(REG_TOS);
00158 }
00159 
00160 void LM75B::alertTemp(float temp)
00161 {
00162     //Use the 9-bit helper to write to the TOS register
00163     return writeAlertTempHelper(REG_TOS, temp);
00164 }
00165 
00166 float LM75B::alertHyst()
00167 {
00168     //Use the 9-bit helper to read the THYST register
00169     return readAlertTempHelper(REG_THYST);
00170 }
00171 
00172 void LM75B::alertHyst(float temp)
00173 {
00174     //Use the 9-bit helper to write to the THYST register
00175     return writeAlertTempHelper(REG_THYST, temp);
00176 }
00177 
00178 float LM75B::temp()
00179 {
00180     //Signed return value
00181     short value;
00182 
00183     //Read the 11-bit raw temperature value
00184     value = read16(REG_TEMP) >> 5;
00185 
00186     //Sign extend negative numbers
00187     if (value & (1 << 10))
00188         value |= 0xFC00;
00189 
00190     //Return the temperature in °C
00191     return value * 0.125;
00192 }
00193 
00194 #ifdef MBED_OPERATORS
00195 LM75B::operator float()
00196 {
00197     //Return the current temperature reading
00198     return temp();
00199 }
00200 #endif
00201 
00202 char LM75B::read8(char reg)
00203 {
00204     //Select the register
00205     m_I2C.write(m_ADDR, &reg, 1, true);
00206 
00207     //Read the 8-bit register
00208     m_I2C.read(m_ADDR, &reg, 1);
00209 
00210     //Return the byte
00211     return reg;
00212 }
00213 
00214 void LM75B::write8(char reg, char data)
00215 {
00216     //Create a temporary buffer
00217     char buff[2];
00218 
00219     //Load the register address and 8-bit data
00220     buff[0] = reg;
00221     buff[1] = data;
00222 
00223     //Write the data
00224     m_I2C.write(m_ADDR, buff, 2);
00225 }
00226 
00227 unsigned short LM75B::read16(char reg)
00228 {
00229     //Create a temporary buffer
00230     char buff[2];
00231 
00232     //Select the register
00233     m_I2C.write(m_ADDR, &reg, 1, true);
00234 
00235     //Read the 16-bit register
00236     m_I2C.read(m_ADDR, buff, 2);
00237 
00238     //Return the combined 16-bit value
00239     return (buff[0] << 8) | buff[1];
00240 }
00241 
00242 void LM75B::write16(char reg, unsigned short data)
00243 {
00244     //Create a temporary buffer
00245     char buff[3];
00246 
00247     //Load the register address and 16-bit data
00248     buff[0] = reg;
00249     buff[1] = data >> 8;
00250     buff[2] = data;
00251 
00252     //Write the data
00253     m_I2C.write(m_ADDR, buff, 3);
00254 }
00255 
00256 float LM75B::readAlertTempHelper(char reg)
00257 {
00258     //Signed return value
00259     short value;
00260 
00261     //Read the 9-bit raw temperature value
00262     value = read16(reg) >> 7;
00263 
00264     //Sign extend negative numbers
00265     if (value & (1 << 8))
00266         value |= 0xFF00;
00267 
00268     //Return the temperature in °C
00269     return value * 0.5;
00270 }
00271 
00272 void LM75B::writeAlertTempHelper(char reg, float temp)
00273 {
00274     //Range limit temp
00275     if (temp < -55.0)
00276         temp = -55.0;
00277     else if (temp > 125.0)
00278         temp = 125.0;
00279 
00280     //Extract and shift the signed integer
00281     short value = temp * 2;
00282     value <<= 7;
00283 
00284     //Send the new value
00285     write16(reg, value);
00286 }