DS18B20 olvasás alatt várakozik a képernyő írása

Processing/Wiring (illetve C) nyelvű programozási fogások, tippek. (AVR-Duino, Arduino, EthDuino, Diecimila, Severino, Nano, LilyPad)
bencsiknorbert
Újonc
Újonc
Hozzászólások: 4
Csatlakozott: 2018. március 9. péntek, 15:31

DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: bencsiknorbert »

Készítettem egy áramkört mely tartalmaz egy 7 szegmenses kijelzőt és még további 3 db 4 digites 7 szegmenses kijelzőt valamint egy potmétert amivel tudom kiválasztani, hogy melyik kijelzőre mit írjon ki. A kijelzőket shift regiszterekkel működtetem. Használok még párdarab DS18B20 hőmérőt is aminek a értékeit szeretném kiíratni. A megírt program és az áramkör szerintem tökéletesen működik egy apró "hibától" eltekintve és nagyon örülnék ha tudnátok segíteni benne hogy kellene módosítani a programot vagy az áramkört.
Mind addig hibátlan a működés amíg meg nem történik hőmérő kiolvasás. Kb. 2 másodpercig kialszik mind a 3 4 digites kijelző (véleményem szerint megszakad a kiírás az olvasás végett) utána tökéletesen ki is íródnak a megfelelő értékek és ez ismétlődik minden beállított ciklusban.

Véleményetek szerint hogyan tudnám megoldani azt, hogy a hőmérő olvasás idejére is folyamatos legyen a képernyő kiírása?

Szoftveresen vagy hardveresen kell módosítanom, sajnos nemrég kezdtem foglalkozni a témával.

Arduino Nano vezérli a kijelzőket, melyre Arduino 1.8.5. verzióval töltöm fel a programot.

Mellékelem a teljes forráskódot is a könnyebb átláthatóság érdekében.

Kód: Egész kijelölése

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 10

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int Homerseklet = 0;

const int digitPins[4] = {7, 8, 3, 2};              //4 common anode pins of the display
const int clockPin = 4;    //74HC595 Pin 11
const int latchPin = 5;    //74HC595 Pin 12
const int dataPin = 6;     //74HC595 Pin 14
const int potPin = A7;     //Potméter sensor pin
const int tempOlvPin = 12;    //Hőmérő olvasás Led pin

const byte digit[14] =      //kijelzőn megjelenített karakterek
{
  B11111100,  //0
  B01100000,  //1
  B11011010,  //2
  B11110010,  //3
  B01100110,  //4
  B10110110,  //5
  B10111110,  //6
  B11100000,  //7
  B11111110,  //8
  B11110110,  //9
  B10011110,  //E
  B00001010,  //r
  B00000000,  //Üres karakter
  B00000010   //-
};
const byte digit1[14] =
{
  B00000011,  //0
  B10011111,  //1
  B00100101,  //2
  B00001101,  //3
  B10011001,  //4
  B01001001,  //5
  B01000001,  //6
  B00011111,  //7
  B00000001,  //8
  B00001001,  //9
  B11111111  //Üres karakter
};
int digitBuffer[4] = {0};
int digitBuffer1[4] = {0};
int digitBuffer2[4] = {0};
int digitBuffer3 = 0;
int digitScan = 0;
int potmAllas = 0;
float tempC, tempC1, tempC2;

