example using DS3234 RTC connected to BBC micro:bit using SPI

Dependencies:   microbit

Committer:
jsa1969
Date:
Fri May 11 12:34:40 2018 +0000
Revision:
1:c740bf7f5c59
Parent:
0:d5fb8cef7c31
set time functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsa1969 0:d5fb8cef7c31 1 /*
jsa1969 0:d5fb8cef7c31 2 The MIT License (MIT)
jsa1969 0:d5fb8cef7c31 3
jsa1969 0:d5fb8cef7c31 4 Copyright (c) 2016 British Broadcasting Corporation.
jsa1969 0:d5fb8cef7c31 5 This software is provided by Lancaster University by arrangement with the BBC.
jsa1969 0:d5fb8cef7c31 6
jsa1969 0:d5fb8cef7c31 7 Permission is hereby granted, free of charge, to any person obtaining a
jsa1969 0:d5fb8cef7c31 8 copy of this software and associated documentation files (the "Software"),
jsa1969 0:d5fb8cef7c31 9 to deal in the Software without restriction, including without limitation
jsa1969 0:d5fb8cef7c31 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
jsa1969 0:d5fb8cef7c31 11 and/or sell copies of the Software, and to permit persons to whom the
jsa1969 0:d5fb8cef7c31 12 Software is furnished to do so, subject to the following conditions:
jsa1969 0:d5fb8cef7c31 13
jsa1969 0:d5fb8cef7c31 14 The above copyright notice and this permission notice shall be included in
jsa1969 0:d5fb8cef7c31 15 all copies or substantial portions of the Software.
jsa1969 0:d5fb8cef7c31 16
jsa1969 0:d5fb8cef7c31 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jsa1969 0:d5fb8cef7c31 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jsa1969 0:d5fb8cef7c31 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
jsa1969 0:d5fb8cef7c31 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jsa1969 0:d5fb8cef7c31 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jsa1969 0:d5fb8cef7c31 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
jsa1969 0:d5fb8cef7c31 23 DEALINGS IN THE SOFTWARE.
jsa1969 0:d5fb8cef7c31 24 */
jsa1969 0:d5fb8cef7c31 25
jsa1969 0:d5fb8cef7c31 26 #include "MicroBit.h"
jsa1969 0:d5fb8cef7c31 27 #include "SparkFunDS3234RTC.h"
jsa1969 0:d5fb8cef7c31 28
jsa1969 0:d5fb8cef7c31 29 MicroBit uBit;
jsa1969 0:d5fb8cef7c31 30 DS3234 rtcDS3234;
jsa1969 0:d5fb8cef7c31 31
jsa1969 1:c740bf7f5c59 32 int mode = 0;
jsa1969 1:c740bf7f5c59 33 bool displaying = false;
jsa1969 1:c740bf7f5c59 34 bool interruptLoop = false;
jsa1969 1:c740bf7f5c59 35 uint8_t sec, minute, hour, day, date, month, year;
jsa1969 1:c740bf7f5c59 36
jsa1969 0:d5fb8cef7c31 37 void displayTime(){
jsa1969 0:d5fb8cef7c31 38 rtcDS3234.update();
jsa1969 0:d5fb8cef7c31 39 uBit.display.scroll(rtcDS3234.hour());
jsa1969 0:d5fb8cef7c31 40 uBit.display.scroll(":");
jsa1969 0:d5fb8cef7c31 41 uBit.display.scroll(rtcDS3234.minute());
jsa1969 0:d5fb8cef7c31 42 uBit.display.scroll(":");
jsa1969 0:d5fb8cef7c31 43 uBit.display.scroll(rtcDS3234.second());
jsa1969 0:d5fb8cef7c31 44 }
jsa1969 1:c740bf7f5c59 45
jsa1969 1:c740bf7f5c59 46 void displayLoop(){
jsa1969 1:c740bf7f5c59 47 if (!displaying) {
jsa1969 1:c740bf7f5c59 48 while (!interruptLoop) {
jsa1969 1:c740bf7f5c59 49 displaying = true;
jsa1969 1:c740bf7f5c59 50 displayTime();
jsa1969 1:c740bf7f5c59 51 uBit.sleep(60000);
jsa1969 1:c740bf7f5c59 52 }
jsa1969 1:c740bf7f5c59 53 interruptLoop = false;
jsa1969 1:c740bf7f5c59 54 }
jsa1969 1:c740bf7f5c59 55 }
jsa1969 0:d5fb8cef7c31 56
jsa1969 0:d5fb8cef7c31 57 void onButtonB(MicroBitEvent e)
jsa1969 0:d5fb8cef7c31 58 {
jsa1969 1:c740bf7f5c59 59 switch(mode) {
jsa1969 1:c740bf7f5c59 60 case 0:
jsa1969 1:c740bf7f5c59 61 displayTime();
jsa1969 1:c740bf7f5c59 62 break;
jsa1969 1:c740bf7f5c59 63 case 1:
jsa1969 1:c740bf7f5c59 64 ++year;
jsa1969 1:c740bf7f5c59 65 break;
jsa1969 1:c740bf7f5c59 66 case 2:
jsa1969 1:c740bf7f5c59 67 ++month;
jsa1969 1:c740bf7f5c59 68 break;
jsa1969 1:c740bf7f5c59 69 case 3:
jsa1969 1:c740bf7f5c59 70 ++date;
jsa1969 1:c740bf7f5c59 71 break;
jsa1969 1:c740bf7f5c59 72 case 4:
jsa1969 1:c740bf7f5c59 73 ++day;
jsa1969 1:c740bf7f5c59 74 break;
jsa1969 1:c740bf7f5c59 75 case 5:
jsa1969 1:c740bf7f5c59 76 ++hour;
jsa1969 1:c740bf7f5c59 77 break;
jsa1969 1:c740bf7f5c59 78 case 6:
jsa1969 1:c740bf7f5c59 79 ++minute;
jsa1969 1:c740bf7f5c59 80 break;
jsa1969 1:c740bf7f5c59 81 }
jsa1969 0:d5fb8cef7c31 82 }
jsa1969 0:d5fb8cef7c31 83
jsa1969 0:d5fb8cef7c31 84 void onButtonA(MicroBitEvent e)
jsa1969 0:d5fb8cef7c31 85 {
jsa1969 1:c740bf7f5c59 86 switch(mode) {
jsa1969 1:c740bf7f5c59 87 case 0:
jsa1969 1:c740bf7f5c59 88 displayLoop();
jsa1969 1:c740bf7f5c59 89 break;
jsa1969 1:c740bf7f5c59 90 case 1:
jsa1969 1:c740bf7f5c59 91 --year;
jsa1969 1:c740bf7f5c59 92 break;
jsa1969 1:c740bf7f5c59 93 case 2:
jsa1969 1:c740bf7f5c59 94 --month;
jsa1969 1:c740bf7f5c59 95 break;
jsa1969 1:c740bf7f5c59 96 case 3:
jsa1969 1:c740bf7f5c59 97 --date;
jsa1969 1:c740bf7f5c59 98 break;
jsa1969 1:c740bf7f5c59 99 case 4:
jsa1969 1:c740bf7f5c59 100 --day;
jsa1969 1:c740bf7f5c59 101 break;
jsa1969 1:c740bf7f5c59 102 case 5:
jsa1969 1:c740bf7f5c59 103 --hour;
jsa1969 1:c740bf7f5c59 104 break;
jsa1969 1:c740bf7f5c59 105 case 6:
jsa1969 1:c740bf7f5c59 106 --minute;
jsa1969 1:c740bf7f5c59 107 break;
jsa1969 1:c740bf7f5c59 108 }
jsa1969 1:c740bf7f5c59 109 }
jsa1969 1:c740bf7f5c59 110
jsa1969 1:c740bf7f5c59 111 void onButtonAB(MicroBitEvent e)
jsa1969 1:c740bf7f5c59 112 {
jsa1969 1:c740bf7f5c59 113 switch(++mode) {
jsa1969 1:c740bf7f5c59 114 case 1:
jsa1969 1:c740bf7f5c59 115 rtcDS3234.update();
jsa1969 1:c740bf7f5c59 116 uBit.display.scroll("y");
jsa1969 1:c740bf7f5c59 117 year = rtcDS3234.year();
jsa1969 1:c740bf7f5c59 118 uBit.display.scroll(year);
jsa1969 1:c740bf7f5c59 119 break;
jsa1969 1:c740bf7f5c59 120 case 2:
jsa1969 1:c740bf7f5c59 121 uBit.display.scroll("m");
jsa1969 1:c740bf7f5c59 122 month = rtcDS3234.month();
jsa1969 1:c740bf7f5c59 123 uBit.display.scroll(month);
jsa1969 1:c740bf7f5c59 124 break;
jsa1969 1:c740bf7f5c59 125 case 3:
jsa1969 1:c740bf7f5c59 126 uBit.display.scroll("d");
jsa1969 1:c740bf7f5c59 127 date = rtcDS3234.date();
jsa1969 1:c740bf7f5c59 128 uBit.display.scroll(date);
jsa1969 1:c740bf7f5c59 129 break;
jsa1969 1:c740bf7f5c59 130 case 4:
jsa1969 1:c740bf7f5c59 131 uBit.display.scroll("wd");
jsa1969 1:c740bf7f5c59 132 day = rtcDS3234.day();
jsa1969 1:c740bf7f5c59 133 uBit.display.scroll(day);
jsa1969 1:c740bf7f5c59 134 break;
jsa1969 1:c740bf7f5c59 135 case 5:
jsa1969 1:c740bf7f5c59 136 uBit.display.scroll("h");
jsa1969 1:c740bf7f5c59 137 hour = rtcDS3234.hour();
jsa1969 1:c740bf7f5c59 138 uBit.display.scroll(hour);
jsa1969 1:c740bf7f5c59 139 break;
jsa1969 1:c740bf7f5c59 140 case 6:
jsa1969 1:c740bf7f5c59 141 uBit.display.scroll("m");
jsa1969 1:c740bf7f5c59 142 minute = rtcDS3234.minute();
jsa1969 1:c740bf7f5c59 143 uBit.display.scroll(minute);
jsa1969 1:c740bf7f5c59 144 break;
jsa1969 1:c740bf7f5c59 145 default:
jsa1969 1:c740bf7f5c59 146 mode = 0;
jsa1969 1:c740bf7f5c59 147 sec = 0;
jsa1969 1:c740bf7f5c59 148 rtcDS3234.setTime(sec, minute, hour, day, date, month, year);
jsa1969 1:c740bf7f5c59 149 uBit.display.scroll(date);
jsa1969 1:c740bf7f5c59 150 uBit.display.scroll(".");
jsa1969 1:c740bf7f5c59 151 uBit.display.scroll(month);
jsa1969 1:c740bf7f5c59 152 uBit.display.scroll(".");
jsa1969 1:c740bf7f5c59 153 uBit.display.scroll(year);
jsa1969 1:c740bf7f5c59 154 uBit.display.scroll(" ");
jsa1969 1:c740bf7f5c59 155 displayTime();
jsa1969 0:d5fb8cef7c31 156 }
jsa1969 0:d5fb8cef7c31 157 }
jsa1969 0:d5fb8cef7c31 158
jsa1969 0:d5fb8cef7c31 159 int main()
jsa1969 0:d5fb8cef7c31 160 {
jsa1969 0:d5fb8cef7c31 161 // Initialise the micro:bit runtime.
jsa1969 0:d5fb8cef7c31 162 uBit.init();
jsa1969 0:d5fb8cef7c31 163
jsa1969 0:d5fb8cef7c31 164 // Insert your code here!
jsa1969 0:d5fb8cef7c31 165 uBit.display.scroll("init");
jsa1969 0:d5fb8cef7c31 166
jsa1969 0:d5fb8cef7c31 167 rtcDS3234.begin(&uBit.io.P16, &uBit);
jsa1969 0:d5fb8cef7c31 168 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:d5fb8cef7c31 169 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 1:c740bf7f5c59 170 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
jsa1969 1:c740bf7f5c59 171
jsa1969 1:c740bf7f5c59 172 displayTime();
jsa1969 0:d5fb8cef7c31 173
jsa1969 0:d5fb8cef7c31 174 release_fiber();
jsa1969 0:d5fb8cef7c31 175 }
jsa1969 0:d5fb8cef7c31 176