Skip to content

Commit

Permalink
Merge pull request #5 from dzalf/library-cleanup
Browse files Browse the repository at this point in the history
Library cleanup. Added more examples
  • Loading branch information
dzalf authored Mar 12, 2023
2 parents 6da0933 + f4e3752 commit d025fb2
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 109 deletions.
53 changes: 49 additions & 4 deletions LM95071.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
Category: Sensors
Date: Oct 2018 - 0.1.5 Initial release
July 2020 - 1.1.0 General tidy up, more comments added
March 2023 = 1.2.0 Added more exmaples. Cleaned up comments
*/



#include "LM95071.h"

// Constructor
Expand Down Expand Up @@ -46,7 +45,7 @@ float LM95071::getTemperature(void) {
}

short LM95071::readTemp(int bytesToRead) {

byte inByte = 0;
short result;

Expand All @@ -62,21 +61,24 @@ short LM95071::readTemp(int bytesToRead) {
Serial.println(result, HEX);

}

if (bytesToRead > 0) {

result = result << 8;

inByte = SPI.transfer(CONTINUOUS_CONV);

result = result | inByte;

if (_debug) {
Serial.print("2nd byte = ");
Serial.println(inByte, HEX);
}

bytesToRead--;

}

SPI.endTransaction();

digitalWrite(_SelDev_Pin, HIGH);
Expand Down Expand Up @@ -109,3 +111,46 @@ float LM95071::celsiusConversion(short val) {

return result;
}

short LM95071::readID(){

byte inByte = 0;
short ID;

SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE1));

digitalWrite(_SelDev_Pin, LOW);
result = SPI.transfer(SHUTDOWN_MODE);
bytesToRead--;


if (_debug) {
Serial.print("1st byte = ");
Serial.println(result, HEX);
}

if (bytesToRead > 0) {

ID = ID << 8;

inByte = SPI.transfer(SHUTDOWN_MODE);

ID = ID | inByte;

if (_debug) {
Serial.print("2nd byte = ");
Serial.println(inByte, HEX);
}

bytesToRead--;

}

SPI.endTransaction();

digitalWrite(_SelDev_Pin, HIGH);

return ID;


}
2 changes: 2 additions & 0 deletions LM95071.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define MASK_XOR 0x03;
#define RESOLUTION 0.03125
#define CONTINUOUS_CONV 0x00
#define SHUTDOWN_MODE 0xFF
#define SHUT_DOWN 0xFF
#define BYTES_TO_READ 2
#define DEBUG_ON true
Expand All @@ -50,6 +51,7 @@ class LM95071 {

void begin(void);
float getTemperature(void);
short readID(void);

private: //these are internal access functions and variables

Expand Down
109 changes: 109 additions & 0 deletions examples/LM95071_ReadDeviceID/LM95071_ReadDeviceID.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@

/*
LM95071 SPI Temperature sensor
Example: Getting the device's ID during SHUTDOWN mode
Author: github.com/dzalf
Ver: 0.1
Date: June 2020
*/

#include "LM95071.h"

#define SERIAL_PLOTTER false

// Variables and constants
unsigned long pollTime;
unsigned long IDTime;
unsigned long prevIDTime;
const unsigned long pollIDDelay = 3000;

unsigned long previousLEDTime = 0;
const unsigned long pollDelay = 500;

float temperature;
int16_t devID;

// Pins definition
const int SSP = 10;

// Instantiate a new LM95071
LM95071 SPI_Sensor(SSP, ledPin, DEBUG_ON);

void setup()
{

Serial.begin(115200);

while (!Serial)
{
};

SPI_Sensor.begin();

delay(100);

SPI_Sensor.sleepMode();

devID = SPI_Sensor.readID();

delay(1000);

SPI_Sensor.conversionMode();

pollTime = 0;
prevIDTime = 0;
}

void loop()
{

unsigned long nowTime = millis();

if (nowTime - IDTime >= pollIDDelay)
{

IDTime = nowTime;

SPI_Sensor.sleepMode();

devID = SPI_Sensor.readID();

SPI_Sensor.conversionMode();

Serial.print(F("Device ID >> "));
Serial.println(devID, HEX);
}

// Sample temperatures at 5 Hz
if (nowTime - pollTime >= pollDelay)
{

pollTime = nowTime;

SPI_Sensor.conversionMode();

temperature = SPI_Sensor.getTemperature();

if (SERIAL_PLOTTER == false)
Serial.print("Current temperature = ");

Serial.print(temperature);

if (SERIAL_PLOTTER == false)
{

Serial.print(char(176));
Serial.println("C");

Serial.print(F("Device ID >> "));
Serial.println(devID, HEX);
}
else
{

Serial.print("\n");
}
}
}
Loading

0 comments on commit d025fb2

Please sign in to comment.