REKLAMA

DS18B20.zip

Freeze Arduino przy odłączonym LCD lub funkcją Serial.print

Kurcze, niezauważyłem że edytowałeś posta z kolejnymi pytaniami :) Jakiej biblioteki używasz dla DS18B20? 935985 pomiar wykonywany jest następująco: // odczyt z czujnika temperatury wyspy void ReadTempWyspaSensors(){ if (sensors.available()) { if (sensors.readTemperature(address)<-100) {sval = 99;} else {sval = sensors.readTemperature(address); } sensors.request(address); } ShowLCDsensor(); }


Pobierz plik - link do postu
  • DS18B20.zip
    • DS18B20.cpp
    • keywords.txt
    • examples
      • search_ds18b20_addresses
        • search_ds18b20_addresses.ino
      • ds18b20_sensor_address_in_flash_memory
        • ds18b20_sensor_address_in_flash_memory.ino
      • ds18b20_read_temperature_one_sensor
        • ds18b20_read_temperature_one_sensor.ino
      • ds18b20_framework
        • ds18b20_framework.ino
      • ds18b20_read_temperature_async
        • ds18b20_read_temperature_async.ino
      • ds18b20_multiple_sensors
        • ds18b20_multiple_sensors.ino
      • ds18b20_debug
        • ds18b20_debug.ino
    • README.md
    • .git
      • packed-refs
      • objects
        • 0d
          • 1f1bb39beb8e516f03b17e8963f0fa10d14d3d
        • ee
          • fcade443e865a126a6a8482d8ac19b400bc3bf
        • b6
          • 1c879e86e628af61fe0918067026bbfc783334
        • 19
          • 2f9f8735c189c5d93d61f1e71ca075a5f0cd8e
        • d1
          • 2fe5efd58eb0770c3f0f74104070f015f639dd
        • bc
          • dd08483518c58de80a07411626fadd4cdd5035
        • f9
          • 1e42feb51b71b56ab651fa0bce585720ec5835
        • 40
          • 940b50d5b570958b8fbe94a0f7fa706f877b83
        • 56
          • 875f2c48b7946a91a48381c780ff43472293a7
        • 3a
          • 91ffa23070fbd8c291c0ea280308329695be83
        • 03
          • d6bad495b3d394ccc298826a787a0093e5b9f0
        • d0
          • cfd69f1991148c29acc430c3b5eda198e5b5ae
        • 8f
          • fa562f9ba635374ef3351231b31982efd3a245
        • ba
          • 02f035596fbc1ed006f161030272701bb2c973
        • bf
          • 3cb000fa9a9f983ffb3fa9dc19661981751539
        • e0
          • 8322ce906070fcdec82007ee077b665eca3019
        • 7a
          • 6964a6f7d0c9390144a755c6f8f022e0e7c755
        • 6f
          • de78ecd093f6585e4e4c09a6736491c5ab8ca8
        • 49
          • f3ea419dc74e661ada48f2336a74cd5acd3ca3
        • 42
          • af1a07ddf615665a7bfe81602c93a7c02f419f
        • Array
        • 07
          • f9d51719f341540363efbedfacd8b4b40c8bd9
        • 74
          • 8ee18e6d3f8376bde25210403e44fd36962e0d
          • 2990028ca83144217a913cd81690bcd4a1ebd5
        • 2e
          • 515b9d942f8112d2bfe709bffa4add5c6ae77d
        • 2f
          • e688e7fa2c4af9cbc05f20254de9fc2c0eb153
        • 3f
          • d60bab0bff655dff210c60c90080764520c9c3
        • c7
          • f4c86419840a096190a43343908ef00ff4dd73
        • e2
          • 701d9b06d824acfce261af6b6e4460b9b787ff
        • 0c
          • e585f41c70264d4597e8938036ceca8ea4f5f4
        • 7c
          • 543b95eee3a8119c952a1a926f44b08c9acc69
        • 14
          • 5ade3333ac159282ee634ab86816def3231bcc
        • c3
          • 5973ab5df8fe96ede3bbb8e00ebfea5ec56552
        • 87
          • ec13ca2d32bb2e6468a5605665e0329ee648ec
        • d3
          • 89e1b54c83a7f9d0346d2d92e4ae1593bb49a0
        • 7e
          • d49c85239fb678fd74ab066e287f94b6148e36
        • Array
        • 36
          • 9446b7d300664afacee48ff45f3b4946dcfb6f
        • ad
          • 06513c3000da77623c5a78a459efcb485ea497
        • f6
          • eb3be662f3b530af04928f87a0bfd5365204b6
          • ae27e08962013803a096ff5af3d9aeca9af43a
        • 81
          • d43305aaf3becc3afccc443c34cf65117c06d2
        • 61
          • 6b5f1145be2a2b80fbd29ade145fa31de208f9
        • b3
          • ef268625b60289a0a3b81cddf52a120d445dfe
        • 83
          • f8d7341fd1592b2e510dd86bbd9b395573e1b7
        • eb
          • 1a6d1bfd2fdda3d828f600ddfd7711b93903a9
        • 51
          • 2a8e0363d78f3a98d8d360f8b9ed5bfc9a5d25
        • e4
          • 490f1fd741a12c3ab5deb79b23a532e3f32232
        • 9c
          • 80e2565fc52ecf726995ac5241cd6a17172f9c
        • db
          • 56488d02e0fed03d707ccc38cd152de518442e
        • 41
          • 512e1aec3ab517b0532842e4f8eaee4141fb61
        • 10
          • 69d2cd4969efa486d6e2c1271240516a1b6f09
      • HEAD
      • description
      • info
        • exclude
      • logs
        • HEAD
        • refs
          • heads
            • master
          • remotes
            • origin
              • master
              • HEAD
      • index
      • Array
      • config
      • FETCH_HEAD
      • hooks
        • post-update.sample
        • pre-commit.sample
        • applypatch-msg.sample
        • pre-rebase.sample
        • commit-msg.sample
        • pre-push.sample
        • prepare-commit-msg.sample
        • pre-applypatch.sample
        • update.sample
      • refs
        • heads
          • master
        • remotes
          • origin
            • master
            • HEAD
        • Array
    • DS18B20.h


