View Full Version : OneWire library for Teensy 3 (DS18B20)

11-12-2012, 05:52 PM
I've been trying to get the OneWire library working so that I can use a DS18B20 and have finally succeeded. I started with Paul's OneWire library V2.1 for the Arduino which can be downloaded here: http://www.pjrc.com/teensy/td_libs_OneWire.html
Unzip it into your libraries directory.

In OneWire.cpp two changes are required to remove the use of PROGMEM from the original Arduino code.
- at line 439 remove PROGMEM from the declaration.
- change line 469 from this:

crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));

to this:

crc = dscrc_table[crc ^ *addr++];

In OneWire.h at line 77 is this code:

//#error "Please define I/O register types here"

Change those three lines to this:

// PAH - add 1Wire for Teensy 3 (works with DS18B20)
#define PIN_TO_BASEREG(pin) (digital_pin_to_info_PGM[(pin)].reg)
#define PIN_TO_BITMASK(pin) (1)
#define IO_REG_TYPE uint32_t
#define IO_REG_ASM
#define DIRECT_READ(base, mask) (*(base+128))
#define DIRECT_MODE_INPUT(base, mask) (*(base+160) = PORT_PCR_MUX(1))
#define DIRECT_MODE_OUTPUT(base, mask) (*(base+160) = 1)
#define DIRECT_WRITE_LOW(base, mask) (*(base+64) = 1)
#define DIRECT_WRITE_HIGH(base, mask) (*(base+32) = 1)

I've only tested it with the DS18B20 but the device is found and the temperature is read so it's at least a good start :-)
When Paul has time perhaps he can cast his experienced eye over my fixes to make sure I've done this properly and haven't missed anything.


01-17-2013, 11:27 PM
I am working on OneWire 2.2, with support for Teensy 3.0 and several small improvements.

Here's a pre-release copy. Please let me know how it works if you try it?


01-18-2013, 12:22 AM
It works with the DS18B20 which is the only onewire device I have.