This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Revision:
0:f30e2135b0db
Child:
1:995212d326ca
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Feb 09 13:57:49 2011 +0000
@@ -0,0 +1,200 @@
+#include <string>
+#include <iostream>
+#include <iomanip>
+
+#include "DS130x_I2C.h"
+#include "EthernetNetIf.h"
+#include "NTPClient.h"
+
+/*
+ * Declare functions
+ */
+void AvailableIndicator(); // LED1 flashing for program while program is alive
+void TimeUpdate(); // Update time from SNTP server
+void RtcClockEvent(); // Event rise on each RTC.SQW/OUT change
+char DisplayMenuAndGetChoice(); // Display and get the user choice
+void SetTimeFromSNTP();
+void ForceTime();
+void GetTime();
+void GetTimeFieldByFiled();
+void WriteStdStringValue(const unsigned char p_address, const std::string & p_string); // Write a string
+void ReadStdStringValue(const unsigned char p_address); // Read a string
+
+/*
+ * Declare statics
+ */
+EthernetNetIf g_eth;
+Host g_ntpServer(IpAddr(), 123, "ntp.unice.fr");
+NTPClient g_ntp;
+
+DigitalOut g_availableLed(LED1); // To verify if program in running
+InterruptIn g_rtcClock(p5); // Used to trigger event from RTC.SQW/OUT 
+Ticker g_available; // LED1 will flash with a period of 2s
+Ticker g_timeUpdate; // Update time from SNTP server
+
+CDS130X_I2C g_myRTC(0xd0, p28, p27, CDS130X_I2C::One_Hz); // Create an instance of the class C24LCXX_I2C, p28: SDA, p29:SDL, Oscillator mode: 1Hz, on 5V I2C bus
+
+int main() {
+
+    // Launch available indicator
+    g_available.attach(&AvailableIndicator, 2.0);
+    // Initialize the module    
+    if (!g_myRTC.Initialize()) {
+        error("Module initialization failed");
+    }
+    // Initialize Ethernet
+    g_eth.setup();   
+    wait(1); // Needed after Ethernet startup   
+    IpAddr ip = g_eth.getIp();
+    std::cout << "IP address: " << (int)ip[0] << "." << (int)ip[1] << "." << (int)ip[2] << "." << (int)ip[3] << "\r" << std::endl;
+    // Update current time
+    g_ntp.setTime(g_ntpServer);
+    time_t ctTime = time(NULL);  
+    std::cout << "Time is now: " << ctime(&ctTime) << " UTC\r" << std::endl; 
+    // Start ticker
+    g_timeUpdate.attach(&TimeUpdate, 60.0);
+    
+    // Start RTC.SQW/OUT trigger
+    g_rtcClock.rise(&RtcClockEvent);
+
+    while (true) {
+        switch (DisplayMenuAndGetChoice()) {
+            case 'a':
+                SetTimeFromSNTP();
+                break;
+            case 'b':
+                GetTime();
+                break;
+            case 'c':
+                ForceTime();
+                break;
+            case 'd':
+                GetTimeFieldByFiled();
+                break;
+
+            case 'l': {
+                    std::string str("Wellcome to Evil...");
+                    WriteStdStringValue(0x04, str);
+                }
+                break;
+            case 'm':
+#if defined(__DEBUG)
+                g_myRTC.DumpMemoryArea(0x04, 0x14);
+#else // __DEBUG
+                std::cout << "DEBUG mode is not set, nothing to do!\r" << std::endl;
+#endif // __DEBUG
+                break;
+            case 'n':
+                g_myRTC.EraseMemoryArea(0x04, 0x14);
+                break;
+            default:
+                std::cout << "Invalid user choice\r" << std::endl;
+                break;
+        } // End of 'switch' statement
+    } // End of 'while' statement
+} // End of program - nerver reached
+
+void AvailableIndicator()
+{
+    g_availableLed = !g_availableLed;
+} // End of AvailableIndicator
+
+void TimeUpdate()
+{
+    // Update current time
+    g_ntp.setTime(g_ntpServer);
+} // End of TimeUpdate funtion
+
+void RtcClockEvent() { /* Www Mmm dd hh:mm:ss yyyy */
+    struct tm t = g_myRTC.GetTime();
+    char buffer[32];
+    strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", &t);
+    DEBUG("RtcClockEvent: '%s'", buffer)
+} // End of RtcClockEvent() funtion
+
+char DisplayMenuAndGetChoice()
+{
+    std::cout << "\r" << std::endl << "\r" << std::endl << "DS13X_I2C v0.1\r" << std::endl;
+    std::cout << "\tSet time from SNTP server:\ta\r" << std::endl;
+    std::cout << "\tRead time from RTC:\t\tb\r" << std::endl;
+    std::cout << "\tForce time:\t\t\tc\r" << std::endl;
+    std::cout << "\tRead time field:\t\td\r" << std::endl;
+    std::cout << "\tSet second:\t\te\r" << std::endl;
+    std::cout << "\tInc hour:\t\tf\r" << std::endl;
+    std::cout << "\tDec hour:\t\tg\r" << std::endl;
+    std::cout << "\tInc month:\t\th\r" << std::endl;
+    std::cout << "\tDec month:\t\ti\r" << std::endl;
+
+
+
+
+
+    std::cout << "\tWrite a string at address 0x04:\tk\r" << std::endl;
+    std::cout << "\tRead a string is address 0x04:\tl\r" << std::endl;
+    std::cout << "\tHexadump from address 0x04:\tm\r" << std::endl;
+    std::cout << "\tErase from address 0x04:\tn\r" << std::endl;
+    std::cout << "Enter your choice: " << std::flush;
+    return getchar();
+}
+
+void SetTimeFromSNTP() {
+    DEBUG_ENTER("SetTimeFromSNTP")
+
+    char buffer[32];
+    time_t seconds = time(NULL);
+    strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", localtime(&seconds)); /* Www Mmm dd hh:mm:ss yyyy */
+    DEBUG("GetTime: Current date is '%s'", buffer)
+
+    std::string str(buffer);
+    g_myRTC.SetTime(str);
+
+    DEBUG_LEAVE("SetTimeFromSNTP")
+}
+
+void ForceTime() {
+    DEBUG_ENTER("ForceTime")
+
+    // Get time frome user
+    std::cout << "Enter the new date using the format 'Www Mmm dd hh:mm:ss yyyy': ";
+    std::string line;
+    std::cin >> line;
+    int removeCR;
+    if ((removeCR = line.rfind("\r")) != -1) {
+        line.erase(removeCR);
+    }
+    // Set it
+    g_myRTC.SetTime(line);
+
+    DEBUG_LEAVE("ForceTime")
+}
+
+void GetTime() {
+    struct tm t = g_myRTC.GetTime();
+    char buffer[32];
+    strftime(buffer, 32, "%a %m %d %H:%M:%S %Y", &t);
+    DEBUG("GetTime: Current date is '%s'", buffer)
+}
+
+void GetTimeFieldByFiled() {
+    unsigned char value = 0xff;
+
+    // Get seconds in BCD format
+    g_myRTC.Read(CDS130X_I2C::SecondsAddress, &value);
+    std::cout << "\tSeconds in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    // Get seconds in BCD format
+    g_myRTC.Read(CDS130X_I2C::MinutesAddress, &value);
+    std::cout << "\tMinutes in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    // Get seconds in BCD format
+    g_myRTC.Read(CDS130X_I2C::HoursAddress, &value);
+    std::cout << "\tHours in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+    // Get seconds in BCD format
+    g_myRTC.Read(CDS130X_I2C::DayAddress, &value);
+    std::cout << "\tDay in BCD: " << std::setw(2) << std::setfill('0') << std::ios::hex << value << "\r" << std::endl;
+}
+
+void WriteStdStringValue(const unsigned char p_address, const std::string & p_string) {
+    g_myRTC.WriteMemory(p_address, p_string);
+}
+
+void ReadStdStringValue(const unsigned char p_address) {
+}