OneWireCRC
.:Intro:.
This library fulfills two purposes - firstly to provide general 1-wire support for mbed (OneWireCRC.h/.cpp + OneWireDefs.h), and secondly to provide support for 1-wire thermometers DS18B20 and DS18S20 (OneWireThermometer.h/.cpp, DS18B20.h/.cpp, DS18S20.h/.cpp).
The DS18B20 is for current designs and supports 9, 10, 11 or 12 bits temperature read-out resolutions. The DS18S20 is a drop-in replacement for the older DS1820, and fundementally has a 9 bit resolution which can be enhanced. They are fundementally the same except for switchable resolutions and code for dealing with their read-outs.
Design-wise both the DS18B20 and DS18S20 could have be coded as one class, with functional paths being switched according the device type, but I decided to go for a class (inheritance) structure, with functionality being switched polymorphically. Anyway its kinda' cooler than a bunch of IF ELSE statements, but involves a class hierarchy maintenance overhead.
The Maxim/Dallas documentation for these devices can be found at http://datasheets.maxim-ic.com/en/ds/DS18B20.pdf and http://datasheets.maxim-ic.com/en/ds/DS18S20.pdf, and are well worth reading through to get a feel for their operation.
The 1-wire code itself (OneWireCRC.h/.cpp) started out as a port of Jim Studt's Arduino library (which in turn was a port of Dallas code). While doing so I ended up refering to the Maxim/Dallas Application Note 126 - 1-Wire Communication Through Software (http://pdfserv.maxim-ic.com/en/an/AN126.pdf), and restored part of the code to that recommended in the application note, including all the recommend bus timings. Another recommend 1-wire document which has a full decription of the 1-wire bus architecture is Application Note 937 (http://pdfserv.maxim-ic.com/en/an/appibstd.pdf).
If you want to use parasitic power for these devices, first read my note at http://mbed.org/users/snatch59/notebook/1-wire-parasitic-power/ for an easier way to set this up.
.:How To Use:.
Connections:
The demo uses mbed pin 25. Change the value in main.cpp to suit your preference. This pin should be connected to the DQ (middle) leg of the DS18B20 (or DS18S20). The device's Ground leg should connect to the mbed GND pin, and the Vdd leg to the mbed VU pin (5.0V). Finally a 4.7K ohm resistor should be connected between DQ and Vdd. If in doubt as to which leg is which, check the appropriate data sheet listed in the .:Intro:. above.
The DS18B20/DS18S20 should be positioned at least 6 inches (15 cm) away from the mbed board and the
(4.7K ohm) pull-up resistor, otherwise heat from the mbed board will affect the readings by up to 2 deg C.
This demo uses 5 seconds between readings. To maintain accuracy it is suggested that readings should not
be taken closer than 5 seconds apart, to lessen the effect of heat generated in the device skewing the readings. If doubt - experiment and find out what works for you.
Demo:
The demo is ready to use with the more common DS18B20. To change to a DS18S20 comment out the DS18B20 define, and remove the comment for the DS18S20 define, and re-build. The demo operates with parasitic power off (in this implementation it doesn't really matter as its the same code for both), CRC on, and device addressing on.
CRC on validates returned data by using a CRC check and can be set to false (0). Device addressing is in out of interest, since with one device on the bus, it is kind of academic. However 'useAddress' true (1) will tell you if you have the wrong device connected, and which one you should have connected, as the device address also contains the device type id.
Usage:
Usage is pretty much as set out in main.cpp. setResolution( ) allows you to change the temperature conversion resolution to 9, 10, 11 or 12 bits. If you don't use setResoluton( ) the BS18B20 will run by default in 12 bit mode. Though you can call setResolution( ) for the BS18S20 it doesn't do anything, and it will run in 9 bit + enhanced mode.
If you wish to look at writing code for other Maxim/Dallas 1-wire devices (of which there seems to be less variety these days, with the addressable switch range having been dropped for example), check out OneWireCRC.h/cpp, and OneWireDefs.h to provide your 1-wire bus interface.
#include
#include "DS18S20.h"
#include "DS18B20.h"
#include "OneWireDefs.h"
//#define THERMOMETER DS18S20
#define THERMOMETER DS18B20
int main()
{
// device( crcOn, useAddress, parasitic, mbed pin )
THERMOMETER device(true, true, false, p25);
while (!device.initialize()); // keep calling until it works
while (true)
{
// changing the resolutions only affects the DS18B20. The DS18S20 is fixed.
device.setResolution(nineBit);
device.readTemperature();
wait(5);
device.setResolution(tenBit);
device.readTemperature();
wait(5);
device.setResolution(elevenBit);
device.readTemperature();
wait(5);
device.setResolution(twelveBit);
device.readTemperature();
wait(5);
}
return EXIT_SUCCESS;
}
.:Library Files:.
DS18B20.h - DS18B20 class declaration. Parent is OneWireThermometer.
DS18B20.cpp - DS18B20 source
DS18S20.h - DS18S20 class declaration. Parent is OneWireThermometer.
DS18B20.cpp - DS18S20 source
OneWireCRC.h - OneWireCRC class declaration
OneWireCRC.cpp - OneWireCRC source
OneWireDefs.h - 1-wire bus definitions
OneWireThermometer.h - OneWireThermometer class declaration
OneWireThermometer.cpp - OneWireThermometer source
+ DebugTrace Library: DebugTrace.h / DebugTrace.cpp
.:TODO Wish List:.
1. Use DQ going high to signal end of temperature conversion, rather than waiting for Tconv (as for parasitic power).
2. Add facility for alarms
2. Smart OneWireCRC - just connect any number and type of device and let the code sort out whats there and report back the temperatures.
18 comments
Please log in to post a comment.








Just seen this, having just ordered a DS18B20 to play with. You've saved me some work! Cheers