Franz Pucher / RTC_DateTime

Files at this revision

API Documentation at this revision

Comitter:
fpucher
Date:
Thu Nov 15 06:55:25 2018 +0000
Commit message:
ifdef target

Changed in this revision

Base.h Show annotated file Show diff for this revision Revisions of this file
Clock.cpp Show annotated file Show diff for this revision Revisions of this file
Clock.h Show annotated file Show diff for this revision Revisions of this file
Date.cpp Show annotated file Show diff for this revision Revisions of this file
Date.h Show annotated file Show diff for this revision Revisions of this file
Datestr.cpp Show annotated file Show diff for this revision Revisions of this file
Datestr.h Show annotated file Show diff for this revision Revisions of this file
Time.cpp Show annotated file Show diff for this revision Revisions of this file
Time.h Show annotated file Show diff for this revision Revisions of this file
const.h Show annotated file Show diff for this revision Revisions of this file
rtc.cpp Show annotated file Show diff for this revision Revisions of this file
rtc.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Base.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,11 @@
+#ifndef BASE_H
+#define BASE_H
+class Base      // abstrakte Klasse, da zumindest eine reine virt. meth.
+{
+    protected:
+    Base(){}    // Konstruktor in protected Bereich, daher keine Instanziierung möglich
+    public:
+    virtual string Ausgabe() = 0;   // pure virtual methode: wird in abgeleiteten Klasse implementiert
+                        // muss für jeweilige abgeleitete Klasse implementiert werden
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Clock.cpp	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,13 @@
+#include "rtc.h"
+#include "string"
+#include "Datestr.h"
+#include "Time.h"
+#include "Clock.h"
+
+string Clock::Print()
+{
+    char buffer[40];
+    sprintf(buffer, "Datum = %d/%d/%d Zeit = %d:%02d:%02d\n", GetYear(), GetMonth(), GetDay(), GetHours(), GetMinutes(), GetSeconds()); // Plain Old Data
+    //sprintf(buffer, "Zeit = %d:%02d:%02d\n", GetHours(), GetMinutes(), GetSeconds());
+    return buffer;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Clock.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,18 @@
+#include "rtc.h"
+#include "string"
+#include "Date.h"
+#include "Time.h"
+
+#ifndef CLOCK_H
+#define CLOCK_H
+
+class Clock : public Datestr, public Time
+{
+private:
+    
+
+public:
+    Clock(){}
+    string Print();
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Date.cpp	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,51 @@
+#include "Date.h"
+#include "string"
+
+/*
+string Date::GetDay(uint8_t y)
+{
+    //return std::to_string(y); // C++ 11
+    char buffer[2];
+    sprintf (buffer, "%d", y);  // ToString
+    return buffer;
+}
+*/
+uint8_t Date :: GetDay()
+{
+    uint8_t s = rtc_read(DAYS);
+    return bcdToUint(s&0x3F);
+}
+uint8_t Date :: GetMonth()
+{
+    uint8_t s = rtc_read(MONTHS);
+    return bcdToUint(s&0x1F);
+}
+
+uint16_t Date :: GetYear()
+{
+    uint8_t y = rtc_read(YEARS);
+    y = bcdToUint(y&0x3F);
+    return y+2000;
+}
+
+uint8_t Date::bcdToUint(uint8_t const nybbles)
+{
+    uint8_t result;
+    result = (nybbles >> 4) * 10 + (nybbles & 0x0F);
+    return result;
+}
+
+string Date::toString(uint8_t value)
+{
+    //return std::to_string(value); // C++ version 11
+    char buffer[2];
+    sprintf (buffer, "%d", value);  // ToString()
+    return buffer;
+}    
+    
+string Date::GetDay(uint8_t y)
+{
+    day = "Date " + toString(y);
+    return day;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Date.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+#include "const.h"
+#include "rtc.h"
+#include "string"
+#include "Base.h"
+
+#ifndef DATE_H
+#define DATE_H
+
+class Date : public RTC //, public Base
+{
+private:
+    uint8_t bcdToUint(uint8_t const nybbles);
+    string toString(uint8_t value);
+    string day, month, year;
+public:
+    Date()
+    {}
+    uint16_t GetYear();
+    uint8_t GetMonth();
+    uint8_t GetDay();
+    virtual string GetDay(uint8_t value);
+/* Funkt nicht!
+    string Ausgabe() {          // Implementierung der rein virt. Meth. aus Base
+        char buffer[40];
+        sprintf(buffer, "Datum = %d/%d/%d \n", GetYear(), GetMonth(), GetDay()); // Plain Old Data
+        //sprintf(buffer, "Zeit = %d:%02d:%02d\n", GetHours(), GetMinutes(), GetSeconds());
+        return buffer;
+    }
+*/
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Datestr.cpp	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,16 @@
+#include "Datestr.h"
+#include "string"
+
+string Datestr::toString(uint8_t value)
+{
+    //return std::to_string(value); // C++ version 11
+    char buffer[2];
+    sprintf (buffer, "%d", value);  // ToString()
+    return buffer;
+}    
+    
+string Datestr::GetDay(uint8_t value)
+{
+    day = "Datestr " + toString(value);
+    return day;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Datestr.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,43 @@
+#include "mbed.h"
+#include "const.h"
+#include "rtc.h"
+#include "string"
+#include "Date.h"
+
+#ifndef DATESTR_H
+#define DATESTR_H
+
+class A
+{
+protected:
+    string test;
+public:
+    void Print(int anzahl, string r);
+    void Print(string r);
+};
+class B : public A
+{
+private:
+    int anzahl;
+public:
+    void Print(string nr);
+    using A::Print;
+};
+
+
+
+
+class Datestr : public Date
+{
+    private:
+    string day, month, year;
+    string toString(uint8_t value);
+    
+    public:
+//    string day, month, year;
+    Datestr()
+    {}
+    virtual string GetDay(uint8_t value);
+    using Date::GetDay;     // ermöglicht den Aufruf der Methode in Basisklasse Date, sonst Error 165
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Time.cpp	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,26 @@
+#include "Time.h"
+
+uint8_t Time :: GetHours()
+{
+    uint8_t h = rtc_read(HOURS);
+    return bcdToUint(h&0x3F);
+}
+
+uint8_t Time :: GetMinutes()
+{
+    uint8_t m = rtc_read(MINUTES);
+    return bcdToUint(m&0x7F);
+}
+
+uint8_t Time :: GetSeconds()
+{
+    uint8_t s = rtc_read(SECONDS);
+    return bcdToUint(s&0x7F);
+}
+
+uint8_t Time::bcdToUint(uint8_t const nybbles)
+{
+    uint8_t result;
+    result = (nybbles >> 4) * 10 + (nybbles & 0x0F);
+    return result;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Time.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,20 @@
+#include "mbed.h"
+#include "const.h"
+#include "rtc.h"
+
+#ifndef TIME_H
+#define TIME_H
+
+class Time : public RTC
+{
+    private:
+    
+    public:
+    Time()
+    {}
+    uint8_t GetHours();
+    uint8_t GetMinutes();
+    uint8_t GetSeconds();
+    uint8_t bcdToUint(uint8_t const nybbles);
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/const.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,40 @@
+/***********************************
+name:   const.h    Version: 0.3
+author: PE HTL BULME
+email:  pe@bulme.at
+description:
+  Named constants definitions for registers 
+  PCF8563 RTC on HIMBED M0 - LPC11U24 
+***********************************/
+ 
+#ifndef CONST_H
+#define CONST_H
+ 
+// Address of RTC
+const uint8_t RTC8563_ADR_WR = 0xA2;
+const uint8_t RTC8563_ADR_RD = 0xA3;
+// Control and status
+const uint8_t CONTROL1 = 0x00;
+const uint8_t CONTROL2 = 0x01;
+// Time and date
+const uint8_t SECONDS = 0x02;   
+const uint8_t MINUTES = 0x03;
+const uint8_t HOURS = 0x04;
+const uint8_t DAYS = 0x05;
+const uint8_t WEEKDAYS = 0x06;
+const uint8_t MONTHS = 0x07;
+const uint8_t YEARS = 0x08;
+// Alarm
+const uint8_t MINUTE_ALARM = 0x09;
+const uint8_t HOUR_ALARM = 0x0A;
+const uint8_t DAY_ALARM = 0x0B;
+const uint8_t WEEKDAY_ALARM = 0x0C;
+// Clock and timer
+const uint8_t CLOCKOUT_FREQ = 0x0D;
+const uint8_t TIMER_CINTROL = 0x0E;
+
+// Alternativ addressing: base + next address
+//const uint8_t RTC8563_ADR = 0xA2;
+//const uint8_t _READ = 0x01;
+ 
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtc.cpp	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,123 @@
+/***********************************
+name:   rtc.cpp    Version: 0.2
+author: PE HTL BULME
+email:  pe@bulme.at
+description:
+    Implementation portion of class rtc 
+    real time clock on himbed0
+      
+***********************************/
+
+#include "mbed.h"
+#include "const.h"
+#include "rtc.h"
+
+RTC::~RTC()
+{}
+
+#ifdef TARGET_LPC11XX
+// Default constructor
+RTC::RTC() : i2c(p28, p27)              // HIMBED M0
+{
+    i2c.frequency(40000);               // I2C Frequenz 40kHz
+    char init1[2] = {0x6, 0x00};
+    char init2[2] = {0x7, 0xff};
+    i2c.write(0x40, init1, 2);
+    i2c.write(0x40, init2, 2);
+
+}
+
+// Param. constructor with pins sda and scl
+RTC::RTC(PinName sda, PinName scl) :  i2c(sda, scl)  //_sda(sda), _scl(scl)   // no I2C default constructor
+{
+    i2c.frequency(40000);               // I2C Frequenz 40kHz
+    char init1[2] = {0x6, 0x00};
+    char init2[2] = {0x7, 0xff};
+    i2c.write(0x40, init1, 2);
+    i2c.write(0x40, init2, 2);
+}
+
+char RTC::rtc_read(char address)
+{
+    char value;
+    i2c.start();
+    //i2c.write(RTC8563_ADR_RD, &address, true);
+    i2c.write(RTC8563_ADR_WR);
+    i2c.write(address);
+    i2c.start();
+    i2c.write(RTC8563_ADR_RD);
+    value = i2c.read(0);
+    i2c.stop();
+    return value;
+}
+void RTC::rtc_write(char address, char value)
+{
+    i2c.start();
+    //i2c.write(RTC8563_ADR_WR, &address, value, true);
+    i2c.write(RTC8563_ADR_WR);
+    i2c.write(address);
+    i2c.write(value);
+    i2c.stop();
+}
+
+void RTC::rtc_init()
+{
+    // Format
+    // 2015/03/31
+    // 22:10:00
+
+    week_val = 0x01;                // Tu
+    rtc_write(CONTROL1, 0x20);      // stop
+    rtc_write(CONTROL2, 0x00);
+    rtc_write(YEARS, (0x15));
+    rtc_write(MONTHS, (0x03));
+    rtc_write(DAYS, (0x31));
+    rtc_write(HOURS, (0x22));
+    rtc_write(MINUTES, (0x10));
+    rtc_write(SECONDS, (0x00));
+    rtc_write(WEEKDAYS, week_val);
+    rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
+    rtc_write(TIMER_CINTROL, 0x00);
+    rtc_write(CONTROL1, 0x00);      // start
+}
+
+void RTC::rtc_alarm()   // Setting up RTC alarm
+{
+    rtc_write(CONTROL1, 0x20);      // stop
+    rtc_write(CONTROL2, 0x02);      // AIE - alarm interrupt enabled
+    rtc_write(DAY_ALARM, (0x80) );
+    rtc_write(HOUR_ALARM, (0x97));
+    rtc_write(MINUTE_ALARM, (0x02));
+    rtc_write(WEEKDAY_ALARM, (0x80));
+    rtc_write(CONTROL1, 0x00);      // start
+}
+#else
+
+// Default constructor
+RTC::RTC() 
+{}
+RTC::RTC(PinName sda, PinName scl) // :  i2c(sda, scl)  //_sda(sda), _scl(scl)   // no I2C default constructor
+{}
+
+char RTC::rtc_read(char address) {
+    return 0x21;
+}
+
+void RTC::rtc_write(char address, char value)
+{ }
+
+void RTC::rtc_init()
+{
+    // Format
+    // 2015/03/31
+    // 22:10:00
+}
+
+void RTC::rtc_alarm()   // Setting up RTC alarm
+{}
+#endif
+
+//-----------------INTERNAL USE ONLY ----------------------------
+void RTC::error()
+{
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rtc.h	Thu Nov 15 06:55:25 2018 +0000
@@ -0,0 +1,56 @@
+/***********************************
+name:   rtc.h      Version: 0.1
+author: PE HTL BULME
+email:  pe@bulme.at
+description:
+    Definition portion of the class rtc
+
+
+***********************************/
+
+#include "mbed.h"
+#include "const.h"
+
+#if !defined (RTC_H)
+#define RTC_H
+
+/** RTC Class
+ */
+ 
+ /** 
+name:   rtc.h      Version: 0.1
+author: PE HTL BULME
+email:  pe@bulme.at
+description:
+    Definition portion of the class rtc
+
+
+***********************************/
+class RTC
+{
+protected:
+    //PinName _sda, _scl;  
+#ifdef TARGET_LPC11XX   
+    I2C i2c;                    /**< I2C definition */
+#endif
+    char week_val;
+
+    /**
+    protected methodes for internal purposes only
+    */
+    void error();               /**< Error: stops and all LEDs are blinking*/
+
+public:
+    /** Create a RTC object connected to the specified pin
+    * @param pin sda/scl pin to connect to
+    */
+    RTC();                          /**< default constructor; sda(p28), sdc(p27) */
+    RTC(PinName sda, PinName scl);  /**< parameterized constructor with sda-pin, sdc-pin)*/
+    ~RTC();
+    char rtc_read(char address);    /**< read value from I2C bus; ToDo --> Error()*/ 
+    void rtc_write(char address, char value);   /**< write value to I2C bus; ToDo --> Error()*/ 
+    void rtc_init();                /**< initialize the RTC to a given date and time; ToDo --> Error()*/ 
+    void rtc_alarm();
+};
+
+#endif
\ No newline at end of file