void setup() {
  for (int i = 0; i < 4; i++)
  {
    pinMode(digitPins[i], OUTPUT);
  }
  pinMode(tempOlvPin, OUTPUT);
  pinMode(potPin, INPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  Serial.begin(9600);
  sensors.begin();
}

void updateDisp(bool kezdo) {
  for (byte j = 0; j < 4; j++)

    digitalWrite(digitPins[j], HIGH);

  digitalWrite(latchPin, LOW);
  for (int i = 0; i < 3; i++)
    shiftOut(dataPin, clockPin, MSBFIRST, digit[12]);
    shiftOut(dataPin, clockPin, MSBFIRST, digit1[10]);
  digitalWrite(latchPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(digitPins[digitScan], LOW);

  //delayMicroseconds(10);
  digitalWrite(latchPin, LOW);
  if (digitScan == 2)
  {
    shiftOut(dataPin, clockPin, LSBFIRST, (digit[digitBuffer2[digitScan]] | 00000001)); //kiirja a 3. kijelzőre az adatokat
    shiftOut(dataPin, clockPin, LSBFIRST, (digit[digitBuffer1[digitScan]] | 00000001)); //kiirja a 2. kijelzőre az adatokat
    shiftOut(dataPin, clockPin, LSBFIRST, (digit[digitBuffer[digitScan]] | 00000001));  //kiirja az 1. kijelzőre az adatokat
  }
  else
  {
    shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer2[digitScan]]);  //kiirja a 3. kijelzőre az adatokat
    shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer1[digitScan]]);  //kiirja a 2. kijelzőre az adatokat
    shiftOut(dataPin, clockPin, LSBFIRST, digit[digitBuffer[digitScan]]);   //kiirja az 1. kijelzőre az adatokat
  
    }
    shiftOut(dataPin, clockPin, LSBFIRST, digit1[potmAllas]);   //kiirja a torony választó kijelzőre az adatokat
    
  digitalWrite(latchPin, HIGH);
  delay(1);
  digitScan++;
  if (digitScan > 3)
  {
    digitScan = 0;
  }
}
void loop() 
{ 
  for(int i = 0; i < 1000; i++)
  {
    KijelzoKiir();
    Serial.println(i);
  }
  //Serial.println();
  digitalWrite(tempOlvPin, HIGH);
  if(digitScan == 3)
  KijelzoKiir();
  if(digitScan == 2)
  KijelzoKiir();
  if(digitScan == 0)
  KijelzoKiir();
  if(digitScan == 1)
  HomeroOlvas();
  digitalWrite(tempOlvPin, LOW);
}
void HomeroOlvas()
{
  sensors.requestTemperatures();    // lekéri a hőmérséklet adatokat
  switch(potmAllas)
  {
    case 1:
            //tempC = sensors.requestTemperaturesByAddress();     //Kiirja a megadott azonosítójú hőmérő által mért értéket a kijelzőre
            tempC = sensors.getTempCByIndex(0);       //Kiirja a megadott sorszámú (0) hőmérő által mért értéket a kijelzőre
            tempC1 = 1.0;//sensors.getTempCByIndex(0);
            tempC2 = 2.0;//sensors.getTempCByIndex(0);
            break;
    case 2:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = -2.0;//sensors.getTempCByIndex(0);
            tempC2 = 4.0;//sensors.getTempCByIndex(0);
            break;
    case 3:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = 3.0;//sensors.getTempCByIndex(0);
            tempC2 = 6.0;//sensors.getTempCByIndex(0);
            break;
    case 4:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = 4.0;//sensors.getTempCByIndex(0);
            tempC2 = -8.0;//sensors.getTempCByIndex(0);
            break;
    case 5:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = -5.0;//sensors.getTempCByIndex(0);
            tempC2 = 10.0;//sensors.getTempCByIndex(0);
            break;
    case 6:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = 6.0;//sensors.getTempCByIndex(0);
            tempC2 = 12.0;//sensors.getTempCByIndex(0);
            break;
    case 7:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = sensors.getTempCByIndex(1);
            tempC2 = sensors.getTempCByIndex(2);
            break;
    case 8:
            tempC = sensors.getTempCByIndex(0);
            tempC1 = sensors.getTempCByIndex(2);
            tempC2 = sensors.getTempCByIndex(4);
            break;
    default:
            tempC = 1.1;
            tempC1 = 2.2;
            tempC2 = 3.3;
            break;
  }
  KijelzoKiir();
}
void KijelzoKiir()
{
  potmAllas = analogRead(potPin) / 100;
  if(potmAllas == 0)
  {
    potmAllas = 10;
  }
  if (tempC > -99.9 && tempC < 99.9)
  {
    digitBuffer[0] = Digit1(tempC * 100);
    digitBuffer[1] = Digit2(tempC * 100);
    digitBuffer[2] = Digit3(tempC * 100);
    digitBuffer[3] = Digit4(tempC * 100);
  }
  else
  {
    digitBuffer[3] = 12;
    digitBuffer[2] = 11;
    digitBuffer[1] = 11;
    digitBuffer[0] = 10;
  }
  if (tempC1 > -99.9 && tempC1 < 99.9)
  {
    digitBuffer1[0] = Digit1(tempC1 * 100);
    digitBuffer1[1] = Digit2(tempC1 * 100);
    digitBuffer1[2] = Digit3(tempC1 * 100);
    digitBuffer1[3] = Digit4(tempC1 * 100);
  }
  else
  {
    digitBuffer1[3] = 12;
    digitBuffer1[2] = 11;
    digitBuffer1[1] = 11;
    digitBuffer1[0] = 10;
  }
  if (tempC2 > -99.9 && tempC2 < 99.9)
  {
    digitBuffer2[0] = Digit1(tempC2 * 100);
    digitBuffer2[1] = Digit2(tempC2 * 100);
    digitBuffer2[2] = Digit3(tempC2 * 100);
    digitBuffer2[3] = Digit4(tempC2 * 100);
  }
  else
  {
    digitBuffer2[3] = 12;
    digitBuffer2[2] = 11;
    digitBuffer2[1] = 11;
    digitBuffer2[0] = 10;
  }
  updateDisp(false);
}
int Digit1(int szam)
{
  if ((szam * 100) < 0)
  {
    return 13;
  }
  else
  {
    return 12;
  }
}
int Digit2(int szam)
{
  if (abs(int(szam) / 1000) == 0)
  {
    return 12;
  }
  else
  {
    return abs(int((szam) / 1000));
  }
}
int Digit3(int szam)
{
  return abs((int(szam) % 1000) / 100);
}
int Digit4(int szam)
{
  return abs((int(szam % 100) / 10));
}

}
Segítségeteket előre is nagyon köszönöm.
vargham
Chipgyilok
Hozzászólások: 308
Csatlakozott: 2014. január 8. szerda, 8:32

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: vargham »

A DallasTemperature könyvtár honnan van? Forrása?
bencsiknorbert
Újonc
Újonc
Hozzászólások: 4
Csatlakozott: 2018. március 9. péntek, 15:31

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: bencsiknorbert »

vargham írta:A DallasTemperature könyvtár honnan van? Forrása?
Ha emlékeim nem csalnak akkor kb. fél éve mikor elkezdtem a témával foglalkozni az Arduino keretrendszerrel együtt került a gépemre, az internetes böngészéseim alkalmával rengeteg példakódot néztem meg, tanulmányoztam és azokból próbáltam meg kihámozni a lényeget és jutottam el ehhez a verzióhoz.
vargham
Chipgyilok
Hozzászólások: 308
Csatlakozott: 2014. január 8. szerda, 8:32

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: vargham »

De ha nem látjuk, akkor nem is tudunk segíteni.
bencsiknorbert
Újonc
Újonc
Hozzászólások: 4
Csatlakozott: 2018. március 9. péntek, 15:31

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: bencsiknorbert »

vargham írta:De ha nem látjuk, akkor nem is tudunk segíteni.
DallasTemperature.h

Kód: Egész kijelölése

#ifndef DallasTemperature_h
#define DallasTemperature_h

#define DALLASTEMPLIBVERSION "3.7.9" // To be deprecated

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

// set to true to include code for new and delete operators
#ifndef REQUIRESNEW
#define REQUIRESNEW false
#endif

// set to true to include code implementing alarm search functions
#ifndef REQUIRESALARMS
#define REQUIRESALARMS true
#endif

#include <inttypes.h>
#include <OneWire.h>

// Model IDs
#define DS18S20MODEL 0x10  // also DS1820
#define DS18B20MODEL 0x28
#define DS1822MODEL  0x22
#define DS1825MODEL  0x3B
#define DS28EA00MODEL 0x42

// Error Codes
#define DEVICE_DISCONNECTED_C -127
#define DEVICE_DISCONNECTED_F -196.6
#define DEVICE_DISCONNECTED_RAW -7040

typedef uint8_t DeviceAddress[8];

