Dear my friends,
I have being working on a 1 Wire network with multiple DS18S20 and a DS1994 for RTC timestamp.
The Question is: There is ant library for converting a timestamp to a standard time format?
Attached you will find my work in progress, some pieces of code taken from here and there.....
Thanks.
Oliveros
#include <OneWire.h>
OneWire ds(10); // on pin 10
byte onewireclock[8]; // Store 1 wire clock device code
//timedate td; // Time - date structure
char dateLCD[9] = {""}; // Variables to send time and date to LCD
char timeLCD[9] = {""}; // Variables to send time and date to LCD
char text1[17]; // General LCD Text
void setup(void) {
// initialize inputs/outputs
// start serial port
Serial.begin(9600);
}
void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int Temp;
if ( !ds.search(addr)) {
//Serial.print("No more addresses.\n");
ds.reset_search();
return;
}
// Serial.print("R="); //R=28 Not sure what this is
for( i = 0; i < 8; i++) {
// Serial.print(addr[i], HEX); //Imprime el serial del dispositivo en reversa
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}
// DS18S20 Temp Only Family Device
if ( addr[0] == 0x28) {
//Serial.print(" T=");//output the temperature to serial port
GetTempS(addr);
//Serial.print("C ");
Serial.println();
return;
}
// DS1994 Family Device
if (addr[0] == 0x04)
{
Serial.println(GetTimeRTC(addr));
return;
}
// DS18S20 Temp Only Family Device
/* if (addr[0] == 0x10)
{
Serial.println(GetTemp(addr));
return;
}
*/
Serial.print("Device not supported.\n");
}
int GetTempS(byte* addr)
{
byte i;
byte j;
int LowByte, HighByte, Temp, TReading, SignBit, Tc_100, Whole, Fract;
byte data[12];
byte dataj[12];
byte present = 0;
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.
ds.reset();
ds.select(addr);
//ds.write(0xCC);
// Serial.print("P=");
// Serial.print(present,HEX);
// Serial.print(" ");
//for ( j = 0; j < 9; i++) { // we need 9 bytes
// dataj[j] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
//LowByte = dataj[j];
//}
//ds.reset();
//ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
// LowByte = data[i];
}
ds.reset();
Temp=(data[1]<<8)+data[0];//take the two bytes from the response relating to temperature
Tc_100 = (6 * Temp) + Temp / 4;
Whole = Tc_100 / 100;
Fract = Tc_100 % 100;
if (SignBit) // If its negative
{
Serial.print("-");
}
Serial.print(Whole);
Serial.print(".");
if (Fract < 1)
{
Fract = Fract * -1;
// Serial.print("0");
}
Serial.print(Fract);
Temp=Temp>>4;//divide by 16 to get pure celcius readout
//next line is Fahrenheit conversion
// Temp=Temp*1.8+32; // comment this line out to get celcius
//Serial.print(Temp);
return Temp;
}
int GetTemp(byte* addr)
{
byte i;
int Temp;
byte data[12];
byte present = 0;
int temp;
float ftemp;
ds.reset();
ds.select(addr);
ds.write(0x44); // start conversion, with parasite power on at the end
delay(1000);
ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
temp = data[0]; // load all 8 bits of the LSB
//get hi-rez data
int cpc; // count per deg C
int cr = data[6];//data[7]; // Count Remain
cpc = data[7]; //data[8];
// Serial.println(cr, HEX);
// Serial.println(cpc, HEX);
if (cpc == 0)
Serial.print("DS1920 BADTEMP");
// According to data sheet, need to truncate 0.5C bit 0 from temperature data
temp = temp >> 1; // Shift one bit to drop bit 0 for following formula
ftemp = temp - (float)0.25 + (cpc - cr)/(float)cpc;
//end hi-rez data
//ftemp = ((ftemp * 9) / 5.0) + 32; //C -> F (F = (9/5)C + 32)
itoa(int (ftemp), text1, 10); // convert int to string
return ftemp;
}
//
// GetTimeRTC(byte* addr)
// Get number of seconds since 1/1/1970 by accessing DS1994 Real Time Clock registers
//
long GetTimeRTC(byte* addr)
{
byte i;
byte data[5];
long utime;
ds.reset();
ds.select(addr); // Button address
ds.write(0xF0); // Read memory
// Clock memory address starts at 0203h (dropping fraction of sec) Memory page 16
// 0203h = dec 515 or 001000000011
// Need to set address registers TA1 and TA2
// TA1 (8bit) first 5 bits represent byte offset within a page
// TA1 = 00000011 or 03h
// TA2 = 0010 or 02h
ds.write(0x03); //TA1
ds.write(0x02); //TA2
for ( i = 1; i < 5; i++) { // we need 4 bytes of time data
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
ds.reset();
// Convert 4 byte time data into 32 bit long int (sec from 1970:~1,215,011,006)
/*
utime = data[4];
utime = utime<<8 | data[3];
utime = utime<<8 | data[2];
utime = utime<<8 | data[1];
*/
utime = data[4];
for ( i = 3; i > 0; i--) { // we need 4 bytes of time data
utime = utime<<8 | data[i];
}
return utime;
}
and the result:
28.18
1251518460
28.12
1251518462
28.12
1251518463
28.12
1251518464
28.12
1251518465
28.12
1251518466