This code reads time value from external RTC using I2C protocol

Dependencies:   mbed

main.cpp

Committer:
kuldipmaharjan
Date:
2014-01-08
Revision:
0:c9c5aaba2891

File content as of revision 0:c9c5aaba2891:

//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]);
       
     }
}