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

Dependencies:   microbit

Committer:
jsa1969
Date:
Fri May 11 09:37:45 2018 +0000
Revision:
0:d5fb8cef7c31
Child:
1:c740bf7f5c59
initial, just show time

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 0:d5fb8cef7c31 32 void displayTime(){
jsa1969 0:d5fb8cef7c31 33 rtcDS3234.update();
jsa1969 0:d5fb8cef7c31 34 uBit.display.scroll(rtcDS3234.hour());
jsa1969 0:d5fb8cef7c31 35 uBit.display.scroll(":");
jsa1969 0:d5fb8cef7c31 36 uBit.display.scroll(rtcDS3234.minute());
jsa1969 0:d5fb8cef7c31 37 uBit.display.scroll(":");
jsa1969 0:d5fb8cef7c31 38 uBit.display.scroll(rtcDS3234.second());
jsa1969 0:d5fb8cef7c31 39 }
jsa1969 0:d5fb8cef7c31 40
jsa1969 0:d5fb8cef7c31 41 void onButtonB(MicroBitEvent e)
jsa1969 0:d5fb8cef7c31 42 {
jsa1969 0:d5fb8cef7c31 43 displayTime();
jsa1969 0:d5fb8cef7c31 44 }
jsa1969 0:d5fb8cef7c31 45
jsa1969 0:d5fb8cef7c31 46 void onButtonA(MicroBitEvent e)
jsa1969 0:d5fb8cef7c31 47 {
jsa1969 0:d5fb8cef7c31 48 while (true) {
jsa1969 0:d5fb8cef7c31 49 displayTime();
jsa1969 0:d5fb8cef7c31 50 uBit.sleep(60000);
jsa1969 0:d5fb8cef7c31 51 }
jsa1969 0:d5fb8cef7c31 52 }
jsa1969 0:d5fb8cef7c31 53
jsa1969 0:d5fb8cef7c31 54 int main()
jsa1969 0:d5fb8cef7c31 55 {
jsa1969 0:d5fb8cef7c31 56 // Initialise the micro:bit runtime.
jsa1969 0:d5fb8cef7c31 57 uBit.init();
jsa1969 0:d5fb8cef7c31 58
jsa1969 0:d5fb8cef7c31 59 // Insert your code here!
jsa1969 0:d5fb8cef7c31 60 uBit.display.scroll("init");
jsa1969 0:d5fb8cef7c31 61
jsa1969 0:d5fb8cef7c31 62 rtcDS3234.begin(&uBit.io.P16, &uBit);
jsa1969 0:d5fb8cef7c31 63 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
jsa1969 0:d5fb8cef7c31 64 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
jsa1969 0:d5fb8cef7c31 65 uBit.display.scroll("rdy");
jsa1969 0:d5fb8cef7c31 66
jsa1969 0:d5fb8cef7c31 67 // If main exits, there may still be other fibers running or registered event handlers etc.
jsa1969 0:d5fb8cef7c31 68 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
jsa1969 0:d5fb8cef7c31 69 // sit in the idle task forever, in a power efficient sleep.
jsa1969 0:d5fb8cef7c31 70 release_fiber();
jsa1969 0:d5fb8cef7c31 71 }
jsa1969 0:d5fb8cef7c31 72