first commit
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
/*
|
||||
oscillator_test.pde
|
||||
Eric Ayars
|
||||
4/11
|
||||
|
||||
Test/demo of oscillator routines for a DS3231 RTC.
|
||||
|
||||
Use a scope after loading this to check if things are
|
||||
working as they should.
|
||||
|
||||
*/
|
||||
|
||||
#include <DS3231.h>
|
||||
#include <Wire.h>
|
||||
|
||||
DS3231 Clock;
|
||||
byte j;
|
||||
bool on = false;
|
||||
|
||||
void setup() {
|
||||
// Start the I2C interface
|
||||
Wire.begin();
|
||||
// Start the serial interface
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
for (j=0;j<4;j++) {
|
||||
// invert state of 32kHz oscillator.
|
||||
on = !on;
|
||||
Clock.enable32kHz(on);
|
||||
// Turn on oscillator pin, frequency j
|
||||
Clock.enableOscillator(true, false, j);
|
||||
delay(4000);
|
||||
}
|
||||
// So... The 32kHz oscillator (pin 1) will turn on or off once each 2s,
|
||||
// and the oscillator out pin (pin 3) will cycle through frequencies.
|
||||
}
|
113
libraries/DS3231/examples/DS3231_set/DS3231_set.pde
Normal file
113
libraries/DS3231/examples/DS3231_set/DS3231_set.pde
Normal file
@ -0,0 +1,113 @@
|
||||
/*
|
||||
DS3231_set.pde
|
||||
Eric Ayars
|
||||
4/11
|
||||
|
||||
Test of set-time routines for a DS3231 RTC
|
||||
|
||||
*/
|
||||
|
||||
#include <DS3231.h>
|
||||
#include <Wire.h>
|
||||
|
||||
DS3231 Clock;
|
||||
|
||||
byte Year;
|
||||
byte Month;
|
||||
byte Date;
|
||||
byte DoW;
|
||||
byte Hour;
|
||||
byte Minute;
|
||||
byte Second;
|
||||
|
||||
void GetDateStuff(byte& Year, byte& Month, byte& Day, byte& DoW,
|
||||
byte& Hour, byte& Minute, byte& Second) {
|
||||
// Call this if you notice something coming in on
|
||||
// the serial port. The stuff coming in should be in
|
||||
// the order YYMMDDwHHMMSS, with an 'x' at the end.
|
||||
boolean GotString = false;
|
||||
char InChar;
|
||||
byte Temp1, Temp2;
|
||||
char InString[20];
|
||||
|
||||
byte j=0;
|
||||
while (!GotString) {
|
||||
if (Serial.available()) {
|
||||
InChar = Serial.read();
|
||||
InString[j] = InChar;
|
||||
j += 1;
|
||||
if (InChar == 'x') {
|
||||
GotString = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Serial.println(InString);
|
||||
// Read Year first
|
||||
Temp1 = (byte)InString[0] -48;
|
||||
Temp2 = (byte)InString[1] -48;
|
||||
Year = Temp1*10 + Temp2;
|
||||
// now month
|
||||
Temp1 = (byte)InString[2] -48;
|
||||
Temp2 = (byte)InString[3] -48;
|
||||
Month = Temp1*10 + Temp2;
|
||||
// now date
|
||||
Temp1 = (byte)InString[4] -48;
|
||||
Temp2 = (byte)InString[5] -48;
|
||||
Day = Temp1*10 + Temp2;
|
||||
// now Day of Week
|
||||
DoW = (byte)InString[6] - 48;
|
||||
// now Hour
|
||||
Temp1 = (byte)InString[7] -48;
|
||||
Temp2 = (byte)InString[8] -48;
|
||||
Hour = Temp1*10 + Temp2;
|
||||
// now Minute
|
||||
Temp1 = (byte)InString[9] -48;
|
||||
Temp2 = (byte)InString[10] -48;
|
||||
Minute = Temp1*10 + Temp2;
|
||||
// now Second
|
||||
Temp1 = (byte)InString[11] -48;
|
||||
Temp2 = (byte)InString[12] -48;
|
||||
Second = Temp1*10 + Temp2;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
// Start the serial port
|
||||
Serial.begin(9600);
|
||||
|
||||
// Start the I2C interface
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// If something is coming in on the serial line, it's
|
||||
// a time correction so set the clock accordingly.
|
||||
if (Serial.available()) {
|
||||
GetDateStuff(Year, Month, Date, DoW, Hour, Minute, Second);
|
||||
|
||||
Clock.setClockMode(false); // set to 24h
|
||||
//setClockMode(true); // set to 12h
|
||||
|
||||
Clock.setYear(Year);
|
||||
Clock.setMonth(Month);
|
||||
Clock.setDate(Date);
|
||||
Clock.setDoW(DoW);
|
||||
Clock.setHour(Hour);
|
||||
Clock.setMinute(Minute);
|
||||
Clock.setSecond(Second);
|
||||
|
||||
// Test of alarm functions
|
||||
// set A1 to one minute past the time we just set the clock
|
||||
// on current day of week.
|
||||
Clock.setA1Time(DoW, Hour, Minute+1, Second, 0x0, true,
|
||||
false, false);
|
||||
// set A2 to two minutes past, on current day of month.
|
||||
Clock.setA2Time(Date, Hour, Minute+2, 0x0, false, false,
|
||||
false);
|
||||
// Turn on both alarms, with external interrupt
|
||||
Clock.turnOnAlarm(1);
|
||||
Clock.turnOnAlarm(2);
|
||||
|
||||
}
|
||||
delay(1000);
|
||||
}
|
157
libraries/DS3231/examples/DS3231_test/DS3231_test.pde
Normal file
157
libraries/DS3231/examples/DS3231_test/DS3231_test.pde
Normal file
@ -0,0 +1,157 @@
|
||||
/*
|
||||
DS3231_test.pde
|
||||
Eric Ayars
|
||||
4/11
|
||||
|
||||
Test/demo of read routines for a DS3231 RTC.
|
||||
|
||||
Turn on the serial monitor after loading this to check if things are
|
||||
working as they should.
|
||||
|
||||
*/
|
||||
|
||||
#include <DS3231.h>
|
||||
#include <Wire.h>
|
||||
|
||||
DS3231 clock;
|
||||
bool century = false;
|
||||
bool h12Flag;
|
||||
bool pmFlag;
|
||||
byte alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits;
|
||||
bool alarmDy, alarmH12Flag, alarmPmFlag;
|
||||
|
||||
void setup() {
|
||||
// Start the I2C interface
|
||||
Wire.begin();
|
||||
|
||||
// Start the serial interface
|
||||
Serial.begin(57600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// send what's going on to the serial monitor.
|
||||
|
||||
// Start with the year
|
||||
Serial.print("2");
|
||||
if (century) { // Won't need this for 89 years.
|
||||
Serial.print("1");
|
||||
} else {
|
||||
Serial.print("0");
|
||||
}
|
||||
Serial.print(clock.getYear(), DEC);
|
||||
Serial.print(' ');
|
||||
|
||||
// then the month
|
||||
Serial.print(clock.getMonth(century), DEC);
|
||||
Serial.print(" ");
|
||||
|
||||
// then the date
|
||||
Serial.print(clock.getDate(), DEC);
|
||||
Serial.print(" ");
|
||||
|
||||
// and the day of the week
|
||||
Serial.print(clock.getDoW(), DEC);
|
||||
Serial.print(" ");
|
||||
|
||||
// Finally the hour, minute, and second
|
||||
Serial.print(clock.getHour(h12Flag, pmFlag), DEC);
|
||||
Serial.print(" ");
|
||||
Serial.print(clock.getMinute(), DEC);
|
||||
Serial.print(" ");
|
||||
Serial.print(clock.getSecond(), DEC);
|
||||
|
||||
// Add AM/PM indicator
|
||||
if (h12Flag) {
|
||||
if (pmFlag) {
|
||||
Serial.print(" PM ");
|
||||
} else {
|
||||
Serial.print(" AM ");
|
||||
}
|
||||
} else {
|
||||
Serial.print(" 24h ");
|
||||
}
|
||||
|
||||
// Display the temperature
|
||||
Serial.print("T=");
|
||||
Serial.print(clock.getTemperature(), 2);
|
||||
|
||||
// Tell whether the time is (likely to be) valid
|
||||
if (clock.oscillatorCheck()) {
|
||||
Serial.print(" O+");
|
||||
} else {
|
||||
Serial.print(" O-");
|
||||
}
|
||||
|
||||
// Indicate whether an alarm went off
|
||||
if (clock.checkIfAlarm(1)) {
|
||||
Serial.print(" A1!");
|
||||
}
|
||||
|
||||
if (clock.checkIfAlarm(2)) {
|
||||
Serial.print(" A2!");
|
||||
}
|
||||
|
||||
// New line on display
|
||||
Serial.println();
|
||||
|
||||
// Display Alarm 1 information
|
||||
Serial.print("Alarm 1: ");
|
||||
clock.getA1Time(alarmDay, alarmHour, alarmMinute, alarmSecond, alarmBits, alarmDy, alarmH12Flag, alarmPmFlag);
|
||||
Serial.print(alarmDay, DEC);
|
||||
if (alarmDy) {
|
||||
Serial.print(" DoW");
|
||||
} else {
|
||||
Serial.print(" Date");
|
||||
}
|
||||
Serial.print(' ');
|
||||
Serial.print(alarmHour, DEC);
|
||||
Serial.print(' ');
|
||||
Serial.print(alarmMinute, DEC);
|
||||
Serial.print(' ');
|
||||
Serial.print(alarmSecond, DEC);
|
||||
Serial.print(' ');
|
||||
if (alarmH12Flag) {
|
||||
if (alarmPmFlag) {
|
||||
Serial.print("pm ");
|
||||
} else {
|
||||
Serial.print("am ");
|
||||
}
|
||||
}
|
||||
if (clock.checkAlarmEnabled(1)) {
|
||||
Serial.print("enabled");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Display Alarm 2 information
|
||||
Serial.print("Alarm 2: ");
|
||||
clock.getA2Time(alarmDay, alarmHour, alarmMinute, alarmBits, alarmDy, alarmH12Flag, alarmPmFlag);
|
||||
Serial.print(alarmDay, DEC);
|
||||
if (alarmDy) {
|
||||
Serial.print(" DoW");
|
||||
} else {
|
||||
Serial.print(" Date");
|
||||
}
|
||||
Serial.print(" ");
|
||||
Serial.print(alarmHour, DEC);
|
||||
Serial.print(" ");
|
||||
Serial.print(alarmMinute, DEC);
|
||||
Serial.print(" ");
|
||||
if (alarmH12Flag) {
|
||||
if (alarmPmFlag) {
|
||||
Serial.print("pm");
|
||||
} else {
|
||||
Serial.print("am");
|
||||
}
|
||||
}
|
||||
if (clock.checkAlarmEnabled(2)) {
|
||||
Serial.print("enabled");
|
||||
}
|
||||
|
||||
// display alarm bits
|
||||
Serial.println();
|
||||
Serial.print("Alarm bits: ");
|
||||
Serial.println(alarmBits, BIN);
|
||||
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
}
|
51
libraries/DS3231/examples/echo_time/echo_time.pde
Normal file
51
libraries/DS3231/examples/echo_time/echo_time.pde
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
|
||||
Prints time stamps for 5 seconds using getXXX functions
|
||||
|
||||
Based on DS3231_set.pde
|
||||
by Eric Ayars
|
||||
4/11
|
||||
|
||||
Added printing back of time stamps and increased baud rate
|
||||
(to better synchronize computer and RTC)
|
||||
Andy Wickert
|
||||
5/15/2011
|
||||
|
||||
*/
|
||||
|
||||
#include <DS3231.h>
|
||||
#include <Wire.h>
|
||||
|
||||
DS3231 clock;
|
||||
|
||||
bool century = false;
|
||||
bool h12Flag;
|
||||
bool pmFlag;
|
||||
|
||||
void setup() {
|
||||
// Start the serial port
|
||||
Serial.begin(57600);
|
||||
|
||||
// Start the I2C interface
|
||||
Wire.begin();
|
||||
|
||||
|
||||
for (int i=0; i<5; i++){
|
||||
delay(1000);
|
||||
Serial.print(clock.getYear(), DEC);
|
||||
Serial.print("-");
|
||||
Serial.print(clock.getMonth(century), DEC);
|
||||
Serial.print("-");
|
||||
Serial.print(clock.getDate(), DEC);
|
||||
Serial.print(" ");
|
||||
Serial.print(clock.getHour(h12Flag, pmFlag), DEC); //24-hr
|
||||
Serial.print(":");
|
||||
Serial.print(clock.getMinute(), DEC);
|
||||
Serial.print(":");
|
||||
Serial.println(clock.getSecond(), DEC);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
40
libraries/DS3231/examples/now/now.pde
Normal file
40
libraries/DS3231/examples/now/now.pde
Normal file
@ -0,0 +1,40 @@
|
||||
// now.pde
|
||||
// Prints a snapshot of the current date and time along with the UNIX time
|
||||
// Modified by Andy Wickert from the JeeLabs / Ladyada RTC library examples
|
||||
// 5/15/11
|
||||
|
||||
#include <Wire.h>
|
||||
#include <DS3231.h>
|
||||
|
||||
RTClib myRTC;
|
||||
|
||||
void setup () {
|
||||
Serial.begin(57600);
|
||||
Wire.begin();
|
||||
}
|
||||
|
||||
void loop () {
|
||||
|
||||
delay(1000);
|
||||
|
||||
DateTime now = myRTC.now();
|
||||
|
||||
Serial.print(now.year(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(now.month(), DEC);
|
||||
Serial.print('/');
|
||||
Serial.print(now.day(), DEC);
|
||||
Serial.print(' ');
|
||||
Serial.print(now.hour(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(now.minute(), DEC);
|
||||
Serial.print(':');
|
||||
Serial.print(now.second(), DEC);
|
||||
Serial.println();
|
||||
|
||||
Serial.print(" since midnight 1/1/1970 = ");
|
||||
Serial.print(now.unixtime());
|
||||
Serial.print("s = ");
|
||||
Serial.print(now.unixtime() / 86400L);
|
||||
Serial.println("d");
|
||||
}
|
182
libraries/DS3231/examples/set_echo/set_echo.pde
Normal file
182
libraries/DS3231/examples/set_echo/set_echo.pde
Normal file
@ -0,0 +1,182 @@
|
||||
/*
|
||||
|
||||
Sets the time from input and prints back time stamps for 5 seconds
|
||||
|
||||
Based on DS3231_set.pde
|
||||
by Eric Ayars
|
||||
4/11
|
||||
|
||||
Added printing back of time stamps and increased baud rate
|
||||
(to better synchronize computer and RTC)
|
||||
Andy Wickert
|
||||
5/15/2011
|
||||
|
||||
Clean for SAMD arch, add explanation, respect code-style and
|
||||
fix interpretation of Serial input if used more than once
|
||||
Olivier Staquet
|
||||
4/26/2020
|
||||
|
||||
*/
|
||||
|
||||
#include <DS3231.h>
|
||||
#include <Wire.h>
|
||||
|
||||
DS3231 clock;
|
||||
|
||||
byte year;
|
||||
byte month;
|
||||
byte date;
|
||||
byte dow;
|
||||
byte hour;
|
||||
byte minute;
|
||||
byte second;
|
||||
|
||||
bool century = false;
|
||||
bool h12Flag;
|
||||
bool pmFlag;
|
||||
|
||||
/*****************************************************************************************************
|
||||
* Setup
|
||||
* - Open Serial and Wire connection
|
||||
* - Explain to the user how to use the program
|
||||
*****************************************************************************************************/
|
||||
void setup() {
|
||||
// Start the serial port
|
||||
Serial.begin(57600);
|
||||
|
||||
// Start the I2C interface
|
||||
Wire.begin();
|
||||
|
||||
// Request the time correction on the Serial
|
||||
delay(4000);
|
||||
Serial.println("Format YYMMDDwhhmmssx");
|
||||
Serial.println("Where YY = Year (ex. 20 for 2020)");
|
||||
Serial.println(" MM = Month (ex. 04 for April)");
|
||||
Serial.println(" DD = Day of month (ex. 09 for 9th)");
|
||||
Serial.println(" w = Day of week from 1 to 7, 1 = Sunday (ex. 5 for Thursday)");
|
||||
Serial.println(" hh = hours in 24h format (ex. 09 for 9AM or 21 for 9PM)");
|
||||
Serial.println(" mm = minutes (ex. 02)");
|
||||
Serial.println(" ss = seconds (ex. 42)");
|
||||
Serial.println("Example for input : 2004095090242x");
|
||||
Serial.println("-----------------------------------------------------------------------------");
|
||||
Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
|
||||
}
|
||||
|
||||
/*****************************************************************************************************
|
||||
* Loop
|
||||
* - Each time we receive the time correction on the Serial
|
||||
* - Set the clock of the DS3231
|
||||
* - Echo the value from the DS3231 during 5 seconds
|
||||
*****************************************************************************************************/
|
||||
void loop() {
|
||||
// If something is coming in on the serial line, it's
|
||||
// a time correction so set the clock accordingly.
|
||||
if (Serial.available()) {
|
||||
inputDateFromSerial();
|
||||
|
||||
clock.setClockMode(false); // set to 24h
|
||||
|
||||
clock.setYear(year);
|
||||
clock.setMonth(month);
|
||||
clock.setDate(date);
|
||||
clock.setDoW(dow);
|
||||
clock.setHour(hour);
|
||||
clock.setMinute(minute);
|
||||
clock.setSecond(second);
|
||||
|
||||
// Give time at next five seconds
|
||||
for (uint8_t i = 0; i < 5; i++){
|
||||
delay(1000);
|
||||
Serial.print(clock.getYear(), DEC);
|
||||
Serial.print("-");
|
||||
Serial.print(clock.getMonth(century), DEC);
|
||||
Serial.print("-");
|
||||
Serial.print(clock.getDate(), DEC);
|
||||
Serial.print(" ");
|
||||
Serial.print(clock.getHour(h12Flag, pmFlag), DEC); //24-hr
|
||||
Serial.print(":");
|
||||
Serial.print(clock.getMinute(), DEC);
|
||||
Serial.print(":");
|
||||
Serial.println(clock.getSecond(), DEC);
|
||||
}
|
||||
|
||||
// Notify that we are ready for the next input
|
||||
Serial.println("Please enter the current time to set on DS3231 ended by 'x':");
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
/*****************************************************************************************************
|
||||
* inputDateFromSerial
|
||||
* - Read and interpret the data from the Serial input
|
||||
* - Store the data in global variables
|
||||
*****************************************************************************************************/
|
||||
void inputDateFromSerial() {
|
||||
// Call this if you notice something coming in on
|
||||
// the serial port. The stuff coming in should be in
|
||||
// the order YYMMDDwHHMMSS, with an 'x' at the end.
|
||||
boolean isStrComplete = false;
|
||||
char inputChar;
|
||||
byte temp1, temp2;
|
||||
char inputStr[20];
|
||||
|
||||
uint8_t currentPos = 0;
|
||||
while (!isStrComplete) {
|
||||
if (Serial.available()) {
|
||||
inputChar = Serial.read();
|
||||
inputStr[currentPos] = inputChar;
|
||||
currentPos += 1;
|
||||
|
||||
// Check if string complete (end with "x")
|
||||
if (inputChar == 'x') {
|
||||
isStrComplete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Serial.println(inputStr);
|
||||
|
||||
// Find the end of char "x"
|
||||
int posX = -1;
|
||||
for(uint8_t i = 0; i < 20; i++) {
|
||||
if(inputStr[i] == 'x') {
|
||||
posX = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Consider 0 character in ASCII
|
||||
uint8_t zeroAscii = '0';
|
||||
|
||||
// Read Year first
|
||||
temp1 = (byte)inputStr[posX - 13] - zeroAscii;
|
||||
temp2 = (byte)inputStr[posX - 12] - zeroAscii;
|
||||
year = temp1 * 10 + temp2;
|
||||
|
||||
// now month
|
||||
temp1 = (byte)inputStr[posX - 11] - zeroAscii;
|
||||
temp2 = (byte)inputStr[posX - 10] - zeroAscii;
|
||||
month = temp1 * 10 + temp2;
|
||||
|
||||
// now date
|
||||
temp1 = (byte)inputStr[posX - 9] - zeroAscii;
|
||||
temp2 = (byte)inputStr[posX - 8] - zeroAscii;
|
||||
date = temp1 * 10 + temp2;
|
||||
|
||||
// now Day of Week
|
||||
dow = (byte)inputStr[posX - 7] - zeroAscii;
|
||||
|
||||
// now Hour
|
||||
temp1 = (byte)inputStr[posX - 6] - zeroAscii;
|
||||
temp2 = (byte)inputStr[posX - 5] - zeroAscii;
|
||||
hour = temp1 * 10 + temp2;
|
||||
|
||||
// now Minute
|
||||
temp1 = (byte)inputStr[posX - 4] - zeroAscii;
|
||||
temp2 = (byte)inputStr[posX - 3] - zeroAscii;
|
||||
minute = temp1 * 10 + temp2;
|
||||
|
||||
// now Second
|
||||
temp1 = (byte)inputStr[posX - 2] - zeroAscii;
|
||||
temp2 = (byte)inputStr[posX - 1] - zeroAscii;
|
||||
second = temp1 * 10 + temp2;
|
||||
}
|
Reference in New Issue
Block a user