Ejemplo RTC DS3231

Dependencies:   mbed ds3231

Committer:
j3
Date:
Thu Mar 19 21:59:08 2015 +0000
Revision:
6:2641d53a460a
Parent:
5:ab9d82256e26
Child:
7:e049867a7716
updated lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 3:826946b4eead 1 /**********************************************************************
j3 3:826946b4eead 2 *
j3 3:826946b4eead 3 * Demo DS3231 Library
j3 3:826946b4eead 4 *
j3 3:826946b4eead 5 ***********************************************************************
j3 4:e5d7d2122d4d 6 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
j3 3:826946b4eead 7 *
j3 3:826946b4eead 8 * Permission is hereby granted, free of charge, to any person obtaining a
j3 3:826946b4eead 9 * copy of this software and associated documentation files (the "Software"),
j3 3:826946b4eead 10 * to deal in the Software without restriction, including without limitation
j3 3:826946b4eead 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 3:826946b4eead 12 * and/or sell copies of the Software, and to permit persons to whom the
j3 3:826946b4eead 13 * Software is furnished to do so, subject to the following conditions:
j3 3:826946b4eead 14 *
j3 3:826946b4eead 15 * The above copyright notice and this permission notice shall be included
j3 3:826946b4eead 16 * in all copies or substantial portions of the Software.
j3 3:826946b4eead 17 *
j3 3:826946b4eead 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 3:826946b4eead 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 3:826946b4eead 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 3:826946b4eead 21 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 3:826946b4eead 22 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 3:826946b4eead 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 3:826946b4eead 24 * OTHER DEALINGS IN THE SOFTWARE.
j3 3:826946b4eead 25 *
j3 3:826946b4eead 26 * Except as contained in this notice, the name of Maxim Integrated
j3 3:826946b4eead 27 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 3:826946b4eead 28 * Products, Inc. Branding Policy.
j3 3:826946b4eead 29 *
j3 3:826946b4eead 30 * The mere transfer of this software does not imply any licenses
j3 3:826946b4eead 31 * of trade secrets, proprietary technology, copyrights, patents,
j3 3:826946b4eead 32 * trademarks, maskwork rights, or any other form of intellectual
j3 3:826946b4eead 33 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 3:826946b4eead 34 * ownership rights.
j3 3:826946b4eead 35 **********************************************************************/
j3 2:76cd47a603b5 36
j3 0:6be499dd402a 37 #include "ds3231.h"
j3 0:6be499dd402a 38
j3 2:76cd47a603b5 39 #define ESC 0x1B
j3 1:7db4a1cc7abb 40
j3 2:76cd47a603b5 41 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member);
j3 2:76cd47a603b5 42 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member);
j3 0:6be499dd402a 43
j3 0:6be499dd402a 44 int main(void)
j3 0:6be499dd402a 45 {
j3 1:7db4a1cc7abb 46 //rtc object
j3 0:6be499dd402a 47 Ds3231 rtc(D14, D15);
j3 0:6be499dd402a 48
j3 2:76cd47a603b5 49 time_t epoch_time;
j3 2:76cd47a603b5 50
j3 1:7db4a1cc7abb 51 //DS3231 rtc variables
j3 2:76cd47a603b5 52
j3 2:76cd47a603b5 53 //default, use bit masks in ds3231.h for desired operation
j3 6:2641d53a460a 54 ds3231_cntl_stat_t rtc_control_status = {0,0};
j3 6:2641d53a460a 55 ds3231_time_t rtc_time;
j3 6:2641d53a460a 56 ds3231_calendar_t rtc_calendar;
j3 1:7db4a1cc7abb 57
j3 1:7db4a1cc7abb 58 rtc.set_cntl_stat_reg(rtc_control_status);
j3 1:7db4a1cc7abb 59
j3 2:76cd47a603b5 60 //get day from user
j3 2:76cd47a603b5 61 get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
j3 2:76cd47a603b5 62 7, &rtc_calendar.day);
j3 2:76cd47a603b5 63
j3 2:76cd47a603b5 64 //get day of month from user
j3 2:76cd47a603b5 65 get_user_input("\nPlease enter day of month (1-31): ", 1, 31,
j3 2:76cd47a603b5 66 &rtc_calendar.date);
j3 2:76cd47a603b5 67
j3 2:76cd47a603b5 68 //get month from user
j3 2:76cd47a603b5 69 get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1,
j3 2:76cd47a603b5 70 12, &rtc_calendar.month);
j3 2:76cd47a603b5 71
j3 2:76cd47a603b5 72 //get year from user
j3 2:76cd47a603b5 73 get_user_input("\nPlease enter the year (0-99): ",0, 99,
j3 2:76cd47a603b5 74 &rtc_calendar.year);
j3 2:76cd47a603b5 75
j3 2:76cd47a603b5 76 //Get time mode
j3 2:76cd47a603b5 77 get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1,
j3 2:76cd47a603b5 78 &rtc_time.mode);
j3 1:7db4a1cc7abb 79
j3 2:76cd47a603b5 80 if(rtc_time.mode)
j3 2:76cd47a603b5 81 {
j3 2:76cd47a603b5 82 //Get AM/PM status
j3 2:76cd47a603b5 83 get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1,
j3 2:76cd47a603b5 84 &rtc_time.am_pm);
j3 2:76cd47a603b5 85 //Get hour from user
j3 2:76cd47a603b5 86 get_user_input("\nPlease enter the hour (1-12): ", 1, 12,
j3 2:76cd47a603b5 87 &rtc_time.hours);
j3 2:76cd47a603b5 88 }
j3 2:76cd47a603b5 89 else
j3 2:76cd47a603b5 90 {
j3 2:76cd47a603b5 91 //Get hour from user
j3 2:76cd47a603b5 92 get_user_input("\nPlease enter the hour (0-23): ", 0, 23,
j3 2:76cd47a603b5 93 &rtc_time.hours);
j3 2:76cd47a603b5 94 }
j3 2:76cd47a603b5 95
j3 1:7db4a1cc7abb 96 //Get minutes from user
j3 1:7db4a1cc7abb 97 get_user_input("\nPlease enter the minute (0-59): ", 0, 59,
j3 1:7db4a1cc7abb 98 &rtc_time.minutes);
j3 1:7db4a1cc7abb 99
j3 1:7db4a1cc7abb 100
j3 1:7db4a1cc7abb 101 //Get seconds from user
j3 1:7db4a1cc7abb 102 get_user_input("\nPlease enter the second (0-59): ", 0, 59,
j3 1:7db4a1cc7abb 103 &rtc_time.seconds);
j3 1:7db4a1cc7abb 104
j3 2:76cd47a603b5 105
j3 2:76cd47a603b5 106
j3 2:76cd47a603b5 107 //Set the time, uses inverted logic for return value
j3 2:76cd47a603b5 108 if(rtc.set_time(rtc_time))
j3 2:76cd47a603b5 109 {
j3 2:76cd47a603b5 110 printf("\nrtc.set_time failed!!\n");
j3 2:76cd47a603b5 111 exit(0);
j3 2:76cd47a603b5 112 }
j3 2:76cd47a603b5 113
j3 2:76cd47a603b5 114 //Set the calendar, uses inverted logic for return value
j3 2:76cd47a603b5 115 if(rtc.set_calendar(rtc_calendar))
j3 2:76cd47a603b5 116 {
j3 2:76cd47a603b5 117 printf("\nrtc.set_calendar failed!!\n");
j3 2:76cd47a603b5 118 exit(0);
j3 2:76cd47a603b5 119 }
j3 2:76cd47a603b5 120
j3 2:76cd47a603b5 121 char buffer[32];
j3 1:7db4a1cc7abb 122
j3 2:76cd47a603b5 123 for(;;)
j3 2:76cd47a603b5 124 {
j3 2:76cd47a603b5 125 printf("%c[2J", ESC); //clear screen
j3 2:76cd47a603b5 126 printf("%c[H", ESC); //move cursor to Home
j3 2:76cd47a603b5 127
j3 2:76cd47a603b5 128 //new epoch time fx
j3 2:76cd47a603b5 129 epoch_time = rtc.get_epoch();
j3 2:76cd47a603b5 130
j3 2:76cd47a603b5 131 printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
j3 2:76cd47a603b5 132
j3 2:76cd47a603b5 133 printf("\nTime as a basic string = %s", ctime(&epoch_time));
j3 2:76cd47a603b5 134
j3 2:76cd47a603b5 135 strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
j3 2:76cd47a603b5 136 printf("\nTime as a custom formatted string = %s", buffer);
j3 2:76cd47a603b5 137
j3 2:76cd47a603b5 138 wait(1.0);
j3 2:76cd47a603b5 139 }//loop
j3 1:7db4a1cc7abb 140 }
j3 1:7db4a1cc7abb 141
j3 1:7db4a1cc7abb 142
j3 1:7db4a1cc7abb 143 /**********************************************************************
j3 1:7db4a1cc7abb 144 * Function: get_user_input()
j3 1:7db4a1cc7abb 145 * Parameters: message - user prompt
j3 1:7db4a1cc7abb 146 * min - minimum value of input
j3 1:7db4a1cc7abb 147 * max - maximum value of input
j3 1:7db4a1cc7abb 148 * member - pointer to struct member
j3 1:7db4a1cc7abb 149 * Returns: none
j3 1:7db4a1cc7abb 150 *
j3 1:7db4a1cc7abb 151 * Description: get time/date input from user
j3 1:7db4a1cc7abb 152 *
j3 1:7db4a1cc7abb 153 **********************************************************************/
j3 2:76cd47a603b5 154 void get_user_input(char* message, uint8_t min, uint8_t max, uint32_t* member)
j3 1:7db4a1cc7abb 155 {
j3 2:76cd47a603b5 156 uint32_t temp;
j3 1:7db4a1cc7abb 157
j3 1:7db4a1cc7abb 158 do
j3 0:6be499dd402a 159 {
j3 2:76cd47a603b5 160 printf("\n%s", message);
j3 2:76cd47a603b5 161
j3 2:76cd47a603b5 162 //for some reason mbed doesn't like a pointer to a member in scanf
j3 2:76cd47a603b5 163 //term.scanf("%d", member); works with gcc on RPi
j3 2:76cd47a603b5 164 scanf("%d", &temp);
j3 2:76cd47a603b5 165
j3 2:76cd47a603b5 166 *member = temp;
j3 1:7db4a1cc7abb 167
j3 1:7db4a1cc7abb 168 if((*(member)< min) || (*(member) > max))
j3 1:7db4a1cc7abb 169 {
j3 2:76cd47a603b5 170 printf("\nERROR-RTI");
j3 1:7db4a1cc7abb 171 }
j3 0:6be499dd402a 172 }
j3 1:7db4a1cc7abb 173 while((*(member) < min) || (*(member) > max));
j3 2:76cd47a603b5 174 }
j3 1:7db4a1cc7abb 175
j3 2:76cd47a603b5 176
j3 2:76cd47a603b5 177 void get_user_input(char* message, uint8_t min, uint8_t max, bool* member)
j3 2:76cd47a603b5 178 {
j3 5:ab9d82256e26 179 uint32_t temp;
j3 2:76cd47a603b5 180
j3 2:76cd47a603b5 181 do
j3 2:76cd47a603b5 182 {
j3 2:76cd47a603b5 183 printf("\n%s", message);
j3 2:76cd47a603b5 184
j3 2:76cd47a603b5 185 //for some reason mbed doesn't like a pointer to a member in scanf
j3 2:76cd47a603b5 186 //term.scanf("%d", member); works with gcc on RPi
j3 2:76cd47a603b5 187 scanf("%d", &temp);
j3 2:76cd47a603b5 188
j3 2:76cd47a603b5 189 *member = temp;
j3 2:76cd47a603b5 190
j3 2:76cd47a603b5 191 if((*(member)< min) || (*(member) > max))
j3 2:76cd47a603b5 192 {
j3 2:76cd47a603b5 193 printf("\nERROR-RTI");
j3 2:76cd47a603b5 194 }
j3 2:76cd47a603b5 195 }
j3 2:76cd47a603b5 196 while((*(member) < min) || (*(member) > max));
j3 0:6be499dd402a 197 }
j3 1:7db4a1cc7abb 198
j3 0:6be499dd402a 199