class DallasTemperature {
public:

	DallasTemperature();
	DallasTemperature(OneWire*);

	void setOneWire(OneWire*);

	// initialise bus
	void begin(void);

	// returns the number of devices found on the bus
	uint8_t getDeviceCount(void);

	// returns the number of DS18xxx Family devices on bus
	uint8_t getDS18Count(void);

	// returns true if address is valid
	bool validAddress(const uint8_t*);

	// returns true if address is of the family of sensors the lib supports.
	bool validFamily(const uint8_t* deviceAddress);

	// finds an address at a given index on the bus
	bool getAddress(uint8_t*, uint8_t);

	// attempt to determine if the device at the given address is connected to the bus
	bool isConnected(const uint8_t*);

	// attempt to determine if the device at the given address is connected to the bus
	// also allows for updating the read scratchpad
	bool isConnected(const uint8_t*, uint8_t*);

	// read device's scratchpad
	bool readScratchPad(const uint8_t*, uint8_t*);

	// write device's scratchpad
	void writeScratchPad(const uint8_t*, const uint8_t*);

	// read device's power requirements
	bool readPowerSupply(const uint8_t*);

	// get global resolution
	uint8_t getResolution();

	// set global resolution to 9, 10, 11, or 12 bits
	void setResolution(uint8_t);

	// returns the device resolution: 9, 10, 11, or 12 bits
	uint8_t getResolution(const uint8_t*);

	// set resolution of a device to 9, 10, 11, or 12 bits
	bool setResolution(const uint8_t*, uint8_t,
			bool skipGlobalBitResolutionCalculation = false);

	// sets/gets the waitForConversion flag
	void setWaitForConversion(bool);
	bool getWaitForConversion(void);

	// sets/gets the checkForConversion flag
	void setCheckForConversion(bool);
	bool getCheckForConversion(void);

	// sends command for all devices on the bus to perform a temperature conversion
	void requestTemperatures(void);

	// sends command for one device to perform a temperature conversion by address
	bool requestTemperaturesByAddress(const uint8_t*);

	// sends command for one device to perform a temperature conversion by index
	bool requestTemperaturesByIndex(uint8_t);

	// returns temperature raw value (12 bit integer of 1/128 degrees C)
	int16_t getTemp(const uint8_t*);

	// returns temperature in degrees C
	float getTempC(const uint8_t*);

	// returns temperature in degrees F
	float getTempF(const uint8_t*);

	// Get temperature for device index (slow)
	float getTempCByIndex(uint8_t);

	// Get temperature for device index (slow)
	float getTempFByIndex(uint8_t);

	// returns true if the bus requires parasite power
	bool isParasitePowerMode(void);

	// Is a conversion complete on the wire? Only applies to the first sensor on the wire.
	bool isConversionComplete(void);

	int16_t millisToWaitForConversion(uint8_t);

#if REQUIRESALARMS

	typedef void AlarmHandler(const uint8_t*);

	// sets the high alarm temperature for a device
	// accepts a int8_t.  valid range is -55C - 125C
	void setHighAlarmTemp(const uint8_t*, int8_t);

	// sets the low alarm temperature for a device
	// accepts a int8_t.  valid range is -55C - 125C
	void setLowAlarmTemp(const uint8_t*, int8_t);

	// returns a int8_t with the current high alarm temperature for a device
	// in the range -55C - 125C
	int8_t getHighAlarmTemp(const uint8_t*);

	// returns a int8_t with the current low alarm temperature for a device
	// in the range -55C - 125C
	int8_t getLowAlarmTemp(const uint8_t*);

	// resets internal variables used for the alarm search
	void resetAlarmSearch(void);

	// search the wire for devices with active alarms
	bool alarmSearch(uint8_t*);

	// returns true if ia specific device has an alarm
	bool hasAlarm(const uint8_t*);

	// returns true if any device is reporting an alarm on the bus
	bool hasAlarm(void);

	// runs the alarm handler for all devices returned by alarmSearch()
	void processAlarms(void);

	// sets the alarm handler
	void setAlarmHandler(const AlarmHandler *);

	// returns true if an AlarmHandler has been set
	bool hasAlarmHandler();

#endif

	// if no alarm handler is used the two bytes can be used as user data
	// example of such usage is an ID.
	// note if device is not connected it will fail writing the data.
	// note if address cannot be found no error will be reported.
	// in short use carefully
	void setUserData(const uint8_t*, int16_t);
	void setUserDataByIndex(uint8_t, int16_t);
	int16_t getUserData(const uint8_t*);
	int16_t getUserDataByIndex(uint8_t);

	// convert from Celsius to Fahrenheit
	static float toFahrenheit(float);

	// convert from Fahrenheit to Celsius
	static float toCelsius(float);

	// convert from raw to Celsius
	static float rawToCelsius(int16_t);

	// convert from raw to Fahrenheit
	static float rawToFahrenheit(int16_t);

#if REQUIRESNEW

	// initialize memory area
	void* operator new (unsigned int);

	// delete memory reference
	void operator delete(void*);

#endif

private:
	typedef uint8_t ScratchPad[9];

	// parasite power on or off
	bool parasite;

	// used to determine the delay amount needed to allow for the
	// temperature conversion to take place
	uint8_t bitResolution;

	// used to requestTemperature with or without delay
	bool waitForConversion;

	// used to requestTemperature to dynamically check if a conversion is complete
	bool checkForConversion;

	// count of devices on the bus
	uint8_t devices;

	// count of DS18xxx Family devices on bus
	uint8_t ds18Count;

	// Take a pointer to one wire instance
	OneWire* _wire;

	// reads scratchpad and returns the raw temperature
	int16_t calculateTemperature(const uint8_t*, uint8_t*);

	void blockTillConversionComplete(uint8_t);

#if REQUIRESALARMS

	// required for alarmSearch
	uint8_t alarmSearchAddress[8];
	int8_t alarmSearchJunction;
	uint8_t alarmSearchExhausted;

	// the alarm handler function pointer
	AlarmHandler *_AlarmHandler;

#endif

};
#endif

DallasTemperature.cpp

Kód: Egész kijelölése

// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.

#include "DallasTemperature.h"

#if ARDUINO >= 100
#include "Arduino.h"
#else
extern "C" {
#include "WConstants.h"
}
#endif

// OneWire commands
#define STARTCONVO      0x44  // Tells device to take a temperature reading and put it on the scratchpad
#define COPYSCRATCH     0x48  // Copy EEPROM
#define READSCRATCH     0xBE  // Read EEPROM
#define WRITESCRATCH    0x4E  // Write to EEPROM
#define RECALLSCRATCH   0xB8  // Reload from last known
#define READPOWERSUPPLY 0xB4  // Determine if device needs parasite power
#define ALARMSEARCH     0xEC  // Query bus for devices with an alarm condition

// Scratchpad locations
#define TEMP_LSB        0
#define TEMP_MSB        1
#define HIGH_ALARM_TEMP 2
#define LOW_ALARM_TEMP  3
#define CONFIGURATION   4
#define INTERNAL_BYTE   5
#define COUNT_REMAIN    6
#define COUNT_PER_C     7
#define SCRATCHPAD_CRC  8

// Device resolution
#define TEMP_9_BIT  0x1F //  9 bit
#define TEMP_10_BIT 0x3F // 10 bit
#define TEMP_11_BIT 0x5F // 11 bit
#define TEMP_12_BIT 0x7F // 12 bit

#define NO_ALARM_HANDLER ((AlarmHandler *)0)

DallasTemperature::DallasTemperature()
{
#if REQUIRESALARMS
	setAlarmHandler(NO_ALARM_HANDLER);
#endif
}
DallasTemperature::DallasTemperature(OneWire* _oneWire)
{
	setOneWire(_oneWire);
#if REQUIRESALARMS
	setAlarmHandler(NO_ALARM_HANDLER);
#endif
}

bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
	switch (deviceAddress[0]) {
	case DS18S20MODEL:
	case DS18B20MODEL:
	case DS1822MODEL:
	case DS1825MODEL:
	case DS28EA00MODEL:
		return true;
	default:
		return false;
	}
}

void DallasTemperature::setOneWire(OneWire* _oneWire) {

	_wire = _oneWire;
	devices = 0;
	ds18Count = 0;
	parasite = false;
	bitResolution = 9;
	waitForConversion = true;
	checkForConversion = true;

}

// initialise the bus
void DallasTemperature::begin(void) {

	DeviceAddress deviceAddress;

	_wire->reset_search();
	devices = 0; // Reset the number of devices when we enumerate wire devices
	ds18Count = 0; // Reset number of DS18xxx Family devices

	while (_wire->search(deviceAddress)) {

		if (validAddress(deviceAddress)) {

			if (!parasite && readPowerSupply(deviceAddress))
				parasite = true;

			bitResolution = max(bitResolution, getResolution(deviceAddress));

			devices++;
			if (validFamily(deviceAddress)) {
				ds18Count++;
			}
		}
	}

}

// returns the number of devices found on the bus
uint8_t DallasTemperature::getDeviceCount(void) {
	return devices;
}

uint8_t DallasTemperature::getDS18Count(void) {
	return ds18Count;
}

// returns true if address is valid
bool DallasTemperature::validAddress(const uint8_t* deviceAddress) {
	return (_wire->crc8(deviceAddress, 7) == deviceAddress[7]);
}

// finds an address at a given index on the bus
// returns true if the device was found
bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) {

	uint8_t depth = 0;

	_wire->reset_search();

	while (depth <= index && _wire->search(deviceAddress)) {
		if (depth == index && validAddress(deviceAddress))
			return true;
		depth++;
	}

	return false;

}

// attempt to determine if the device at the given address is connected to the bus
bool DallasTemperature::isConnected(const uint8_t* deviceAddress) {

	ScratchPad scratchPad;
	return isConnected(deviceAddress, scratchPad);

}

// attempt to determine if the device at the given address is connected to the bus
// also allows for updating the read scratchpad
bool DallasTemperature::isConnected(const uint8_t* deviceAddress,
		uint8_t* scratchPad) {
	bool b = readScratchPad(deviceAddress, scratchPad);
	return b && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
}

bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress,
		uint8_t* scratchPad) {

	// send the reset command and fail fast
	int b = _wire->reset();
	if (b == 0)
		return false;

	_wire->select(deviceAddress);
	_wire->write(READSCRATCH);

	// Read all registers in a simple loop
	// byte 0: temperature LSB
	// byte 1: temperature MSB
	// byte 2: high alarm temp
	// byte 3: low alarm temp
	// byte 4: DS18S20: store for crc
	//         DS18B20 & DS1822: configuration register
	// byte 5: internal use & crc
	// byte 6: DS18S20: COUNT_REMAIN
	//         DS18B20 & DS1822: store for crc
	// byte 7: DS18S20: COUNT_PER_C
	//         DS18B20 & DS1822: store for crc
	// byte 8: SCRATCHPAD_CRC
	for (uint8_t i = 0; i < 9; i++) {
		scratchPad[i] = _wire->read();
	}

	b = _wire->reset();
	return (b == 1);
}

void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress,
		const uint8_t* scratchPad) {

	_wire->reset();
	_wire->select(deviceAddress);
	_wire->write(WRITESCRATCH);
	_wire->write(scratchPad[HIGH_ALARM_TEMP]); // high alarm temp
	_wire->write(scratchPad[LOW_ALARM_TEMP]); // low alarm temp

	// DS1820 and DS18S20 have no configuration register
	if (deviceAddress[0] != DS18S20MODEL)
		_wire->write(scratchPad[CONFIGURATION]);

	_wire->reset();

	// save the newly written values to eeprom
	_wire->select(deviceAddress);
	_wire->write(COPYSCRATCH, parasite);
	delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)

	if (parasite)
		delay(10); // 10ms delay
	_wire->reset();

}

bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress) {

	bool ret = false;
	_wire->reset();
	_wire->select(deviceAddress);
	_wire->write(READPOWERSUPPLY);
	if (_wire->read_bit() == 0)
		ret = true;
	_wire->reset();
	return ret;

}

