Fork of Demo program for ard2pmod library. Alarm features of RTC have not been tested, please try them out.

Dependencies:   Terminal ard2pmod mbed

Fork of ard2pmod_demo by Maxim Integrated

Committer:
j3
Date:
Tue Mar 29 01:23:35 2016 +0000
Revision:
14:08a83dc0e58e
Parent:
13:067920a73c78
updated libraries

Who changed what in which revision?

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