Skip to content

Commit 8e74bf6

Browse files
committed
lib: Add main LM75A temperature sensor library
Add the most important part of this project (the lib). A keywords.txt file has been added to improve lib display in arduino interface.
1 parent 97bac67 commit 8e74bf6

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

LM75A/LM75A.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* \brief I2C LM75A temperature sensor library (implementation)
3+
*
4+
* \author Quentin Comte-Gaz <[email protected]>
5+
* \date 8 July 2016
6+
* \license MIT License (contact me if too restrictive)
7+
* \copyright Copyright (c) 2016 Quentin Comte-Gaz
8+
* \version 1.0
9+
*/
10+
11+
#include "LM75A.h"
12+
#include <Wire.h>
13+
14+
namespace LM75AConstValues
15+
{
16+
17+
const int LM75A_BASE_ADDRESS = 0x48;
18+
19+
const float LM75A_DEGREES_RESOLUTION = 0.125;
20+
21+
const int LM75A_REG_ADDR_TEMP = 0;
22+
//const int LM75A_REG_ADDR_CONF = 1; // Not used for now
23+
//const int LM57A_REG_ADDR_THYST = 2; // Not used for now
24+
//const int LM57A_REG_ADDR_TOS = 3; // Not used for now
25+
26+
}
27+
28+
using namespace LM75AConstValues;
29+
30+
LM75A::LM75A(bool A0_value, bool A1_value, bool A2_value)
31+
{
32+
_i2c_device_address = LM75A_BASE_ADDRESS;
33+
34+
if (A0_value) {
35+
_i2c_device_address += 1;
36+
}
37+
38+
if (A1_value) {
39+
_i2c_device_address += 2;
40+
}
41+
42+
if (A2_value) {
43+
_i2c_device_address += 4;
44+
}
45+
46+
Wire.begin();
47+
}
48+
49+
float LM75A::fahrenheitToDegrees(float temperature_in_fahrenheit)
50+
{
51+
return ((temperature_in_fahrenheit - 32.0) / 1.8);
52+
}
53+
54+
float LM75A::degreesToFahrenheit(float temperature_in_degrees)
55+
{
56+
return ((temperature_in_degrees * 1.8) + 32.0);
57+
}
58+
59+
float LM75A::getTemperatureInFahrenheit() const
60+
{
61+
return degreesToFahrenheit(getTemperatureInDegrees());
62+
}
63+
64+
float LM75A::getTemperatureInDegrees() const
65+
{
66+
uint16_t real_result = INVALID_LM75A_TEMPERATURE;
67+
uint16_t i2c_received = 0;
68+
69+
// Go to temperature data register
70+
Wire.beginTransmission(_i2c_device_address);
71+
Wire.write(LM75A_REG_ADDR_TEMP);
72+
if(Wire.endTransmission()) {
73+
// Transmission error
74+
return real_result;
75+
}
76+
77+
// Get content
78+
if (Wire.requestFrom(_i2c_device_address, 2)) {
79+
Wire.readBytes((uint8_t*)&i2c_received, 2);
80+
} else {
81+
// Can't read temperature
82+
return real_result;
83+
}
84+
85+
// Modify the value (only 11 MSB are relevant if swapped)
86+
uint16_t refactored_value;
87+
uint8_t* ptr = (uint8_t*)&refactored_value;
88+
89+
// Swap bytes
90+
*ptr = *((uint8_t*)&i2c_received + 1);
91+
*(ptr + 1) = *(uint8_t*)&i2c_received;
92+
93+
// Shift data (left-aligned)
94+
refactored_value >>= 5;
95+
96+
// Relocate negative bit (11th bit to 16th bit)
97+
if (refactored_value & 0x0400) {
98+
refactored_value &= 0x03FF;
99+
refactored_value |= 0x8000;
100+
}
101+
102+
// Real value can be calculated with sensor resolution
103+
real_result = (float)refactored_value * LM75A_DEGREES_RESOLUTION;
104+
105+
return (float)refactored_value * LM75A_DEGREES_RESOLUTION;
106+
}

LM75A/LM75A.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* \brief I2C LM75A temperature sensor library
3+
*
4+
* \author Quentin Comte-Gaz <[email protected]>
5+
* \date 8 July 2016
6+
* \license MIT License (contact me if too restrictive)
7+
* \copyright Copyright (c) 2016 Quentin Comte-Gaz
8+
* \version 1.0
9+
*/
10+
11+
#ifndef LM75A_h
12+
#define LM75A_h
13+
14+
#if defined(ARDUINO) && ARDUINO >= 100
15+
#include "Arduino.h"
16+
#else
17+
#include "WProgram.h"
18+
#endif
19+
20+
#define INVALID_LM75A_TEMPERATURE 1000
21+
22+
class LM75A
23+
{
24+
public:
25+
/*!
26+
* \brief LM75A Initialize I2C LM75A Temperature sensor instance
27+
* \param A0_value (bool) A0 Pin value (used for address)
28+
* \param A1_value (bool) A1 Pin value (used for address)
29+
* \param A2_value (bool) A2 Pin value (used for address)
30+
*/
31+
LM75A(bool A0_value = false, bool A1_value = false, bool A2_value = false);
32+
33+
/*!
34+
* \brief getTemperatureInDegrees Get temperature from LM75A sensor in degrees
35+
* \return (float) Sensor temperature in degrees (return INVALID_LM75A_TEMPERATURE if error happened)
36+
*/
37+
float getTemperatureInDegrees() const;
38+
39+
/*!
40+
* \brief getTemperatureInFahrenheit Get temperature from LM75A sensor in fahrenheit
41+
* \return (float) Sensor temperature in fahrenheit (return INVALID_LM75A_TEMPERATURE if error happened)
42+
*/
43+
float getTemperatureInFahrenheit() const;
44+
45+
/*!
46+
* \brief fahrenheitToDegrees Convert temperature from fahrenheit to degrees
47+
*/
48+
static float fahrenheitToDegrees(float temperature_in_fahrenheit);
49+
50+
/*!
51+
* \brief degreesToFahrenheit Convert temperature from degrees to fahrenheit
52+
*/
53+
static float degreesToFahrenheit(float temperature_in_degrees);
54+
55+
private:
56+
int _i2c_device_address;
57+
};
58+
59+
#endif //LM75A_h
60+

LM75A/keywords.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LM75A KEYWORD1
2+
getTemperatureInDegrees KEYWORD2
3+
getTemperatureInFahrenheit KEYWORD2
4+
fahrenheitToDegrees KEYWORD2
5+
degreesToFahrenheit KEYWORD2
6+
INVALID_LM75A_TEMPERATURE KEYWORD2

0 commit comments

Comments
 (0)