OneWire library for Teensy 3 (DS18B20)

el_supremo

Well-known member
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:
Code:
		crc = pgm_read_byte(dscrc_table + (crc ^ *addr++));
to this:
Code:
		crc = dscrc_table[crc ^ *addr++];

In OneWire.h at line 77 is this code:
Code:
#else
//#error "Please define I/O register types here"
#endif

Change those three lines to this:
Code:
#else
// 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)
#endif

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.

Pete
 
Back
Top