origine inconnue
utilisée pour
PotrollLoggerTemp et ses ancêtres.
DS18B20.h
#ifndef DS18B20_H
#define DS18B20_H
#include <Arduino.h>
class DS18B20 {
public:
void begin(int p);
int read();
private:
byte OneWireInByte();
void OneWireReset();
void OneWireOutByte(byte d) ;
int pin;
long idhigh;
long idlow;
};
#endif
DS18B20.cpp
#include "DS18B20.h"
#include <Arduino.h>
void DS18B20::begin(int p){
pin=p;
}
int DS18B20::read(){
int HighByte, LowByte, TReading ;
OneWireReset();
OneWireOutByte(0xcc);
OneWireOutByte( 0x44); // perform temperature conversion, strong pullup for one sec
OneWireReset();
OneWireOutByte(0xcc);
OneWireOutByte( 0xbe);
LowByte = OneWireInByte();
HighByte = OneWireInByte();
TReading = (HighByte << 8) + LowByte;
return TReading;
}
void DS18B20::OneWireReset() // reset. Should improve to act as a presence pulse
{
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT); // bring low for 500 us
delayMicroseconds(500);
pinMode(pin, INPUT_PULLUP);
delayMicroseconds(500);
}
void DS18B20::OneWireOutByte(byte d) // output byte d (least sig bit first).
{
byte n;
for(n=8; n!=0; n--)
{
if ((d & 0x01) == 1) // test least sig bit
{
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT);
delayMicroseconds(5);
pinMode(pin, INPUT_PULLUP);
delayMicroseconds(60);
}
else
{
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT);
delayMicroseconds(60);
pinMode(pin, INPUT_PULLUP);
}
d=d>>1; // now the next bit is in the least sig bit position.
}
}
byte DS18B20::OneWireInByte() // read byte, least sig byte first
{
byte d, n, b;
for (n=0; n<8; n++)
{
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT);
delayMicroseconds(5);
pinMode(pin, INPUT_PULLUP);
delayMicroseconds(5);
b = digitalRead(pin);
delayMicroseconds(50);
d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}
return(d);
}