Hello World demo for DS3231 library

Dependencies:   ds3231 mbed

DS3231 Component Page

Committer:
j3
Date:
Thu Mar 12 19:38:11 2015 +0000
Revision:
3:826946b4eead
Parent:
2:76cd47a603b5
Child:
4:e5d7d2122d4d
Updated library to version 1.0

Who changed what in which revision?

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