Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamCummick committed Nov 27, 2023
0 parents commit de468ce
Showing 10 changed files with 801 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 FACTS Engineering, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# PCF8563 RTC Arduino Library

Arduino library for the P1AM-200 PCF8563 RTC chip.

Use the Arduino Library Manager to install.

## Example
Here is a simple example which shows the capabilities of the library
```cpp
#include <PCF8563.h>

void setup(){
Serial.begin(9600);
while(!Serial){
;
}
Serial.println(PCF8563_RTC.getEpoch()); //Print RTC's epoch time
}

void loop(){
}
```
42 changes: 42 additions & 0 deletions examples/CheckLowVoltageBit/CheckLowVoltageBit.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Example: CheckLowVoltageBit
This example checks the voltage low bit of the RTC.
The voltage low bit in the RTC is set when the RTC voltage falls below the low voltage threshold
(0.9V).
The current Unix time stamp is measured as seconds since January 1st, 1970 and can be
found here. https://www.unixtimestamp.com/index.php
This example will check the low voltage bit of the PCF8563 RTC. In the case that the low voltage
bit is set the time is invalid. Otherwise the saved time will be displayed.
Written by FACTS Engineering
Copyright (c) 2023 FACTS Engineering, LLC
Licensed under the MIT license.
*/

#include <PCF8563.h>


void setup(){ // the setup routine runs once:

Serial.begin(9600); //start serial monitor connection for display
while(!Serial){ //wait for connection to serial monitor
;
}

bool checkVolt = PCF8563_RTC.lowVolt();
Serial.print("Is saved time valid? ");
if(PCF8563_RTC.lowVolt()){
Serial.println("No. ");
}
else{
Serial.print("Yes: ");
Serial.println(PCF8563_RTC.getEpoch()); //get saved time
}
delay(1000); //delay 1 second
}


void loop(){ // the loop routine runs over and over again forever
}
128 changes: 128 additions & 0 deletions examples/NTPserver/NTPserver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Example: NTPServer
This example gets the current time from an NTP server (time.nist.gov).
The RTC is set to the Unix time stamp read from the server.
The current Unix time stamp is measured as seconds since January 1st, 1970 and can be
found here. https://www.unixtimestamp.com/index.php
This example uses and ethernet shield.
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
Modified by FACTS Engineering
*/

#include <Ethernet.h>
#include <PCF8563.h>

uint8_t mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

IPAddress ip(192, 168, 6, 177);
IPAddress myDns(192, 168, 0, 1);

const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

EthernetUDP Udp;
void setup(){ // the setup routine runs once:

Serial.begin(9600); //start serial monitor connection for display
while(!Serial){ //wait for connection to serial monitor
;
}

Ethernet.init(5); // MKR ETH shield
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
}
else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}

Udp.begin(8888);
// give the Ethernet shield a second to initialize:
delay(1000);
setTime();
}

unsigned int current_time = 0;

void loop(){ // the loop routine runs over and over again forever:
current_time = PCF8563_RTC.getEpoch();
Serial.println(current_time);
Serial.println();

delay(5000); //delay 5 seconds
}

// send an NTP request to the time server at the given address
void sendNTPpacket(const char * address) {
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); // NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}

void setTime() {
sendNTPpacket(timeServer); // send an NTP packet to a time server

// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

// the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, extract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;

// now convert NTP time into everyday time:
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
PCF8563_RTC.setEpoch(epoch);
delay(1000); //delay 1 second
}
}
37 changes: 37 additions & 0 deletions examples/SetEpoch/SetEpoch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Example: SetEpochUnix
This example shows how to set the RTC to a given date and time using char arrays.
The current Unix time stamp is measured as seconds since January 1st, 1970 and can be
found here. https://www.unixtimestamp.com/index.php
This example will set the time to 8:00am Janurary 1st, 2021 and read the date and time from
the RTC every 5 seconds. This Unix time stamp is 1609488000.
Written by FACTS Engineering
Copyright (c) 2023 FACTS Engineering, LLC
Licensed under the MIT license.
*/

#include <PCF8563.h>

void setup(){ // the setup routine runs once:

Serial.begin(9600); //start serial monitor connection for display
while(!Serial){ //wait for connection to serial monitor
;
}

PCF8563_RTC.setEpoch("Jan 01 2021 Fri", "08:00:00"); //set time to 8:00am Janurary 1st, 2021
delay(1000); //delay 1 second
}

unsigned int current_time = 0;

void loop(){ // the loop routine runs over and over again forever:
current_time = PCF8563_RTC.getEpoch(); //get current time from PCF8563 RTC
Serial.println(current_time); //print time
Serial.println();

delay(5000); //delay 5 seconds
}
37 changes: 37 additions & 0 deletions examples/SetEpochUnix/SetEpochUnix.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Example: SetEpochUnix
This example shows how to set the RTC to a Unix time stamp.
The current Unix time stamp is measured as seconds since January 1st, 1970 and can be
found here. https://www.unixtimestamp.com/index.php
This example will set the time to 8:00am Janurary 1st, 2021 and read the date and time from
the RTC every 5 seconds.This Unix time stamp is 1609488000.
Written by FACTS Engineering
Copyright (c) 2023 FACTS Engineering, LLC
Licensed under the MIT license.
*/

#include <PCF8563.h>

void setup(){ // the setup routine runs once:

Serial.begin(9600); //start serial monitor connection for display
while(!Serial){ //wait for connection to serial monitor
;
}

PCF8563_RTC.setEpoch(1609488000); //set time to 8:00am Janurary 1st, 2021
delay(1000); //delay 1 second
}

unsigned int current_time = 0;

void loop(){ // the loop routine runs over and over again forever:
current_time = PCF8563_RTC.getEpoch(); //get current time from PCF8563 RTC
Serial.println(current_time); //print time
Serial.println();

delay(5000); //delay 5 seconds
}
23 changes: 23 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#######################################
# Syntax Coloring Map For RTC
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

PCF8563 KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

getEpoch KEYWORD2
setEpoch KEYWORD2
startClock KEYWORD2
stopClock KEYWORD2
lowVolt KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=PCF8563_RTC
version=1.0.0
author=FACTS Engineering
maintainer=FACTS Engineering <michaels@facts-eng.com>
sentence=A library that interfaces with the PCF8563 RTC.
paragraph=
category=Timing
url=https://github.com/facts-engineering/PCF8563_RTC
architectures=samd
Loading

0 comments on commit de468ce

Please sign in to comment.