Hi I have only 3KB left of my 328P and need to get a unix timestamp. I'm considering a DS3231 but would need a very small code to request the timestamp. Timestamp is needed to calculate the diffference between different request
timtailors:
Hi I have only 3KB left of my 328P . . .
To properly answer this we would have to know what is in the other 29K.
Don
Why do you need to know this? It's a display, temperature, gsm library.
I'm just looking for a way to request a timestamp from the ds3231 maybe that's possible without a library and anybody has done that already
What should the timestamp looks like? DS3231 provides the time in parts sec, min... so you have to put it together somehow. Anyway, 3k could be enough. You have to try. My personal experience is that there is usually room for improvement of your previous code to save some additional space.
It's a display, temperature, gsm library.
Is Wire.h already being used?
Hi, yes for the SSD1306 display
This is pretty minimal code to read the time/date registers.
#include <Wire.h>
#define DS3231_I2C_ADDRESS 0x68
byte Second, Minute, Hour, Day, Month, Year;
void setup() {
Wire.begin();
Serial.begin(115200);
Serial.println("Starting");
}
void loop() {
getTimeStamp();
Serial.print(Day);
Serial.print('/');
Serial.print(Month);
Serial.print('/');
Serial.print(Year);
Serial.print(' ');
if (Hour < 10)//added code to give leading zeros
Serial.print("0");
Serial.print(Hour);
Serial.print(':');
if (Minute < 10)//added code to give leading zeros
Serial.print("0");
Serial.print(Minute);
Serial.print(':');
if (Second < 10)//added code to give leading zeros
Serial.print("0");
Serial.println(Second);
delay(1000);
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
void getTimeStamp()
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0x00);//set to seconds register
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
Second = bcdToDec(Wire.read());
Minute = bcdToDec(Wire.read());
Hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
//dayOfWeek = bcdToDec(Wire.read());
//throw away day of week value
Wire.read();
Day = bcdToDec(Wire.read());
Month = bcdToDec(Wire.read() & 0x7f);//no century bit
Year = bcdToDec(Wire.read());
}