Friends of the PCF8563 Real-Time Clock from Arduino to I2C

Hi, Habr. I happened to use RTC on the PCF 8563 chip via I2C. Since I did not find normal functions for more convenient work with Arduino, I had to work directly through I2C. I found an explanatory tutorial on the English Internet with a detailed description of the process . This article is partly a translation in part of its own experience with this RTC.







Set date and time



The first thing we climb into the manual ( it is here ) and look for a table of register organization there.







To set the date and time, we need registers from 02h to 08h. The data in these registers is stored in binary decimal format (BCD), therefore, to record seconds, minutes, etc. First, we implement the translation function (from decimal format to BCD and vice versa) and then use the Wire.write () command to write them into registers starting from 02h. We read in the same way, transferring from the BCD back to the decimal system, starting from the same register 02h. As you can see from the table, bits are not used in some registers (indicated by the letter x), so as not to overload information when reading, we will use the AND bit operation (logical β€œAND”) which will reset the bits we don’t need. For example, the days of the month (Days), we need bits 0 through 5, using the operation (dayOfMonth & B00111111) we reset the 6 and 7 bits, everything else remains untouched.



Directly the code itself with the output of information on the serial monitor:



#include "Wire.h" #define PCF8563address 0x51 //     byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; //     -    . byte bcdToDec(byte value) { return ((value / 16) * 10 + value % 16); } //   byte decToBcd(byte value){ return (value / 10 * 16 + value % 10); } //       PCF8563 void setPCF8563() { Wire.beginTransmission(PCF8563address); Wire.write(0x02); Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); } //       PCF8563 void readPCF8563() { Wire.beginTransmission(PCF8563address); Wire.write(0x02); Wire.endTransmission(); Wire.requestFrom(PCF8563address, 7); second = bcdToDec(Wire.read() & B01111111); //      minute = bcdToDec(Wire.read() & B01111111); hour = bcdToDec(Wire.read() & B00111111); dayOfMonth = bcdToDec(Wire.read() & B00111111); dayOfWeek = bcdToDec(Wire.read() & B00000111); month = bcdToDec(Wire.read() & B00011111); year = bcdToDec(Wire.read()); } void setup() { Wire.begin(); Serial.begin(9600); //     second = 0; minute = 28; hour = 9; dayOfWeek = 2; dayOfMonth = 13; month = 8; year = 13; setPCF8563(); } void loop() { readPCF8563(); Serial.print(days[dayOfWeek]); Serial.print(" "); Serial.print(dayOfMonth, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/20"); Serial.print(year, DEC); Serial.print(" - "); Serial.print(hour, DEC); Serial.print(":"); if (minute < 10) { Serial.print("0"); } Serial.print(minute, DEC); Serial.print(":"); if (second < 10) { Serial.print("0"); } Serial.println(second, DEC); delay(1000); }
      
      







Setting an alarm



In PCF 8563, you can enable the alarm and set it for a specific time, day of the week or month. The alarm settings are located in the register from 09h to 0h in binary - decimal format.







To enable the alarm by some settings, for example, minutes and hours, you need to set the 7th bit (enable bit) to one. For this, use the logical operation OR (OR) and the value B10000000.



Checking the alarm can be done in 2 ways: hard and soft. Using the soft method, we check the 3rd bit of register 0x01 (AF alarm flag bit). When triggered, it is equal to one, setting it to 0, the alarm goes off.







For hard verification, you need to convert 1 bit in the same register (AIE) to one. When the alarm goes off, the INT (interrupt) pin on the board goes into a conducting position, being an open-drain output, so you can safely solder an LED with a resistor, for example, and connect it all to 5 volts.



Now the sketch itself:



