Skip to content
Jacajack edited this page Dec 28, 2015 · 17 revisions

Welcome to the wiki!

Avr-dallas1820 is a C library for AVR microcontrollers to control DS1820 temperature sensors. It's pretty simple to use and should work with most of AVR family devices. Below You'll find description of functions and code snippets, have fun!

DS1820
DS1820 temperature sensor

Adding to project

Whole library contains 4 files:

  • OneWire.c - contains OneWire communication protocol related things
  • OneWire.h - OneWire function prototypes etc.
  • Dallas1820.c - contains stuff for using DS1820 sensor
  • Dallas1820.h - Dallas1820 function prototypes and other boring things

You need to add *.c files to project and include *.h files in Your main file using #include directive. It should look like this:

#include "Dallas1820/Dallas1820.h"

In fact, as You can see, it only includes Dallas1820.h file. That's because Dallas1820.h automatically detect whether OneWire.h is included before, and if not, it adds it. (preprocessor magic!)

Wiring

Basic wiring is really, really simple:

Basic wiring For testing You can use breadboard, AVR, two resistors, and couple of wires.

Sample code

Using this library shouldn't be a problem for anyone, because of its simplicity. Below You can find code example for reading ROM and Temperature from sensor.

#include <stdio.h>

#include <avr/io.h>
#include <util/delay.h>

#include "Dallas1820/Dallas1820.h"

int main( )
{
    float Temperature = 0.0f;

    //Make sensor pin output (optional)
    DDRD = ( 1 << 7 );

    //Sensor configuration (port direction register, port output register, port input register and mask)
    OneWireConfiguration Thermometer = { &DDRD, &PORTD, &PIND, ( 1 << 7 ) };

    Dallas1820ReadROM( &Thermometer ); //Read ROM

    while ( 1 )
    {
        //Request Dallas 1820 temperature conversion
        Dallas1820Request( &Thermometer );

        //Read temperature
        Temperature = Dallas18B20ToCelcius( Dallas18B20Read( &Thermometer ) );

        //Use data (printf( ) is only example)
        printf( "%f\n", (double) Temperature );

        //Wait 1s
        for ( unsigned char i = 0; i < 100; i++ )
            _delay_ms( 10 );
    }
    return 0;
}

Above code step by step:

  • Create variable for storing temperatures
  • Make sensor pin output (You don't need to do that)
  • Create variable with sensor configuration (Direction register, output register, input register, mask, etc.)
  • Read sensor's ROM to structure created step before
  • In loop:
  • Begin temperature conversion.
  • Store temperature in variable created before, after converting it to Celcius degrees
  • Use data
  • Wait 1s

Note: Important thing is, that You can't use printf( ). You'll have to code Your own function for sending data through UART, but that's easy, so don't worry.

###Specification For more technical specification visit this wiki page.

Clone this wiki locally