Updated versions of DallasTemperature and OneWire libraries

This commit is contained in:
Robo Print
2015-06-27 22:50:24 +02:00
parent 2b41450e09
commit 0300df686e
16 changed files with 1001 additions and 460 deletions
File diff suppressed because it is too large Load Diff
+76 -36
View File
@@ -1,6 +1,8 @@
#ifndef DallasTemperature_h
#define DallasTemperature_h
#define DALLASTEMPLIBVERSION "3.7.2"
// 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
@@ -20,9 +22,10 @@
#include <OneWire.h>
// Model IDs
#define DS18S20MODEL 0x10
#define DS18S20MODEL 0x10 // also DS1820
#define DS18B20MODEL 0x28
#define DS1822MODEL 0x22
#define DS1825MODEL 0x3B
// OneWire commands
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
@@ -51,7 +54,9 @@
#define TEMP_12_BIT 0x7F // 12 bit
// Error Codes
#define DEVICE_DISCONNECTED -127
#define DEVICE_DISCONNECTED_C -127
#define DEVICE_DISCONNECTED_F -196.6
#define DEVICE_DISCONNECTED_RAW -7040
typedef uint8_t DeviceAddress[8];
@@ -61,54 +66,71 @@ class DallasTemperature
DallasTemperature(OneWire*);
// initalize bus
// initialise bus
void begin(void);
// returns the number of devices found on the bus
uint8_t getDeviceCount(void);
// returns true if address is valid
bool validAddress(uint8_t*);
bool validAddress(const uint8_t*);
// finds an address at a given index on the bus
bool getAddress(uint8_t*, const uint8_t);
bool getAddress(uint8_t*, uint8_t);
// attempt to determine if the device at the given address is connected to the bus
bool isConnected(uint8_t*);
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(uint8_t*, uint8_t*);
bool isConnected(const uint8_t*, uint8_t*);
// read device's scratchpad
void readScratchPad(uint8_t*, uint8_t*);
void readScratchPad(const uint8_t*, uint8_t*);
// write device's scratchpad
void writeScratchPad(uint8_t*, const uint8_t*);
void writeScratchPad(const uint8_t*, const uint8_t*);
// read device's power requirements
bool readPowerSupply(uint8_t*);
bool readPowerSupply(const uint8_t*);
// returns the current resolution, 9-12
uint8_t getResolution(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
void setResolution(uint8_t*, uint8_t);
// sends command for all devices on the bus to perform a temperature conversion
bool setResolution(const uint8_t*, uint8_t);
// 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
void requestTemperaturesByAddress(uint8_t*);
bool requestTemperaturesByAddress(const uint8_t*);
// sends command for one device to perform a temperature conversion by index
void requestTemperaturesByIndex(uint8_t);
bool requestTemperaturesByIndex(uint8_t);
// returns temperature raw value (12 bit integer of 1/16 degrees C)
int16_t getTemp(const uint8_t*);
// returns temperature in degrees C
float getTempC(uint8_t*);
float getTempC(const uint8_t*);
// returns temperature in degrees F
float getTempF(uint8_t*);
float getTempF(const uint8_t*);
// Get temperature for device index (slow)
float getTempCByIndex(uint8_t);
@@ -118,26 +140,28 @@ class DallasTemperature
// returns true if the bus requires parasite power
bool isParasitePowerMode(void);
bool isConversionAvailable(const uint8_t*);
#if REQUIRESALARMS
typedef void AlarmHandler(uint8_t*);
typedef void AlarmHandler(const uint8_t*);
// sets the high alarm temperature for a device
// accepts a char. valid range is -55C - 125C
void setHighAlarmTemp(uint8_t*, const char);
void setHighAlarmTemp(const uint8_t*, char);
// sets the low alarm temperature for a device
// accepts a char. valid range is -55C - 125C
void setLowAlarmTemp(uint8_t*, const char);
void setLowAlarmTemp(const uint8_t*, char);
// returns a signed char with the current high alarm temperature for a device
// in the range -55C - 125C
char getHighAlarmTemp(uint8_t*);
char getHighAlarmTemp(const uint8_t*);
// returns a signed char with the current low alarm temperature for a device
// in the range -55C - 125C
char getLowAlarmTemp(uint8_t*);
char getLowAlarmTemp(const uint8_t*);
// resets internal variables used for the alarm search
void resetAlarmSearch(void);
@@ -146,7 +170,7 @@ class DallasTemperature
bool alarmSearch(uint8_t*);
// returns true if ia specific device has an alarm
bool hasAlarm(uint8_t*);
bool hasAlarm(const uint8_t*);
// returns true if any device is reporting an alarm on the bus
bool hasAlarm(void);
@@ -155,22 +179,28 @@ class DallasTemperature
void processAlarms(void);
// sets the alarm handler
void setAlarmHandler(AlarmHandler *);
void setAlarmHandler(const AlarmHandler *);
// The default alarm handler
static void defaultAlarmHandler(uint8_t*);
static void defaultAlarmHandler(const uint8_t*);
#endif
// convert from celcius to farenheit
static float toFahrenheit(const float);
// convert from Celsius to Fahrenheit
static float toFahrenheit(float);
// convert from farenheit to celsius
static float toCelsius(const 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
// initalize memory area
// initialize memory area
void* operator new (unsigned int);
// delete memory reference
@@ -186,16 +216,26 @@ class DallasTemperature
// used to determine the delay amount needed to allow for the
// temperature conversion to take place
int conversionDelay;
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;
// Take a pointer to one wire instance
OneWire* _wire;
// reads scratchpad and returns the temperature in degrees C
float calculateTemperature(uint8_t*, uint8_t*);
// reads scratchpad and returns the raw temperature
int16_t calculateTemperature(const uint8_t*, uint8_t*);
int16_t millisToWaitForConversion(uint8_t);
void blockTillConversionComplete(uint8_t, const uint8_t*);
#if REQUIRESALARMS
@@ -4,10 +4,14 @@ Arduino Library for Dallas Temperature ICs
Usage
-----
This library supports the following devices:
DS18B20
DS18S20 - Please note there appears to be an issue with this series.
DS1822
This library supports the following devices :
* DS18B20
* DS18S20 - Please note there appears to be an issue with this series.
* DS1822
* DS1820
You will need a pull-up resistor of about 5 KOhm between the 1-Wire data line
and your 5V power. If you are using the DS18B20, ground pins 1 and 3. The
@@ -15,7 +19,18 @@ centre pin is the data line '1-wire'.
We have included a "REQUIRESNEW" and "REQUIRESALARMS" definition. If you
want to slim down the code feel free to use either of these by including
#define REQUIRESNEW or #define REQUIRESALARMS a the top of DallasTemperature.h
#define REQUIRESNEW
or
#define REQUIRESALARMS
at the top of DallasTemperature.h
Credits
-------
@@ -26,6 +41,8 @@ Miles Burton <miles@mnetcs.com> originally developed this library.
Tim Newsome <nuisance@casualhacker.net> added support for multiple sensors on
the same bus.
Guil Barros [gfbarros@bappos.com] added getTempByAddress (v3.5)
Rob Tillaart [rob.tillaart@gmail.com] added async modus (v3.7.0)
Website
-------
+85
View File
@@ -0,0 +1,85 @@
This file contains the change history of the Dallas Temperature Control Library.
VERSION 3.7.2 BETA
===================
DATE: 6 DEC 2011
- Jordan Hochenbaum [jhochenbaum@gmail.com] updated library for compatibility with Arduino 1.0.
VERSION 3.7.0 BETA
===================
DATE: 11 JAN 2011
- Rob Tillaart [rob.tillaart@gmail.com] added async modus (v3.7.0)
The library is backwards compatible with version 3.6.0
MAJOR: async modus
------------------
- Added - private bool waitForConversion.
This boolean is default set to true in the Constructor to keep the library backwards compatible. If this flag is true calls to requestTemperatures(), requestTemperaturesByAddress() et al, will be blocking with the appropiate time specified (in datasheet) for the resolution used. If the flag is set to false, requestTemperatures() et al, will return immediately after the conversion command is send over the 1-wire interface. The programmer is responsible to wait long enough before reading the temperature values. This enables the application to do other things while waiting for a new reading, like calculations, update LCD, read/write other IO lines etc. See examples.
- Added - void setWaitForConversion(bool);
To set the flag to true or false, depending on the modus needed.
- Added - bool getWaitForConversion(void);
To get the current value of the flag.
- Changed - void requestTemperatures(void);
Added a test (false == waitForConversion) to return immediately after the conversion command instead of waiting until the conversion is ready.
- Changed - bool requestTemperaturesByAddress(uint8_t*);
Added a test (false == waitForConversion) to return immediately after the conversion command instead of waiting until the conversion is ready.
MINOR version number
--------------------
- Added - #define DALLASTEMPLIBVERSION "3.7.0"
To indicate the version number in .h file
MINOR internal var bitResolution
----------------------------
- Changed - private int conversionDelay - is renamed to - private int bitResolution
As this variable holds the resolution. The delay for the conversion is derived from it.
- Changed - uint8_t getResolution(uint8_t* deviceAddress);
If the device is not connected, it returns 0, otherwise it returns the resolution of the device.
- Changed - bool setResolution(uint8_t* deviceAddress, uint8_t newResolution);
If the device is not connected, it returns FALSE (fail), otherwise it returns TRUE (succes).
- Added - uint8_t getResolution();
Returns bitResolution.
- Added - void setResolution(uint8_t newResolution)
Sets the internal variable bitResolution, and all devices to this value
MINOR check connected state
----------------------------
- Changed - bool requestTemperaturesByIndex(deviceIndex)
Changed return type from void to bool. The function returns false if the device identified with [deviceIndex] is not found on the bus and true otherwise.
- Changed - bool requestTemperaturesByAddress(deviceAddress)
Changed return type from void to bool. The function returns false if the device identified with [deviceAddress] is not found on the bus and true otherwise.
Added code to handle the DS18S20 which has a 9 bit resolution separately.
Changed code so the blocking delay matches the bitResolution set in the device with deviceAddress.
- Changed - bool requestTemperaturesByIndex(uint8_t deviceIndex)
Changed return type from void to bool. The function returns false if the device identified with [deviceIndex] is not found on the bus and true otherwise.
VERSION 3.6.0
==============
DATE: 2010-10-10
- no detailed change history known except:
- The OneWire code has been derived from
http://www.arduino.cc/playground/Learning/OneWire.
- Miles Burton <miles@mnetcs.com> originally developed this library.
- Tim Newsome <nuisance@casualhacker.net> added support for multiple sensors on
the same bus.
- Guil Barros [gfbarros@bappos.com] added getTempByAddress (v3.5)
@@ -2,7 +2,7 @@
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
@@ -2,8 +2,7 @@
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 9
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
@@ -2,7 +2,7 @@
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
@@ -74,8 +74,8 @@ void setup(void)
Serial.println();
// set the resolution to 9 bit
sensors.setResolution(insideThermometer, 9);
sensors.setResolution(outsideThermometer, 9);
sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
Serial.print("Device 0 Resolution: ");
Serial.print(sensors.getResolution(insideThermometer), DEC);
@@ -2,7 +2,7 @@
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
@@ -2,7 +2,7 @@
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
@@ -52,9 +52,9 @@ void setup(void)
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION,DEC);
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
@@ -0,0 +1,66 @@
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(115200);
Serial.println("Dallas Temperature Control Library - Async Demo");
Serial.println("\nDemo shows the difference in length of the call\n\n");
// Start up the library
sensors.begin();
}
void loop(void)
{
// Request temperature conversion (traditional)
Serial.println("Before blocking requestForConversion");
unsigned long start = millis();
sensors.requestTemperatures();
unsigned long stop = millis();
Serial.println("After blocking requestForConversion");
Serial.print("Time used: ");
Serial.println(stop - start);
// get temperature
Serial.print("Temperature: ");
Serial.println(sensors.getTempCByIndex(0));
Serial.println("\n");
// Request temperature conversion - non-blocking / async
Serial.println("Before NON-blocking/async requestForConversion");
start = millis();
sensors.setWaitForConversion(false); // makes it async
sensors.requestTemperatures();
sensors.setWaitForConversion(true);
stop = millis();
Serial.println("After NON-blocking/async requestForConversion");
Serial.print("Time used: ");
Serial.println(stop - start);
// 9 bit resolution by default
// Note the programmer is responsible for the right delay
// we could do something usefull here instead of the delay
int resolution = 9;
delay(750/ (1 << (12-resolution)));
// get temperature
Serial.print("Temperature: ");
Serial.println(sensors.getTempCByIndex(0));
Serial.println("\n\n\n\n");
delay(5000);
}
@@ -0,0 +1,80 @@
//
// Sample of using Async reading of Dallas Temperature Sensors
//
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
int resolution = 12;
unsigned long lastTempRequest = 0;
int delayInMillis = 0;
float temperature = 0.0;
int idle = 0;
//
// SETUP
//
void setup(void)
{
Serial.begin(115200);
Serial.println("Dallas Temperature Control Library - Async Demo");
Serial.print("Library Version: ");
Serial.println(DALLASTEMPLIBVERSION);
Serial.println("\n");
sensors.begin();
sensors.getAddress(tempDeviceAddress, 0);
sensors.setResolution(tempDeviceAddress, resolution);
sensors.setWaitForConversion(false);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
pinMode(13, OUTPUT);
}
void loop(void)
{
if (millis() - lastTempRequest >= delayInMillis) // waited long enough??
{
digitalWrite(13, LOW);
Serial.print(" Temperature: ");
temperature = sensors.getTempCByIndex(0);
Serial.println(temperature, resolution - 8);
Serial.print(" Resolution: ");
Serial.println(resolution);
Serial.print("Idle counter: ");
Serial.println(idle);
Serial.println();
idle = 0;
// immediately after fetching the temperature we request a new sample
// in the async modus
// for the demo we let the resolution change to show differences
resolution++;
if (resolution > 12) resolution = 9;
sensors.setResolution(tempDeviceAddress, resolution);
sensors.requestTemperatures();
delayInMillis = 750 / (1 << (12 - resolution));
lastTempRequest = millis();
}
digitalWrite(13, HIGH);
// we can do usefull things here
// for the demo we just count the idle time in millis
delay(1);
idle++;
}
+9 -6
View File
@@ -7,8 +7,8 @@
#######################################
DallasTemperature KEYWORD1
OneWire KEYWORD1
AlarmHandler KEYWORD1
DeviceAddress KEYWORD1
AlarmHandler KEYWORD1
DeviceAddress KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
@@ -20,12 +20,14 @@ getTempC KEYWORD2
toFahrenheit KEYWORD2
getTempF KEYWORD2
getTempCByIndex KEYWORD2
getTempFByIndex KEYWORD2
getTempFByIndex KEYWORD2
setWaitForConversion KEYWORD2
getWaitForConversion KEYWORD2
requestTemperatures KEYWORD2
requestTemperaturesByAddress KEYWORD2
requestTemperaturesByIndex KEYWORD2
requestTemperaturesByAddress KEYWORD2
requestTemperaturesByIndex KEYWORD2
isParasitePowerMode KEYWORD2
begin KEYWORD2
begin KEYWORD2
getDeviceCount KEYWORD2
getAddress KEYWORD2
validAddress KEYWORD2
@@ -45,6 +47,7 @@ processAlarmss KEYWORD2
setAlarmHandlers KEYWORD2
defaultAlarmHandler KEYWORD2
calculateTemperature KEYWORD2
millisToWaitForConversion KEYWORD2
#######################################
# Constants (LITERAL1)
+39
View File
@@ -0,0 +1,39 @@
{
"name": "DallasTemperature",
"keywords": "onewire, 1-wire, bus, sensor, temperature",
"description": "Arduino Library for Dallas Temperature ICs (DS18B20, DS18S20, DS1822, DS1820)",
"repository":
{
"type": "git",
"url": "https://github.com/milesburton/Arduino-Temperature-Control-Library.git"
},
"authors":
[
{
"name": "Miles Burton",
"email": "miles@mnetcs.com",
"url": "http://www.milesburton.com",
"maintainer": true
},
{
"name": "Tim Newsome",
"email": "nuisance@casualhacker.net"
},
{
"name": "Guil Barros",
"email": "gfbarros@bappos.com"
},
{
"name": "Rob Tillaart",
"email": "rob.tillaart@gmail.com"
}
],
"dependencies":
{
"name": "OneWire",
"authors": "Paul Stoffregen",
"frameworks": "arduino"
},
"frameworks": "arduino",
"platforms": "atmelavr"
}
+47 -17
View File
@@ -4,6 +4,24 @@ Copyright (c) 2007, Jim Studt (original old version - many contributors since)
The latest version of this library may be found at:
http://www.pjrc.com/teensy/td_libs_OneWire.html
OneWire has been maintained by Paul Stoffregen (paul@pjrc.com) since
January 2010. At the time, it was in need of many bug fixes, but had
been abandoned the original author (Jim Studt). None of the known
contributors were interested in maintaining OneWire. Paul typically
works on OneWire every 6 to 12 months. Patches usually wait that
long. If anyone is interested in more actively maintaining OneWire,
please contact Paul.
Version 2.2:
Teensy 3.0 compatibility, Paul Stoffregen, paul@pjrc.com
Arduino Due compatibility, http://arduino.cc/forum/index.php?topic=141030
Fix DS18B20 example negative temperature
Fix DS18B20 example's low res modes, Ken Butcher
Improve reset timing, Mark Tillotson
Add const qualifiers, Bertrik Sikken
Add initial value input to crc16, Bertrik Sikken
Add target_search() function, Scott Roberts
Version 2.1:
Arduino 1.0 compatibility, Paul Stoffregen
Improve temperature example, Paul Stoffregen
@@ -136,13 +154,13 @@ uint8_t OneWire::reset(void)
DIRECT_WRITE_LOW(reg, mask);
DIRECT_MODE_OUTPUT(reg, mask); // drive output low
interrupts();
delayMicroseconds(500);
delayMicroseconds(480);
noInterrupts();
DIRECT_MODE_INPUT(reg, mask); // allow it to float
delayMicroseconds(80);
delayMicroseconds(70);
r = !DIRECT_READ(reg, mask);
interrupts();
delayMicroseconds(420);
delayMicroseconds(410);
return r;
}
@@ -249,13 +267,13 @@ void OneWire::read_bytes(uint8_t *buf, uint16_t count) {
//
// Do a ROM select
//
void OneWire::select( uint8_t rom[8])
void OneWire::select(const uint8_t rom[8])
{
int i;
uint8_t i;
write(0x55); // Choose ROM
for( i = 0; i < 8; i++) write(rom[i]);
for (i = 0; i < 8; i++) write(rom[i]);
}
//
@@ -280,17 +298,30 @@ void OneWire::depower()
// You do not need to do it for the first search, though you could.
//
void OneWire::reset_search()
{
{
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = FALSE;
LastFamilyDiscrepancy = 0;
for(int i = 7; ; i--)
{
for(int i = 7; ; i--) {
ROM_NO[i] = 0;
if ( i == 0) break;
}
}
}
// Setup the search to find the device type 'family_code' on the next call
// to search(*newAddr) if it is present.
//
void OneWire::target_search(uint8_t family_code)
{
// set the search state to find SearchFamily type devices
ROM_NO[0] = family_code;
for (uint8_t i = 1; i < 8; i++)
ROM_NO[i] = 0;
LastDiscrepancy = 64;
LastFamilyDiscrepancy = 0;
LastDeviceFlag = FALSE;
}
//
// Perform a search. If this function returns a '1' then it has
@@ -461,7 +492,7 @@ static const uint8_t PROGMEM dscrc_table[] = {
// compared to all those delayMicrosecond() calls. But I got
// confused, so I use this table from the examples.)
//
uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)
uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
@@ -475,7 +506,7 @@ uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)
// Compute a Dallas Semiconductor 8 bit CRC directly.
// this is much slower, but much smaller, than the lookup table.
//
uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)
uint8_t OneWire::crc8(const uint8_t *addr, uint8_t len)
{
uint8_t crc = 0;
@@ -493,23 +524,22 @@ uint8_t OneWire::crc8( uint8_t *addr, uint8_t len)
#endif
#if ONEWIRE_CRC16
bool OneWire::check_crc16(uint8_t* input, uint16_t len, uint8_t* inverted_crc)
bool OneWire::check_crc16(const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc)
{
uint16_t crc = ~crc16(input, len);
crc = ~crc16(input, len, crc);
return (crc & 0xFF) == inverted_crc[0] && (crc >> 8) == inverted_crc[1];
}
uint16_t OneWire::crc16(uint8_t* input, uint16_t len)
uint16_t OneWire::crc16(const uint8_t* input, uint16_t len, uint16_t crc)
{
static const uint8_t oddparity[16] =
{ 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
uint16_t crc = 0; // Starting seed is zero.
for (uint16_t i = 0 ; i < len ; i++) {
// Even though we're just copying a byte from the input,
// we'll be doing 16-bit computation with it.
uint16_t cdata = input[i];
cdata = (cdata ^ (crc & 0xff)) & 0xff;
cdata = (cdata ^ crc) & 0xff;
crc >>= 8;
if (oddparity[cdata & 0x0F] ^ oddparity[cdata >> 4])
+46 -9
View File
@@ -56,13 +56,44 @@
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM asm("r30")
#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) ((*(base+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*(base+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask) ((*(base+2)) |= (mask))
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+2)) |= (mask))
#elif defined(__MK20DX128__)
#define PIN_TO_BASEREG(pin) (portOutputRegister(pin))
#define PIN_TO_BITMASK(pin) (1)
#define IO_REG_TYPE uint8_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) (*((base)+512))
#define DIRECT_MODE_INPUT(base, mask) (*((base)+640) = 0)
#define DIRECT_MODE_OUTPUT(base, mask) (*((base)+640) = 1)
#define DIRECT_WRITE_LOW(base, mask) (*((base)+256) = 1)
#define DIRECT_WRITE_HIGH(base, mask) (*((base)+128) = 1)
#elif defined(__SAM3X8E__)
// Arduino 1.5.1 may have a bug in delayMicroseconds() on Arduino Due.
// http://arduino.cc/forum/index.php/topic,141030.msg1076268.html#msg1076268
// If you have trouble with OneWire on Arduino Due, please check the
// status of delayMicroseconds() before reporting a bug in OneWire!
#define PIN_TO_BASEREG(pin) (&(digitalPinToPort(pin)->PIO_PER))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) (((*((base)+15)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) ((*((base)+5)) = (mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*((base)+4)) = (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*((base)+13)) = (mask))
#define DIRECT_WRITE_HIGH(base, mask) ((*((base)+12)) = (mask))
#ifndef PROGMEM
#define PROGMEM
#endif
#ifndef pgm_read_byte
#define pgm_read_byte(addr) (*(const uint8_t *)(addr))
#endif
#elif defined(__PIC32MX__)
#include <plib.h> // is this necessary?
#define PIN_TO_BASEREG(pin) (portModeRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin) (digitalPinToBitMask(pin))
#define IO_REG_TYPE uint32_t
@@ -101,7 +132,7 @@ class OneWire
uint8_t reset(void);
// Issue a 1-Wire rom select command, you do the reset first.
void select( uint8_t rom[8]);
void select(const uint8_t rom[8]);
// Issue a 1-Wire rom skip command, to address all on bus.
void skip(void);
@@ -137,6 +168,10 @@ class OneWire
// 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
@@ -149,7 +184,7 @@ class OneWire
#if ONEWIRE_CRC
// Compute a Dallas Semiconductor 8 bit CRC, these are used in the
// ROM and scratchpad registers.
static uint8_t crc8( uint8_t *addr, uint8_t len);
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.
@@ -170,8 +205,9 @@ class OneWire
// @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(uint8_t* input, uint16_t len, uint8_t* inverted_crc);
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
@@ -183,8 +219,9 @@ class OneWire
// 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(uint8_t* input, uint16_t len);
static uint16_t crc16(const uint8_t* input, uint16_t len, uint16_t crc = 0);
#endif
#endif
};
@@ -7,7 +7,7 @@
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library
OneWire ds(10); // on pin 10
OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)
void setup(void) {
Serial.begin(9600);
@@ -62,7 +62,7 @@ void loop(void) {
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
@@ -72,7 +72,7 @@ void loop(void) {
ds.write(0xBE); // Read Scratchpad
Serial.print(" Data = ");
Serial.print(present,HEX);
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
@@ -83,21 +83,24 @@ void loop(void) {
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();
// convert the data to actual temperature
unsigned int raw = (data[1] << 8) | data[0];
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;