初始化提交
This commit is contained in:
114
arduino-cli/libraries/LM75A/LM75A.cpp
Normal file
114
arduino-cli/libraries/LM75A/LM75A.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* \brief I2C LM75A temperature sensor library (implementation)
|
||||
*
|
||||
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
|
||||
* \date 8 July 2016 & 14 January 2018
|
||||
* \license MIT License (contact me if too restrictive)
|
||||
* \copyright Copyright (c) 2016 Quentin Comte-Gaz
|
||||
* \version 1.1
|
||||
*/
|
||||
|
||||
#include "LM75A.h"
|
||||
#include <Wire.h>
|
||||
|
||||
namespace LM75AConstValues
|
||||
{
|
||||
|
||||
const int LM75A_BASE_ADDRESS = 0x48;
|
||||
|
||||
const float LM75A_DEGREES_RESOLUTION = 0.125;
|
||||
|
||||
const int LM75A_REG_ADDR_TEMP = 0;
|
||||
//const int LM75A_REG_ADDR_CONF = 1; // Not used for now
|
||||
//const int LM57A_REG_ADDR_THYST = 2; // Not used for now
|
||||
//const int LM57A_REG_ADDR_TOS = 3; // Not used for now
|
||||
|
||||
}
|
||||
|
||||
using namespace LM75AConstValues;
|
||||
|
||||
LM75A::LM75A(bool A0_value, bool A1_value, bool A2_value)
|
||||
{
|
||||
_i2c_device_address = LM75A_BASE_ADDRESS;
|
||||
|
||||
if (A0_value) {
|
||||
_i2c_device_address += 1;
|
||||
}
|
||||
|
||||
if (A1_value) {
|
||||
_i2c_device_address += 2;
|
||||
}
|
||||
|
||||
if (A2_value) {
|
||||
_i2c_device_address += 4;
|
||||
}
|
||||
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
float LM75A::fahrenheitToDegrees(float temperature_in_fahrenheit)
|
||||
{
|
||||
return ((temperature_in_fahrenheit - 32.0) / 1.8);
|
||||
}
|
||||
|
||||
float LM75A::degreesToFahrenheit(float temperature_in_degrees)
|
||||
{
|
||||
return ((temperature_in_degrees * 1.8) + 32.0);
|
||||
}
|
||||
|
||||
float LM75A::getTemperatureInFahrenheit() const
|
||||
{
|
||||
float temperature_in_degrees = getTemperatureInDegrees();
|
||||
|
||||
if (temperature_in_degrees == INVALID_LM75A_TEMPERATURE) {
|
||||
return INVALID_LM75A_TEMPERATURE;
|
||||
}
|
||||
|
||||
return degreesToFahrenheit(temperature_in_degrees);
|
||||
}
|
||||
|
||||
float LM75A::getTemperatureInDegrees() const
|
||||
{
|
||||
uint16_t real_result = INVALID_LM75A_TEMPERATURE;
|
||||
uint16_t i2c_received = 0;
|
||||
|
||||
// Go to temperature data register
|
||||
Wire.beginTransmission(_i2c_device_address);
|
||||
Wire.write(LM75A_REG_ADDR_TEMP);
|
||||
if(Wire.endTransmission()) {
|
||||
// Transmission error
|
||||
return real_result;
|
||||
}
|
||||
|
||||
// Get content
|
||||
if (Wire.requestFrom(_i2c_device_address, 2)) {
|
||||
Wire.readBytes((uint8_t*)&i2c_received, 2);
|
||||
} else {
|
||||
// Can't read temperature
|
||||
return real_result;
|
||||
}
|
||||
|
||||
// Modify the value (only 11 MSB are relevant if swapped)
|
||||
int16_t refactored_value;
|
||||
uint8_t* ptr = (uint8_t*)&refactored_value;
|
||||
|
||||
// Swap bytes
|
||||
*ptr = *((uint8_t*)&i2c_received + 1);
|
||||
*(ptr + 1) = *(uint8_t*)&i2c_received;
|
||||
|
||||
// Shift data (left-aligned)
|
||||
refactored_value >>= 5;
|
||||
|
||||
float real_value;
|
||||
if (refactored_value & 0x0400) {
|
||||
// When sign bit is set, set upper unused bits, then 2's complement
|
||||
refactored_value |= 0xf800;
|
||||
refactored_value = ~refactored_value + 1;
|
||||
real_value = (float)refactored_value * (-1) * LM75A_DEGREES_RESOLUTION;
|
||||
}
|
||||
else {
|
||||
real_value = (float)refactored_value * LM75A_DEGREES_RESOLUTION;
|
||||
}
|
||||
|
||||
return real_value;
|
||||
}
|
||||
60
arduino-cli/libraries/LM75A/LM75A.h
Normal file
60
arduino-cli/libraries/LM75A/LM75A.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* \brief I2C LM75A temperature sensor library
|
||||
*
|
||||
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
|
||||
* \date 8 July 2016 & 14 January 2018
|
||||
* \license MIT License (contact me if too restrictive)
|
||||
* \copyright Copyright (c) 2016 Quentin Comte-Gaz
|
||||
* \version 1.1
|
||||
*/
|
||||
|
||||
#ifndef LM75A_h
|
||||
#define LM75A_h
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#define INVALID_LM75A_TEMPERATURE 1000
|
||||
|
||||
class LM75A
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* \brief LM75A Initialize I2C LM75A Temperature sensor instance
|
||||
* \param A0_value (bool) A0 Pin value (used for address)
|
||||
* \param A1_value (bool) A1 Pin value (used for address)
|
||||
* \param A2_value (bool) A2 Pin value (used for address)
|
||||
*/
|
||||
LM75A(bool A0_value = false, bool A1_value = false, bool A2_value = false);
|
||||
|
||||
/*!
|
||||
* \brief getTemperatureInDegrees Get temperature from LM75A sensor in degrees
|
||||
* \return (float) Sensor temperature in degrees (return INVALID_LM75A_TEMPERATURE if error happened)
|
||||
*/
|
||||
float getTemperatureInDegrees() const;
|
||||
|
||||
/*!
|
||||
* \brief getTemperatureInFahrenheit Get temperature from LM75A sensor in fahrenheit
|
||||
* \return (float) Sensor temperature in fahrenheit (return INVALID_LM75A_TEMPERATURE if error happened)
|
||||
*/
|
||||
float getTemperatureInFahrenheit() const;
|
||||
|
||||
/*!
|
||||
* \brief fahrenheitToDegrees Convert temperature from fahrenheit to degrees
|
||||
*/
|
||||
static float fahrenheitToDegrees(float temperature_in_fahrenheit);
|
||||
|
||||
/*!
|
||||
* \brief degreesToFahrenheit Convert temperature from degrees to fahrenheit
|
||||
*/
|
||||
static float degreesToFahrenheit(float temperature_in_degrees);
|
||||
|
||||
private:
|
||||
int _i2c_device_address;
|
||||
};
|
||||
|
||||
#endif //LM75A_h
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* \brief Show temperature in degrees and fahrenheit every second
|
||||
*
|
||||
* \author Quentin Comte-Gaz <quentin@comte-gaz.com>
|
||||
* \date 8 July 2016
|
||||
* \license MIT License (contact me if too restrictive)
|
||||
* \copyright Copyright (c) 2016 Quentin Comte-Gaz
|
||||
* \version 1.0
|
||||
*/
|
||||
|
||||
#include <LM75A.h>
|
||||
|
||||
// Create I2C LM75A instance
|
||||
LM75A lm75a_sensor(false, //A0 LM75A pin state
|
||||
false, //A1 LM75A pin state
|
||||
false); //A2 LM75A pin state
|
||||
// Equivalent to "LM75A lm75a_sensor;"
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println("Temperatures will be displayed every second:");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
float temperature_in_degrees = lm75a_sensor.getTemperatureInDegrees();
|
||||
|
||||
if (temperature_in_degrees == INVALID_LM75A_TEMPERATURE) {
|
||||
Serial.println("Error while getting temperature");
|
||||
} else {
|
||||
Serial.print("Temperature: ");
|
||||
Serial.print(temperature_in_degrees);
|
||||
Serial.print(" degrees (");
|
||||
Serial.print(LM75A::degreesToFahrenheit(temperature_in_degrees));
|
||||
Serial.println(" fahrenheit)");
|
||||
}
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
6
arduino-cli/libraries/LM75A/keywords.txt
Normal file
6
arduino-cli/libraries/LM75A/keywords.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
LM75A KEYWORD1
|
||||
getTemperatureInDegrees KEYWORD2
|
||||
getTemperatureInFahrenheit KEYWORD2
|
||||
fahrenheitToDegrees KEYWORD2
|
||||
degreesToFahrenheit KEYWORD2
|
||||
INVALID_LM75A_TEMPERATURE KEYWORD2
|
||||
Reference in New Issue
Block a user