Programming PCF85263AT with Arduino IDE

From media_wiki
Revision as of 04:15, 23 March 2019 by U731219879 rc (talk | contribs) (Created page with "Reference page 9 for the PCF85263AT-RTC data registers in the data sheet: https://www.nxp.com/docs/en/data-sheet/PCF85263A.pdf <br> Connect '''Pin 5 (SDA) & Pin 4 (SCL)''' on...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Reference page 9 for the PCF85263AT-RTC data registers in the data sheet: https://www.nxp.com/docs/en/data-sheet/PCF85263A.pdf
Connect Pin 5 (SDA) & Pin 4 (SCL) on the PCF8526AT board to Pin A5 (SDA) & Pin A4 (SCL) on the Arduino Uno
Copy and Paste the PCF85263AT RTC program into the Arduino IDE.
Update the values in the void setup() second, minute, hour, dayOfWeek, dayOfMonth, month, year to the current values.
Test to make sure everything was entered correctly by clicking on the Check-mark. Correct any errors.
Upload the program to the Arduino Uno.
PCF85263AT-RTC.INO

// PCF85263AT RTC write/read demonstration
#include "Wire.h"              //Enable I2C library
#define PCF85263address 0x51   //define I2C address 
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);
}
void setPCF85263()            // this function sets the time and date to the PCF85263
 
 {
   Wire.beginTransmission(PCF85263address);
   Wire.write(0x01);
   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 readPCF85263()          // this gets the time and date from the PCF85263
 {
   Wire.beginTransmission(PCF85263address);
   Wire.write(0x01);
   Wire.endTransmission();
   Wire.requestFrom(PCF85263address, 7);
   second     = bcdToDec(Wire.read() & B01111111); // remove VL error bit
   minute     = bcdToDec(Wire.read() & B01111111); // remove unwanted bits from MSB
   hour       = bcdToDec(Wire.read() & B00111111); 
   dayOfMonth = bcdToDec(Wire.read() & B00111111);
   dayOfWeek  = bcdToDec(Wire.read() & B00000111);  
   month      = bcdToDec(Wire.read() & B00011111); // remove century bit, 1999 is over
   year       = bcdToDec(Wire.read());
 }
void setup()
 {
   Wire.begin();
   Serial.begin(9600);
 // change the following to set your initial time
   second = 0;
   minute = 9;
   hour = 21;
   dayOfWeek = 0;
   dayOfMonth = 6;
   month = 1;
   year = 19;
 // comment out the next line and upload again to set and keep the time from resetting every reset
 setPCF85263();
 }
void loop()
 {
   readPCF85263();
   Serial.print(days[dayOfWeek]); 
   Serial.print(" ");  
   Serial.print(month, DEC);
   Serial.print("/");
   Serial.print(dayOfMonth, 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);
}


Open Console port and watch output.