// set resolution of all devices to 9, 10, 11, or 12 bits
// if new resolution is out of range, it is constrained.
void DallasTemperature::setResolution(uint8_t newResolution) {

	bitResolution = constrain(newResolution, 9, 12);
	DeviceAddress deviceAddress;
	for (int i = 0; i < devices; i++) {
		getAddress(deviceAddress, i);
		setResolution(deviceAddress, bitResolution, true);
	}

}

// set resolution of a device to 9, 10, 11, or 12 bits
// if new resolution is out of range, 9 bits is used.
bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
		uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {

	// ensure same behavior as setResolution(uint8_t newResolution)
	newResolution = constrain(newResolution, 9, 12);

	// return when stored value == new value
	if (getResolution(deviceAddress) == newResolution)
		return true;

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {

		// DS1820 and DS18S20 have no resolution configuration register
		if (deviceAddress[0] != DS18S20MODEL) {

			switch (newResolution) {
			case 12:
				scratchPad[CONFIGURATION] = TEMP_12_BIT;
				break;
			case 11:
				scratchPad[CONFIGURATION] = TEMP_11_BIT;
				break;
			case 10:
				scratchPad[CONFIGURATION] = TEMP_10_BIT;
				break;
			case 9:
			default:
				scratchPad[CONFIGURATION] = TEMP_9_BIT;
				break;
			}
			writeScratchPad(deviceAddress, scratchPad);

			// without calculation we can always set it to max
			bitResolution = max(bitResolution, newResolution);

			if (!skipGlobalBitResolutionCalculation
					&& (bitResolution > newResolution)) {
				bitResolution = newResolution;
				DeviceAddress deviceAddr;
				for (int i = 0; i < devices; i++) {
					getAddress(deviceAddr, i);
					bitResolution = max(bitResolution,
							getResolution(deviceAddr));
				}
			}
		}
		return true;  // new value set
	}

	return false;

}

// returns the global resolution
uint8_t DallasTemperature::getResolution() {
	return bitResolution;
}

// returns the current resolution of the device, 9-12
// returns 0 if device not found
uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {

	// DS1820 and DS18S20 have no resolution configuration register
	if (deviceAddress[0] == DS18S20MODEL)
		return 12;

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {
		switch (scratchPad[CONFIGURATION]) {
		case TEMP_12_BIT:
			return 12;

		case TEMP_11_BIT:
			return 11;

		case TEMP_10_BIT:
			return 10;

		case TEMP_9_BIT:
			return 9;
		}
	}
	return 0;

}

// sets the value of the waitForConversion flag
// TRUE : function requestTemperature() etc returns when conversion is ready
// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
//        (1) programmer has to check if the needed delay has passed
//        (2) but the application can do meaningful things in that time
void DallasTemperature::setWaitForConversion(bool flag) {
	waitForConversion = flag;
}

// gets the value of the waitForConversion flag
bool DallasTemperature::getWaitForConversion() {
	return waitForConversion;
}

// sets the value of the checkForConversion flag
// TRUE : function requestTemperature() etc will 'listen' to an IC to determine whether a conversion is complete
// FALSE: function requestTemperature() etc will wait a set time (worst case scenario) for a conversion to complete
void DallasTemperature::setCheckForConversion(bool flag) {
	checkForConversion = flag;
}

// gets the value of the waitForConversion flag
bool DallasTemperature::getCheckForConversion() {
	return checkForConversion;
}

bool DallasTemperature::isConversionComplete() {
	uint8_t b = _wire->read_bit();
	return (b == 1);
}

// sends command for all devices on the bus to perform a temperature conversion
void DallasTemperature::requestTemperatures() {

	_wire->reset();
	_wire->skip();
	_wire->write(STARTCONVO, parasite);

	// ASYNC mode?
	if (!waitForConversion)
		return;
	blockTillConversionComplete(bitResolution);

}

// sends command for one device to perform a temperature by address
// returns FALSE if device is disconnected
// returns TRUE  otherwise
bool DallasTemperature::requestTemperaturesByAddress(
		const uint8_t* deviceAddress) {

	uint8_t bitResolution = getResolution(deviceAddress);
	if (bitResolution == 0) {
		return false; //Device disconnected
	}

	_wire->reset();
	_wire->select(deviceAddress);
	_wire->write(STARTCONVO, parasite);

	// ASYNC mode?
	if (!waitForConversion)
		return true;

	blockTillConversionComplete(bitResolution);

	return true;

}

// Continue to check if the IC has responded with a temperature
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {

	int delms = millisToWaitForConversion(bitResolution);
	if (checkForConversion && !parasite) {
		unsigned long now = millis();
		while (!isConversionComplete() && (millis() - delms < now))
			;
	} else {
		delay(delms);
	}

}

// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {

	switch (bitResolution) {
	case 9:
		return 94;
	case 10:
		return 188;
	case 11:
		return 375;
	default:
		return 750;
	}

}

// sends command for one device to perform a temp conversion by index
bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {

	DeviceAddress deviceAddress;
	getAddress(deviceAddress, deviceIndex);

	return requestTemperaturesByAddress(deviceAddress);

}

// Fetch temperature for device index
float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {

	DeviceAddress deviceAddress;
	if (!getAddress(deviceAddress, deviceIndex)) {
		return DEVICE_DISCONNECTED_C;
	}

	return getTempC((uint8_t*) deviceAddress);

}

// Fetch temperature for device index
float DallasTemperature::getTempFByIndex(uint8_t deviceIndex) {

	DeviceAddress deviceAddress;

	if (!getAddress(deviceAddress, deviceIndex)) {
		return DEVICE_DISCONNECTED_F;
	}

	return getTempF((uint8_t*) deviceAddress);

}

