Skip to content
Permalink
4435a9949d
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
84 lines (62 sloc) 1.73 KB
#include <mbed.h>
#include "Rtc_Ds1307.h"
I2C i2c(I2C_SDA , I2C_SCL);
int CLOCK_READ = 0xD1;
int CLOCK_WRITE = 0xD0;
int bcdtodec( int bcd) {
int low = 0;
int high = 0;
high = bcd / 16;
low = bcd - (high * 16);
return ((high * 10) + low);
}
void getTime(){
int status;
//Send Satrt Signal
i2c.start();
//Write Seconds
status = i2c.write(CLOCK_WRITE);
printf("Status was %d\n", status);
i2c.write(0x00) ; //Seconds Register
//Send Read Signal
i2c.stop();
i2c.start();
status = i2c.write(CLOCK_READ); /* slave address with read mode */
printf("Read Status was %d\n", status);
int seconds = i2c.read(1); // Read the slave with ACK
int minutes= i2c.read(1);
int hour = i2c.read(1);
int day = i2c.read(1);
int date = i2c.read(1);
int month = i2c.read(1);
int year = i2c.read(0); // Read the slave with not-ACK
//Stop
i2c.stop();
printf("Seconds is %d\n", seconds);
printf("Minutes is %d\n", minutes);
printf("Hours %d\n", hour);
printf("D %d\n", day);
printf("M %d\n", month);
printf("Y %d\n", year);
int newSeconds = seconds & 0x7f;
printf("New Seconds %d\n", newSeconds);
int printSeconds = bcdtodec(newSeconds);
printf("BCD Seconds %d", printSeconds);
printf("BCD Year %d\n", bcdtodec(year));
}
int main2() {
DigitalOut led(LED1);
// put your setup code here, to run once:
time_t localTime;
getTime();
while(1) {
// put your main code here, to run repeatedly:
//Get the Local Time
localTime = time(NULL);
printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)localTime);
printf("Time as a basic string = %s\n", ctime(&localTime));
getTime();
led = !led;
thread_sleep_for(10000);
}
}