listing
 #include "Wire.h" #define PCF8563address 0x51 byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; byte alarmMinute, alarmHour, alarmDay, alarmDayOfWeek; String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; byte bcdToDec(byte value) { return ((value / 16) * 10 + value % 16); } byte decToBcd(byte value){ return (value / 10 * 16 + value % 10); } //        void setPCF8563alarm() { byte am, ah, ad, adow; am = decToBcd(alarmMinute); am = am | 100000000; //       ah = decToBcd(alarmHour); ah = ah | 100000000; //       ad = decToBcd(alarmDay); ad = ad | 100000000; //        adow = decToBcd(alarmDayOfWeek); adow = ad | 100000000; //       //        PCF8563 Wire.beginTransmission(PCF8563address); Wire.write(0x09); Wire.write(am); Wire.write(ah); // .         /* Wire.write(ad); Wire.write(adow); */ Wire.endTransmission(); //  .  INT     //     PCF8563alarmOff() Wire.beginTransmission(PCF8563address); Wire.write(0x01); Wire.write(B00000010); Wire.endTransmission(); } void PCF8563alarmOff() //      . { byte test; //    0x01h Wire.beginTransmission(PCF8563address); Wire.write(0x01); Wire.endTransmission(); Wire.requestFrom(PCF8563address, 1); test = Wire.read(); //  3    0 test = test - B00001000; //      0x01h Wire.beginTransmission(PCF8563address); Wire.write(0x01); Wire.write(test); Wire.endTransmission(); } void checkPCF8563alarm() //    { byte test; //    0x01h     test Wire.beginTransmission(PCF8563address); Wire.write(0x01); Wire.endTransmission(); Wire.requestFrom(PCF8563address, 1); test = Wire.read(); test = test & B00001000; if (test == B00001000) //    { Serial.println("** alarm **"); delay(2000); //     PCF8563alarmOff(); } } void setPCF8563() { Wire.beginTransmission(PCF8563address); Wire.write(0x02); Wire.write(decToBcd(second)); Wire.write(decToBcd(minute)); Wire.write(decToBcd(hour)); Wire.write(decToBcd(dayOfMonth)); Wire.write(decToBcd(dayOfWeek)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); } void readPCF8563() { Wire.beginTransmission(PCF8563address); Wire.write(0x02); Wire.endTransmission(); Wire.requestFrom(PCF8563address, 7); second = bcdToDec(Wire.read() & B01111111); minute = bcdToDec(Wire.read() & B01111111); hour = bcdToDec(Wire.read() & B00111111); dayOfMonth = bcdToDec(Wire.read() & B00111111); dayOfWeek = bcdToDec(Wire.read() & B00000111); month = bcdToDec(Wire.read() & B00011111); year = bcdToDec(Wire.read()); } void setup() { Wire.begin(); Serial.begin(9600); second = 50; minute = 44; hour = 13; dayOfWeek = 1; dayOfMonth = 19; month = 8; year = 13; setPCF8563(); //       alarmMinute = 45; alarmHour = 13; setPCF8563alarm(); } void loop() { readPCF8563(); Serial.print(days[dayOfWeek]); Serial.print(" "); Serial.print(dayOfMonth, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/20"); Serial.print(year, DEC); Serial.print(" - "); Serial.print(hour, DEC); Serial.print(":"); if (minute < 10) { Serial.print("0"); } Serial.print(minute, DEC); Serial.print(":"); if (second < 10) { Serial.print("0"); } Serial.println(second, DEC); delay(1000); //  checkPCF8563alarm(); }
      
      







Use as a generator



Like many RTCs, the PCF8563 can be used as a signal generator. The COT pin (pin 7 of the microcircuit) is an open drain, so you can blink an LED through this pin with a different frequency. Register 0x0D is responsible for the frequency parameters.







The seventh bit of this register (FE) turns on the generator. Bits 0 and 1 set the desired frequency. Bits 6 through 2 are not used.



Writing the following values ​​into the register, you can get the desired frequency:





Sketch example:



 #include "Wire.h" #define PCF8563address 0x51 void PCF8563oscOFF() //   { Wire.beginTransmission(PCF8563address); Wire.write(0x0D); Wire.write(0); Wire.endTransmission(); } void PCF8563osc1Hz() //    1 Hz { Wire.beginTransmission(PCF8563address); Wire.write(0x0D); Wire.write(B10000011); Wire.endTransmission(); } void PCF8563osc32Hz() //    32 Hz { Wire.beginTransmission(PCF8563address); Wire.write(0x0D); Wire.write(B10000010); Wire.endTransmission(); } void PCF8563osc1024kHz() //    1.024 kHz { Wire.beginTransmission(PCF8563address); Wire.write(0x0D); Wire.write(B10000001); Wire.endTransmission(); } void PCF8563osc32768kHz() //    32.768 kHz { Wire.beginTransmission(PCF8563address); Wire.write(0x0D); Wire.write(B10000000); Wire.endTransmission(); } void setup() { Wire.begin(); } void loop() { PCF8563osc1Hz(); delay(2000); PCF8563osc32Hz(); delay(2000); PCF8563osc1024kHz(); delay(2000); PCF8563osc32768kHz(); delay(2000); PCF8563oscOFF(); delay(2000); }
      
      







Examples of waveforms taken from the LED



1 Hz







32 Hz







1.024 kHz







32.768 kHz










All Articles