// reads scratchpad and returns fixed-point temperature, scaling factor 2^-7
int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
		uint8_t* scratchPad) {

	int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
			| (((int16_t) scratchPad[TEMP_LSB]) << 3);

	/*
	 DS1820 and DS18S20 have a 9-bit temperature register.

	 Resolutions greater than 9-bit can be calculated using the data from
	 the temperature, and COUNT REMAIN and COUNT PER °C registers in the
	 scratchpad.  The resolution of the calculation depends on the model.

	 While the COUNT PER °C register is hard-wired to 16 (10h) in a
	 DS18S20, it changes with temperature in DS1820.

	 After reading the scratchpad, the TEMP_READ value is obtained by
	 truncating the 0.5°C bit (bit 0) from the temperature data. The
	 extended resolution temperature can then be calculated using the
	 following equation:

	 COUNT_PER_C - COUNT_REMAIN
	 TEMPERATURE = TEMP_READ - 0.25 + --------------------------
	 COUNT_PER_C

	 Hagai Shatz simplified this to integer arithmetic for a 12 bits
	 value for a DS18S20, and James Cameron added legacy DS1820 support.

	 See - http://myarduinotoy.blogspot.co.uk/2013/02/12bit-result-from-ds18s20.html
	 */

	if (deviceAddress[0] == DS18S20MODEL) {
		fpTemperature = ((fpTemperature & 0xfff0) << 3) - 16
				+ (((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7)
						/ scratchPad[COUNT_PER_C]);
	}

	return fpTemperature;
}

// returns temperature in 1/128 degrees C or DEVICE_DISCONNECTED_RAW if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_RAW is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad))
		return calculateTemperature(deviceAddress, scratchPad);
	return DEVICE_DISCONNECTED_RAW;

}

// returns temperature in degrees C or DEVICE_DISCONNECTED_C if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_C is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
float DallasTemperature::getTempC(const uint8_t* deviceAddress) {
	return rawToCelsius(getTemp(deviceAddress));
}

// returns temperature in degrees F or DEVICE_DISCONNECTED_F if the
// device's scratch pad cannot be read successfully.
// the numeric value of DEVICE_DISCONNECTED_F is defined in
// DallasTemperature.h. It is a large negative number outside the
// operating range of the device
float DallasTemperature::getTempF(const uint8_t* deviceAddress) {
	return rawToFahrenheit(getTemp(deviceAddress));
}

// returns true if the bus requires parasite power
bool DallasTemperature::isParasitePowerMode(void) {
	return parasite;
}

// IF alarm is not used one can store a 16 bit int of userdata in the alarm
// registers. E.g. an ID of the sensor.
// See github issue #29

// note if device is not connected it will fail writing the data.
void DallasTemperature::setUserData(const uint8_t* deviceAddress,
		int16_t data) {
	// return when stored value == new value
	if (getUserData(deviceAddress) == data)
		return;

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {
		scratchPad[HIGH_ALARM_TEMP] = data >> 8;
		scratchPad[LOW_ALARM_TEMP] = data & 255;
		writeScratchPad(deviceAddress, scratchPad);
	}
}

int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress) {
	int16_t data = 0;
	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {
		data = scratchPad[HIGH_ALARM_TEMP] << 8;
		data += scratchPad[LOW_ALARM_TEMP];
	}
	return data;
}

// note If address cannot be found no error will be reported.
int16_t DallasTemperature::getUserDataByIndex(uint8_t deviceIndex) {
	DeviceAddress deviceAddress;
	getAddress(deviceAddress, deviceIndex);
	return getUserData((uint8_t*) deviceAddress);
}

void DallasTemperature::setUserDataByIndex(uint8_t deviceIndex, int16_t data) {
	DeviceAddress deviceAddress;
	getAddress(deviceAddress, deviceIndex);
	setUserData((uint8_t*) deviceAddress, data);
}

// Convert float Celsius to Fahrenheit
float DallasTemperature::toFahrenheit(float celsius) {
	return (celsius * 1.8) + 32;
}

// Convert float Fahrenheit to Celsius
float DallasTemperature::toCelsius(float fahrenheit) {
	return (fahrenheit - 32) * 0.555555556;
}

// convert from raw to Celsius
float DallasTemperature::rawToCelsius(int16_t raw) {

	if (raw <= DEVICE_DISCONNECTED_RAW)
		return DEVICE_DISCONNECTED_C;
	// C = RAW/128
	return (float) raw * 0.0078125;

}

// convert from raw to Fahrenheit
float DallasTemperature::rawToFahrenheit(int16_t raw) {

	if (raw <= DEVICE_DISCONNECTED_RAW)
		return DEVICE_DISCONNECTED_F;
	// C = RAW/128
	// F = (C*1.8)+32 = (RAW/128*1.8)+32 = (RAW*0.0140625)+32
	return ((float) raw * 0.0140625) + 32;

}

#if REQUIRESALARMS

/*

 ALARMS:

 TH and TL Register Format

 BIT 7 BIT 6 BIT 5 BIT 4 BIT 3 BIT 2 BIT 1 BIT 0
 S    2^6   2^5   2^4   2^3   2^2   2^1   2^0

 Only bits 11 through 4 of the temperature register are used
 in the TH and TL comparison since TH and TL are 8-bit
 registers. If the measured temperature is lower than or equal
 to TL or higher than or equal to TH, an alarm condition exists
 and an alarm flag is set inside the DS18B20. This flag is
 updated after every temperature measurement; therefore, if the
 alarm condition goes away, the flag will be turned off after
 the next temperature conversion.

 */

// sets the high alarm temperature for a device in degrees Celsius
// accepts a float, but the alarm resolution will ignore anything
// after a decimal point.  valid range is -55C - 125C
void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress,
		int8_t celsius) {

	// return when stored value == new value
	if (getHighAlarmTemp(deviceAddress) == celsius)
		return;

	// make sure the alarm temperature is within the device's range
	if (celsius > 125)
		celsius = 125;
	else if (celsius < -55)
		celsius = -55;

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {
		scratchPad[HIGH_ALARM_TEMP] = (uint8_t) celsius;
		writeScratchPad(deviceAddress, scratchPad);
	}

}

// sets the low alarm temperature for a device in degrees Celsius
// accepts a float, but the alarm resolution will ignore anything
// after a decimal point.  valid range is -55C - 125C
void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress,
		int8_t celsius) {

	// return when stored value == new value
	if (getLowAlarmTemp(deviceAddress) == celsius)
		return;

	// make sure the alarm temperature is within the device's range
	if (celsius > 125)
		celsius = 125;
	else if (celsius < -55)
		celsius = -55;

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {
		scratchPad[LOW_ALARM_TEMP] = (uint8_t) celsius;
		writeScratchPad(deviceAddress, scratchPad);
	}

}

