OneWire + DS18X20 Library
Basic functions for OneWire operations + specific DS18x20 operations
 All Functions Groups Pages
/*****************************************************************************
DS18x20 library
Copyright (C) 2016 Falk Brunner
*****************************************************************************/
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <Falk.Brunner@gmx.de> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Falk Brunner
* ----------------------------------------------------------------------------
*/
#include <util/delay.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
#include "onewire.h"
#include "ds18x20.h"
uint8_t ds18x20_convert_t(uint8_t parasitic_power) {
if (onewire_reset()) {
return 1; // no response
} else {
onewire_write_byte(DS1820_CMD_SKIP_ROM);
if (parasitic_power) {
//ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
uint8_t sreg_tmp=SREG; cli(); // Arduino workaround :-0
onewire_write_byte(DS1820_CMD_CONVERT_T);
ONEWIRE_STRONG_PU_ON
//}
SREG = sreg_tmp; // Arduino workaround :-0
} else {
onewire_write_byte(DS1820_CMD_CONVERT_T);
}
}
return 0;
}
uint8_t ds18B20_read_temp(int16_t *temperature) {
int16_t temp;
uint8_t scratchpad[9];
uint8_t rc;
rc=onewire_crc(scratchpad, 9);
if (rc) {
return ONEWIRE_CRC_ERROR;
}
temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
// calculate temperature with 0.1 C resolution using fixed point arithmetic
// t(0.1C) = t(1/16C) * 10/16
temp = (temp * 10) >> 4;
*temperature = temp;
return ONEWIRE_OK;
}
uint8_t ds18S20_read_temp(int16_t *temperature) {
int16_t temp;
uint8_t scratchpad[9];
uint8_t rc;
rc=onewire_crc(scratchpad, 9);
if (rc) {
return ONEWIRE_CRC_ERROR;
}
temp = ((int16_t)scratchpad[1] << 8) | scratchpad[0];
temp &= ~1; // clear bit#0
temp <<= 3; // x8 -> resolution 1/16 C
// calculate extended resolution
// /16 must be omitted, since we are using a resolution of 1/16 degree C
temp = temp - 4 + (16-scratchpad[6]);
// calculate temperature with 0.1 C resolution using fixed point arithmetic
// t(0.1C) = t(1/16C) * 10/16
temp = (temp * 10) >> 4;
*temperature = temp;
return ONEWIRE_OK;
}
void ds18x20_read_scratchpad(uint8_t *buffer) {
uint8_t i;
onewire_write_byte(DS1820_CMD_READ_SCRATCHPAD);
for (i=0; i<9; i++) {
buffer[i]=onewire_read_byte();
}
}
void ds18S20_write_scratchpad(int8_t th, int8_t tl) {
onewire_write_byte(DS1820_CMD_WRITE_SCRATCHPAD);
}
void ds18B20_write_scratchpad(int8_t th, int8_t tl, uint8_t config) {
onewire_write_byte(DS1820_CMD_WRITE_SCRATCHPAD);
}
void ds18x20_copy_scratchpad(uint8_t parasitic_power) {
if (parasitic_power) {
//ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
uint8_t sreg_tmp=SREG; cli(); // Arduino workaround :-0
onewire_write_byte(DS1820_CMD_COPY_SCRATCHPAD);
ONEWIRE_STRONG_PU_ON
//}
SREG = sreg_tmp; // Arduino workaround :-0
} else {
onewire_write_byte(DS1820_CMD_COPY_SCRATCHPAD);
}
_delay_ms(10);
ONEWIRE_STRONG_PU_OFF
}
void ds18x20_recall_E2(void) {
onewire_write_byte(DS1820_CMD_RECALL_E2);
_delay_ms(1);
}
onewire_write_byte(DS1820_CMD_READ_POWER_SUPPLY);
return !onewire_read_bit();
}