Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit bf7be28

Browse files
authored
Added support for PCF8563T (#7)
Added support for PCF8563T to automation carrier library
1 parent 275b47e commit bf7be28

File tree

5 files changed

+258
-2
lines changed

5 files changed

+258
-2
lines changed

examples/RTC_Example/RTC_Example.ino

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
RTC Example
3+
4+
This sketch shows how to use the RTC PCF8563T on the Automation
5+
Carrier and how to configure the PCF8563T's time registers.
6+
7+
Circuit:
8+
- Portenta H7
9+
- Automation Carrier
10+
11+
12+
created 24 September 2020
13+
by Riccardo Rizzo
14+
*/
15+
#include <AutomationCarrier.h>
16+
17+
using namespace automation;
18+
19+
int years = 20;
20+
int months = 9;
21+
int days = 24;
22+
int hours = 12;
23+
int minutes = 43;
24+
int seconds = 31;
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
while (!Serial) {
29+
; // wait for serial port to connect.
30+
}
31+
32+
Serial.println("Initialization");
33+
if(!rtc_controller.begin()) {
34+
Serial.println("Initialization fail!");
35+
}
36+
Serial.println("Initialization Done!");
37+
38+
// APIs to set date's fields: years, months, days, hours, minutes and seconds
39+
rtc_controller.setYears(years);
40+
rtc_controller.setMonths(months);
41+
rtc_controller.setDays(days);
42+
rtc_controller.setHours(hours);
43+
rtc_controller.setMinutes(minutes);
44+
rtc_controller.setSeconds(seconds);
45+
}
46+
47+
void loop() {
48+
// APIs to get date's fields.
49+
Serial.print("Date: ");
50+
Serial.print(rtc_controller.getYears());
51+
Serial.print("/");
52+
Serial.print(rtc_controller.getMonths());
53+
Serial.print("/");
54+
Serial.print(rtc_controller.getDays());
55+
Serial.print(" - ");
56+
Serial.print(rtc_controller.getHours());
57+
Serial.print(":");
58+
Serial.print(rtc_controller.getMinutes());
59+
Serial.print(":");
60+
Serial.println(rtc_controller.getSeconds());
61+
delay(60000);
62+
}

src/AutomationCarrier.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "utility/RS485/RS485.h"
77
#include "utility/QEI/QEI.h"
88
#include "utility/ioexpander/TCA6424A.h"
9+
#include "utility/RTC/PCF8563T.h"
910

1011
#include "Arduino.h"
1112
#include "mbed.h"
@@ -222,6 +223,17 @@ class DigitalInputsClass {
222223
};
223224

224225
extern DigitalInputsClass digital_inputs;
226+
227+
228+
class RtcControllerClass : public PCF8563TClass {
229+
public:
230+
mbed::DigitalIn int_pin = mbed::DigitalIn(PB_9);
231+
private:
232+
233+
};
234+
235+
extern RtcControllerClass rtc_controller;
236+
225237
}
226238

227239
#endif

src/Objects.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ EncoderClass encoders;
1010
DigitalOutputsClass digital_outputs;
1111
DigitalInputsClass digital_inputs;
1212
ProgrammableDIOClass programmable_digital_io;
13-
14-
}
13+
RtcControllerClass rtc_controller;
14+
}

