Nov 19

I recently acquired a DS1616 IC.  This device offers the following:

DS1616 on Arduino proto shield

DS1616 on Arduino proto shield

  • Measures four channels of data:
    • Integrated 8-bit temperature sensor
    • Integrated 8-bit analog-to-digital converter (ADC) with a three-input mux
  • Real -time clock/calendar
  • Unique, factory lasered 64-bit serial number
  • A whole bunch of other logging and alarm functions

This device was crying out to be interfaced with the Arduino.

The DS1616 allows 2 forms of serial communication, 2 wire asynchronous and 3 wire synchronous.  I decided to go with the 2 wire configuration, what with Arduino pins being a precious commodity.  Connecting the DS1616 to the Arduino is a simple affair.  Two pins for the serial communcations.  It’s a good idea to include a battery, so that the time remains correct when power is removed.

Great schematic drawing, eh?

Great schematic drawing, eh?

That’s the hardware out of the way.  Let’s move onto the software.  Communication with the DS1616 is established using the NewSoftSerial library.  Getting data is essentially a case of lots of bit banging.  The DS1616 library (downloadable below) provides all the functions needed to gather data from the DS1616.  The best way to show the functions provided is to view the sample sketch below.

Sample code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
    DS1616 library sample code.
    Used for communicating with the DS1616 RTC and temperature device/
    Created by Pete Foster (http://www.xmob.co.uk)
 
    REQUIRES NewSoftSerial library.  (http://arduiniana.org/libraries/NewSoftSerial/)
*/


#include <NewSoftSerial.h>
#include <DS1616.h>

NewSoftSerial ds(8, 9); // The serial port that the DS1616 is connected to
DS1616 chip; // The class

void setup() {
    Serial.begin(9600);
    ds.begin(9600); // DS1616 works at 9600 baud
   
    byte page[35]; // A buffer for the class to work with
 
    // The DS1616 uses an internal Y2K flag to determine the
    // century.  This means that only years between 1900 and
    // 2099 can be used.  The set_current_date function
    // automatically sets the flag based on the supplied year.
    char new_date [] = "2009-11-19"; // The date string.  Format is YYYY-MM-DD.
    // set_current_date(char working_buffer, char new_date, NewSoftSerial serial_port)
    chip.set_current_date(page, new_date, ds); // Set the date
 
    // The DS1616 RTC can operate in 12 or 24 hour mode.
    // This can be set, depending on the format of the string
    // used to set the time.
    // If the string ends with an 'A' or a 'P' then 12 hour
    // format is used.  Otherwise, 24 hour will be used.
    // e.g.
    // "01:30:00P"
    // "13:30:00"
    char new_time [] = "12:00:00"; // The time string
    // set_current_time(char working_buffer, char new_time, NewSoftSerial serial_port)
    chip.set_current_time(page, new_time, ds); // Set the time
}

void loop() {
    // DS1616 returns data in 32 byte pages plus a 2 byte CRC.
    // CRC is ignored in this library.
    byte page[35];  // A buffer for the class to work with

    // The vars where data from requests will be stored
    float temp;
    int analog1, analog2, analog3;
    char time[] = "00:00:00A";
    char date[] = "0000-00-00";
    char serial_num [] = "0000000000000000";
 
    // Get the serial number of the chip.
    // This is represented as a string of hex.
    // get_serial_number(char working_buffer, char serial_num_buffer, NewSoftSerial serial_port)
    // Serial number is returned in serial_num_buffer.
    chip.get_serial_number(page, serial_num, ds);
 
    // Get the current time from the chip.
    // get_current_time(char working_buffer, char time_buffer, NewSoftSerial serial_port)
    // Time us returned in time_buffer
    chip.get_current_time(page, time, ds);
 
    // Get the current date from the chip.
    // get_current_date(char working_buffer, char date_buffer, NewSoftSerial serial_port)
    // Date us returned in date_buffer
    chip.get_current_date(page, date, ds);
 
    // Get the current reading from the internal temperature sensor.
    // get_current_temp(char working_buffer, NewSoftSerial serial_port)
    // Returns float
    temp = chip.get_current_temp(page, ds);
 
    // Read the 3 analog channels.  These are 8-bit.
    // 0v on input is 0
    // 5v (Vcc) on input is 255
    // read_analog(char working_buffer, int analog_channel, NewSoftSerial serial_port)
    // Returns int
    analog1 = chip.read_analog(page, 1, ds);
    analog2 = chip.read_analog(page, 2, ds);
    analog3 = chip.read_analog(page, 3, ds);
 
    // Display the data
    Serial.print(serial_num);
    Serial.print(',');
    Serial.print(date);
    Serial.print(',');
    Serial.print(time);
    Serial.print(",");
    Serial.print(temp);
    Serial.print(',');
    Serial.print(analog1);
    Serial.print(',');
    Serial.print(analog2);
    Serial.print(',');
    Serial.print(analog3);
    Serial.print("\n");
    delay(1000);
}

Download

Current release: arduino_DS1616-0.0.1.tar.gz

If you use this library, I would love to hear about it.

Tagged with:
Get Adobe Flash playerPlugin by wpburn.com wordpress themes
Copyright © 2009 - Pete Foster
preload preload preload