DS18B20.zip > DS18B20.h

#ifndef _DS18B20_h_
#define _DS18B20_h_

#include &amp; lt; Arduino.h &amp; gt;
#include &amp; lt; inttypes.h &amp; gt;
#include &amp; lt; avr/pgmspace.h &amp; gt;
#include &amp; lt; OneWire.h &amp; gt;

#define TEMP_ERROR -273.15f

// Pointer type to an array in flash memory of device address
#define FA( pgm_ptr ) ( reinterpret_cast &amp; lt; const __FlashStringHelper * &amp; gt; ( pgm_ptr ) )

// Exceptions catcher
#define E(a) __check(a, __LINE__)

// Teperature value exception catcher
#define TE(a) __check(a != TEMP_ERROR, __LINE__)


void __check(bool value, uint16_t line);

class DS18B20
{
public:
DS18B20(OneWire *oneWire);
bool begin(uint8_t quality=12);
bool request(void);
bool request(uint8_t *address);
bool request(const __FlashStringHelper *_address);

bool available(void);
float readTemperature(uint8_t *address);
float readTemperature(const __FlashStringHelper *_address);

private:
OneWire *_oneWire;
uint8_t _quality;
bool _samePowerType;
bool _powerType;
uint32_t _beginConversionTime;

bool _sendCommand(uint8_t *address, uint8_t command);
bool _sendQuality(uint8_t *address);
bool _receivePowerType(uint8_t *address);
void _readFlashAddress(const __FlashStringHelper *_address, uint8_t *address);
};
#endif


DS18B20.zip > DS18B20.cpp

#include &quot; DS18B20.h &quot;

// Exception handler
// Prints the line number of the exception and block the program, when the function returns false
void __check(bool value, uint16_t line)
{
if (value)
return;

Serial.print(F( &quot; EXCEPTION at line: &quot; ));
Serial.println(line);
while(1);
}

// Constructor
// Argument: Pointer to OneWire object
// Return: New DS18B20 object
DS18B20::DS18B20(OneWire *oneWire)
{
_oneWire = oneWire;
_quality = 0;
_samePowerType = false;
_powerType = false;
_beginConversionTime = 0;
}

// Setup for all ds19b20 sensors in 1-Wire bus.
// Argument: quality - measurement resolution in bits (from 9 to 12)
// Return:
// true - if all operations were successful
// false - when the bus is physically damaged
// - when devices not respond
// - when device address is not valid
// - when not detect any device
bool DS18B20::begin(uint8_t quality)
{
_quality = constrain(quality, 9, 12);
uint8_t address[8];
uint8_t devices = 0;
uint8_t parasiteDevices = 0;

uint32_t beginResetTimeout = millis();

while(!_oneWire- &amp; gt; reset())
{
uint32_t elapsedResetTimeout = millis() - beginResetTimeout;

if (elapsedResetTimeout &amp; gt; 1000)
return false;
}

_oneWire- &amp; gt; reset_search();
while (_oneWire- &amp; gt; search(address))
{
if (OneWire::crc8(address, 7) != address[7])
return false;

if (address[0] != 0x28)
continue;

if (!_sendQuality(address))
return false;

_powerType = _receivePowerType(address);
if (!_powerType)
parasiteDevices++;

devices++;
}

if (parasiteDevices == devices || parasiteDevices == 0)
_samePowerType = true;

if (devices == 0)
return false;

return true;
}

