I am running a similar setup, where I log data from 3x DS18B20 temperature sensors to SD card and also use RTC based on DS1307 to timestamp each record. No problems so far. I use Sanguino platform, but in principle this is very similar to atmega 168.
My RTC is this one:
http://www.futurlec.com.au/Mini_DS1307.jsp, however, any board based on DS1307 should work the same.
The only problem I have got working with this setup is that DS18B20 should be externally powered but not using parasitic power mode.
Here is a code where DS1307 and DS18B20 is working together with all the libraries I use:
http://kom.aau.dk/~kod/temp_time.rarAnd here is only the program:
// libraries for RTC clock DS1307
#include <Wire.h>
#include <DS1307.h>
#include <OneWire.h> // For temperature sensors 1-Wire interface library
OneWire ds(20); // 1-wire temperature sensors connected on pin 20
int rtc[7]; // RTC clock output
//int ledPin = 0; // LED connected to digital pin 13
//define unique sensor serial code
//call search_devices() to determine codes
byte temp_in[8] = {0x28, 0x26, 0x82, 0xC7, 0x01, 0x00, 0x00, 0x46};
byte temp_out[8] = {0x28, 0x95, 0x9B, 0xC7, 0x01, 0x00, 0x00, 0x97};
byte temp_spare[8] = {0x28, 0xED, 0xAB, 0xC7, 0x01, 0x00, 0x00, 0x2A};
void setup() // run once, when the sketch starts
{
//pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600); // Open the first serial port (debug/upload/testing/etc.)
Serial.println("Hello world!"); // Print something to the serial port
/*
// Set the time for RTC
RTC.stop();
RTC.set(DS1307_YR,9); // Year (0-99)
RTC.set(DS1307_MTH,2); // Month (1-12)
RTC.set(DS1307_DATE,13); // Day of month
RTC.set(DS1307_HR,23); // Hours (0-23)
RTC.set(DS1307_MIN,16); // Minutes (0-59)
RTC.set(DS1307_SEC,0); // Seconds (0-59)
RTC.set(DS1307_DOW,5); // Day of week (1-7)
RTC.start();
*/
}
// Function searches for 1-Wire device and prints its address if found
// Use one device at a time
void search_devices(){
byte addr[8];
int i=0;
if ( !ds.search(addr)) {
Serial.println("No more addresses.\n");
ds.reset_search();
return;
}
Serial.print("R=");
for( i = 0; i < 8; i++) {
Serial.print(addr[i], HEX);
Serial.print(" ");
}
Serial.println();
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!\n");
return;
}
if ( addr[0] != 0x28) { // DS18B20 sensor identification number
Serial.println("Device is not a DS18B20 family device.\n");
return;
}
}
// Function reads the temperature from the temperature sensor when addr is a sensor ID
int get_temp(byte* addr){
byte present = 0;
byte i;
byte data[12];
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
//delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
//Serial.print("P=");
//Serial.print(present,HEX);
//Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
//Serial.print(data[i], HEX);
//Serial.print(" ");
}
//Serial.println();
int temp;
// compute temperature
temp = data[1]*256 + data[0];
// check if the number is negative
if (data[1] > 0x07) // sign bit set, temp is negative
temp = temp-65536; // correct the number acordingly
// Convert temperature C -> F (multiplied by 10 to avoid float)
//temp = (int)(((temp * 1.8) + 32)*10);
return temp;
}
void loop() // run over and over again
{
//digitalWrite(ledPin, HIGH); // sets the LED on
//delay(1000); // waits for a second
//digitalWrite(ledPin, LOW); // sets the LED off
//delay(1000); // waits for a second
/*
// Procedure to test if there is any communication going on in 1-Wire line
Serial.println("Starting test...");
int result = ds.reset(); //perform 1-wire bus rest and see if any devices wake up.
if( result == 1 )
Serial.println("A device was found on 1-wire bus");
else
Serial.println("No device answered. Bus broken or shorted");
Serial.println("...end test\n\n");
*/
// Enable this line to get sensor device id. Comment out when finished. Test one sensor at a time
//search_devices();
float temp1, temp2, temp3; // true temperature
int temp11, temp21, temp31; // temperature before comma
int temp12, temp22, temp32; // temperature 1 digit after comma
temp1 = get_temp(temp_in)/16.0;
temp2 = get_temp(temp_out)/16.0;
temp3 = get_temp(temp_spare)/16.0;
temp11 = (int) temp1;
temp21 = (int) temp2;
temp31 = (int) temp3;
temp12 = temp1*10 - temp11*10;
temp22 = temp2*10 - temp21*10;
temp32 = temp3*10 - temp31*10;
// send temperatures to the serial port in a format xx.x
Serial.print("Temp_in = ");
Serial.print(temp11, DEC);
Serial.print(".");
Serial.print(temp12, DEC);
//Serial.print(" ");
//Serial.println(temp1*100, DEC);
Serial.print(" Temp_out = ");
Serial.print(temp21, DEC);
Serial.print(".");
Serial.print(temp22, DEC);
//Serial.print(" ");
//Serial.println(temp2*100, DEC);
Serial.print(" Temp_spare = ");
Serial.print(temp31, DEC);
Serial.print(".");
Serial.println(temp32, DEC);
//Serial.print(" ");
//Serial.println(temp3*100, DEC);
Serial.println();
// Get the time from RTC
RTC.get(rtc,true);
// print date to the serial port
Serial.print("Time: ");
Serial.print(rtc[6], DEC);
Serial.print("-");
Serial.print(rtc[5], DEC);
Serial.print("-");
Serial.print(rtc[4], DEC);
Serial.print(" ");
Serial.print(rtc[2], DEC);
Serial.print(":");
Serial.print(rtc[1], DEC);
Serial.print(":");
Serial.print(rtc[0], DEC);
switch (rtc[3]) {
case 1:
Serial.println(" Monday");
break;
case 2:
Serial.println(" Tuesday");
break;
case 3:
Serial.println(" Wednesday");
break;
case 4:
Serial.println(" Thursday");
break;
case 5:
Serial.println(" Friday");
break;
case 6:
Serial.println(" Saturday");
break;
case 7:
Serial.println(" Sunday");
break;
}
delay(1000); // waits for 1 second
}