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 SparkFunDS3234RTC.h
jsa1969 0:d5fb8cef7c31 3 Jim Lindblom @ SparkFun Electronics
jsa1969 0:d5fb8cef7c31 4 original creation date: October 2, 2016
jsa1969 0:d5fb8cef7c31 5 https://github.com/sparkfun/SparkFun_DS3234_RTC_Arduino_Library
jsa1969 0:d5fb8cef7c31 6
jsa1969 0:d5fb8cef7c31 7 Prototypes the DS3234 class. Defines sketch-usable global variables.
jsa1969 0:d5fb8cef7c31 8
jsa1969 0:d5fb8cef7c31 9 Resources:
jsa1969 0:d5fb8cef7c31 10 Wire.h - Arduino I2C Library
jsa1969 0:d5fb8cef7c31 11
jsa1969 0:d5fb8cef7c31 12 Development environment specifics:
jsa1969 0:d5fb8cef7c31 13 Arduino 1.6.8
jsa1969 0:d5fb8cef7c31 14 SparkFun RedBoard
jsa1969 0:d5fb8cef7c31 15 SparkFun Real Time Clock Module (v14)
jsa1969 0:d5fb8cef7c31 16
jsa1969 0:d5fb8cef7c31 17 Updated 16 October 2016 by Vassilis Serasidis <avrsite@yahoo.gr>
jsa1969 0:d5fb8cef7c31 18 - Added readFromSRAM' and 'writeToSRAM' functions
jsa1969 0:d5fb8cef7c31 19
jsa1969 0:d5fb8cef7c31 20 ******************************************************************************/
jsa1969 0:d5fb8cef7c31 21 //#include <Arduino.h>
jsa1969 0:d5fb8cef7c31 22 #include "mbed.h"
jsa1969 0:d5fb8cef7c31 23 #include "MicroBit.h"
jsa1969 0:d5fb8cef7c31 24
jsa1969 0:d5fb8cef7c31 25 #ifndef SPARKFUNDS3234RTC_H
jsa1969 0:d5fb8cef7c31 26 #define SPARKFUNDS3234RTC_H
jsa1969 0:d5fb8cef7c31 27
jsa1969 0:d5fb8cef7c31 28 #define TWELVE_HOUR_MODE (1<<6) // 12/24-hour Mode bit in Hour register
jsa1969 0:d5fb8cef7c31 29 #define TWELVE_HOUR_PM (1<<5) // am/pm bit in hour register
jsa1969 0:d5fb8cef7c31 30
jsa1969 0:d5fb8cef7c31 31 #define AM false
jsa1969 0:d5fb8cef7c31 32 #define PM true
jsa1969 0:d5fb8cef7c31 33
jsa1969 0:d5fb8cef7c31 34 #define SQW_CONTROL_MASK 0xE3 // SQW bit(s) mask in control register
jsa1969 0:d5fb8cef7c31 35 #define SQW_ENABLE_BIT (1<<2) // SQW output enable bit in control register
jsa1969 0:d5fb8cef7c31 36
jsa1969 0:d5fb8cef7c31 37 #define ALARM_MODE_BIT (1<<7) // Alarm mode bit (A1M1, A1M2, etc) in alarm registers
jsa1969 0:d5fb8cef7c31 38 #define ALARM_DAY_BIT (1<<6) // Alarm day/date control bit in alarm day registers
jsa1969 0:d5fb8cef7c31 39 #define ALARM_1_FLAG_BIT (1<<0) // Alarm 1 flag in control/status register
jsa1969 0:d5fb8cef7c31 40 #define ALARM_2_FLAG_BIT (1<<1) // Alarm 2 flag in control/status register
jsa1969 0:d5fb8cef7c31 41 #define ALARM_INTCN_BIT (1<<2) // Interrupt-enable bit in control register
jsa1969 0:d5fb8cef7c31 42
jsa1969 0:d5fb8cef7c31 43 #define TIME_ARRAY_LENGTH 7 // Total number of writable time values in device
jsa1969 0:d5fb8cef7c31 44 enum time_order {
jsa1969 0:d5fb8cef7c31 45 TIME_SECONDS, // 0
jsa1969 0:d5fb8cef7c31 46 TIME_MINUTES, // 1
jsa1969 0:d5fb8cef7c31 47 TIME_HOURS, // 2
jsa1969 0:d5fb8cef7c31 48 TIME_DAY, // 3
jsa1969 0:d5fb8cef7c31 49 TIME_DATE, // 4
jsa1969 0:d5fb8cef7c31 50 TIME_MONTH, // 5
jsa1969 0:d5fb8cef7c31 51 TIME_YEAR, // 6
jsa1969 0:d5fb8cef7c31 52 };
jsa1969 0:d5fb8cef7c31 53
jsa1969 0:d5fb8cef7c31 54 // sqw_rate -- enum for possible SQW pin output settings
jsa1969 0:d5fb8cef7c31 55 enum sqw_rate {
jsa1969 0:d5fb8cef7c31 56 SQW_SQUARE_1, // 0
jsa1969 0:d5fb8cef7c31 57 SQW_SQUARE_1K, // 1
jsa1969 0:d5fb8cef7c31 58 SQW_SQUARE_4K, // 2
jsa1969 0:d5fb8cef7c31 59 SQW_SQUARE_8K // 3
jsa1969 0:d5fb8cef7c31 60 };
jsa1969 0:d5fb8cef7c31 61
jsa1969 0:d5fb8cef7c31 62 // DS3234_registers -- Definition of DS3234 registers
jsa1969 0:d5fb8cef7c31 63 enum DS3234_registers {
jsa1969 0:d5fb8cef7c31 64 DS3234_REGISTER_SECONDS, // 0x00
jsa1969 0:d5fb8cef7c31 65 DS3234_REGISTER_MINUTES, // 0x01
jsa1969 0:d5fb8cef7c31 66 DS3234_REGISTER_HOURS, // 0x02
jsa1969 0:d5fb8cef7c31 67 DS3234_REGISTER_DAY, // 0x03
jsa1969 0:d5fb8cef7c31 68 DS3234_REGISTER_DATE, // 0x04
jsa1969 0:d5fb8cef7c31 69 DS3234_REGISTER_MONTH, // 0x05
jsa1969 0:d5fb8cef7c31 70 DS3234_REGISTER_YEAR, // 0x06
jsa1969 0:d5fb8cef7c31 71 DS3234_REGISTER_A1SEC, // 0x07
jsa1969 0:d5fb8cef7c31 72 DS3234_REGISTER_A1MIN, // 0x08
jsa1969 0:d5fb8cef7c31 73 DS3234_REGISTER_A1HR, // 0x09
jsa1969 0:d5fb8cef7c31 74 DS3234_REGISTER_A1DA, // 0x0A
jsa1969 0:d5fb8cef7c31 75 DS3234_REGISTER_A2MIN, // 0x0B
jsa1969 0:d5fb8cef7c31 76 DS3234_REGISTER_A2HR, // 0x0C
jsa1969 0:d5fb8cef7c31 77 DS3234_REGISTER_A2DA, // 0x0D
jsa1969 0:d5fb8cef7c31 78 DS3234_REGISTER_CONTROL, // 0x0E
jsa1969 0:d5fb8cef7c31 79 DS3234_REGISTER_STATUS, // 0x0F
jsa1969 0:d5fb8cef7c31 80 DS3234_REGISTER_XTAL, // 0x10
jsa1969 0:d5fb8cef7c31 81 DS3234_REGISTER_TEMPM, // 0x11
jsa1969 0:d5fb8cef7c31 82 DS3234_REGISTER_TEMPL, // 0x12
jsa1969 0:d5fb8cef7c31 83 DS3234_REGISTER_TEMPEN, // 0x13
jsa1969 0:d5fb8cef7c31 84 DS3234_REGISTER_RESERV1, // 0x14
jsa1969 0:d5fb8cef7c31 85 DS3234_REGISTER_RESERV2, // 0x15
jsa1969 0:d5fb8cef7c31 86 DS3234_REGISTER_RESERV3, // 0x16
jsa1969 0:d5fb8cef7c31 87 DS3234_REGISTER_RESERV4, // 0x17
jsa1969 0:d5fb8cef7c31 88 DS3234_REGISTER_SRAMA, // 0x18
jsa1969 0:d5fb8cef7c31 89 DS3234_REGISTER_SRAMD // 0x19
jsa1969 0:d5fb8cef7c31 90 };
jsa1969 0:d5fb8cef7c31 91
jsa1969 0:d5fb8cef7c31 92 // Base register for complete time/date readings
jsa1969 0:d5fb8cef7c31 93 #define DS3234_REGISTER_BASE DS3234_REGISTER_SECONDS
jsa1969 0:d5fb8cef7c31 94
jsa1969 0:d5fb8cef7c31 95 // dayIntToStr -- convert day integer to the string
jsa1969 0:d5fb8cef7c31 96 static const char *dayIntToStr[7] {
jsa1969 0:d5fb8cef7c31 97 "Sunday",
jsa1969 0:d5fb8cef7c31 98 "Monday",
jsa1969 0:d5fb8cef7c31 99 "Tuesday",
jsa1969 0:d5fb8cef7c31 100 "Wednesday",
jsa1969 0:d5fb8cef7c31 101 "Thursday",
jsa1969 0:d5fb8cef7c31 102 "Friday",
jsa1969 0:d5fb8cef7c31 103 "Saturday"
jsa1969 0:d5fb8cef7c31 104 };
jsa1969 0:d5fb8cef7c31 105
jsa1969 0:d5fb8cef7c31 106 // dayIntToChar -- convert day integer to character
jsa1969 0:d5fb8cef7c31 107 static const char dayIntToChar[7] = {'U', 'M', 'T', 'W', 'R', 'F', 'S' };
jsa1969 0:d5fb8cef7c31 108
jsa1969 0:d5fb8cef7c31 109 class DS3234
jsa1969 0:d5fb8cef7c31 110 {
jsa1969 0:d5fb8cef7c31 111 public:
jsa1969 0:d5fb8cef7c31 112
jsa1969 0:d5fb8cef7c31 113 ////////////////////
jsa1969 0:d5fb8cef7c31 114 // Initialization //
jsa1969 0:d5fb8cef7c31 115 ////////////////////
jsa1969 0:d5fb8cef7c31 116 // Constructor -- Initialize class variables to 0
jsa1969 0:d5fb8cef7c31 117 DS3234();
jsa1969 0:d5fb8cef7c31 118 // Begin -- Initialize SPI interface, and sets up the chip-select pin
jsa1969 0:d5fb8cef7c31 119 void begin(MicroBitPin* csPin, MicroBit* uBit);
jsa1969 0:d5fb8cef7c31 120
jsa1969 0:d5fb8cef7c31 121 ///////////////////////
jsa1969 0:d5fb8cef7c31 122 // Setting the Clock //
jsa1969 0:d5fb8cef7c31 123 ///////////////////////
jsa1969 0:d5fb8cef7c31 124 // setTime -- Set time and date/day registers of DS3234
jsa1969 0:d5fb8cef7c31 125 void setTime(uint8_t sec, uint8_t min, uint8_t hour,
jsa1969 0:d5fb8cef7c31 126 uint8_t day, uint8_t date, uint8_t month, uint8_t year);
jsa1969 0:d5fb8cef7c31 127 void setTime(uint8_t sec, uint8_t min, uint8_t hour12, bool pm,
jsa1969 0:d5fb8cef7c31 128 uint8_t day, uint8_t date, uint8_t month, uint8_t year);
jsa1969 0:d5fb8cef7c31 129 // setTime -- Set time and date/day registers of DS3234 (using data array)
jsa1969 0:d5fb8cef7c31 130 void setTime(uint8_t * time, uint8_t len);
jsa1969 0:d5fb8cef7c31 131 // autoTime -- Set time with compiler time/date
jsa1969 0:d5fb8cef7c31 132 bool autoTime();
jsa1969 0:d5fb8cef7c31 133
jsa1969 0:d5fb8cef7c31 134 // To set specific values of the clock, use the set____ functions:
jsa1969 0:d5fb8cef7c31 135 void setSecond(uint8_t s);
jsa1969 0:d5fb8cef7c31 136 void setMinute(uint8_t m);
jsa1969 0:d5fb8cef7c31 137 void setHour(uint8_t h);
jsa1969 0:d5fb8cef7c31 138 void setDay(uint8_t d);
jsa1969 0:d5fb8cef7c31 139 void setDate(uint8_t d);
jsa1969 0:d5fb8cef7c31 140 void setMonth(uint8_t mo);
jsa1969 0:d5fb8cef7c31 141 void setYear(uint8_t y);
jsa1969 0:d5fb8cef7c31 142
jsa1969 0:d5fb8cef7c31 143 ///////////////////////
jsa1969 0:d5fb8cef7c31 144 // Reading the Clock //
jsa1969 0:d5fb8cef7c31 145 ///////////////////////
jsa1969 0:d5fb8cef7c31 146 // update -- Read all time/date registers and update the _time array
jsa1969 0:d5fb8cef7c31 147 void update(void);
jsa1969 0:d5fb8cef7c31 148 // update should be performed before any of the following. It will update
jsa1969 0:d5fb8cef7c31 149 // all values at one time.
jsa1969 0:d5fb8cef7c31 150 inline uint8_t second(void) { return BCDtoDEC(_time[TIME_SECONDS]); };
jsa1969 0:d5fb8cef7c31 151 inline uint8_t minute(void) { return BCDtoDEC(_time[TIME_MINUTES]); };
jsa1969 0:d5fb8cef7c31 152 inline uint8_t hour(void) { return BCDtoDEC(_time[TIME_HOURS]); };
jsa1969 0:d5fb8cef7c31 153 inline uint8_t day(void) { return BCDtoDEC(_time[TIME_DAY]); };
jsa1969 0:d5fb8cef7c31 154 inline const char dayChar(void) { return dayIntToChar[BCDtoDEC(_time[TIME_DAY]) - 1]; };
jsa1969 0:d5fb8cef7c31 155 inline const char * dayStr(void) { return dayIntToStr[BCDtoDEC(_time[TIME_DAY]) - 1]; };
jsa1969 0:d5fb8cef7c31 156 inline uint8_t date(void) { return BCDtoDEC(_time[TIME_DATE]); };
jsa1969 0:d5fb8cef7c31 157 inline uint8_t month(void) { return BCDtoDEC(_time[TIME_MONTH]); };
jsa1969 0:d5fb8cef7c31 158 inline uint8_t year(void) { return BCDtoDEC(_time[TIME_YEAR]); };
jsa1969 0:d5fb8cef7c31 159
jsa1969 0:d5fb8cef7c31 160 // To read a single value at a time, use the get___ functions:
jsa1969 0:d5fb8cef7c31 161 uint8_t getSecond(void);
jsa1969 0:d5fb8cef7c31 162 uint8_t getMinute(void);
jsa1969 0:d5fb8cef7c31 163 uint8_t getHour(void);
jsa1969 0:d5fb8cef7c31 164 uint8_t getDay(void);
jsa1969 0:d5fb8cef7c31 165 uint8_t getDate(void);
jsa1969 0:d5fb8cef7c31 166 uint8_t getMonth(void);
jsa1969 0:d5fb8cef7c31 167 uint8_t getYear(void);
jsa1969 0:d5fb8cef7c31 168
jsa1969 0:d5fb8cef7c31 169 // is12Hour -- check if the DS3234 is in 12-hour mode | returns true if 12-hour mode
jsa1969 0:d5fb8cef7c31 170 bool is12Hour(void);
jsa1969 0:d5fb8cef7c31 171 // pm -- Check if 12-hour state is AM or PM | returns true if PM
jsa1969 0:d5fb8cef7c31 172 bool pm(void);
jsa1969 0:d5fb8cef7c31 173
jsa1969 0:d5fb8cef7c31 174 // DS3234 has a die-temperature reading. Value is produced in multiples of 0.25 deg C
jsa1969 0:d5fb8cef7c31 175 float temperature(void);
jsa1969 0:d5fb8cef7c31 176
jsa1969 0:d5fb8cef7c31 177 /////////////////////////////
jsa1969 0:d5fb8cef7c31 178 // Alarm Setting Functions //
jsa1969 0:d5fb8cef7c31 179 /////////////////////////////
jsa1969 0:d5fb8cef7c31 180 // Alarm 1 can be set to trigger on seconds, minutes, hours, and/or date/day.
jsa1969 0:d5fb8cef7c31 181 // Any of those can be masked out -- ignored for an alarm match. By setting
jsa1969 0:d5fb8cef7c31 182 // If a value is set to 255, the library will mask out that data.
jsa1969 0:d5fb8cef7c31 183 // By default the "date" value is the day-of-month (1-31)
jsa1969 0:d5fb8cef7c31 184 // The "day" boolean changes the "date" value to a day (between 1-7).
jsa1969 0:d5fb8cef7c31 185 void setAlarm1(uint8_t second = 255, uint8_t minute = 255, uint8_t hour = 255, uint8_t date = 255, bool day = false);
jsa1969 0:d5fb8cef7c31 186 // Second alarm 1 set function if the clock is in 12-hour mode:
jsa1969 0:d5fb8cef7c31 187 void setAlarm1(uint8_t second, uint8_t minute, uint8_t hour12, bool pm, uint8_t date = 255, bool day = false);
jsa1969 0:d5fb8cef7c31 188 // Alarm 2 functions similarly to alarm 1, but does not have a seconds value:
jsa1969 0:d5fb8cef7c31 189 void setAlarm2(uint8_t minute = 255, uint8_t hour = 255, uint8_t date = 255, bool day = false);
jsa1969 0:d5fb8cef7c31 190 // Second alarm 2 set function if the clock is in 12-hour mode:
jsa1969 0:d5fb8cef7c31 191 void setAlarm2(uint8_t minute, uint8_t hour12, bool pm, uint8_t date = 255, bool day = false);
jsa1969 0:d5fb8cef7c31 192
jsa1969 0:d5fb8cef7c31 193 // The SQW pin can be used as an interrupt. It is active-low, and can be
jsa1969 0:d5fb8cef7c31 194 // set to trigger on alarm 1 and/or alarm 2:
jsa1969 0:d5fb8cef7c31 195 void enableAlarmInterrupt(bool alarm1 = true, bool alarm2 = true);
jsa1969 0:d5fb8cef7c31 196
jsa1969 0:d5fb8cef7c31 197 // alarm1 and alarm2 check their respective flag in the control/status
jsa1969 0:d5fb8cef7c31 198 // register. They return true is the flag is set.
jsa1969 0:d5fb8cef7c31 199 // The "clear" boolean, commands the alarm function to clear the flag
jsa1969 0:d5fb8cef7c31 200 // (assuming it was set).
jsa1969 0:d5fb8cef7c31 201 bool alarm1(bool clear = true);
jsa1969 0:d5fb8cef7c31 202 bool alarm2(bool clear = true);
jsa1969 0:d5fb8cef7c31 203
jsa1969 0:d5fb8cef7c31 204 ///////////////////////////////
jsa1969 0:d5fb8cef7c31 205 // SQW Pin Control Functions //
jsa1969 0:d5fb8cef7c31 206 ///////////////////////////////
jsa1969 0:d5fb8cef7c31 207 void writeSQW(sqw_rate value); // Write SQW pin high, low, or to a set rate
jsa1969 0:d5fb8cef7c31 208
jsa1969 0:d5fb8cef7c31 209 /////////////////////////////
jsa1969 0:d5fb8cef7c31 210 // Misc. Control Functions //
jsa1969 0:d5fb8cef7c31 211 /////////////////////////////
jsa1969 0:d5fb8cef7c31 212 void enable(void); // Enable the oscillator
jsa1969 0:d5fb8cef7c31 213 void disable(void); // Disable the oscillator (no counting!)
jsa1969 0:d5fb8cef7c31 214
jsa1969 0:d5fb8cef7c31 215 void set12Hour(bool enable12 = true); // Enable/disable 12-hour mode
jsa1969 0:d5fb8cef7c31 216 void set24Hour(bool enable24 = true); // Enable/disable 24-hour mode
jsa1969 0:d5fb8cef7c31 217
jsa1969 0:d5fb8cef7c31 218 void writeToSRAM(uint8_t address, uint8_t data);
jsa1969 0:d5fb8cef7c31 219 uint8_t readFromSRAM(uint8_t address);
jsa1969 0:d5fb8cef7c31 220
jsa1969 0:d5fb8cef7c31 221 private:
jsa1969 0:d5fb8cef7c31 222 MicroBitPin* _csPin;
jsa1969 0:d5fb8cef7c31 223 MicroBit* _uBit;
jsa1969 0:d5fb8cef7c31 224 uint8_t _time[TIME_ARRAY_LENGTH];
jsa1969 0:d5fb8cef7c31 225 bool _pm;
jsa1969 0:d5fb8cef7c31 226
jsa1969 0:d5fb8cef7c31 227 uint8_t BCDtoDEC(uint8_t val);
jsa1969 0:d5fb8cef7c31 228 uint8_t DECtoBCD(uint8_t val);
jsa1969 0:d5fb8cef7c31 229
jsa1969 0:d5fb8cef7c31 230 void spiWriteBytes(DS3234_registers reg, uint8_t * values, uint8_t len);
jsa1969 0:d5fb8cef7c31 231 void spiWriteByte(DS3234_registers reg, uint8_t value);
jsa1969 0:d5fb8cef7c31 232 uint8_t spiReadByte(DS3234_registers reg);
jsa1969 0:d5fb8cef7c31 233 void spiReadBytes(DS3234_registers reg, uint8_t * dest, uint8_t len);
jsa1969 0:d5fb8cef7c31 234 };
jsa1969 0:d5fb8cef7c31 235
jsa1969 0:d5fb8cef7c31 236 extern DS3234 rtc;
jsa1969 0:d5fb8cef7c31 237
jsa1969 0:d5fb8cef7c31 238 #endif // SPARKFUNDS3234RTC_H