Franz Pucher / PCF8563
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCF8563.cpp Source File

PCF8563.cpp

00001 /***********************************
00002 name:   PCF8563.cpp    Version: 0.3
00003 author: PE HTL BULME
00004 email:  pe@bulme.at
00005 description:
00006     Implementation portion of class rtc 
00007     real time clock on himbed0
00008       
00009 ***********************************/
00010 
00011 #include "mbed.h"
00012 #include "const.h"
00013 #include "PCF8563.h"
00014 
00015 PCF8563::~PCF8563()
00016 {}
00017 
00018 // Default constructor
00019 PCF8563::PCF8563() : i2c(p28, p27)              // HIMBED M0
00020 {
00021     i2c.frequency(40000);               // I2C Frequenz 40kHz
00022     char init1[2] = {0x6, 0x00};
00023     char init2[2] = {0x7, 0xff};
00024     i2c.write(0x40, init1, 2);
00025     i2c.write(0x40, init2, 2);
00026 
00027 }
00028 
00029 // Param. constructor with pins sda and scl
00030 PCF8563::PCF8563(PinName sda, PinName scl) :  i2c(sda, scl)  //_sda(sda), _scl(scl)   // no I2C default constructor
00031 {
00032     i2c.frequency(40000);               // I2C Frequenz 40kHz
00033     char init1[2] = {0x6, 0x00};
00034     char init2[2] = {0x7, 0xff};
00035     i2c.write(0x40, init1, 2);
00036     i2c.write(0x40, init2, 2);
00037 }
00038 
00039 char PCF8563::read(char address)
00040 {
00041     char value;
00042     i2c.start();
00043     //i2c.write(PCF8563_ADR_RD, &address, true);
00044     i2c.write(PCF8563_ADR_WR);
00045     i2c.write(address);
00046     i2c.start();
00047     i2c.write(PCF8563_ADR_RD);
00048     value = i2c.read(0);
00049     i2c.stop();
00050     return value;
00051 }
00052 void PCF8563::write(char address, char value)
00053 {
00054     i2c.start();
00055     //i2c.write(PCF8563_ADR_WR, &address, value, true);
00056     i2c.write(PCF8563_ADR_WR);
00057     i2c.write(address);
00058     i2c.write(value);
00059     i2c.stop();
00060 }
00061 
00062 void PCF8563::init()
00063 {
00064     // Format
00065     // 2015/03/31
00066     // 22:10:00
00067 
00068     week_val = 0x01;                // Tu
00069     write(CONTROL1, 0x20);      // stop
00070     write(CONTROL2, 0x00);
00071     write(YEARS, (0x15));
00072     write(MONTHS, (0x03));
00073     write(DAYS, (0x31));
00074     write(HOURS, (0x22));
00075     write(MINUTES, (0x10));
00076     write(SECONDS, (0x00));
00077     write(WEEKDAYS, week_val);
00078     write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
00079     write(TIMER_CINTROL, 0x00);
00080     write(CONTROL1, 0x00);      // start
00081 }
00082 
00083 void PCF8563::alarm()   // Setting up RTC alarm
00084 {
00085     write(CONTROL1, 0x20);      // stop
00086     write(CONTROL2, 0x02);      // AIE - alarm interrupt enabled
00087     write(DAY_ALARM, (0x80) );
00088     write(HOUR_ALARM, (0x97));
00089     write(MINUTE_ALARM, (0x02));
00090     write(WEEKDAY_ALARM, (0x80));
00091     write(CONTROL1, 0x00);      // start
00092 }
00093 
00094 //-----------------INTERNAL USE ONLY ----------------------------
00095 void PCF8563::error()
00096 {
00097 }