RTC Sample program

Dependencies:   RTC-DS1307 mbed

Fork of Rtc_Ds1307_Sample by Henry Leinen

Files at this revision

API Documentation at this revision

Comitter:
manojn
Date:
Sun Jun 12 11:17:26 2016 +0000
Parent:
2:3be003301107
Commit message:
RTC DS 1307 sample program with red and write operations

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jun 26 21:07:13 2013 +0000
+++ b/main.cpp	Sun Jun 12 11:17:26 2016 +0000
@@ -1,87 +1,126 @@
 #include "mbed.h"
-#include "Rtc_Ds1307.h"
 
-//RtcCls rtc(p28, p27, p29, true);
-Rtc_Ds1307 rtc(p28, p27);
+const int addr=0xD0;
+unsigned char var1,var2,var3,var4,var5,var6;
+int i;
 
 Serial pc(USBTX, USBRX, "pc");
 
-char buffer[128];
-int readptr = 0;
+I2C i2c(p28, p27); 
 
-int main() {
-    char c;
-    Rtc_Ds1307::Time_rtc tm = {};
+void read_rtc_sec()
+{  
+    i2c.start();   
+    i2c.write(0xD0);
+    i2c.write(0x00);
+          
+    i2c.start();
+    i2c.write(0xD1);
+    var1=i2c.read(0x00);
+  
+    var2=var1;
+    var1=var1&0x0f;
+    var2=var2>>4;
+    i2c.stop();
+      
+    }
+
+void read_rtc_min()
+{  
+    i2c.start();  
+    i2c.write(0xD0);
+    i2c.write(0x01);
+ 
+    i2c.start();
+    i2c.write(0xD1);
+    var3=i2c.read(0x00);    
     
-    while(1) {
-        pc.printf("*************************************\n");
-        pc.printf("* Menu for RTC Test :               *\n");
-        pc.printf("* read  - reads the clock           *\n");
-        pc.printf("* start - start the clock           *\n");
-        pc.printf("* stop  - stop the clock            *\n");
-        pc.printf("* write - write the clock           *\n");
-        pc.printf("* ena   - enable Square wave output *\n");
-        pc.printf("* dis   - disable square wave outp. *\n");
-        pc.printf("*************************************\n");
+    var4=var3;
+    var3=var3&0x0f;
+    var4=var4>>4;
+
+    i2c.stop();
+    }
+
+
+void read_rtc_hrs()
+{  
+    i2c.start();  
+    i2c.write(0xD0);
+    i2c.write(0x02);
         
-        while( (c = pc.getc()) != '\n') {
-            buffer[readptr++] = c;
-        }
-        buffer[readptr++] = 0;
-        if (strncmp(buffer, "read", 4) == 0) {
-            //  perform read
-            pc.printf("Performing read operation\n");
-            if (rtc.getTime(tm) ) {
-                pc.printf("The current time is : %02d:%02d:%02d\n", tm.hour, tm.min, tm.sec);
-                pc.printf("The current date is : %s, %02d/%02d/%04d\n", rtc.weekdayToString(tm.wday), tm.mon, tm.date, tm.year);
-            }
-            
-        }
-        else if (strncmp(buffer, "write", 5) == 0) {
-            //  perform write
-            pc.printf("Enter the date (date 0..31)");
-            pc.scanf("%d", &tm.date);
-            pc.printf("Enter the date (month 1..12)");
-            pc.scanf("%d", &tm.mon);
-            pc.printf("Enter the date (year)");
-            pc.scanf("%d", &tm.year);
-            pc.printf("Enter the time (hours 0..23)");
-            pc.scanf("%d", &tm.hour);
-            pc.printf("Enter the time (minutes 0..59)");
-            pc.scanf("%d", &tm.min);
-            pc.printf("Enter the time (seconds 0..59)");
-            pc.scanf("%d", &tm.sec);
-            pc.printf("Performing write operation\n");
-            
-            while(pc.readable()) 
-                pc.getc();
-            rtc.setTime(tm, false, false);
-        }
-        else if (strncmp(buffer, "start", 5) == 0) {
-            //  start
-            pc.printf("Performing start operation\n");
-            rtc.startClock();
-        }
-        else if (strncmp(buffer, "stop", 4) == 0) {
-            //  stop
-            pc.printf("Performing stop operation\n");
-            rtc.stopClock();
-        }
-        else if (strncmp(buffer, "ena", 3) == 0) {
-            int rs;
-            pc.printf("Please specify the frequency : [0 = 1Hz, 1 = 4.096kHz, 2 = 8.192kHz, 3 = 32.768kHz] ");
-            scanf("%d", &rs);
-            pc.printf("Enabling the output with %d option\n", rs);
-            rtc.setSquareWaveOutput(true, (Rtc_Ds1307::SqwRateSelect_t)rs);
-        }
-        else if (strncmp(buffer, "dis", 3) == 0) {
-            pc.printf("Disableing square wave output\n");
-            rtc.setSquareWaveOutput(false, Rtc_Ds1307::RS1Hz);
-        }
-        else {
-            pc.printf("syntax error\n");
-        }
-        readptr = 0;
-        pc.printf("\n\n\n");
+    i2c.start();
+    i2c.write(0xD1);
+    var5=i2c.read(0x00);    
+
+    var6=var5;
+    var5=var5&0x0f;
+    var6=var6>>4;
+    var6=var6&0x01;
+ 
+    i2c.stop();
     }
+    
+void write_rtc_min(unsigned char mn)
+{
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x01);
+    i2c.write(mn); //write min
+    i2c.stop();
+   
 }
+
+ void write_rtc_sec(unsigned char sc)
+ {
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x00);
+    i2c.write(sc); // write sec 
+    i2c.stop();
+  }
+  
+
+void write_rtc_hrs(unsigned char hr)  
+{    
+    i2c.start();
+    i2c.write(addr);
+    i2c.write(0x02);
+    i2c.write(hr); //write hrs
+    i2c.stop();
+         
+    }
+
+
+int main() 
+{
+
+char sec=0x45; // second field value
+char min=0x59; // min field value
+char hrs=0x52; // hrs field value 
+    
+    
+    i2c.frequency(100000);
+    i2c.start();
+    wait(1);
+    
+    i2c.start();
+    write_rtc_min(min);
+    write_rtc_sec(sec);
+    write_rtc_hrs(hrs);
+
+ 
+    
+    while(1)
+    {
+    read_rtc_hrs();
+    read_rtc_min();
+    read_rtc_sec();
+ 
+    pc.printf("Current time = (%d%d:%d%d:%d%d)\n\r",var6,var5,var4,var3,var2,var1);
+    
+    wait(1);
+     
+    }
+    
+     }
\ No newline at end of file