Skip to content

Instantly share code, notes, and snippets.

@ZhukV
Created January 29, 2014 19:22
Show Gist options
  • Save ZhukV/8694966 to your computer and use it in GitHub Desktop.
Save ZhukV/8694966 to your computer and use it in GitHub Desktop.
Work with temperature sensor DS18B20
/**
ds18b20.c - work with temperature sensor DS18B20
Author: Vitaiy Zhuk
*/
#include <util/delay.h>
#include "ds18b20.h"
/**
Write bit \a bit to 1-Wire PIN.
*/
void ds18b20WriteBit(uint8_t bit)
{
DS18B20_LOW();
DS18B20_OUTPUT();
_delay_us(1);
if (bit) {
DS18B20_INPUT();
}
_delay_us(60);
DS18B20_INPUT();
}
/**
Read bit from 1-Wire PIN
*/
uint8_t ds18b20ReadBit()
{
uint8_t bit = 0;
DS18B20_LOW();
DS18B20_OUTPUT();
_delay_us(1);
DS18B20_INPUT();
_delay_us(14);
if (DS18B20_PORT_PIN & (1 << DS18B20_PIN)) {
bit = 1;
}
_delay_us(45);
return bit;
}
uint8_t ds18b20Reset()
{
uint8_t status;
DS18B20_LOW();
DS18B20_OUTPUT();
_delay_us(480);
DS18B20_INPUT();
_delay_us(60);
status = (DS18B20_PORT_PIN & (1 << DS18B20_PIN));
_delay_us(420);
return status;
}
void ds18b20WriteByte(uint8_t byte)
{
uint8_t i = 8;
while (i--) {
ds18b20WriteBit(byte & 1);
byte >>= 1;
}
}
uint8_t ds18b20ReadByte()
{
uint8_t byte = 0, i = 8;
while (i--) {
byte >>= 1;
byte |= (ds18b20ReadBit() << 7);
}
return byte;
}
void ds18b20ReadTemperature(int8_t *temperature)
{
ds18b20Reset();
ds18b20WriteByte(DS18B20_CMD_SCIPROM);
ds18b20WriteByte(DS18B20_CMD_CONVERT);
while (!ds18b20ReadBit());
ds18b20Reset();
ds18b20WriteByte(DS18B20_CMD_SCIPROM);
ds18b20WriteByte(DS18B20_CMD_MEMREAD);
uint8_t secondByte = ds18b20ReadByte();
uint8_t firstByte = ds18b20ReadByte();
uint8_t positive = ~(1 & (firstByte >> 4));
temperature[0] |= (firstByte << 4) | (secondByte >> 4);
temperature[0] &= ~(0b10000000);
temperature[1] = secondByte & 0b00001111;
if (!positive) {
temperature[0] ^= 0xFF;
temperature[1] ^= 0x0F;
}
}
void ds18b20WriteConfig(uint8_t th, uint8_t tl, uint8_t config)
{
ds18b20Reset();
ds18b20WriteByte(DS18B20_CMD_SCIPROM);
ds18b20WriteByte(DS18B20_CMD_MEMWRITE);
ds18b20WriteByte(th);
ds18b20WriteByte(tl);
ds18b20WriteByte(config);
ds18b20WriteByte(DS18B20_CMD_MEMWRITE);
}
/**
ds18b20.h - work with temperature sensor DS18B20
Author: Vitaiy Zhuk
*/
// Data PORT/DDR must be defined
#ifndef DS18B20_DDR
# error "DS18B20_DDR not defined for temp/ds18b20.h"
#endif
#ifndef DS18B20_PORT
# error "DS18B20_PORT not defined for temp/ds18b20.h"
#endif
#ifndef DS18B20_PORT_PIN
# error "DS18B20_PORT_PIN not defined for temp/ds18b29.h"
#endif
#ifndef DS18B20_PIN
# error "DS18B20_PIN not defined for temp/ds18b20.h"
#endif
// Define macros
#define DS18B20_INPUT() DS18B20_DDR &= ~(1 << DS18B20_PIN)
#define DS18B20_OUTPUT() DS18B20_DDR |= (1 << DS18B20_PIN)
#define DS18B20_LOW() DS18B20_PORT &= ~(1 << DS18B20_PIN)
#define DS18B20_HIGH() DS18B20_PORT |= (1 << DS18B20_PIN)
/** ROM COMMANDS **/
#define DS18B20_CMD_SEARCHROM 0xF0
#define DS18B20_CMD_READROM 0x33
#define DS18B20_CMD_MATCHROM 0x55
#define DS18B20_CMD_SCIPROM 0xCC
#define DS18B20_CMD_ALARMSEARCH 0xEC
/** FUNCTIONAL COMMANDS **/
#define DS18B20_CMD_CONVERT 0x44
#define DS18B20_CMD_MEMWRITE 0x4E
#define DS18B20_CMD_MEMREAD 0xBE
#define DS18B20_CMD_MEMCOPY 0x48
#define DS18B20_CMD_RETRYLOAD 0xB8
#define DS18B20_CMD_RPWRSUPPLY 0xB4
// Configurations
#define DS18B20_CONFIG_R1 6
#define DS18B20_CONFIG_R0 5
/**
Reset sensor
*/
uint8_t ds18b20Reset();
/**
Write byte \a byte to sensor
*/
void ds18b20WriteByte(uint8_t byte);
/**
Read byte from sensor
*/
uint8_t ds18b20ReadByte();
/**
Read temperature.
*/
void ds18b20ReadTemperature(int8_t *temperature);
/**
Write configuration bytes to DS18B20 memory
\a th - Alert of temperature high
\a tl - Alert of temperature low
\a config - Configuration byte
*/
void ds18b20WriteConfig(uint8_t th, uint8_t tl, uint8_t config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment