Class library for the Dallas Semiconductor DS1307 RTC Basic Functionality, read and set RTC

Committer:
jebradshaw
Date:
Sat Oct 11 06:04:41 2014 +0000
Revision:
0:2bbb23558624
Child:
1:95a90bac5d6d
DS1307 Real Time Clock Library (I2C Interface)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 0:2bbb23558624 1 // J Bradshaw
jebradshaw 0:2bbb23558624 2 // 20141010 - I2C Real Time Clock
jebradshaw 0:2bbb23558624 3
jebradshaw 0:2bbb23558624 4 #include "DS1307_RTC.h"
jebradshaw 0:2bbb23558624 5
jebradshaw 0:2bbb23558624 6 #define YEAR_OFFSET 2000
jebradshaw 0:2bbb23558624 7
jebradshaw 0:2bbb23558624 8 DS1307_RTC::DS1307_RTC(PinName sda, PinName scl) : _i2c(sda, scl)
jebradshaw 0:2bbb23558624 9 {
jebradshaw 0:2bbb23558624 10 _i2c.frequency(400000);
jebradshaw 0:2bbb23558624 11
jebradshaw 0:2bbb23558624 12 // *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
jebradshaw 0:2bbb23558624 13 // *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
jebradshaw 0:2bbb23558624 14
jebradshaw 0:2bbb23558624 15 Day[0] = "";
jebradshaw 0:2bbb23558624 16 Day[1] = "Sun";
jebradshaw 0:2bbb23558624 17
jebradshaw 0:2bbb23558624 18 rtc_sec = 0;
jebradshaw 0:2bbb23558624 19 rtc_minute = 0;
jebradshaw 0:2bbb23558624 20 rtc_hours = 0;
jebradshaw 0:2bbb23558624 21 rtc_day = 0;
jebradshaw 0:2bbb23558624 22 rtc_date = 0;
jebradshaw 0:2bbb23558624 23 rtc_month = 0;
jebradshaw 0:2bbb23558624 24 rtc_year = 0;
jebradshaw 0:2bbb23558624 25 }
jebradshaw 0:2bbb23558624 26
jebradshaw 0:2bbb23558624 27 //function for writing to RTC registers
jebradshaw 0:2bbb23558624 28 void DS1307_RTC::RTC_write_reg(int reg, int val)
jebradshaw 0:2bbb23558624 29 {
jebradshaw 0:2bbb23558624 30 char cmd[3];
jebradshaw 0:2bbb23558624 31 cmd[0] = reg;
jebradshaw 0:2bbb23558624 32 cmd[1] = val & 0xFF;
jebradshaw 0:2bbb23558624 33
jebradshaw 0:2bbb23558624 34 _i2c.write(DS1307_addr, cmd, 2);
jebradshaw 0:2bbb23558624 35 }
jebradshaw 0:2bbb23558624 36
jebradshaw 0:2bbb23558624 37 int DS1307_RTC::RTC_read_reg(char reg)
jebradshaw 0:2bbb23558624 38 {
jebradshaw 0:2bbb23558624 39 char byte_read[2];
jebradshaw 0:2bbb23558624 40
jebradshaw 0:2bbb23558624 41 if(_i2c.write(DS1307_addr, &reg, 1)) //tell the RTC which reg
jebradshaw 0:2bbb23558624 42 printf("\r\nDS1307 - Error sending address!\0");
jebradshaw 0:2bbb23558624 43
jebradshaw 0:2bbb23558624 44 if(_i2c.read(DS1307_addr, byte_read, 1)) //read one byte
jebradshaw 0:2bbb23558624 45 printf("\r\nDS1307 - Error reading!\0");
jebradshaw 0:2bbb23558624 46 return byte_read[0];
jebradshaw 0:2bbb23558624 47 }
jebradshaw 0:2bbb23558624 48
jebradshaw 0:2bbb23558624 49 int DS1307_RTC::bcd(int dec)
jebradshaw 0:2bbb23558624 50 {
jebradshaw 0:2bbb23558624 51 return ((dec/10)<<4) + (dec%10);
jebradshaw 0:2bbb23558624 52 }
jebradshaw 0:2bbb23558624 53
jebradshaw 0:2bbb23558624 54 int DS1307_RTC::unbcd(int bcd)
jebradshaw 0:2bbb23558624 55 {
jebradshaw 0:2bbb23558624 56 return ((bcd>>4)*10) + bcd%16;
jebradshaw 0:2bbb23558624 57 }
jebradshaw 0:2bbb23558624 58
jebradshaw 0:2bbb23558624 59 void DS1307_RTC::set_RTC_time(void)
jebradshaw 0:2bbb23558624 60 {
jebradshaw 0:2bbb23558624 61 printf("\r\n\r\nSet the Real Time Clock.\r\nEnter Year and press Enter...\r\n");
jebradshaw 0:2bbb23558624 62 scanf("%d",&rtc_year); //Read year
jebradshaw 0:2bbb23558624 63 RTC_write_reg(6, bcd(rtc_year)); //seconds reg
jebradshaw 0:2bbb23558624 64 printf("%d\r\n",rtc_year + YEAR_OFFSET); //print year
jebradshaw 0:2bbb23558624 65
jebradshaw 0:2bbb23558624 66 printf("Enter Month and press Enter...\r\n");
jebradshaw 0:2bbb23558624 67 scanf("%d",&rtc_month); //Read month
jebradshaw 0:2bbb23558624 68 RTC_write_reg(5, bcd(rtc_month)); //month reg
jebradshaw 0:2bbb23558624 69 printf("%d\r\n",rtc_month); //print month
jebradshaw 0:2bbb23558624 70
jebradshaw 0:2bbb23558624 71 printf("Enter Day (Sun=1,Mon=2,Tues=3,etc...) and press Enter...\r\n");
jebradshaw 0:2bbb23558624 72 scanf("%d",&rtc_day); //Read day
jebradshaw 0:2bbb23558624 73 RTC_write_reg(3, bcd(rtc_day)); //day reg
jebradshaw 0:2bbb23558624 74 printf("%d\r\n",rtc_day); //print day
jebradshaw 0:2bbb23558624 75
jebradshaw 0:2bbb23558624 76 printf("Enter Date and press Enter...\r\n");
jebradshaw 0:2bbb23558624 77 scanf("%d",&rtc_date); //Read date
jebradshaw 0:2bbb23558624 78 RTC_write_reg(4, bcd(rtc_date)); //date reg
jebradshaw 0:2bbb23558624 79 printf("%d\r\n",rtc_date); //print date
jebradshaw 0:2bbb23558624 80
jebradshaw 0:2bbb23558624 81 printf("Enter hours and press Enter...\r\n");
jebradshaw 0:2bbb23558624 82 scanf("%d",&rtc_hours); //Read hour
jebradshaw 0:2bbb23558624 83 RTC_write_reg(2, bcd(rtc_hours)); //hour reg
jebradshaw 0:2bbb23558624 84 printf("%d\r\n",rtc_hours); //print hour
jebradshaw 0:2bbb23558624 85
jebradshaw 0:2bbb23558624 86 printf("Enter Minutes and press Enter...\r\n");
jebradshaw 0:2bbb23558624 87 scanf("%d",&rtc_minute); //Read minute
jebradshaw 0:2bbb23558624 88 RTC_write_reg(1, bcd(rtc_minute)); //minute reg
jebradshaw 0:2bbb23558624 89 printf("%d\r\n",rtc_minute); //print minute
jebradshaw 0:2bbb23558624 90
jebradshaw 0:2bbb23558624 91 printf("Enter seconds and press Enter...\r\n");
jebradshaw 0:2bbb23558624 92 scanf("%d",&rtc_sec); //Read sec
jebradshaw 0:2bbb23558624 93 printf("%d\r\n",rtc_sec); //print sec
jebradshaw 0:2bbb23558624 94 RTC_write_reg(0, bcd(rtc_sec)); //seconds reg
jebradshaw 0:2bbb23558624 95 }
jebradshaw 0:2bbb23558624 96
jebradshaw 0:2bbb23558624 97 void DS1307_RTC::get_RTC_time(char *str)
jebradshaw 0:2bbb23558624 98 {
jebradshaw 0:2bbb23558624 99 rtc_sec = unbcd(RTC_read_reg(0)); //
jebradshaw 0:2bbb23558624 100 rtc_minute = unbcd(RTC_read_reg(1));
jebradshaw 0:2bbb23558624 101 rtc_hours = unbcd(RTC_read_reg(2));
jebradshaw 0:2bbb23558624 102 rtc_day = unbcd(RTC_read_reg(3));
jebradshaw 0:2bbb23558624 103 rtc_date = unbcd(RTC_read_reg(4));
jebradshaw 0:2bbb23558624 104 rtc_month = unbcd(RTC_read_reg(5));
jebradshaw 0:2bbb23558624 105 rtc_year = unbcd(RTC_read_reg(6)) + YEAR_OFFSET;
jebradshaw 0:2bbb23558624 106
jebradshaw 0:2bbb23558624 107 sprintf(str, "%2d:%02d:%02d %s %s %d %d", rtc_hours,rtc_minute,rtc_sec,Day[rtc_day],Mon[rtc_month], rtc_date, rtc_year);
jebradshaw 0:2bbb23558624 108 //pc.printf("%s", str);
jebradshaw 0:2bbb23558624 109 }