Ejemplo RTC DS3231

Dependencies:   mbed ds3231

Committer:
j3
Date:
Fri Feb 06 05:14:26 2015 +0000
Revision:
2:76cd47a603b5
Parent:
1:7db4a1cc7abb
Child:
3:826946b4eead
Changed date time members to uint32_t to prevent roll over (unless user enters something bigger than 4x10^9++); ; fixed 24 hr mode reporting, & w/0x3F instead of 0x1F (12hr mode)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:6be499dd402a 1 //Test Ds3231 class
j3 0:6be499dd402a 2
j3 2:76cd47a603b5 3
j3 0:6be499dd402a 4 #include "ds3231.h"
j3 0:6be499dd402a 5
j3 2:76cd47a603b5 6 #define ESC 0x1B
j3 1:7db4a1cc7abb 7
j3 2:76cd47a603b5 8 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
j3 2:76cd47a603b5 9 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
j3 0:6be499dd402a 10
j3 0:6be499dd402a 11 int main(void)
j3 0:6be499dd402a 12 {
j3 1:7db4a1cc7abb 13 //rtc object
j3 0:6be499dd402a 14 Ds3231 rtc(D14, D15);
j3 0:6be499dd402a 15
j3 2:76cd47a603b5 16 time_t epoch_time;
j3 2:76cd47a603b5 17
j3 1:7db4a1cc7abb 18 //DS3231 rtc variables
j3 2:76cd47a603b5 19
j3 2:76cd47a603b5 20 //default, use bit masks in ds3231.h for desired operation
j3 2:76cd47a603b5 21 ds3231_cntl_stat_t rtc_control_status = {0,0};
j3 2:76cd47a603b5 22 ds3231_time_t rtc_time;
j3 2:76cd47a603b5 23 ds3231_calendar_t rtc_calendar;
j3 1:7db4a1cc7abb 24
j3 1:7db4a1cc7abb 25 rtc.set_cntl_stat_reg(rtc_control_status);
j3 1:7db4a1cc7abb 26
j3 2:76cd47a603b5 27 //get day from user
j3 2:76cd47a603b5 28 get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
j3 2:76cd47a603b5 29 7, &rtc_calendar.day);
j3 2:76cd47a603b5 30
j3 2:76cd47a603b5 31 //get day of month from user
j3 2:76cd47a603b5 32 get_user_input("\nPlease enter day of month (1-31): ", 1, 31,
j3 2:76cd47a603b5 33 &rtc_calendar.date);
j3 2:76cd47a603b5 34
j3 2:76cd47a603b5 35 //get month from user
j3 2:76cd47a603b5 36 get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1,
j3 2:76cd47a603b5 37 12, &rtc_calendar.month);
j3 2:76cd47a603b5 38
j3 2:76cd47a603b5 39 //get year from user
j3 2:76cd47a603b5 40 get_user_input("\nPlease enter the year (0-99): ",0, 99,
j3 2:76cd47a603b5 41 &rtc_calendar.year);
j3 2:76cd47a603b5 42
j3 2:76cd47a603b5 43 //Get time mode
j3 2:76cd47a603b5 44 get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1,
j3 2:76cd47a603b5 45 &rtc_time.mode);
j3 1:7db4a1cc7abb 46
j3 2:76cd47a603b5 47 if(rtc_time.mode)
j3 2:76cd47a603b5 48 {
j3 2:76cd47a603b5 49 //Get AM/PM status
j3 2:76cd47a603b5 50 get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1,
j3 2:76cd47a603b5 51 &rtc_time.am_pm);
j3 2:76cd47a603b5 52 //Get hour from user
j3 2:76cd47a603b5 53 get_user_input("\nPlease enter the hour (1-12): ", 1, 12,
j3 2:76cd47a603b5 54 &rtc_time.hours);
j3 2:76cd47a603b5 55 }
j3 2:76cd47a603b5 56 else
j3 2:76cd47a603b5 57 {
j3 2:76cd47a603b5 58 //Get hour from user
j3 2:76cd47a603b5 59 get_user_input("\nPlease enter the hour (0-23): ", 0, 23,
j3 2:76cd47a603b5 60 &rtc_time.hours);
j3 2:76cd47a603b5 61 }
j3 2:76cd47a603b5 62
j3 1:7db4a1cc7abb 63 //Get minutes from user
j3 1:7db4a1cc7abb 64 get_user_input("\nPlease enter the minute (0-59): ", 0, 59,
j3 1:7db4a1cc7abb 65 &rtc_time.minutes);
j3 1:7db4a1cc7abb 66
j3 1:7db4a1cc7abb 67
j3 1:7db4a1cc7abb 68 //Get seconds from user
j3 1:7db4a1cc7abb 69 get_user_input("\nPlease enter the second (0-59): ", 0, 59,
j3 1:7db4a1cc7abb 70 &rtc_time.seconds);
j3 1:7db4a1cc7abb 71
j3 2:76cd47a603b5 72
j3 2:76cd47a603b5 73
j3 2:76cd47a603b5 74 //Set the time, uses inverted logic for return value
j3 2:76cd47a603b5 75 if(rtc.set_time(rtc_time))
j3 2:76cd47a603b5 76 {
j3 2:76cd47a603b5 77 printf("\nrtc.set_time failed!!\n");
j3 2:76cd47a603b5 78 exit(0);
j3 2:76cd47a603b5 79 }
j3 2:76cd47a603b5 80
j3 2:76cd47a603b5 81 //Set the calendar, uses inverted logic for return value
j3 2:76cd47a603b5 82 if(rtc.set_calendar(rtc_calendar))
j3 2:76cd47a603b5 83 {
j3 2:76cd47a603b5 84 printf("\nrtc.set_calendar failed!!\n");
j3 2:76cd47a603b5 85 exit(0);
j3 2:76cd47a603b5 86 }
j3 2:76cd47a603b5 87
j3 2:76cd47a603b5 88 char buffer[32];
j3 1:7db4a1cc7abb 89
j3 2:76cd47a603b5 90 for(;;)
j3 2:76cd47a603b5 91 {
j3 2:76cd47a603b5 92 printf("%c[2J", ESC); //clear screen
j3 2:76cd47a603b5 93 printf("%c[H", ESC); //move cursor to Home
j3 2:76cd47a603b5 94
j3 2:76cd47a603b5 95 //new epoch time fx
j3 2:76cd47a603b5 96 epoch_time = rtc.get_epoch();
j3 2:76cd47a603b5 97
j3 2:76cd47a603b5 98 printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
j3 2:76cd47a603b5 99
j3 2:76cd47a603b5 100 printf("\nTime as a basic string = %s", ctime(&epoch_time));
j3 2:76cd47a603b5 101
j3 2:76cd47a603b5 102 strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
j3 2:76cd47a603b5 103 printf("\nTime as a custom formatted string = %s", buffer);
j3 2:76cd47a603b5 104
j3 2:76cd47a603b5 105 wait(1.0);
j3 2:76cd47a603b5 106 }//loop
j3 1:7db4a1cc7abb 107 }
j3 1:7db4a1cc7abb 108
j3 1:7db4a1cc7abb 109
j3 1:7db4a1cc7abb 110 /**********************************************************************
j3 1:7db4a1cc7abb 111 * Function: get_user_input()
j3 1:7db4a1cc7abb 112 * Parameters: message - user prompt
j3 1:7db4a1cc7abb 113 * min - minimum value of input
j3 1:7db4a1cc7abb 114 * max - maximum value of input
j3 1:7db4a1cc7abb 115 * member - pointer to struct member
j3 1:7db4a1cc7abb 116 * Returns: none
j3 1:7db4a1cc7abb 117 *
j3 1:7db4a1cc7abb 118 * Description: get time/date input from user
j3 1:7db4a1cc7abb 119 *
j3 1:7db4a1cc7abb 120 **********************************************************************/
j3 2:76cd47a603b5 121 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
j3 1:7db4a1cc7abb 122 {
j3 2:76cd47a603b5 123 uint32_t temp;
j3 1:7db4a1cc7abb 124
j3 1:7db4a1cc7abb 125 do
j3 0:6be499dd402a 126 {
j3 2:76cd47a603b5 127 printf("\n%s", message);
j3 2:76cd47a603b5 128
j3 2:76cd47a603b5 129 //for some reason mbed doesn't like a pointer to a member in scanf
j3 2:76cd47a603b5 130 //term.scanf("%d", member); works with gcc on RPi
j3 2:76cd47a603b5 131 scanf("%d", &temp);
j3 2:76cd47a603b5 132
j3 2:76cd47a603b5 133 *member = temp;
j3 1:7db4a1cc7abb 134
j3 1:7db4a1cc7abb 135 if((*(member)< min) || (*(member) > max))
j3 1:7db4a1cc7abb 136 {
j3 2:76cd47a603b5 137 printf("\nERROR-RTI");
j3 1:7db4a1cc7abb 138 }
j3 0:6be499dd402a 139 }
j3 1:7db4a1cc7abb 140 while((*(member) < min) || (*(member) > max));
j3 2:76cd47a603b5 141 }
j3 1:7db4a1cc7abb 142
j3 2:76cd47a603b5 143
j3 2:76cd47a603b5 144 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
j3 2:76cd47a603b5 145 {
j3 2:76cd47a603b5 146 bool temp;
j3 2:76cd47a603b5 147
j3 2:76cd47a603b5 148 do
j3 2:76cd47a603b5 149 {
j3 2:76cd47a603b5 150 printf("\n%s", message);
j3 2:76cd47a603b5 151
j3 2:76cd47a603b5 152 //for some reason mbed doesn't like a pointer to a member in scanf
j3 2:76cd47a603b5 153 //term.scanf("%d", member); works with gcc on RPi
j3 2:76cd47a603b5 154 scanf("%d", &temp);
j3 2:76cd47a603b5 155
j3 2:76cd47a603b5 156 *member = temp;
j3 2:76cd47a603b5 157
j3 2:76cd47a603b5 158 if((*(member)< min) || (*(member) > max))
j3 2:76cd47a603b5 159 {
j3 2:76cd47a603b5 160 printf("\nERROR-RTI");
j3 2:76cd47a603b5 161 }
j3 2:76cd47a603b5 162 }
j3 2:76cd47a603b5 163 while((*(member) < min) || (*(member) > max));
j3 0:6be499dd402a 164 }
j3 1:7db4a1cc7abb 165
j3 0:6be499dd402a 166