// Request for temperature measurements on all devices
// Return:
// - true - if operation were successful
// - false - if devices have different ways of power (combinations of normal and parasite in one bus)
// - if devices not responding
bool DS18B20::request()
{
if (!_samePowerType)
return false;

if (!_oneWire- &amp; gt; reset())
return false;

_oneWire- &amp; gt; skip();
_oneWire- &amp; gt; write(0x44, !_powerType);

_beginConversionTime = millis();

return true;
}

// Request for temperature measurements on device
// Argument: Pointer to an array of device address
// Return:
// - true - if operation were successful
// - false - if device not responding
bool DS18B20::request(uint8_t *address)
{
if (!_samePowerType)
{
_powerType = _receivePowerType(address);
}

if (!_oneWire- &amp; gt; reset())
return false;

_oneWire- &amp; gt; select(address);
_oneWire- &amp; gt; write(0x44, !_powerType);

_beginConversionTime = millis();

return true;
}

// Request for temperature measurements on device
// Argument: Pointer to an array in flash memory of device address
// Return:
// - true - if operation were successful
// - false - if device not responding
bool DS18B20::request(const __FlashStringHelper *_address)
{
uint8_t address[8];
_readFlashAddress(_address, address);

return request(address);
}

// Checks if devices completed the measurement
// Return:
// - true - the measurement was completed
// - false - the measurement wasn't completed
bool DS18B20::available(void)
{
uint32_t durationTime[] = {94, 188, 375, 750};
uint32_t elapsedTime = millis() - _beginConversionTime;
bool timeout = elapsedTime &amp; gt; = durationTime[_quality-9];

if (_powerType)
{
bool ready = _oneWire- &amp; gt; read_bit();

if (ready || timeout)
return true;
}

if (timeout)
return true;

return false;
}

// Read temperature from device
// Argument: Pointer to an array of device address
// Return: temperature in degrees Celsius
// If the temperature is TEMP_ERROR value - measurement failed because:
// - the bus is physically damaged
// - devices not respond
// - when data from the device is not valid
// - when not detect device of thad address
float DS18B20::readTemperature(uint8_t *address)
{
uint8_t scratchpad[9];

if (!_sendCommand(address, 0xbe))
return TEMP_ERROR;

_oneWire- &amp; gt; read_bytes(scratchpad, 9);

if (OneWire::crc8(scratchpad, 8) != scratchpad[8])
return TEMP_ERROR;

float quality[] = {0.5, 0.25, 0.125, 0.0625};
uint8_t shift[] = {3, 2, 1, 0};
int16_t raw = word(scratchpad[1], scratchpad[0]);
raw &amp; gt; &amp; gt; = shift[_quality-9];

return raw * quality[_quality-9];
}

// Read temperature from device
// Argument: Pointer to an array in flash memory of device address
// Return: temperature in degrees Celsius
// If the temperature is TEMP_ERROR value - measurement failed because:
// - the bus is physically damaged
// - devices not respond
// - when data from the device is not valid
// - when not detect device of thad address
float DS18B20::readTemperature(const __FlashStringHelper *_address)
{
uint8_t address[8];
_readFlashAddress(_address, address);

return readTemperature(address);
}

// private methods
bool DS18B20::_sendCommand(uint8_t *address, uint8_t command)
{
if (!_oneWire- &amp; gt; reset())
return false;

_oneWire- &amp; gt; select(address);
_oneWire- &amp; gt; write(command);

return true;
}

bool DS18B20::_sendQuality(uint8_t *address)
{
if (!_sendCommand(address, 0x4e))
return false;

_oneWire- &amp; gt; write(0);
_oneWire- &amp; gt; write(0);

uint8_t quality = _quality;
quality -= 9;
quality &amp; lt; &amp; lt; = 5;
quality |= 0b00011111;
_oneWire- &amp; gt; write(quality);

return true;
}

bool DS18B20::_receivePowerType(uint8_t *address)
{
_sendCommand(address, 0xb4);

return _oneWire- &amp; gt; read();
}

void DS18B20::_readFlashAddress(const __FlashStringHelper *_address, uint8_t *address)
{
const uint8_t *pgmAddress PROGMEM = (const uint8_t PROGMEM *) _address;

for (uint8_t i=0; i &amp; lt; 8; i++)
{
address[i] = pgm_read_byte(pgmAddress++);
}
}


DS18B20.zip > keywords.txt

#######################################
# Syntax Coloring Map DS18B20
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

DS18B20 KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
request KEYWORD2
available KEYWORD2
readTemperature KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

TEMP_ERROR LITERAL1
FA LITERAL1
E LITERAL1
TE LITERAL1