pp

Dependencies:   mbed

Committer:
Peeraya
Date:
Wed Dec 09 02:09:36 2015 +0000
Revision:
5:60262753a05f
Parent:
1:ba7154d2d7ca
Super Cute Alarm Clock

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Peeraya 0:b2064b3558b6 1 #include "mbed.h"
Peeraya 0:b2064b3558b6 2 #include "DS1307.h"
Peeraya 0:b2064b3558b6 3
NamSom 1:ba7154d2d7ca 4
Peeraya 0:b2064b3558b6 5 DS1307::DS1307(PinName sda,PinName scl):rtc(sda,scl){}
Peeraya 0:b2064b3558b6 6 void DS1307::settime(int sec,int min,int hour,int day,int date,int month,int year)
Peeraya 0:b2064b3558b6 7 {
Peeraya 0:b2064b3558b6 8 char data[7][2] = { {0x00, dectobcd(sec)},
Peeraya 0:b2064b3558b6 9 {0x01, dectobcd(min)},
Peeraya 0:b2064b3558b6 10 {0x02, dectobcd(hour)},
Peeraya 0:b2064b3558b6 11 {0x03, dectobcd(day)},
Peeraya 0:b2064b3558b6 12 {0x04, dectobcd(date)},
Peeraya 0:b2064b3558b6 13 {0x05, dectobcd(month)},
Peeraya 0:b2064b3558b6 14 {0x06, dectobcd(year)} };
Peeraya 0:b2064b3558b6 15 int i;
Peeraya 0:b2064b3558b6 16 for(i=0;i<7;i++)
Peeraya 0:b2064b3558b6 17 {
Peeraya 0:b2064b3558b6 18 rtc.write(DS1307_ADDRESS, data[i], 2);
Peeraya 0:b2064b3558b6 19 }
Peeraya 0:b2064b3558b6 20 }
Peeraya 0:b2064b3558b6 21 void DS1307::gettime(int *sec,int *min,int *hour,int *day,int *date,int *month,int *year)
Peeraya 0:b2064b3558b6 22 {
Peeraya 0:b2064b3558b6 23 char j = 0;
Peeraya 0:b2064b3558b6 24 char data[7];
Peeraya 0:b2064b3558b6 25 int i;
Peeraya 0:b2064b3558b6 26 for(i=0;i<7;i++)
Peeraya 0:b2064b3558b6 27 {
Peeraya 0:b2064b3558b6 28 rtc.write(DS1307_ADDRESS, &j, 1);
Peeraya 0:b2064b3558b6 29 rtc.read(DS1307_ADDRESS, &data[i], 1);
Peeraya 0:b2064b3558b6 30 j++;
Peeraya 0:b2064b3558b6 31 }
Peeraya 0:b2064b3558b6 32 *sec=bcdtodec(data[0]);
Peeraya 0:b2064b3558b6 33 *min=bcdtodec(data[1]);
Peeraya 0:b2064b3558b6 34 *hour=bcdtodec(data[2]);
Peeraya 0:b2064b3558b6 35 *day=bcdtodec(data[3]);
Peeraya 0:b2064b3558b6 36 *date=bcdtodec(data[4]);
Peeraya 0:b2064b3558b6 37 *month=bcdtodec(data[5]);
Peeraya 0:b2064b3558b6 38 *year=bcdtodec(data[6]);
Peeraya 0:b2064b3558b6 39 }
Peeraya 0:b2064b3558b6 40 int DS1307::bcdtodec(int num)
Peeraya 0:b2064b3558b6 41 {
Peeraya 0:b2064b3558b6 42 return 10*(num/16) + num % 16;
Peeraya 0:b2064b3558b6 43 }
Peeraya 0:b2064b3558b6 44 int DS1307::dectobcd(int num)
Peeraya 0:b2064b3558b6 45 {
Peeraya 0:b2064b3558b6 46 return 16*(num/10) + num % 10;
Peeraya 0:b2064b3558b6 47 }