Skip to content

Instantly share code, notes, and snippets.

@dvsseed
Created January 4, 2018 07:07
Show Gist options
  • Save dvsseed/c8c9800ba92cb9bc74f95b0e729f52ff to your computer and use it in GitHub Desktop.
Save dvsseed/c8c9800ba92cb9bc74f95b0e729f52ff to your computer and use it in GitHub Desktop.
DS3231 RTC Demo Code on Mbed micro:bit platform
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
DS3231 libs
* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************
Copyright David Wright 2016. DAW9000.
Feel free to copy, ammend , or do what you will but a shout out would be nice :)
*/
/* This program uses the Maxim Integrated DS3231 library to create a BBC MicroBit
Clock. Requires a DS3231 Real Time clock connected to pin 19(SCL) and 20(SDA).
Recommended that an edge connector is used for DS3231 connection.
Program will display :-
Date then Time then Temp (from DS3231 not MicroBit as it seems for
accurate as to ambient temp.)
Pressing Button B and keeping it pressed between rotations ie at the end
of each display cycle will change mode cyclically.
1. Date, Time, Temp
2. Date only.
3. Time only.
4. Temp only.
5. Time and Temp
I am sure better things can be done here! Improvements welcome.
Pressing Buttons A and B together and keeping pressed as above will take
you into clock setting mode.
Clock is set :-
Year, Month, Day, Hour, Minute, Second, Day of Week (1 to 7 eg Sun=7 but you can
make any day the start of week ie Sun=1).
Setting is done after DATE and again after TIME. So if only setting date
you can abort after the msg "Date Set".
If in setting mode abort changes by powering off at any time.
This again can be improved .... not the easiest to set date and time with
no keypad and limited screen!
DS3231 has 2 alarms, so alarm clock feature with speaker/music attached to
MicroBit is a good future mod.
Modified by Davis, 2018/1/4
*/
#include "MicroBit.h"
#include "ds3231.h"
MicroBit uBit;
int iss, imm, ihh, iDD, iMM, iYY;
ManagedString ss, mm, hh, dOw, DD, MM, YY, txtMonth, txtDay, tempOut;
ManagedString tsep = ":";
ManagedString spc = "-";
Ds3231 mRTC(I2C_SDA0, I2C_SCL0);
uint16_t rtn;
uint16_t rval;
ds3231_time_t mTime = {0, 0, 0, false, false}; // 24 hr mode - seconds, minutes, hours, am_pm(pm=1, am=0), mode(0=24, 1=12hr)
ds3231_calendar_t mCalendar; // day(day of week), date(day), month, year
// Seconds and am1 not used for alarm2
uint16_t mTemp; // raw temp data
char * monthn[12] = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
int displayMode = 0;
// predeclare functions
void getTimeAndDate();
void setDateTimeByUser(int uYear, int uMonth, int uDay, int uHour, int uMinute, int uSecond);
void displayDate();
void displayTime();
void displayTemp();
void getTimeAndDate()
{
// get date and time and populate data vars
mTime.mode = false; // 24 hour
rval = mRTC.get_time(&mTime); // get time
mTemp = mRTC.get_temperature(); // get temperature
rtn = mRTC.get_calendar(&mCalendar); // get date
YY = (int)(mCalendar.year + 2000); // 4 digit
txtMonth = monthn[((int)mCalendar.month) - 1];
iss = (int)mTime.seconds;
imm = (int)mTime.minutes;
ihh = (int)mTime.hours;
iDD = (int)mCalendar.date;
iMM = (int)mCalendar.month;
iYY = (int)mCalendar.year;
ManagedString s1 (iss / 10);
ManagedString s2 (iss % 10);
ss = s1 + s2;
ManagedString m1 (imm / 10);
ManagedString m2 (imm % 10);
mm = m1 + m2;
ManagedString h1 (ihh / 10);
ManagedString h2 (ihh % 10);
hh = h1 + h2;
ManagedString D1 (iDD / 10);
ManagedString D2 (iDD % 10);
DD = D1 + D2;
ManagedString M1 (iMM / 10);
ManagedString M2 (iMM % 10);
// lets setup the Temperature here too
mTemp = mTemp / 256;
ManagedString tmp (mTemp);
tempOut = (tmp + " degC");
}
void setDateTimeByUser(int uYear, int uMonth, int uDay, int uHour, int uMinute, int uSecond)
{
int rVal;
bool setMode, setAM_PM;
getTimeAndDate(); // get latest
// set date
mCalendar.year = uYear;
mCalendar.month = uMonth;
mCalendar.date = uDay;
rVal = mRTC.set_calendar(mCalendar);
mTime.hours = uHour;
mTime.minutes = uMinute;
mTime.seconds = uSecond;
setMode = false; // 24 hour clock only
mTime.mode = setMode;
mTime.am_pm = setAM_PM;
rVal = mRTC.set_time(mTime);
}
void displayDate()
{
ManagedString dateOut (YY + spc + txtMonth + spc + DD);
uBit.display.scroll(dateOut);
uBit.sleep(1000);
}
void displayTime()
{
ManagedString timeOut (hh + tsep + mm + tsep + ss);
uBit.display.scroll(timeOut);
uBit.sleep(1000);
}
void displayTemp()
{
uBit.display.scroll(tempOut);
uBit.sleep(1000);
}
int main(void)
{
// Initialise the micro:bit runtime.
uBit.init();
// Must be setup by User
setDateTimeByUser(18, 1, 4, 14, 53, 50); // ex: 2018-1-4 14:53:50
while (true) {
getTimeAndDate();
uBit.sleep(100);
if (uBit.buttonAB.isPressed()) {
displayTemp();
}
else if (uBit.buttonB.isPressed()) {
displayTime();
}
else if (uBit.buttonA.isPressed()) {
displayDate();
}
}
// forever, in a power efficient sleep.
release_fiber();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment