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
main.cpp@1:ce6a3accca77, 2016-05-31 (annotated)
- Committer:
- j3
- Date:
- Tue May 31 22:32:53 2016 +0000
- Revision:
- 1:ce6a3accca77
- Parent:
- 0:52f9ecc09233
Updated libraries
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:ce6a3accca77 | 48 | set_rtc(rtc); |
j3 | 0:52f9ecc09233 | 49 | |
j3 | 0:52f9ecc09233 | 50 | Max7219 display(D11, D12, D13, D10); |
j3 | 0:52f9ecc09233 | 51 | |
j3 | 0:52f9ecc09233 | 52 | //struct for holding MAX7219 configuration data |
j3 | 0:52f9ecc09233 | 53 | max7219_configuration_t display_config; |
j3 | 0:52f9ecc09233 | 54 | |
j3 | 0:52f9ecc09233 | 55 | //configuration data |
j3 | 0:52f9ecc09233 | 56 | display_config.decode_mode = 0; //no BCD decode |
j3 | 0:52f9ecc09233 | 57 | display_config.intensity = 0x0F; //max intensity |
j3 | 0:52f9ecc09233 | 58 | display_config.scan_limit = 0x07; //scan all digits |
j3 | 0:52f9ecc09233 | 59 | |
j3 | 0:52f9ecc09233 | 60 | //set number of MAX7219 devices being used |
j3 | 0:52f9ecc09233 | 61 | display.set_num_devices(4); |
j3 | 0:52f9ecc09233 | 62 | |
j3 | 0:52f9ecc09233 | 63 | //config display |
j3 | 0:52f9ecc09233 | 64 | display.init_display(display_config); |
j3 | 0:52f9ecc09233 | 65 | |
j3 | 0:52f9ecc09233 | 66 | //ensure all data registers are 0 |
j3 | 0:52f9ecc09233 | 67 | display.display_all_off(); |
j3 | 0:52f9ecc09233 | 68 | |
j3 | 0:52f9ecc09233 | 69 | display.enable_display(); |
j3 | 0:52f9ecc09233 | 70 | |
j3 | 0:52f9ecc09233 | 71 | time_t epoch_time; |
j3 | 0:52f9ecc09233 | 72 | string str; |
j3 | 0:52f9ecc09233 | 73 | char time_buff[32]; |
j3 | 0:52f9ecc09233 | 74 | |
j3 | 0:52f9ecc09233 | 75 | for(;;) |
j3 | 0:52f9ecc09233 | 76 | { |
j3 | 0:52f9ecc09233 | 77 | //get epoch time from rtc |
j3 | 0:52f9ecc09233 | 78 | epoch_time = rtc.get_epoch(); |
j3 | 0:52f9ecc09233 | 79 | |
j3 | 0:52f9ecc09233 | 80 | //format time |
j3 | 0:52f9ecc09233 | 81 | strftime(time_buff, 32, "%a %b %d %Y %I:%M %p", localtime(&epoch_time)); |
j3 | 0:52f9ecc09233 | 82 | |
j3 | 0:52f9ecc09233 | 83 | //assign to str |
j3 | 0:52f9ecc09233 | 84 | str.assign(time_buff); |
j3 | 0:52f9ecc09233 | 85 | |
j3 | 0:52f9ecc09233 | 86 | //find '\n' and remove it |
j3 | 0:52f9ecc09233 | 87 | if(str.find('\n') != std::string::npos) |
j3 | 0:52f9ecc09233 | 88 | { |
j3 | 0:52f9ecc09233 | 89 | str.erase(str.find('\n')); |
j3 | 0:52f9ecc09233 | 90 | } |
j3 | 0:52f9ecc09233 | 91 | |
j3 | 0:52f9ecc09233 | 92 | //append spaces to the end |
j3 | 0:52f9ecc09233 | 93 | str.append(" "); |
j3 | 0:52f9ecc09233 | 94 | |
j3 | 0:52f9ecc09233 | 95 | //print to display |
j3 | 0:52f9ecc09233 | 96 | print_string(&display, 33, str.c_str()); |
j3 | 0:52f9ecc09233 | 97 | |
j3 | 0:52f9ecc09233 | 98 | //shift the display the appropriate number of positions |
j3 | 0:52f9ecc09233 | 99 | //1 char is 6 columns wide |
j3 | 0:52f9ecc09233 | 100 | shift_display_left(&display, (str.length()*6), 150); |
j3 | 0:52f9ecc09233 | 101 | |
j3 | 0:52f9ecc09233 | 102 | //clear the display |
j3 | 0:52f9ecc09233 | 103 | all_off(&display); |
j3 | 0:52f9ecc09233 | 104 | |
j3 | 0:52f9ecc09233 | 105 | //clears all data in buffer |
j3 | 0:52f9ecc09233 | 106 | clear_buffer(); |
j3 | 0:52f9ecc09233 | 107 | |
j3 | 0:52f9ecc09233 | 108 | wait(0.5); |
j3 | 0:52f9ecc09233 | 109 | } |
j3 | 0:52f9ecc09233 | 110 | } |
j3 | 0:52f9ecc09233 | 111 | |
j3 | 0:52f9ecc09233 | 112 | |
j3 | 0:52f9ecc09233 | 113 | //********************************************************************* |
j3 | 0:52f9ecc09233 | 114 | void set_rtc(Ds3231 & rtc) |
j3 | 0:52f9ecc09233 | 115 | { |
j3 | 0:52f9ecc09233 | 116 | //default, use bit masks in ds3231.h for desired operation |
j3 | 0:52f9ecc09233 | 117 | ds3231_cntl_stat_t rtc_control_status = {0,0}; |
j3 | 0:52f9ecc09233 | 118 | ds3231_time_t rtc_time; |
j3 | 0:52f9ecc09233 | 119 | ds3231_calendar_t rtc_calendar; |
j3 | 0:52f9ecc09233 | 120 | |
j3 | 0:52f9ecc09233 | 121 | rtc.set_cntl_stat_reg(rtc_control_status); |
j3 | 0:52f9ecc09233 | 122 | |
j3 | 0:52f9ecc09233 | 123 | //get day from user |
j3 | 0:52f9ecc09233 | 124 | rtc_calendar.day = get_user_input("\nPlease enter day of week, 1 for Sunday (1-7): ", 7); |
j3 | 0:52f9ecc09233 | 125 | |
j3 | 0:52f9ecc09233 | 126 | //get day of month from user |
j3 | 0:52f9ecc09233 | 127 | rtc_calendar.date = get_user_input("\nPlease enter day of month (1-31): ", 31); |
j3 | 0:52f9ecc09233 | 128 | |
j3 | 0:52f9ecc09233 | 129 | //get month from user |
j3 | 0:52f9ecc09233 | 130 | rtc_calendar.month = get_user_input("\nPlease enter the month, 1 for January (1-12): ", 12); |
j3 | 0:52f9ecc09233 | 131 | |
j3 | 0:52f9ecc09233 | 132 | //get year from user |
j3 | 0:52f9ecc09233 | 133 | rtc_calendar.year = get_user_input("\nPlease enter the year (0-99): ", 99); |
j3 | 0:52f9ecc09233 | 134 | |
j3 | 0:52f9ecc09233 | 135 | //Get time mode |
j3 | 0:52f9ecc09233 | 136 | rtc_time.mode = get_user_input("\nWhat time mode? 1 for 12hr 0 for 24hr: ", 1); |
j3 | 0:52f9ecc09233 | 137 | |
j3 | 0:52f9ecc09233 | 138 | if(rtc_time.mode) |
j3 | 0:52f9ecc09233 | 139 | { |
j3 | 0:52f9ecc09233 | 140 | //Get AM/PM status |
j3 | 0:52f9ecc09233 | 141 | rtc_time.am_pm = get_user_input("\nIs it AM or PM? 0 for AM 1 for PM: ", 1); |
j3 | 0:52f9ecc09233 | 142 | //Get hour from user |
j3 | 0:52f9ecc09233 | 143 | rtc_time.hours = get_user_input("\nPlease enter the hour (1-12): ", 12); |
j3 | 0:52f9ecc09233 | 144 | } |
j3 | 0:52f9ecc09233 | 145 | else |
j3 | 0:52f9ecc09233 | 146 | { |
j3 | 0:52f9ecc09233 | 147 | //Get hour from user |
j3 | 0:52f9ecc09233 | 148 | rtc_time.hours = get_user_input("\nPlease enter the hour (0-23): ", 23); |
j3 | 0:52f9ecc09233 | 149 | } |
j3 | 0:52f9ecc09233 | 150 | |
j3 | 0:52f9ecc09233 | 151 | //Get minutes from user |
j3 | 0:52f9ecc09233 | 152 | rtc_time.minutes = get_user_input("\nPlease enter the minute (0-59): ", 59); |
j3 | 0:52f9ecc09233 | 153 | |
j3 | 0:52f9ecc09233 | 154 | |
j3 | 0:52f9ecc09233 | 155 | //Get seconds from user |
j3 | 0:52f9ecc09233 | 156 | rtc_time.seconds = get_user_input("\nPlease enter the second (0-59): ", 59); |
j3 | 0:52f9ecc09233 | 157 | |
j3 | 0:52f9ecc09233 | 158 | rtc.set_time(rtc_time); |
j3 | 0:52f9ecc09233 | 159 | rtc.set_calendar(rtc_calendar); |
j3 | 0:52f9ecc09233 | 160 | } |
j3 | 0:52f9ecc09233 | 161 | |
j3 | 0:52f9ecc09233 | 162 |