This code reads time value from external RTC using I2C protocol

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
kuldipmaharjan
Date:
Wed Jan 08 00:31:09 2014 +0000
Commit message:
This code reads time value from external RTC using I2C protocol

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jan 08 00:31:09 2014 +0000
@@ -0,0 +1,57 @@
+//Author: Kuldip Maharjan
+//Email : kuldipmaharjan@gmail.com
+//Anyone can use this code if it helps in their projects or 
+//for learning programing in mbed besides for commercial purposes
+
+//This code reads time value from external RTC using I2C protocol
+
+#include "mbed.h"
+I2C    i2c( p28, p27 );        // sda, scl
+Serial pc(USBTX, USBRX); // tx, rx
+
+int main() {
+    char v[10];
+    char cmd[1];
+    char data[10];
+    char data2[2];
+    pc.baud(115200);
+    i2c.frequency(100000);
+    
+    cmd[0] = 0x00;
+   // cmd[1] = 0x05;
+   //set the min data[0] =0, data[1] = 1
+   //set the hr data[0] =1, data[1] = 1
+   //
+   /*
+        data[0] = 0x0;  // MSB address
+        data[1] = 0x00; // sec
+        data[2] = 0x19; // min
+        data[3] = 0x14; // hr
+        data[4] = 0x06; // day
+        data[5] = 0x07; // date
+        data[6] = 0x12; // month
+        data[7] = 0x12; // year
+           
+        if(i2c.write(0xD0, data, 8)) {
+            error("Write failed\n");
+        }
+        while(i2c.write(0xD0, NULL, 0)); // wait to complete
+   
+  */
+   
+    while(1) {
+   
+        i2c.write( 0xD0, cmd , 1 );
+        
+        i2c.read( 0xD0, v , 7 );
+       
+        wait(1);
+        pc.printf("\n\r %d%d sec  %d%d min",v[0]>>4, v[0]&0x0F,v[1]>>4, v[1]&0x0F);
+        pc.printf("\n\r %d%d hr  %d day",(v[2]>>4), v[2]&0x0F, v[3]&0x07);
+        pc.printf("\n\r %d%d date  %d%d month  %d%d year",v[4]>>4, v[4]&0x0F,v[5]>>4, v[5]&0x0F,v[6]>>4,v[6]&0x0F);
+        
+        //pc.printf("\r %d/%d/%d %d day %d hrs %d min %d sec\n", v[5]+6,v[4]+2,v[6]+2012,v[3],v[2],v[1],v[0]);
+        //pc.printf("v[1] = %d \t ", v[1]);
+       
+     }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Jan 08 00:31:09 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b60934f96c0c
\ No newline at end of file