Version 0.3

Committer:
fpucher
Date:
Fri Mar 11 20:44:37 2016 +0000
Revision:
2:39c99dbc6368
Parent:
1:43c0cae8d6b0
Only comments

Who changed what in which revision?

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