Dependents:   Bulme-Timer Mbed-21-Clock-to-7Segment WS_7_Seg_mit_LM1635 WS_7_Seg_mit_LM1635 ... more

Fork of PCF8563 by Franz Pucher

Committer:
04M04
Date:
Thu Jun 23 18:59:15 2016 +0000
Revision:
4:1246b8a8f15f
Parent:
3:d7f120bcedd7
PCF8563

Who changed what in which revision?

UserRevisionLine numberNew contents of line
04M04 4:1246b8a8f15f 1
fpucher 1:43c0cae8d6b0 2 /***********************************
fpucher 2:39c99dbc6368 3 name: PCF8563.cpp Version: 0.3
fpucher 1:43c0cae8d6b0 4 author: PE HTL BULME
fpucher 1:43c0cae8d6b0 5 email: pe@bulme.at
fpucher 1:43c0cae8d6b0 6 description:
fpucher 1:43c0cae8d6b0 7 Implementation portion of class rtc
fpucher 1:43c0cae8d6b0 8 real time clock on himbed0
fpucher 1:43c0cae8d6b0 9
fpucher 1:43c0cae8d6b0 10 ***********************************/
fpucher 1:43c0cae8d6b0 11
fpucher 1:43c0cae8d6b0 12 #include "mbed.h"
fpucher 1:43c0cae8d6b0 13 #include "const.h"
fpucher 1:43c0cae8d6b0 14 #include "PCF8563.h"
fpucher 1:43c0cae8d6b0 15
fpucher 1:43c0cae8d6b0 16 PCF8563::~PCF8563()
fpucher 1:43c0cae8d6b0 17 {}
fpucher 1:43c0cae8d6b0 18
fpucher 1:43c0cae8d6b0 19 // Default constructor
fpucher 1:43c0cae8d6b0 20 PCF8563::PCF8563() : i2c(p28, p27) // HIMBED M0
fpucher 1:43c0cae8d6b0 21 {
fpucher 1:43c0cae8d6b0 22 i2c.frequency(40000); // I2C Frequenz 40kHz
fpucher 1:43c0cae8d6b0 23 char init1[2] = {0x6, 0x00};
fpucher 1:43c0cae8d6b0 24 char init2[2] = {0x7, 0xff};
fpucher 1:43c0cae8d6b0 25 i2c.write(0x40, init1, 2);
fpucher 1:43c0cae8d6b0 26 i2c.write(0x40, init2, 2);
fpucher 1:43c0cae8d6b0 27
fpucher 1:43c0cae8d6b0 28 }
fpucher 1:43c0cae8d6b0 29
fpucher 1:43c0cae8d6b0 30 // Param. constructor with pins sda and scl
fpucher 1:43c0cae8d6b0 31 PCF8563::PCF8563(PinName sda, PinName scl) : i2c(sda, scl) //_sda(sda), _scl(scl) // no I2C default constructor
fpucher 1:43c0cae8d6b0 32 {
fpucher 1:43c0cae8d6b0 33 i2c.frequency(40000); // I2C Frequenz 40kHz
fpucher 1:43c0cae8d6b0 34 char init1[2] = {0x6, 0x00};
fpucher 1:43c0cae8d6b0 35 char init2[2] = {0x7, 0xff};
fpucher 1:43c0cae8d6b0 36 i2c.write(0x40, init1, 2);
fpucher 1:43c0cae8d6b0 37 i2c.write(0x40, init2, 2);
fpucher 1:43c0cae8d6b0 38 }
fpucher 1:43c0cae8d6b0 39
fpucher 1:43c0cae8d6b0 40 char PCF8563::read(char address)
fpucher 1:43c0cae8d6b0 41 {
fpucher 1:43c0cae8d6b0 42 char value;
fpucher 1:43c0cae8d6b0 43 i2c.start();
fpucher 1:43c0cae8d6b0 44 //i2c.write(PCF8563_ADR_RD, &address, true);
fpucher 1:43c0cae8d6b0 45 i2c.write(PCF8563_ADR_WR);
fpucher 1:43c0cae8d6b0 46 i2c.write(address);
fpucher 1:43c0cae8d6b0 47 i2c.start();
fpucher 1:43c0cae8d6b0 48 i2c.write(PCF8563_ADR_RD);
fpucher 1:43c0cae8d6b0 49 value = i2c.read(0);
fpucher 1:43c0cae8d6b0 50 i2c.stop();
fpucher 1:43c0cae8d6b0 51 return value;
fpucher 1:43c0cae8d6b0 52 }
fpucher 1:43c0cae8d6b0 53 void PCF8563::write(char address, char value)
fpucher 1:43c0cae8d6b0 54 {
fpucher 1:43c0cae8d6b0 55 i2c.start();
fpucher 1:43c0cae8d6b0 56 //i2c.write(PCF8563_ADR_WR, &address, value, true);
fpucher 1:43c0cae8d6b0 57 i2c.write(PCF8563_ADR_WR);
fpucher 1:43c0cae8d6b0 58 i2c.write(address);
fpucher 1:43c0cae8d6b0 59 i2c.write(value);
fpucher 1:43c0cae8d6b0 60 i2c.stop();
fpucher 1:43c0cae8d6b0 61 }
fpucher 1:43c0cae8d6b0 62
fpucher 1:43c0cae8d6b0 63 void PCF8563::init()
fpucher 1:43c0cae8d6b0 64 {
fpucher 1:43c0cae8d6b0 65 // Format
fpucher 1:43c0cae8d6b0 66 // 2015/03/31
fpucher 1:43c0cae8d6b0 67 // 22:10:00
fpucher 1:43c0cae8d6b0 68
fpucher 1:43c0cae8d6b0 69 week_val = 0x01; // Tu
fpucher 1:43c0cae8d6b0 70 write(CONTROL1, 0x20); // stop
fpucher 1:43c0cae8d6b0 71 write(CONTROL2, 0x00);
fpucher 1:43c0cae8d6b0 72 write(YEARS, (0x15));
fpucher 1:43c0cae8d6b0 73 write(MONTHS, (0x03));
fpucher 1:43c0cae8d6b0 74 write(DAYS, (0x31));
fpucher 1:43c0cae8d6b0 75 write(HOURS, (0x22));
fpucher 1:43c0cae8d6b0 76 write(MINUTES, (0x10));
fpucher 1:43c0cae8d6b0 77 write(SECONDS, (0x00));
fpucher 1:43c0cae8d6b0 78 write(WEEKDAYS, week_val);
fpucher 1:43c0cae8d6b0 79 write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
04M04 3:d7f120bcedd7 80 write(TIMER_CONTROL, 0x00);
fpucher 1:43c0cae8d6b0 81 write(CONTROL1, 0x00); // start
fpucher 1:43c0cae8d6b0 82 }
fpucher 1:43c0cae8d6b0 83
fpucher 1:43c0cae8d6b0 84 void PCF8563::alarm() // Setting up RTC alarm
fpucher 1:43c0cae8d6b0 85 {
fpucher 1:43c0cae8d6b0 86 write(CONTROL1, 0x20); // stop
fpucher 1:43c0cae8d6b0 87 write(CONTROL2, 0x02); // AIE - alarm interrupt enabled
fpucher 1:43c0cae8d6b0 88 write(DAY_ALARM, (0x80) );
fpucher 1:43c0cae8d6b0 89 write(HOUR_ALARM, (0x97));
fpucher 1:43c0cae8d6b0 90 write(MINUTE_ALARM, (0x02));
fpucher 1:43c0cae8d6b0 91 write(WEEKDAY_ALARM, (0x80));
fpucher 1:43c0cae8d6b0 92 write(CONTROL1, 0x00); // start
fpucher 1:43c0cae8d6b0 93 }
fpucher 1:43c0cae8d6b0 94
fpucher 1:43c0cae8d6b0 95 //-----------------INTERNAL USE ONLY ----------------------------
fpucher 1:43c0cae8d6b0 96 void PCF8563::error()
fpucher 1:43c0cae8d6b0 97 {
fpucher 1:43c0cae8d6b0 98 }