// #ifndef OneWire_h
// #define OneWire_h
// #ifdef __cplusplus
#include <stdint.h>
#if defined(__AVR__)
#include <util/crc16.h>
#endif
// #if ARDUINO >= 100
// #include <Arduino.h> // for delayMicroseconds, digitalPinToBitMask, etc
// #else
// #include "WProgram.h" // for delayMicroseconds
// #include "pins_arduino.h" // for digitalPinToBitMask, etc
// #endif
// You can exclude certain features from OneWire. In theory, this
// might save some space. In practice, the compiler automatically
// removes unused code (technically, the linker, using -fdata-sections
// and -ffunction-sections when compiling, and Wl,--gc-sections
// when linking), so most of these will not result in any code size
// reduction. Well, unless you try to use the missing features
// and redesign your program to not need them! ONEWIRE_CRC8_TABLE
// is the exception, because it selects a fast but large algorithm
// or a small but slow algorithm.
// you can exclude onewire_search by defining that to 0
#ifndef ONEWIRE_SEARCH
#define ONEWIRE_SEARCH 1
#endif
// You can exclude CRC checks altogether by defining this to 0
#ifndef ONEWIRE_CRC
#define ONEWIRE_CRC 1
#endif
// Select the table-lookup method of computing the 8-bit CRC
// by setting this to 1. The lookup table enlarges code size by
// about 250 bytes. It does NOT consume RAM (but did in very
// old versions of OneWire). If you disable this, a slower
// but very compact algorithm is used.
#ifndef ONEWIRE_CRC8_TABLE
#define ONEWIRE_CRC8_TABLE 1
#endif
// You can allow 16-bit CRC checks by defining this to 1
// (Note that ONEWIRE_CRC must also be 1.)
#ifndef ONEWIRE_CRC16
#define ONEWIRE_CRC16 1
#endif
// Board-specific macros for direct GPIO
#include "util/OneWire_direct_regtype.h"
class OneWire
{
private:
IO_REG_TYPE bitmask;
volatile IO_REG_TYPE *baseReg;
#if ONEWIRE_SEARCH
// global search state
unsigned char ROM_NO[8];
uint8_t LastDiscrepancy;
uint8_t LastFamilyDiscrepancy;
bool LastDeviceFlag;
#endif
public:
OneWire() { }
OneWire(uint8_t pin) { begin(pin); }
void begin(uint8_t pin);
// Perform a 1-Wire reset cycle. Returns 1 if a device responds
// with a presence pulse. Returns 0 if there is no device or the
// bus is shorted or otherwise held low for more than 250uS
uint8_t reset(void);
// Issue a 1-Wire rom select command, you do the reset first.
void select(const uint8_t rom[8]);
// Issue a 1-Wire rom skip command, to address all on bus.
void skip(void);
// Write a byte. If 'power' is one then the wire is held high at
// the end for parasitically powered devices. You are responsible
// for eventually depowering it by calling depower() or doing
// another read or write.
void write(uint8_t v, uint8_t power = 0);
void write_bytes(const uint8_t *buf, uint16_t count, bool power = 0);
// Read a byte.
uint8_t read(void);
void read_bytes(uint8_t *buf, uint16_t count);
// Write a bit. The bus is always left powered at the end, see
// note in write() about that.
void write_bit(uint8_t v);
// Read a bit.
uint8_t read_bit(void);
// Stop forcing power onto the bus. You only need to do this if
// you used the 'power' flag to write() or used a write_bit() call
// and aren't about to do another read or write. You would rather
// not leave this powered if you don't have to, just in case
// someone shorts your bus.
void depower(void);
#if ONEWIRE_SEARCH
// Clear the search state so that if will start from the beginning again.
void reset_search();
// Setup the search to find the device type 'family_code' on the next call
// to search(*newAddr) if it is present.
void target_search(uint8_t family_code);
// Look for the next device. Returns 1 if a new address has been
// returned. A zero might mean that the bus is shorted, there are
// no devices, or you have already retrieved all of them. It
// might be a good idea to check the CRC to make sure you didn't
// get garbage. The order is deterministic. You will always get
// the same devices in the same order.
bool search(uint8_t *newAddr, bool search_mode = true);
#endif
#if ONEWIRE_CRC
// Compute a Dallas Semiconductor 8 bit CRC, these are used in the
// ROM and scratchpad registers.
static uint8_t crc8(const uint8_t *addr, uint8_t len);
#if ONEWIRE_CRC16
// Compute the 1-Wire CRC16 and compare it against the received CRC.
// Example usage (reading a DS2408):
// // Put everything in a buffer so we can compute the CRC easily.
// uint8_t buf[13];
// buf[0] = 0xF0; // Read PIO Registers
// buf[1] = 0x88; // LSB address
// buf[2] = 0x00; // MSB address
// WriteBytes(net, buf, 3); // Write 3 cmd bytes
// ReadBytes(net, buf+3, 10); // Read 6 data bytes, 2 0xFF, 2 CRC16
// if (!CheckCRC16(buf, 11, &buf[11])) {
// // Handle error.
// }
//
// @param input - Array of bytes to checksum.
// @param len - How many bytes to use.
// @param inverted_crc - The two CRC16 bytes in the received data.
// This should just point into the received data,
// *not* at a 16-bit integer.
// @param crc - The crc starting value (optional)
// @return True, iff the CRC matches.
static bool check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc = 0);
// Compute a Dallas Semiconductor 16 bit CRC. This is required to check
// the integrity of data received from many 1-Wire devices. Note that the
// CRC computed here is *not* what you'll get from the 1-Wire network,
// for two reasons:
// 1) The CRC is transmitted bitwise inverted.
// 2) Depending on the endian-ness of your processor, the binary
// representation of the two-byte return value may have a different
// byte order than the two bytes you get from 1-Wire.
// @param input - Array of bytes to checksum.
// @param len - How many bytes to use.
// @param crc - The crc starting value (optional)
// @return The CRC16, as defined by Dallas Semiconductor.
static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
#endif
#endif
};
// Prevent this name from leaking into Arduino sketches
#ifdef IO_REG_TYPE
#undef IO_REG_TYPE
#endif
#endif // __cplusplus
#endif // OneWire_h
#ifndef DallasTemperature_h
#define DallasTemperature_h
#define DALLASTEMPLIBVERSION "4.0.5"
// Configuration
#ifndef REQUIRESNEW
#define REQUIRESNEW false
#endif
#ifndef REQUIRESALARMS
#define REQUIRESALARMS true
#endif
// Includes
// #include <inttypes.h>
// #include <Arduino.h>
// #ifdef __STM32F1__
// #include <OneWireSTM.h>
// #else
// #include <OneWire.h>
// #endif
// Constants for device models
#define DS18S20MODEL 0x10 // also DS1820
#define DS18B20MODEL 0x28 // also MAX31820
#define DS1822MODEL 0x22
#define DS1825MODEL 0x3B // also MAX31850
#define DS28EA00MODEL 0x42
// Error Codes
#define DEVICE_DISCONNECTED_C -127
#define DEVICE_DISCONNECTED_F -196.6
#define DEVICE_DISCONNECTED_RAW -7040
#define DEVICE_FAULT_OPEN_C -254
#define DEVICE_FAULT_OPEN_F -425.199982
#define DEVICE_FAULT_OPEN_RAW -32512
#define DEVICE_FAULT_SHORTGND_C -253
#define DEVICE_FAULT_SHORTGND_F -423.399994
#define DEVICE_FAULT_SHORTGND_RAW -32384
#define DEVICE_FAULT_SHORTVDD_C -252
#define DEVICE_FAULT_SHORTVDD_F -421.599976
#define DEVICE_FAULT_SHORTVDD_RAW -32256
// Configuration Constants
#define MAX_CONVERSION_TIMEOUT 750
#define MAX_INITIALIZATION_RETRIES 3
#define INITIALIZATION_DELAY_MS 50
typedef uint8_t DeviceAddress[8];
class DallasTemperature {
public:
struct request_t {
bool result;
unsigned long timestamp;
operator bool() { return result; }
};
// Constructors
DallasTemperature();
DallasTemperature(OneWire*);
DallasTemperature(OneWire*, uint8_t);
// Setup & Configuration
void setOneWire(OneWire*);
void setPullupPin(uint8_t);
void begin(void);
bool verifyDeviceCount(void);
// Device Information
uint8_t getDeviceCount(void);
uint8_t getDS18Count(void);
bool validAddress(const uint8_t*);
bool validFamily(const uint8_t* deviceAddress);
bool getAddress(uint8_t*, uint8_t);
bool isConnected(const uint8_t*);
bool isConnected(const uint8_t*, uint8_t*);
// Scratchpad Operations
bool readScratchPad(const uint8_t*, uint8_t*);
void writeScratchPad(const uint8_t*, const uint8_t*);
bool readPowerSupply(const uint8_t* deviceAddress = nullptr);
// Resolution Control
uint8_t getResolution();
void setResolution(uint8_t);
uint8_t getResolution(const uint8_t*);
bool setResolution(const uint8_t*, uint8_t, bool skipGlobalBitResolutionCalculation = false);
// Conversion Configuration
void setWaitForConversion(bool);
bool getWaitForConversion(void);
void setCheckForConversion(bool);
bool getCheckForConversion(void);
// Temperature Operations
request_t requestTemperatures(void);
request_t requestTemperaturesByAddress(const uint8_t*);
request_t requestTemperaturesByIndex(uint8_t);
int32_t getTemp(const uint8_t*, byte retryCount = 0);
float getTempC(const uint8_t*, byte retryCount = 0);
float getTempF(const uint8_t*);
float getTempCByIndex(uint8_t);
float getTempFByIndex(uint8_t);
// Conversion Status
bool isParasitePowerMode(void);
bool isConversionComplete(void);
static uint16_t millisToWaitForConversion(uint8_t);
uint16_t millisToWaitForConversion();
// EEPROM Operations
bool saveScratchPadByIndex(uint8_t);
bool saveScratchPad(const uint8_t* = nullptr);
bool recallScratchPadByIndex(uint8_t);
bool recallScratchPad(const uint8_t* = nullptr);
void setAutoSaveScratchPad(bool);
bool getAutoSaveScratchPad(void);
#if REQUIRESALARMS
typedef void AlarmHandler(const uint8_t*);
void setHighAlarmTemp(const uint8_t*, int8_t);
void setLowAlarmTemp(const uint8_t*, int8_t);
int8_t getHighAlarmTemp(const uint8_t*);
int8_t getLowAlarmTemp(const uint8_t*);
void resetAlarmSearch(void);
bool alarmSearch(uint8_t*);
bool hasAlarm(const uint8_t*);
bool hasAlarm(void);
void processAlarms(void);
void setAlarmHandler(const AlarmHandler*);
bool hasAlarmHandler();
#endif
// User Data Operations
void setUserData(const uint8_t*, int16_t);
void setUserDataByIndex(uint8_t, int16_t);
int16_t getUserData(const uint8_t*);
int16_t getUserDataByIndex(uint8_t);
// Temperature Conversion Utilities
static float toFahrenheit(float);
static float toCelsius(float);
static float rawToCelsius(int32_t);
static int16_t celsiusToRaw(float);
static float rawToFahrenheit(int32_t);
#if REQUIRESNEW
void* operator new(unsigned int);
void operator delete(void*);
#endif
// Conversion Completion Methods
void blockTillConversionComplete(uint8_t);
void blockTillConversionComplete(uint8_t, unsigned long);
void blockTillConversionComplete(uint8_t, request_t);
private:
typedef uint8_t ScratchPad[9];
// Internal State
bool parasite;
bool useExternalPullup;
uint8_t pullupPin;
uint8_t bitResolution;
bool waitForConversion;
bool checkForConversion;
bool autoSaveScratchPad;
uint8_t devices;
uint8_t ds18Count;
OneWire* _wire;
// Internal Methods
int32_t calculateTemperature(const uint8_t*, uint8_t*);
bool isAllZeros(const uint8_t* const scratchPad, const size_t length = 9);
void activateExternalPullup(void);
void deactivateExternalPullup(void);
#if REQUIRESALARMS
uint8_t alarmSearchAddress[8];
int8_t alarmSearchJunction;
uint8_t alarmSearchExhausted;
AlarmHandler* _AlarmHandler;
#endif
};
#endif // DallasTemperature_h
// Pin data DS18B20
#define ONE_WIRE_BUS D2
// Setup OneWire dan DallasTemperature
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
pinMode(ONE_WIRE_BUS, INPUT);
digitalWrite(ONE_WIRE_BUS, LOW);
Serial.begin(115200);
sensors.begin();
delay(1000);
}
void loop() {
// Request pembacaan suhu dari DS18B20
sensors.requestTemperatures();
// Ambil suhu dalam Celsius
float tempC = sensors.getTempCByIndex(0);
// Print suhu ke serial monitor
if (tempC == DEVICE_DISCONNECTED_C) {
Serial.println("Error: No temperature reading");
} else {
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" C");
}
delay(1000); // Delay 1 detik
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6