src/utility/RTC/PCF8563T.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
This file is part of the AutomationCarrier library.
3+
Copyright (c) 2020 Arduino SA.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "PCF8563T.h"
21+
22+
#define PCF8563T_ADDRESS 0x51
23+
#define PCF8563T_VL_SECONDS_REG 0X02
24+
#define PCF8563T_MINUTES_REG 0x03
25+
#define PCF8563T_HOURS_REG 0X04
26+
#define PCF8563T_DAYS_REG 0x05
27+
#define PCF8563T_MONTHS_REG 0x07
28+
#define PCF8563T_YEARS_REG 0x08
29+
30+
PCF8563TClass::PCF8563TClass()
31+
{
32+
}
33+
34+
bool PCF8563TClass::begin()
35+
{
36+
Wire1.begin(); // join i2c bus
37+
38+
Wire1.beginTransmission(PCF8563T_ADDRESS);
39+
if (!Wire1.endTransmission()) {
40+
return true;
41+
}
42+
return false;
43+
}
44+
45+
void PCF8563TClass::setYears(uint8_t years) {
46+
uint8_t dec = years / 10;
47+
uint8_t unit = years - (dec * 10);
48+
writeByte(PCF8563T_YEARS_REG, ((dec << 4) + unit));
49+
}
50+
51+
void PCF8563TClass::setMonths(uint8_t months) {
52+
uint8_t offset = 0;
53+
if (months > 9) {
54+
offset = 6;
55+
}
56+
writeByte(PCF8563T_MONTHS_REG, months + offset);
57+
}
58+
59+
void PCF8563TClass::setDays(uint8_t days) {
60+
uint8_t dec = days / 10;
61+
uint8_t unit = days - (dec * 10);
62+
writeByte(PCF8563T_DAYS_REG, ((dec << 4) + unit));
63+
}
64+
65+
void PCF8563TClass::setHours(uint8_t hours) {
66+
uint8_t dec = hours / 10;
67+
uint8_t unit = hours - (dec * 10);
68+
writeByte(PCF8563T_HOURS_REG, ((dec << 4) + unit)); //check formula on datasheet val + 6 * (val / 10)
69+
}
70+
71+
void PCF8563TClass::setMinutes(uint8_t minutes) {
72+
uint8_t dec = minutes / 10;
73+
uint8_t unit = minutes - (dec * 10);
74+
writeByte(PCF8563T_MINUTES_REG, ((dec << 4) + unit));
75+
}
76+
77+
void PCF8563TClass::setSeconds(uint8_t seconds) {
78+
uint8_t dec = seconds / 10;
79+
uint8_t unit = seconds - (dec * 10);
80+
writeByte(PCF8563T_VL_SECONDS_REG, ((dec << 4) + unit));
81+
}
82+
83+
uint8_t PCF8563TClass::getYears() {
84+
uint8_t years = readByte(PCF8563T_YEARS_REG);
85+
return (years & 0x0F) + ((years >> 4)*10);
86+
}
87+
88+
uint8_t PCF8563TClass::getMonths() {
89+
uint8_t months = readByte(PCF8563T_MONTHS_REG) & 0x1F;
90+
if(months > 9) {
91+
return months - 6;
92+
} else {
93+
return months;
94+
}
95+
}
96+
97+
uint8_t PCF8563TClass::getDays() {
98+
uint8_t days = readByte(PCF8563T_DAYS_REG);
99+
return (days & 0x0F) + ((days >> 4)*10);
100+
}
101+
102+
uint8_t PCF8563TClass::getHours() {
103+
uint8_t hours = readByte(PCF8563T_HOURS_REG) & 0x3F;
104+
return (hours & 0x0F) + ((hours >> 4)*10);
105+
}
106+
107+
uint8_t PCF8563TClass::getMinutes() {
108+
uint8_t minutes = (readByte(PCF8563T_MINUTES_REG)) & 0x7F ;
109+
return (minutes & 0x0F) + ((minutes >> 4)*10);
110+
}
111+
112+
uint8_t PCF8563TClass::getSeconds() {
113+
uint8_t seconds = readByte(PCF8563T_VL_SECONDS_REG) & 0x7F;
114+
return (seconds & 0x0F) + ((seconds >> 4)*10);
115+
}
116+
117+
void PCF8563TClass::writeByte(uint8_t regAddres, uint8_t data) {
118+
Wire1.beginTransmission(PCF8563T_ADDRESS);
119+
Wire1.write(regAddres);
120+
Wire1.write(data);
121+
Wire1.endTransmission();
122+
}
123+
124+
uint8_t PCF8563TClass::readByte(uint8_t regAddres) {
125+
Wire1.beginTransmission(PCF8563T_ADDRESS);
126+
Wire1.write(regAddres); // Day Register
127+
Wire1.endTransmission();
128+
Wire1.requestFrom(PCF8563T_ADDRESS, 1);
129+
130+
return Wire1.read();
131+
}

src/utility/RTC/PCF8563T.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
This file is part of the AutomationCarrier library.
3+
Copyright (c) 2020 Arduino SA.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _PCF8563T_H_INCLUDED
21+
#define _PCF8563T_H_INCLUDED
22+
23+
#include "Arduino.h"
24+
#include "mbed.h"
25+
#include "Wire.h"
26+
27+
class PCF8563TClass {
28+
29+
public:
30+
PCF8563TClass();
31+
bool begin();
32+
void setYears(uint8_t years);
33+
void setMonths(uint8_t months);
34+
void setDays(uint8_t days);
35+
void setHours(uint8_t hours);
36+
void setMinutes(uint8_t minutes);
37+
void setSeconds(uint8_t seconds);
38+
39+
uint8_t getYears();
40+
uint8_t getMonths();
41+
uint8_t getDays();
42+
uint8_t getHours();
43+
uint8_t getMinutes();
44+
uint8_t getSeconds();
45+
46+
private:
47+
void writeByte(uint8_t regAddres, uint8_t data);
48+
uint8_t readByte(uint8_t regAddres);
49+
};
50+
51+
#endif

0 commit comments

Comments
 (0)