// returns a int8_t with the current high alarm temperature or
// DEVICE_DISCONNECTED for an address
int8_t DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress) {

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad))
		return (int8_t) scratchPad[HIGH_ALARM_TEMP];
	return DEVICE_DISCONNECTED_C;

}

// returns a int8_t with the current low alarm temperature or
// DEVICE_DISCONNECTED for an address
int8_t DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress) {

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad))
		return (int8_t) scratchPad[LOW_ALARM_TEMP];
	return DEVICE_DISCONNECTED_C;

}

// resets internal variables used for the alarm search
void DallasTemperature::resetAlarmSearch() {

	alarmSearchJunction = -1;
	alarmSearchExhausted = 0;
	for (uint8_t i = 0; i < 7; i++) {
		alarmSearchAddress[i] = 0;
	}

}

// This is a modified version of the OneWire::search method.
//
// Also added the OneWire search fix documented here:
// http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
//
// Perform an alarm search. If this function returns a '1' then it has
// enumerated the next device and you may retrieve the ROM from the
// OneWire::address variable. If there are no devices, no further
// devices, or something horrible happens in the middle of the
// enumeration then a 0 is returned.  If a new device is found then
// its address is copied to newAddr.  Use
// DallasTemperature::resetAlarmSearch() to start over.
bool DallasTemperature::alarmSearch(uint8_t* newAddr) {

	uint8_t i;
	int8_t lastJunction = -1;
	uint8_t done = 1;

	if (alarmSearchExhausted)
		return false;
	if (!_wire->reset())
		return false;

	// send the alarm search command
	_wire->write(0xEC, 0);

	for (i = 0; i < 64; i++) {

		uint8_t a = _wire->read_bit();
		uint8_t nota = _wire->read_bit();
		uint8_t ibyte = i / 8;
		uint8_t ibit = 1 << (i & 7);

		// I don't think this should happen, this means nothing responded, but maybe if
		// something vanishes during the search it will come up.
		if (a && nota)
			return false;

		if (!a && !nota) {
			if (i == alarmSearchJunction) {
				// this is our time to decide differently, we went zero last time, go one.
				a = 1;
				alarmSearchJunction = lastJunction;
			} else if (i < alarmSearchJunction) {

				// take whatever we took last time, look in address
				if (alarmSearchAddress[ibyte] & ibit) {
					a = 1;
				} else {
					// Only 0s count as pending junctions, we've already exhausted the 0 side of 1s
					a = 0;
					done = 0;
					lastJunction = i;
				}
			} else {
				// we are blazing new tree, take the 0
				a = 0;
				alarmSearchJunction = i;
				done = 0;
			}
			// OneWire search fix
			// See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238032295
		}

		if (a)
			alarmSearchAddress[ibyte] |= ibit;
		else
			alarmSearchAddress[ibyte] &= ~ibit;

		_wire->write_bit(a);
	}

	if (done)
		alarmSearchExhausted = 1;
	for (i = 0; i < 8; i++)
		newAddr[i] = alarmSearchAddress[i];
	return true;

}

// returns true if device address might have an alarm condition
// (only an alarm search can verify this)
bool DallasTemperature::hasAlarm(const uint8_t* deviceAddress) {

	ScratchPad scratchPad;
	if (isConnected(deviceAddress, scratchPad)) {

		int8_t temp = calculateTemperature(deviceAddress, scratchPad) >> 7;

		// check low alarm
		if (temp <= (int8_t) scratchPad[LOW_ALARM_TEMP])
			return true;

		// check high alarm
		if (temp >= (int8_t) scratchPad[HIGH_ALARM_TEMP])
			return true;
	}

	// no alarm
	return false;

}

// returns true if any device is reporting an alarm condition on the bus
bool DallasTemperature::hasAlarm(void) {

	DeviceAddress deviceAddress;
	resetAlarmSearch();
	return alarmSearch(deviceAddress);
}

// runs the alarm handler for all devices returned by alarmSearch()
// unless there no _AlarmHandler exist.
void DallasTemperature::processAlarms(void) {

if (!hasAlarmHandler())
{
	return;
}

	resetAlarmSearch();
	DeviceAddress alarmAddr;

	while (alarmSearch(alarmAddr)) {
		if (validAddress(alarmAddr)) {
			_AlarmHandler(alarmAddr);
		}
	}
}

// sets the alarm handler
void DallasTemperature::setAlarmHandler(const AlarmHandler *handler) {
	_AlarmHandler = handler;
}

// checks if AlarmHandler has been set.
bool DallasTemperature::hasAlarmHandler()
{
  return _AlarmHandler != NO_ALARM_HANDLER;
}

#endif

#if REQUIRESNEW

// MnetCS - Allocates memory for DallasTemperature. Allows us to instance a new object
void* DallasTemperature::operator new(unsigned int size) { // Implicit NSS obj size

	void * p;// void pointer
	p = malloc(size);// Allocate memory
	memset((DallasTemperature*)p,0,size);// Initialise memory

	//!!! CANT EXPLICITLY CALL CONSTRUCTOR - workaround by using an init() methodR - workaround by using an init() method
	return (DallasTemperature*) p;// Cast blank region to NSS pointer
}

// MnetCS 2009 -  Free the memory used by this instance
void DallasTemperature::operator delete(void* p) {

	DallasTemperature* pNss = (DallasTemperature*) p; // Cast to NSS pointer
	pNss->~DallasTemperature();// Destruct the object

	free(p);// Free the memory
}

#endif
vargham
Chipgyilok
Hozzászólások: 308
Csatlakozott: 2014. január 8. szerda, 8:32

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: vargham »

Szinkron működésű a lib. Vagyis addig blokkolja az egész mikrokontrollert, amíg kész nincs egy szenzor beolvasás. Írd át aszinkronra, vagyis mérés indít, timer indítás, a program fut tovább, frissíti a kijelzőt, majd a timer lejártakor a mért érték beolvasása a szenzorból.

