try this

Fork of DS1307_RTC by Joseph Bradshaw

Committer:
jebradshaw
Date:
Sat Nov 01 14:05:25 2014 +0000
Revision:
1:95a90bac5d6d
Parent:
0:2bbb23558624
Child:
2:a9a0784930bd
Added month and day names

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 1:95a90bac5d6d 17 Day[2] = "Mon";
jebradshaw 1:95a90bac5d6d 18 Day[3] = "Tue";
jebradshaw 1:95a90bac5d6d 19 Day[4] = "Wed";
jebradshaw 1:95a90bac5d6d 20 Day[5] = "Thu";
jebradshaw 1:95a90bac5d6d 21 Day[6] = "Fri";
jebradshaw 1:95a90bac5d6d 22 Day[7] = "Sat";
jebradshaw 1:95a90bac5d6d 23
jebradshaw 1:95a90bac5d6d 24 Mon[0] = "";
jebradshaw 1:95a90bac5d6d 25 Mon[1] = "Jan";
jebradshaw 1:95a90bac5d6d 26 Mon[2] = "Feb";
jebradshaw 1:95a90bac5d6d 27 Mon[3] = "Mar";
jebradshaw 1:95a90bac5d6d 28 Mon[4] = "Apr";
jebradshaw 1:95a90bac5d6d 29 Mon[5] = "May";
jebradshaw 1:95a90bac5d6d 30 Mon[6] = "Jun";
jebradshaw 1:95a90bac5d6d 31 Mon[7] = "Jul";
jebradshaw 1:95a90bac5d6d 32 Mon[8] = "Aug";
jebradshaw 1:95a90bac5d6d 33 Mon[9] = "Sep";
jebradshaw 1:95a90bac5d6d 34 Mon[10] = "Oct";
jebradshaw 1:95a90bac5d6d 35 Mon[11] = "Nov";
jebradshaw 1:95a90bac5d6d 36 Mon[12] = "Dec";
jebradshaw 0:2bbb23558624 37
jebradshaw 0:2bbb23558624 38 rtc_sec = 0;
jebradshaw 0:2bbb23558624 39 rtc_minute = 0;
jebradshaw 0:2bbb23558624 40 rtc_hours = 0;
jebradshaw 0:2bbb23558624 41 rtc_day = 0;
jebradshaw 0:2bbb23558624 42 rtc_date = 0;
jebradshaw 0:2bbb23558624 43 rtc_month = 0;
jebradshaw 0:2bbb23558624 44 rtc_year = 0;
jebradshaw 0:2bbb23558624 45 }
jebradshaw 0:2bbb23558624 46
jebradshaw 0:2bbb23558624 47 //function for writing to RTC registers
jebradshaw 0:2bbb23558624 48 void DS1307_RTC::RTC_write_reg(int reg, int val)
jebradshaw 0:2bbb23558624 49 {
jebradshaw 0:2bbb23558624 50 char cmd[3];
jebradshaw 0:2bbb23558624 51 cmd[0] = reg;
jebradshaw 0:2bbb23558624 52 cmd[1] = val & 0xFF;
jebradshaw 0:2bbb23558624 53
jebradshaw 0:2bbb23558624 54 _i2c.write(DS1307_addr, cmd, 2);
jebradshaw 0:2bbb23558624 55 }
jebradshaw 0:2bbb23558624 56
jebradshaw 0:2bbb23558624 57 int DS1307_RTC::RTC_read_reg(char reg)
jebradshaw 0:2bbb23558624 58 {
jebradshaw 0:2bbb23558624 59 char byte_read[2];
jebradshaw 0:2bbb23558624 60
jebradshaw 0:2bbb23558624 61 if(_i2c.write(DS1307_addr, &reg, 1)) //tell the RTC which reg
jebradshaw 0:2bbb23558624 62 printf("\r\nDS1307 - Error sending address!\0");
jebradshaw 0:2bbb23558624 63
jebradshaw 0:2bbb23558624 64 if(_i2c.read(DS1307_addr, byte_read, 1)) //read one byte
jebradshaw 0:2bbb23558624 65 printf("\r\nDS1307 - Error reading!\0");
jebradshaw 0:2bbb23558624 66 return byte_read[0];
jebradshaw 0:2bbb23558624 67 }
jebradshaw 0:2bbb23558624 68
jebradshaw 0:2bbb23558624 69 int DS1307_RTC::bcd(int dec)
jebradshaw 0:2bbb23558624 70 {
jebradshaw 0:2bbb23558624 71 return ((dec/10)<<4) + (dec%10);
jebradshaw 0:2bbb23558624 72 }
jebradshaw 0:2bbb23558624 73
jebradshaw 0:2bbb23558624 74 int DS1307_RTC::unbcd(int bcd)
jebradshaw 0:2bbb23558624 75 {
jebradshaw 0:2bbb23558624 76 return ((bcd>>4)*10) + bcd%16;
jebradshaw 0:2bbb23558624 77 }
jebradshaw 0:2bbb23558624 78
jebradshaw 0:2bbb23558624 79 void DS1307_RTC::set_RTC_time(void)
jebradshaw 0:2bbb23558624 80 {
jebradshaw 0:2bbb23558624 81 printf("\r\n\r\nSet the Real Time Clock.\r\nEnter Year and press Enter...\r\n");
jebradshaw 0:2bbb23558624 82 scanf("%d",&rtc_year); //Read year
jebradshaw 0:2bbb23558624 83 RTC_write_reg(6, bcd(rtc_year)); //seconds reg
jebradshaw 0:2bbb23558624 84 printf("%d\r\n",rtc_year + YEAR_OFFSET); //print year
jebradshaw 0:2bbb23558624 85
jebradshaw 0:2bbb23558624 86 printf("Enter Month and press Enter...\r\n");
jebradshaw 0:2bbb23558624 87 scanf("%d",&rtc_month); //Read month
jebradshaw 0:2bbb23558624 88 RTC_write_reg(5, bcd(rtc_month)); //month reg
jebradshaw 0:2bbb23558624 89 printf("%d\r\n",rtc_month); //print month
jebradshaw 0:2bbb23558624 90
jebradshaw 0:2bbb23558624 91 printf("Enter Day (Sun=1,Mon=2,Tues=3,etc...) and press Enter...\r\n");
jebradshaw 0:2bbb23558624 92 scanf("%d",&rtc_day); //Read day
jebradshaw 0:2bbb23558624 93 RTC_write_reg(3, bcd(rtc_day)); //day reg
jebradshaw 0:2bbb23558624 94 printf("%d\r\n",rtc_day); //print day
jebradshaw 0:2bbb23558624 95
jebradshaw 0:2bbb23558624 96 printf("Enter Date and press Enter...\r\n");
jebradshaw 0:2bbb23558624 97 scanf("%d",&rtc_date); //Read date
jebradshaw 0:2bbb23558624 98 RTC_write_reg(4, bcd(rtc_date)); //date reg
jebradshaw 0:2bbb23558624 99 printf("%d\r\n",rtc_date); //print date
jebradshaw 0:2bbb23558624 100
jebradshaw 0:2bbb23558624 101 printf("Enter hours and press Enter...\r\n");
jebradshaw 0:2bbb23558624 102 scanf("%d",&rtc_hours); //Read hour
jebradshaw 0:2bbb23558624 103 RTC_write_reg(2, bcd(rtc_hours)); //hour reg
jebradshaw 0:2bbb23558624 104 printf("%d\r\n",rtc_hours); //print hour
jebradshaw 0:2bbb23558624 105
jebradshaw 0:2bbb23558624 106 printf("Enter Minutes and press Enter...\r\n");
jebradshaw 0:2bbb23558624 107 scanf("%d",&rtc_minute); //Read minute
jebradshaw 0:2bbb23558624 108 RTC_write_reg(1, bcd(rtc_minute)); //minute reg
jebradshaw 0:2bbb23558624 109 printf("%d\r\n",rtc_minute); //print minute
jebradshaw 0:2bbb23558624 110
jebradshaw 0:2bbb23558624 111 printf("Enter seconds and press Enter...\r\n");
jebradshaw 0:2bbb23558624 112 scanf("%d",&rtc_sec); //Read sec
jebradshaw 0:2bbb23558624 113 printf("%d\r\n",rtc_sec); //print sec
jebradshaw 0:2bbb23558624 114 RTC_write_reg(0, bcd(rtc_sec)); //seconds reg
jebradshaw 0:2bbb23558624 115 }
jebradshaw 0:2bbb23558624 116
jebradshaw 1:95a90bac5d6d 117 void DS1307_RTC::set_RTC_time_UTC(float utc_time, int timezone){
jebradshaw 1:95a90bac5d6d 118 int Hour = (int)((int)utc_time/10000) + timezone;
jebradshaw 1:95a90bac5d6d 119 if(Hour < 0)
jebradshaw 1:95a90bac5d6d 120 Hour += 24;
jebradshaw 1:95a90bac5d6d 121
jebradshaw 1:95a90bac5d6d 122 int Min = (int)((int)utc_time/100%100);
jebradshaw 1:95a90bac5d6d 123 int Sec = (int)utc_time % 100;
jebradshaw 1:95a90bac5d6d 124
jebradshaw 1:95a90bac5d6d 125 /* RTC_write_reg(6, bcd(rtc_year)); //seconds reg
jebradshaw 1:95a90bac5d6d 126 RTC_write_reg(5, bcd(rtc_month)); //month reg
jebradshaw 1:95a90bac5d6d 127 RTC_write_reg(3, bcd(rtc_day)); //day reg
jebradshaw 1:95a90bac5d6d 128 RTC_write_reg(4, bcd(rtc_date)); //date reg*/
jebradshaw 1:95a90bac5d6d 129
jebradshaw 1:95a90bac5d6d 130 RTC_write_reg(2, bcd(Hour)); //hour reg
jebradshaw 1:95a90bac5d6d 131 RTC_write_reg(1, bcd(Min)); //minute reg
jebradshaw 1:95a90bac5d6d 132 RTC_write_reg(0, bcd(Sec)); //seconds reg
jebradshaw 1:95a90bac5d6d 133 }
jebradshaw 1:95a90bac5d6d 134
jebradshaw 0:2bbb23558624 135 void DS1307_RTC::get_RTC_time(char *str)
jebradshaw 0:2bbb23558624 136 {
jebradshaw 0:2bbb23558624 137 rtc_sec = unbcd(RTC_read_reg(0)); //
jebradshaw 0:2bbb23558624 138 rtc_minute = unbcd(RTC_read_reg(1));
jebradshaw 0:2bbb23558624 139 rtc_hours = unbcd(RTC_read_reg(2));
jebradshaw 0:2bbb23558624 140 rtc_day = unbcd(RTC_read_reg(3));
jebradshaw 0:2bbb23558624 141 rtc_date = unbcd(RTC_read_reg(4));
jebradshaw 0:2bbb23558624 142 rtc_month = unbcd(RTC_read_reg(5));
jebradshaw 0:2bbb23558624 143 rtc_year = unbcd(RTC_read_reg(6)) + YEAR_OFFSET;
jebradshaw 0:2bbb23558624 144
jebradshaw 0:2bbb23558624 145 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 146 //pc.printf("%s", str);
jebradshaw 0:2bbb23558624 147 }