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:
Fri Dec 05 02:54:36 2014 +0000
Revision:
4:e2b18050c4d5
Parent:
3:a9453e8c6641
Child:
5:46b14ed96df3
updated mbed to rev92, I2C wasn't working on K64F with old mbed library

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 2:9b1e5efd9a09 13 * Copyright (C) 2013 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 0:f906040920d7 45 #include "ard2pmod.h"
j3 0:f906040920d7 46
j3 3:a9453e8c6641 47 #define ESC 0x1B
j3 2:9b1e5efd9a09 48
j3 1:37fd05629bd3 49 void get_user_input(char* message, uint8_t min, uint8_t max, uint8_t* member);
j3 1:37fd05629bd3 50
j3 1:37fd05629bd3 51 Serial term(USBTX, USBRX);
j3 0:f906040920d7 52
j3 0:f906040920d7 53 int main(void)
j3 0:f906040920d7 54 {
j3 4:e2b18050c4d5 55 /*
j3 4:e2b18050c4d5 56 available PMOD types
j3 1:37fd05629bd3 57 PMOD_TYPE_I2C_A,
j3 1:37fd05629bd3 58 PMOD_TYPE_I2C_B,
j3 1:37fd05629bd3 59 PMOD_TYPE_I2C_AB,
j3 0:f906040920d7 60 PMOD_TYPE_1_GPIO,
j3 0:f906040920d7 61 PMOD_TYPE_2_SPI,
j3 0:f906040920d7 62 PMOD_TYPE_3_UART,
j3 0:f906040920d7 63 PMOD_TYPE_4_UART,
j3 0:f906040920d7 64 PMOD_TYPE_5_HBRIDGE,
j3 0:f906040920d7 65 PMOD_TYPE_6_HBRIDGE
j3 0:f906040920d7 66 */
j3 4:e2b18050c4d5 67 Ard2Pmod ard2pmod;
j3 0:f906040920d7 68
j3 2:9b1e5efd9a09 69 //can be used to wiggle lines for PMOD_TYPE_1_GPIO
j3 1:37fd05629bd3 70 //BusOut pmod_A(D10, D11, D12, D13);
j3 1:37fd05629bd3 71 //BusOut pmod_B(D4, D5, D6, D7);
j3 1:37fd05629bd3 72
j3 1:37fd05629bd3 73 //DS3231 rtc variables
j3 1:37fd05629bd3 74
j3 2:9b1e5efd9a09 75 //default for control and status registers,
j3 2:9b1e5efd9a09 76 //use bit masks in ds3231.h for desired operation
j3 1:37fd05629bd3 77 ds3231_cntl_stat_t rtc_control_status = {0,0};
j3 1:37fd05629bd3 78 ds3231_time_t rtc_time;
j3 1:37fd05629bd3 79 ds3231_calendar_t rtc_calendar;
j3 1:37fd05629bd3 80
j3 3:a9453e8c6641 81 time_t epoch_time;
j3 3:a9453e8c6641 82
j3 1:37fd05629bd3 83 ard2pmod.set_cntl_stat_reg(rtc_control_status);
j3 1:37fd05629bd3 84
j3 1:37fd05629bd3 85 //get day from user
j3 1:37fd05629bd3 86 get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 1,
j3 1:37fd05629bd3 87 7, &rtc_calendar.day);
j3 1:37fd05629bd3 88
j3 1:37fd05629bd3 89 //get day of month from user
j3 1:37fd05629bd3 90 get_user_input("\nPlease enter day of month (1-31): ", 1, 31,
j3 1:37fd05629bd3 91 &rtc_calendar.date);
j3 1:37fd05629bd3 92
j3 1:37fd05629bd3 93 //get month from user
j3 1:37fd05629bd3 94 get_user_input("\nPlease enter the month, 1 for January (1-12): ", 1,
j3 1:37fd05629bd3 95 12, &rtc_calendar.month);
j3 3:a9453e8c6641 96
j3 1:37fd05629bd3 97 //get year from user
j3 1:37fd05629bd3 98 get_user_input("\nPlease enter the year (0-99): ",0, 99,
j3 1:37fd05629bd3 99 &rtc_calendar.year);
j3 1:37fd05629bd3 100
j3 1:37fd05629bd3 101 //Get time mode
j3 1:37fd05629bd3 102 get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 0, 1,
j3 1:37fd05629bd3 103 (uint8_t*) &rtc_time.mode);
j3 1:37fd05629bd3 104
j3 1:37fd05629bd3 105 if(rtc_time.mode)
j3 1:37fd05629bd3 106 {
j3 1:37fd05629bd3 107 //Get AM/PM status
j3 1:37fd05629bd3 108 get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 0, 1,
j3 1:37fd05629bd3 109 (uint8_t*) &rtc_time.am_pm);
j3 1:37fd05629bd3 110 //Get hour from user
j3 1:37fd05629bd3 111 get_user_input("\nPlease enter the hour (1-12): ", 1, 12,
j3 1:37fd05629bd3 112 &rtc_time.hours);
j3 1:37fd05629bd3 113 }
j3 1:37fd05629bd3 114 else
j3 1:37fd05629bd3 115 {
j3 1:37fd05629bd3 116 //Get hour from user
j3 1:37fd05629bd3 117 get_user_input("\nPlease enter the hour (0-23): ", 0, 23,
j3 1:37fd05629bd3 118 &rtc_time.hours);
j3 1:37fd05629bd3 119 }
j3 1:37fd05629bd3 120
j3 1:37fd05629bd3 121 //Get minutes from user
j3 1:37fd05629bd3 122 get_user_input("\nPlease enter the minute (0-59): ", 0, 59,
j3 1:37fd05629bd3 123 &rtc_time.minutes);
j3 1:37fd05629bd3 124
j3 1:37fd05629bd3 125 //Get seconds from user
j3 1:37fd05629bd3 126 get_user_input("\nPlease enter the second (0-59): ", 0, 59,
j3 1:37fd05629bd3 127 &rtc_time.seconds);
j3 3:a9453e8c6641 128
j3 1:37fd05629bd3 129 //Set the time
j3 1:37fd05629bd3 130 ard2pmod.set_time(rtc_time);
j3 1:37fd05629bd3 131
j3 1:37fd05629bd3 132 //Set the calendar
j3 1:37fd05629bd3 133 ard2pmod.set_calendar(rtc_calendar);
j3 0:f906040920d7 134
j3 3:a9453e8c6641 135 char buffer[32];
j3 3:a9453e8c6641 136
j3 0:f906040920d7 137 for(;;)
j3 0:f906040920d7 138 {
j3 3:a9453e8c6641 139 term.printf("%c[2J", ESC); //clear screen
j3 3:a9453e8c6641 140 term.printf("%c[H", ESC); //move cursor to Home
j3 0:f906040920d7 141
j3 3:a9453e8c6641 142 //new epoch time fx
j3 3:a9453e8c6641 143 epoch_time = ard2pmod.get_epoch();
j3 1:37fd05629bd3 144
j3 3:a9453e8c6641 145 term.printf("\nTime as seconds since January 1, 1970 = %d\n", epoch_time);
j3 1:37fd05629bd3 146
j3 3:a9453e8c6641 147 term.printf("\nTime as a basic string = %s", ctime(&epoch_time));
j3 3:a9453e8c6641 148
j3 3:a9453e8c6641 149 strftime(buffer, 32, "%I:%M %p\n", localtime(&epoch_time));
j3 3:a9453e8c6641 150 term.printf("\nTime as a custom formatted string = %s", buffer);
j3 3:a9453e8c6641 151
j3 3:a9453e8c6641 152 wait(1.0);
j3 1:37fd05629bd3 153 }//loop
j3 1:37fd05629bd3 154 }
j3 1:37fd05629bd3 155
j3 1:37fd05629bd3 156
j3 1:37fd05629bd3 157 /**********************************************************************
j3 1:37fd05629bd3 158 * Function: get_user_input()
j3 1:37fd05629bd3 159 * Parameters: message - user prompt
j3 1:37fd05629bd3 160 * min - minimum value of input
j3 1:37fd05629bd3 161 * max - maximum value of input
j3 1:37fd05629bd3 162 * member - pointer to struct member
j3 1:37fd05629bd3 163 * Returns: none
j3 1:37fd05629bd3 164 *
j3 1:37fd05629bd3 165 * Description: get time/date input from user
j3 1:37fd05629bd3 166 *
j3 1:37fd05629bd3 167 **********************************************************************/
j3 1:37fd05629bd3 168 void get_user_input(char* message, uint8_t min, uint8_t max, uint8_t* member)
j3 1:37fd05629bd3 169 {
j3 1:37fd05629bd3 170 uint8_t temp;
j3 1:37fd05629bd3 171
j3 1:37fd05629bd3 172 do
j3 1:37fd05629bd3 173 {
j3 1:37fd05629bd3 174 term.printf("\n%s", message);
j3 1:37fd05629bd3 175
j3 1:37fd05629bd3 176 //for some reason mbed doesn't like a pointer to a member in scanf
j3 1:37fd05629bd3 177 //term.scanf("%d", member); works with gcc on RPi
j3 1:37fd05629bd3 178 term.scanf("%d", &temp);
j3 1:37fd05629bd3 179 *member = temp;
j3 1:37fd05629bd3 180
j3 1:37fd05629bd3 181 if((*(member)< min) || (*(member) > max))
j3 1:37fd05629bd3 182 {
j3 1:37fd05629bd3 183 term.printf("\nERROR-RTI");
j3 1:37fd05629bd3 184 }
j3 0:f906040920d7 185 }
j3 1:37fd05629bd3 186 while((*(member) < min) || (*(member) > max));
j3 1:37fd05629bd3 187 }