1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
10 Driver for NXP's LM75B I2C temperature sensor. This temperature
11 sensor has an accuracy of 0.125°C, and returns a temperature value
12 in degrees celsius where each unit is equal to 0.125°C. For example,
13 if the temperature reading is 198, it means that the temperature in
14 degree celsius is: 198 / 8 = 24.75°C.
19 #include "core/cpu/cpu.h"
20 #include "drivers/sensors/lm75b/lm75b.h"
28 // Initialise the LM75B
33 // Get the current temperature (in 0.125°C units)
34 lm75bGetTemperature(&temp);
36 // Multiply value by 125 for fixed-point math (0.125°C per unit)
39 // Use modulus operator to display decimal value
40 printf("Current Temperature: %d.%d C\n", temp / 1000, temp % 1000);
42 // Alternatively, you could also use floating point math, though
43 // this will result in larger compiled code if you add in floating
44 // point support for printf, etc.
46 // float tempFloat = 0.0F;
47 // lm75bGetTemperature(&temp);
48 // tempFloat = (float)temp / 8.0F;
55 Software License Agreement (BSD License)
57 Copyright (c) 2010, microBuilder SARL
60 Redistribution and use in source and binary forms, with or without
61 modification, are permitted provided that the following conditions are met:
62 1. Redistributions of source code must retain the above copyright
63 notice, this list of conditions and the following disclaimer.
64 2. Redistributions in binary form must reproduce the above copyright
65 notice, this list of conditions and the following disclaimer in the
66 documentation and/or other materials provided with the distribution.
67 3. Neither the name of the copyright holders nor the
68 names of its contributors may be used to endorse or promote products
69 derived from this software without specific prior written permission.
71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
72 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
73 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
75 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
76 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
77 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
78 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
79 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
80 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 /**************************************************************************/
86 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
87 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
88 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
92 static bool _lm75bInitialised
= false;
94 /**************************************************************************/
96 @brief Writes an 8 bit values over I2C
98 /**************************************************************************/
99 lm75bError_e
lm75bWrite8 (uint8_t reg
, uint32_t value
)
101 // Clear write buffers
102 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
104 I2CMasterBuffer
[i
] = 0x00;
109 I2CMasterBuffer
[0] = LM75B_ADDRESS
; // I2C device address
110 I2CMasterBuffer
[1] = reg
; // Command register
111 I2CMasterBuffer
[2] = (value
& 0xFF); // Value to write
113 return LM75B_ERROR_OK
;
116 /**************************************************************************/
118 @brief Reads a 16 bit values over I2C
120 /**************************************************************************/
121 lm75bError_e
lm75bRead16(uint8_t reg
, int32_t *value
)
123 // Clear write buffers
124 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
126 I2CMasterBuffer
[i
] = 0x00;
131 I2CMasterBuffer
[0] = LM75B_ADDRESS
; // I2C device address
132 I2CMasterBuffer
[1] = reg
; // Command register
133 // Append address w/read bit
134 I2CMasterBuffer
[2] = LM75B_ADDRESS
| LM75B_READBIT
;
137 // Shift values to create properly formed integer
138 *value
= ((I2CSlaveBuffer
[0] << 8) | I2CSlaveBuffer
[1]) >> 5;
140 // Sign extend negative numbers
141 if (I2CSlaveBuffer
[0] & 0x80)
144 *value
|= 0xFFFFFC00;
146 return LM75B_ERROR_OK
;
149 /**************************************************************************/
151 @brief Initialises the I2C block
153 /**************************************************************************/
154 lm75bError_e
lm75bInit(void)
157 if (i2cInit(I2CMASTER
) == false)
159 return LM75B_ERROR_I2CINIT
; /* Fatal error */
162 _lm75bInitialised
= true;
164 return LM75B_ERROR_OK
;
166 // Set device to shutdown mode by default (saves power)
167 return lm75bConfigWrite (LM75B_CONFIG_SHUTDOWN_SHUTDOWN
);
170 /**************************************************************************/
172 @brief Reads the current temperature from the LM75B
174 @note This method will assign a signed 32-bit value (int32) to 'temp',
175 where each unit represents +/- 0.125°C. To convert the numeric
176 value to degrees celsius, you must divide the value of 'temp'
177 by 8. This conversion is not done automatically, since you may
178 or may not want to use floating point math for the calculations.
180 /**************************************************************************/
181 lm75bError_e
lm75bGetTemperature (int32_t *temp
)
183 if (!_lm75bInitialised
) lm75bInit();
186 lm75bConfigWrite (LM75B_CONFIG_SHUTDOWN_POWERON
);
189 lm75bError_e error
= LM75B_ERROR_OK
;
190 error
= lm75bRead16 (LM75B_REGISTER_TEMPERATURE
, temp
);
192 // Shut device back down
193 lm75bConfigWrite (LM75B_CONFIG_SHUTDOWN_SHUTDOWN
);
198 /**************************************************************************/
200 @brief Writes the supplied 8-bit value to the LM75B config register
202 /**************************************************************************/
203 lm75bError_e
lm75bConfigWrite (uint8_t configValue
)
205 if (!_lm75bInitialised
) lm75bInit();
207 return lm75bWrite8 (LM75B_REGISTER_CONFIGURATION
, configValue
);