Valamint használhatnál célzott 7 szegmens vezérlő IC-t. Nem kellene a CPU-val frissítgetni, csak egyszer kiküldeni (pl I2C), hogy mit jelezzen ki, az pedig elintézné.

Lentebb látszik a blokkoló rész:

Kód: Egész kijelölése

// Continue to check if the IC has responded with a temperature
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {

   int delms = millisToWaitForConversion(bitResolution);
   if (checkForConversion && !parasite) {
      unsigned long now = millis();
      while (!isConversionComplete() && (millis() - delms < now))
         ;
   } else {
      delay(delms);
   }

}

// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {

   switch (bitResolution) {
   case 9:
      return 94;
   case 10:
      return 188;
   case 11:
      return 375;
   default:
      return 750;
   }

}
bencsiknorbert
Újonc
Újonc
Hozzászólások: 4
Csatlakozott: 2018. március 9. péntek, 15:31

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: bencsiknorbert »

Köszönöm szépen a gyors segítséget, azt hiszem megpróbálkozok új alapokra építeni a hardvert és levenni a 7 szegmens kijelzők vezérlését a CPU-ról, addig még megérkeznek az új alkatrészek megpróbálom átírni aszinkronra a programot.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: Robert »

Tipp:
1, a hőmérés _nem_ online., hanem késleltetett:
a, eltelt min 750 msec -> hőfok kiolvasás történjen meg,
b, kiírási adat frissít
c, hőfokkonverzió indít...

2, a kiírás interrupt/timerINT alapú (frissítéssel)
a, INT tilt
b, hőfokkiolvasás
c, INT engedélyez
d, kiírás = kiolvasott érték
e, INT tilt
f, hőfok konverzió indít
g, INT engedélyez

Fontos: a hőfok konverzió/kiolvasás SW emulált 1Wire, ami _nem_ tűri a szinkronitáshibát! Ezért kell a kijelző INT-et letiltani.

A legjobb, legkézenfekvőbb az a külön kijelzőIC lenne. Ami lehet akár videochip (TM16xx sorozat, TDA..., MAX7219/7221 vagy bármi más 7szegmensvezérlő) vagy akár másik AVR is (ami SPI vagy I2C-n kapja az adatot).
Így az egyidejűség/frissítés nem okoz gondot.
http://www.tavir.hu - a gazda :)
bat4
Újonc
Újonc
Hozzászólások: 1
Csatlakozott: 2018. szeptember 10. hétfő, 21:55

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: bat4 »

Kicsit régi a téma, de hátha valakinek használ a hozzászólásom.
"Non blocking" (aszinkron) módba lehet kapcsolni az olvasást a következő paranccsal:

Kód: Egész kijelölése

sensors.setWaitForConversion(false); 
Így a program tovább lép várakozás nélkül és a következő ciklusban kiírja a hőmérsékletet.

Esetleg ha nem tetszik a "non blocking" (aszinkron) mód akkor fel lehet gyorsítani az olvasást (gyakorlatilag a hőmérséklet konverziót) 9 bit felbontásba kapcsolással:

Kód: Egész kijelölése

sensors.setResolution(9);
93 msec 9bit felbontás esetén.
750 msec 12bit felbontás esetén.

Nálam elegendő volt az az első vagy második kódrész beleírása a setup() részbe (Arduino uno).
stegyo
Újonc
Újonc
Hozzászólások: 8
Csatlakozott: 2020. július 3. péntek, 16:49

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: stegyo »

Köszi, segítettél!
stegyo
Újonc
Újonc
Hozzászólások: 8
Csatlakozott: 2020. július 3. péntek, 16:49

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: stegyo »

Az alábbiakban kérném a segítségeteket! Adott 4 db DS18B20, különböző helyeken mérnek hőmérsékletet. Hogyan tudom meghatározni a buszra kötött szenzorok sorszámát/azonosítóját, hogy a kijelzőn az jelenjen meg, amire szükség van az adott szituációban?
Előre is köszi
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: Robert »

Csak próbával:
1, Valamelyik 1wire program és a hőmérőket listázod. És 1-1 darabot lekötsz. ami a listából hiányzik azonosító - azt kötötted le.
2, Valamelyik 1wire program és a hőmérőket és hőfokaikat listázod. Az egyiket meleg vízbe vagy jégbe rakod. Így a hőfok kiugrásból tudod melyik azonosítójú a vizsgált.
http://www.tavir.hu - a gazda :)
stegyo
Újonc
Újonc
Hozzászólások: 8
Csatlakozott: 2020. július 3. péntek, 16:49

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: stegyo »

Köszönöm a segítséget, sikerült megoldani az útmutatásod alapján, és nagyon jól működik is!
Üdv
Gyuri :D
stegyo
Újonc
Újonc
Hozzászólások: 8
Csatlakozott: 2020. július 3. péntek, 16:49

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: stegyo »

Szia Robi!
A szoftverben két megoldást próbáltam ki:
- az index alapú lekérdezés egy kicsit egyszerűbb megoldás, hátránya, hogy a szenzorok bekötésének sorrendjére figyelni kell, mert ez alapján kapják az indexüket a buszon, az sem szerencsés, ha pl. kontakthiba miatt kiesik az egyik, akkor szenzorok a buszon új indexet kapnak és felborul a sorrend
- jobb megoldásnak találtam a szenzorok cím alapú lekérdezését. Igaz egy kis többlet munkával jár, hiszen ki kell deríteni a címeket egy külön kis progival, majd be kell őket másolni a főprogramba, de megéri, mert így nem borul a sorrend, minden a helyén van és marad.
Üdv
Gyuri
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: DS18B20 olvasás alatt várakozik a képernyő írása

Hozzászólás Szerző: Robert »

Cím alapú, és ne index alapút használj.
Ha kiesik egy szenzor, akkor az index alapú eltolódik, míg a cím alapú a nem válaszol (-127) eredményt adja vissza.
Ha meg 85-t kapsz vissza, akkor a szenzor resetelt vagy energiaellátási probléma.
http://www.tavir.hu - a gazda :)
Válasz küldése