Use the MAXREFDES99 to display the time from a DS3231 RTC. Requires the DS3231 RTC, or a MAXREFDES72 which has the rtc on it.

Dependencies:   MAX7219 ds3231 mbed

Committer:
j3
Date:
Fri Mar 25 22:50:52 2016 +0000
Revision:
0:52f9ecc09233
Child:
1:ce6a3accca77
Init Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:52f9ecc09233 1 /***********************************************************************
j3 0:52f9ecc09233 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:52f9ecc09233 3 *
j3 0:52f9ecc09233 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:52f9ecc09233 5 * copy of this software and associated documentation files (the "Software"),
j3 0:52f9ecc09233 6 * to deal in the Software without restriction, including without limitation
j3 0:52f9ecc09233 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:52f9ecc09233 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:52f9ecc09233 9 * Software is furnished to do so, subject to the following conditions:
j3 0:52f9ecc09233 10 *
j3 0:52f9ecc09233 11 * The above copyright notice and this permission notice shall be included
j3 0:52f9ecc09233 12 * in all copies or substantial portions of the Software.
j3 0:52f9ecc09233 13 *
j3 0:52f9ecc09233 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:52f9ecc09233 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:52f9ecc09233 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:52f9ecc09233 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:52f9ecc09233 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:52f9ecc09233 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:52f9ecc09233 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:52f9ecc09233 21 *
j3 0:52f9ecc09233 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:52f9ecc09233 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:52f9ecc09233 24 * Products, Inc. Branding Policy.
j3 0:52f9ecc09233 25 *
j3 0:52f9ecc09233 26 * The mere transfer of this software does not imply any licenses
j3 0:52f9ecc09233 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:52f9ecc09233 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:52f9ecc09233 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:52f9ecc09233 30 * ownership rights.
j3 0:52f9ecc09233 31 **********************************************************************/
j3 0:52f9ecc09233 32
j3 0:52f9ecc09233 33
j3 0:52f9ecc09233 34 #include "mbed.h"
j3 0:52f9ecc09233 35 #include "ds3231.h"
j3 0:52f9ecc09233 36 #include "maxrefdes99.h"
j3 0:52f9ecc09233 37 #include <string>
j3 0:52f9ecc09233 38
j3 0:52f9ecc09233 39
j3 0:52f9ecc09233 40 void set_rtc(Ds3231 & rtc);
j3 0:52f9ecc09233 41
j3 0:52f9ecc09233 42
j3 0:52f9ecc09233 43 int main(void)
j3 0:52f9ecc09233 44 {
j3 0:52f9ecc09233 45 //rtc object
j3 0:52f9ecc09233 46 Ds3231 rtc(D14, D15);
j3 0:52f9ecc09233 47
j3 0:52f9ecc09233 48 //switch for setting clock, requires tera term interface at 9600 8N1
j3 0:52f9ecc09233 49 DigitalIn sw(SW3);
j3 0:52f9ecc09233 50
j3 0:52f9ecc09233 51 if(sw.read() == 0)
j3 0:52f9ecc09233 52 {
j3 0:52f9ecc09233 53 set_rtc(rtc);
j3 0:52f9ecc09233 54 }
j3 0:52f9ecc09233 55
j3 0:52f9ecc09233 56 Max7219 display(D11, D12, D13, D10);
j3 0:52f9ecc09233 57
j3 0:52f9ecc09233 58 //struct for holding MAX7219 configuration data
j3 0:52f9ecc09233 59 max7219_configuration_t display_config;
j3 0:52f9ecc09233 60
j3 0:52f9ecc09233 61 //configuration data
j3 0:52f9ecc09233 62 display_config.decode_mode = 0; //no BCD decode
j3 0:52f9ecc09233 63 display_config.intensity = 0x0F; //max intensity
j3 0:52f9ecc09233 64 display_config.scan_limit = 0x07; //scan all digits
j3 0:52f9ecc09233 65
j3 0:52f9ecc09233 66 //set number of MAX7219 devices being used
j3 0:52f9ecc09233 67 display.set_num_devices(4);
j3 0:52f9ecc09233 68
j3 0:52f9ecc09233 69 //config display
j3 0:52f9ecc09233 70 display.init_display(display_config);
j3 0:52f9ecc09233 71
j3 0:52f9ecc09233 72 //ensure all data registers are 0
j3 0:52f9ecc09233 73 display.display_all_off();
j3 0:52f9ecc09233 74
j3 0:52f9ecc09233 75 display.enable_display();
j3 0:52f9ecc09233 76
j3 0:52f9ecc09233 77 time_t epoch_time;
j3 0:52f9ecc09233 78 string str;
j3 0:52f9ecc09233 79 char time_buff[32];
j3 0:52f9ecc09233 80
j3 0:52f9ecc09233 81 for(;;)
j3 0:52f9ecc09233 82 {
j3 0:52f9ecc09233 83 //get epoch time from rtc
j3 0:52f9ecc09233 84 epoch_time = rtc.get_epoch();
j3 0:52f9ecc09233 85
j3 0:52f9ecc09233 86 //format time
j3 0:52f9ecc09233 87 strftime(time_buff, 32, "%a %b %d %Y %I:%M %p", localtime(&epoch_time));
j3 0:52f9ecc09233 88
j3 0:52f9ecc09233 89 //assign to str
j3 0:52f9ecc09233 90 str.assign(time_buff);
j3 0:52f9ecc09233 91
j3 0:52f9ecc09233 92 //find '\n' and remove it
j3 0:52f9ecc09233 93 if(str.find('\n') != std::string::npos)
j3 0:52f9ecc09233 94 {
j3 0:52f9ecc09233 95 str.erase(str.find('\n'));
j3 0:52f9ecc09233 96 }
j3 0:52f9ecc09233 97
j3 0:52f9ecc09233 98 //append spaces to the end
j3 0:52f9ecc09233 99 str.append(" ");
j3 0:52f9ecc09233 100
j3 0:52f9ecc09233 101 //print to display
j3 0:52f9ecc09233 102 print_string(&display, 33, str.c_str());
j3 0:52f9ecc09233 103
j3 0:52f9ecc09233 104 //shift the display the appropriate number of positions
j3 0:52f9ecc09233 105 //1 char is 6 columns wide
j3 0:52f9ecc09233 106 shift_display_left(&display, (str.length()*6), 150);
j3 0:52f9ecc09233 107
j3 0:52f9ecc09233 108 //clear the display
j3 0:52f9ecc09233 109 all_off(&display);
j3 0:52f9ecc09233 110
j3 0:52f9ecc09233 111 //clears all data in buffer
j3 0:52f9ecc09233 112 clear_buffer();
j3 0:52f9ecc09233 113
j3 0:52f9ecc09233 114 wait(0.5);
j3 0:52f9ecc09233 115 }
j3 0:52f9ecc09233 116 }
j3 0:52f9ecc09233 117
j3 0:52f9ecc09233 118
j3 0:52f9ecc09233 119 //*********************************************************************
j3 0:52f9ecc09233 120 void set_rtc(Ds3231 & rtc)
j3 0:52f9ecc09233 121 {
j3 0:52f9ecc09233 122 //default, use bit masks in ds3231.h for desired operation
j3 0:52f9ecc09233 123 ds3231_cntl_stat_t rtc_control_status = {0,0};
j3 0:52f9ecc09233 124 ds3231_time_t rtc_time;
j3 0:52f9ecc09233 125 ds3231_calendar_t rtc_calendar;
j3 0:52f9ecc09233 126
j3 0:52f9ecc09233 127 rtc.set_cntl_stat_reg(rtc_control_status);
j3 0:52f9ecc09233 128
j3 0:52f9ecc09233 129 //get day from user
j3 0:52f9ecc09233 130 rtc_calendar.day = get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 7);
j3 0:52f9ecc09233 131
j3 0:52f9ecc09233 132 //get day of month from user
j3 0:52f9ecc09233 133 rtc_calendar.date = get_user_input("\nPlease enter day of month (1-31): ", 31);
j3 0:52f9ecc09233 134
j3 0:52f9ecc09233 135 //get month from user
j3 0:52f9ecc09233 136 rtc_calendar.month = get_user_input("\nPlease enter the month, 1 for January (1-12): ", 12);
j3 0:52f9ecc09233 137
j3 0:52f9ecc09233 138 //get year from user
j3 0:52f9ecc09233 139 rtc_calendar.year = get_user_input("\nPlease enter the year (0-99): ", 99);
j3 0:52f9ecc09233 140
j3 0:52f9ecc09233 141 //Get time mode
j3 0:52f9ecc09233 142 rtc_time.mode = get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 1);
j3 0:52f9ecc09233 143
j3 0:52f9ecc09233 144 if(rtc_time.mode)
j3 0:52f9ecc09233 145 {
j3 0:52f9ecc09233 146 //Get AM/PM status
j3 0:52f9ecc09233 147 rtc_time.am_pm = get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 1);
j3 0:52f9ecc09233 148 //Get hour from user
j3 0:52f9ecc09233 149 rtc_time.hours = get_user_input("\nPlease enter the hour (1-12): ", 12);
j3 0:52f9ecc09233 150 }
j3 0:52f9ecc09233 151 else
j3 0:52f9ecc09233 152 {
j3 0:52f9ecc09233 153 //Get hour from user
j3 0:52f9ecc09233 154 rtc_time.hours = get_user_input("\nPlease enter the hour (0-23): ", 23);
j3 0:52f9ecc09233 155 }
j3 0:52f9ecc09233 156
j3 0:52f9ecc09233 157 //Get minutes from user
j3 0:52f9ecc09233 158 rtc_time.minutes = get_user_input("\nPlease enter the minute (0-59): ", 59);
j3 0:52f9ecc09233 159
j3 0:52f9ecc09233 160
j3 0:52f9ecc09233 161 //Get seconds from user
j3 0:52f9ecc09233 162 rtc_time.seconds = get_user_input("\nPlease enter the second (0-59): ", 59);
j3 0:52f9ecc09233 163
j3 0:52f9ecc09233 164 rtc.set_time(rtc_time);
j3 0:52f9ecc09233 165 rtc.set_calendar(rtc_calendar);
j3 0:52f9ecc09233 166 }
j3 0:52f9ecc09233 167
j3 0:52f9ecc09233 168