|
| 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 | +} |
0 commit comments