j

Dependents:   LPC-SD-35

Fork of ds3231 by Maxim Integrated

Committer:
dexterg
Date:
Tue Sep 29 21:29:28 2015 +0000
Revision:
15:9670e0a3aeec
Parent:
12:b9f13fd8c1b6
Data logger with SD and onboard RTC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:b00c4699ae6f 1 /******************************************************************//**
j3 0:b00c4699ae6f 2 * @file ds3231.cpp
j3 0:b00c4699ae6f 3 *
j3 0:b00c4699ae6f 4 * @author Justin Jordan
j3 0:b00c4699ae6f 5 *
j3 11:1654fcc0a5ea 6 * @version 1.0
j3 0:b00c4699ae6f 7 *
j3 0:b00c4699ae6f 8 * Started: 11NOV14
j3 0:b00c4699ae6f 9 *
j3 0:b00c4699ae6f 10 * Updated:
j3 0:b00c4699ae6f 11 *
j3 0:b00c4699ae6f 12 * @brief Source file for DS3231 class
j3 0:b00c4699ae6f 13 *
j3 0:b00c4699ae6f 14 ***********************************************************************
j3 0:b00c4699ae6f 15 *
j3 0:b00c4699ae6f 16 * @copyright
j3 12:b9f13fd8c1b6 17 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:b00c4699ae6f 18 *
j3 0:b00c4699ae6f 19 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:b00c4699ae6f 20 * copy of this software and associated documentation files (the "Software"),
j3 0:b00c4699ae6f 21 * to deal in the Software without restriction, including without limitation
j3 0:b00c4699ae6f 22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:b00c4699ae6f 23 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:b00c4699ae6f 24 * Software is furnished to do so, subject to the following conditions:
j3 0:b00c4699ae6f 25 *
j3 0:b00c4699ae6f 26 * The above copyright notice and this permission notice shall be included
j3 0:b00c4699ae6f 27 * in all copies or substantial portions of the Software.
j3 0:b00c4699ae6f 28 *
j3 0:b00c4699ae6f 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:b00c4699ae6f 30 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:b00c4699ae6f 31 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:b00c4699ae6f 32 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:b00c4699ae6f 33 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:b00c4699ae6f 34 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:b00c4699ae6f 35 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:b00c4699ae6f 36 *
j3 0:b00c4699ae6f 37 * Except as contained in this notice, the name of Maxim Integrated
j3 0:b00c4699ae6f 38 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:b00c4699ae6f 39 * Products, Inc. Branding Policy.
j3 0:b00c4699ae6f 40 *
j3 0:b00c4699ae6f 41 * The mere transfer of this software does not imply any licenses
j3 0:b00c4699ae6f 42 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:b00c4699ae6f 43 * trademarks, maskwork rights, or any other form of intellectual
j3 0:b00c4699ae6f 44 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:b00c4699ae6f 45 * ownership rights.
j3 0:b00c4699ae6f 46 **********************************************************************/
j3 0:b00c4699ae6f 47
j3 0:b00c4699ae6f 48
j3 0:b00c4699ae6f 49 #include "ds3231.h"
j3 0:b00c4699ae6f 50
j3 0:b00c4699ae6f 51
j3 0:b00c4699ae6f 52 /**********************************************************//**
j3 0:b00c4699ae6f 53 * Constructor for Ds3231 Class
j3 0:b00c4699ae6f 54 *
j3 0:b00c4699ae6f 55 * On Entry:
j3 1:c814af60fdbf 56 * @param[in] sda - sda pin of I2C bus
j3 1:c814af60fdbf 57 * @param[in] scl - scl pin of I2C bus
j3 0:b00c4699ae6f 58 *
j3 0:b00c4699ae6f 59 * On Exit:
j3 0:b00c4699ae6f 60 * @return none
j3 0:b00c4699ae6f 61 *
j3 0:b00c4699ae6f 62 * Example:
j3 0:b00c4699ae6f 63 * @code
j3 0:b00c4699ae6f 64 *
j3 0:b00c4699ae6f 65 * //instantiate rtc object
j3 1:c814af60fdbf 66 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 67 *
j3 0:b00c4699ae6f 68 * @endcode
j3 0:b00c4699ae6f 69 **************************************************************/
j3 1:c814af60fdbf 70 Ds3231::Ds3231(PinName sda, PinName scl) : I2C(sda, scl)
j3 0:b00c4699ae6f 71 {
j3 0:b00c4699ae6f 72 w_adrs = ((DS3231_I2C_ADRS << 1) | I2C_WRITE);
j3 0:b00c4699ae6f 73 r_adrs = ((DS3231_I2C_ADRS << 1) | I2C_READ);
j3 0:b00c4699ae6f 74 }
j3 0:b00c4699ae6f 75
j3 0:b00c4699ae6f 76
j3 0:b00c4699ae6f 77 /**********************************************************//**
j3 0:b00c4699ae6f 78 * Sets the time on DS3231
j3 2:4e6e761c60f2 79 * Struct data is in integrer format, not BCD. Fx will convert
j3 2:4e6e761c60f2 80 * to BCD for you.
j3 0:b00c4699ae6f 81 *
j3 0:b00c4699ae6f 82 * On Entry:
j3 0:b00c4699ae6f 83 * @param[in] time - struct cotaining time data;
j3 0:b00c4699ae6f 84 *
j3 0:b00c4699ae6f 85 * On Exit:
j3 0:b00c4699ae6f 86 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 87 *
j3 0:b00c4699ae6f 88 * Example:
j3 0:b00c4699ae6f 89 * @code
j3 0:b00c4699ae6f 90 *
j3 0:b00c4699ae6f 91 * //instantiate rtc object
j3 1:c814af60fdbf 92 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 93 *
j3 4:0beb5e9ac927 94 * //time = 12:00:00 AM 12hr mode
j3 4:0beb5e9ac927 95 * ds3231_time_t time = {12, 0, 0, 0, 1}
j3 0:b00c4699ae6f 96 * uint16_t rtn_val;
j3 0:b00c4699ae6f 97 *
j3 0:b00c4699ae6f 98 * rtn_val = rtc.set_time(time);
j3 0:b00c4699ae6f 99 *
j3 0:b00c4699ae6f 100 * @endcode
j3 0:b00c4699ae6f 101 **************************************************************/
j3 0:b00c4699ae6f 102 uint16_t Ds3231::set_time(ds3231_time_t time)
j3 0:b00c4699ae6f 103 {
j3 2:4e6e761c60f2 104 uint8_t data[] = {0,0,0,0};
j3 2:4e6e761c60f2 105 uint8_t data_length = 0;
j3 2:4e6e761c60f2 106 uint8_t max_hour = 24;
j3 0:b00c4699ae6f 107
j3 2:4e6e761c60f2 108 data[data_length++] = SECONDS;
j3 2:4e6e761c60f2 109 data[data_length++] = uchar_2_bcd(time.seconds);
j3 2:4e6e761c60f2 110 data[data_length++] = uchar_2_bcd(time.minutes);
j3 2:4e6e761c60f2 111
j3 2:4e6e761c60f2 112 //format Hours register
j3 2:4e6e761c60f2 113 data[data_length] = uchar_2_bcd(time.hours);
j3 2:4e6e761c60f2 114 if(time.mode)
j3 2:4e6e761c60f2 115 {
j3 2:4e6e761c60f2 116 max_hour = max_hour/2;
j3 2:4e6e761c60f2 117
j3 2:4e6e761c60f2 118 data[data_length] |= MODE;
j3 2:4e6e761c60f2 119 if(time.am_pm)
j3 2:4e6e761c60f2 120 {
j3 2:4e6e761c60f2 121 data[data_length] |= AM_PM;
j3 2:4e6e761c60f2 122 }
j3 2:4e6e761c60f2 123
j3 2:4e6e761c60f2 124 }
j3 2:4e6e761c60f2 125 else
j3 2:4e6e761c60f2 126 {
j3 2:4e6e761c60f2 127 max_hour = max_hour - 1;
j3 2:4e6e761c60f2 128 }
j3 2:4e6e761c60f2 129 data_length++;
j3 2:4e6e761c60f2 130
j3 7:0551d7e7c13f 131 //Make sure data is within range.
j3 2:4e6e761c60f2 132 if((time.seconds > 59) || (time.minutes > 59) || (time.hours > max_hour))
j3 2:4e6e761c60f2 133 {
j3 2:4e6e761c60f2 134 return(1);
j3 2:4e6e761c60f2 135 }
j3 2:4e6e761c60f2 136 else
j3 2:4e6e761c60f2 137 {
j3 2:4e6e761c60f2 138 return(write(w_adrs,(const char*) data, data_length));
j3 2:4e6e761c60f2 139 }
j3 0:b00c4699ae6f 140 }
j3 0:b00c4699ae6f 141
j3 0:b00c4699ae6f 142
j3 0:b00c4699ae6f 143 /**********************************************************//**
j3 0:b00c4699ae6f 144 * Sets the calendar on DS3231
j3 0:b00c4699ae6f 145 *
j3 0:b00c4699ae6f 146 * On Entry:
j3 3:312589d8185c 147 * @param[in] calendar - struct cotaining calendar data
j3 0:b00c4699ae6f 148 *
j3 0:b00c4699ae6f 149 * On Exit:
j3 0:b00c4699ae6f 150 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 151 *
j3 0:b00c4699ae6f 152 * Example:
j3 0:b00c4699ae6f 153 * @code
j3 0:b00c4699ae6f 154 *
j3 0:b00c4699ae6f 155 * //instantiate rtc object
j3 1:c814af60fdbf 156 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 157 *
j3 0:b00c4699ae6f 158 * //see datasheet for calendar format
j3 0:b00c4699ae6f 159 * ds3231_calendar_t calendar = {1, 1, 1, 0};
j3 0:b00c4699ae6f 160 * uint16_t rtn_val;
j3 0:b00c4699ae6f 161 *
j3 0:b00c4699ae6f 162 * rtn_val = rtc.set_calendar(calendar);
j3 0:b00c4699ae6f 163 *
j3 0:b00c4699ae6f 164 * @endcode
j3 0:b00c4699ae6f 165 **************************************************************/
j3 0:b00c4699ae6f 166 uint16_t Ds3231::set_calendar(ds3231_calendar_t calendar)
j3 0:b00c4699ae6f 167 {
j3 2:4e6e761c60f2 168 uint8_t data[] = {0,0,0,0,0};
j3 2:4e6e761c60f2 169 uint8_t data_length = 0;
j3 2:4e6e761c60f2 170
j3 2:4e6e761c60f2 171 data[data_length++] = DAY;
j3 2:4e6e761c60f2 172 data[data_length++] = uchar_2_bcd(calendar.day);
j3 2:4e6e761c60f2 173 data[data_length++] = uchar_2_bcd(calendar.date);
j3 2:4e6e761c60f2 174 data[data_length++] = uchar_2_bcd(calendar.month);
j3 2:4e6e761c60f2 175 data[data_length++] = uchar_2_bcd(calendar.year);
j3 0:b00c4699ae6f 176
j3 7:0551d7e7c13f 177 //Make sure data is within range.
j3 7:0551d7e7c13f 178 if(((calendar.day < 1) || (calendar.day > 7)) ||
j3 7:0551d7e7c13f 179 ((calendar.date < 1) || (calendar.date > 31)) ||
j3 7:0551d7e7c13f 180 ((calendar.month < 1) || (calendar.month > 12)) ||
j3 7:0551d7e7c13f 181 (calendar.year > 99))
j3 7:0551d7e7c13f 182 {
j3 7:0551d7e7c13f 183 return(1);
j3 7:0551d7e7c13f 184 }
j3 7:0551d7e7c13f 185 else
j3 7:0551d7e7c13f 186 {
j3 7:0551d7e7c13f 187 return(write(w_adrs,(const char*) data, data_length));
j3 7:0551d7e7c13f 188 }
j3 0:b00c4699ae6f 189 }
j3 0:b00c4699ae6f 190
j3 0:b00c4699ae6f 191
j3 0:b00c4699ae6f 192 /**********************************************************//**
j3 0:b00c4699ae6f 193 * Set either Alarm1 or Alarm2 of DS3231
j3 0:b00c4699ae6f 194 *
j3 0:b00c4699ae6f 195 * On Entry:
j3 0:b00c4699ae6f 196 * @param[in] alarm - struct cotaining alarm data
j3 3:312589d8185c 197 *
j3 0:b00c4699ae6f 198 * @param[in] one_r_two - TRUE for Alarm1 and FALSE for
j3 0:b00c4699ae6f 199 * Alarm2
j3 0:b00c4699ae6f 200 *
j3 0:b00c4699ae6f 201 * On Exit:
j3 0:b00c4699ae6f 202 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 203 *
j3 0:b00c4699ae6f 204 * Example:
j3 0:b00c4699ae6f 205 * @code
j3 0:b00c4699ae6f 206 *
j3 0:b00c4699ae6f 207 * //instantiate rtc object
j3 1:c814af60fdbf 208 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 209 *
j3 4:0beb5e9ac927 210 * //see ds3231.h for .members and datasheet for alarm format
j3 4:0beb5e9ac927 211 * ds3231_alrm_t alarm;
j3 0:b00c4699ae6f 212 * uint16_t rtn_val;
j3 0:b00c4699ae6f 213 *
j3 0:b00c4699ae6f 214 * rtn_val = rtc.set_alarm(alarm, FALSE);
j3 0:b00c4699ae6f 215 *
j3 0:b00c4699ae6f 216 * @endcode
j3 0:b00c4699ae6f 217 **************************************************************/
j3 0:b00c4699ae6f 218 uint16_t Ds3231::set_alarm(ds3231_alrm_t alarm, bool one_r_two)
j3 0:b00c4699ae6f 219 {
j3 2:4e6e761c60f2 220 uint8_t data[] = {0,0,0,0,0};
j3 2:4e6e761c60f2 221 uint8_t data_length = 0;
j3 2:4e6e761c60f2 222 uint8_t max_hour = 24;
j3 2:4e6e761c60f2 223 uint8_t mask_var = 0;
j3 2:4e6e761c60f2 224
j3 2:4e6e761c60f2 225 //setting alarm 1 or 2?
j3 2:4e6e761c60f2 226 if(one_r_two)
j3 2:4e6e761c60f2 227 {
j3 2:4e6e761c60f2 228 data[data_length++] = ALRM1_SECONDS;
j3 2:4e6e761c60f2 229
j3 2:4e6e761c60f2 230 //config seconds register
j3 2:4e6e761c60f2 231 if(alarm.am1)
j3 2:4e6e761c60f2 232 {
j3 2:4e6e761c60f2 233 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 234 }
j3 2:4e6e761c60f2 235 data[data_length++] = (mask_var | uchar_2_bcd(alarm.seconds));
j3 2:4e6e761c60f2 236 mask_var = 0;
j3 2:4e6e761c60f2 237
j3 2:4e6e761c60f2 238 //config minutes register
j3 2:4e6e761c60f2 239 if(alarm.am2)
j3 2:4e6e761c60f2 240 {
j3 2:4e6e761c60f2 241 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 242 }
j3 2:4e6e761c60f2 243 data[data_length++] = (mask_var | uchar_2_bcd(alarm.minutes));
j3 2:4e6e761c60f2 244 mask_var = 0;
j3 2:4e6e761c60f2 245
j3 2:4e6e761c60f2 246 //config hours register
j3 2:4e6e761c60f2 247 if(alarm.am3)
j3 2:4e6e761c60f2 248 {
j3 2:4e6e761c60f2 249 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 250 }
j3 2:4e6e761c60f2 251 if(alarm.mode)
j3 2:4e6e761c60f2 252 {
j3 2:4e6e761c60f2 253 max_hour = max_hour/2;
j3 2:4e6e761c60f2 254 mask_var |= MODE;
j3 2:4e6e761c60f2 255 if(alarm.am_pm)
j3 2:4e6e761c60f2 256 {
j3 2:4e6e761c60f2 257 mask_var |= AM_PM;
j3 2:4e6e761c60f2 258 }
j3 2:4e6e761c60f2 259 }
j3 2:4e6e761c60f2 260 else
j3 2:4e6e761c60f2 261 {
j3 2:4e6e761c60f2 262 max_hour = max_hour - 1;
j3 2:4e6e761c60f2 263 }
j3 2:4e6e761c60f2 264 data[data_length++] = (mask_var | uchar_2_bcd(alarm.hours));
j3 2:4e6e761c60f2 265 mask_var = 0;
j3 2:4e6e761c60f2 266
j3 2:4e6e761c60f2 267 //config day/date register
j3 2:4e6e761c60f2 268 if(alarm.am4)
j3 2:4e6e761c60f2 269 {
j3 2:4e6e761c60f2 270 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 271 }
j3 2:4e6e761c60f2 272 if(alarm.dy_dt)
j3 2:4e6e761c60f2 273 {
j3 2:4e6e761c60f2 274 mask_var |= DY_DT;
j3 2:4e6e761c60f2 275 data[data_length++] = (mask_var | uchar_2_bcd(alarm.day));
j3 2:4e6e761c60f2 276 }
j3 2:4e6e761c60f2 277 else
j3 2:4e6e761c60f2 278 {
j3 2:4e6e761c60f2 279 data[data_length++] = (mask_var | uchar_2_bcd(alarm.date));
j3 2:4e6e761c60f2 280 }
j3 2:4e6e761c60f2 281 mask_var = 0;
j3 2:4e6e761c60f2 282 }
j3 2:4e6e761c60f2 283 else
j3 2:4e6e761c60f2 284 {
j3 2:4e6e761c60f2 285 data[data_length++] = ALRM2_MINUTES;
j3 2:4e6e761c60f2 286
j3 2:4e6e761c60f2 287 //config minutes register
j3 2:4e6e761c60f2 288 if(alarm.am2)
j3 2:4e6e761c60f2 289 {
j3 2:4e6e761c60f2 290 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 291 }
j3 2:4e6e761c60f2 292 data[data_length++] = (mask_var | uchar_2_bcd(alarm.minutes));
j3 2:4e6e761c60f2 293 mask_var = 0;
j3 2:4e6e761c60f2 294
j3 2:4e6e761c60f2 295 //config hours register
j3 2:4e6e761c60f2 296 if(alarm.am3)
j3 2:4e6e761c60f2 297 {
j3 2:4e6e761c60f2 298 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 299 }
j3 2:4e6e761c60f2 300 if(alarm.mode)
j3 2:4e6e761c60f2 301 {
j3 2:4e6e761c60f2 302 max_hour = max_hour/2;
j3 2:4e6e761c60f2 303 mask_var |= MODE;
j3 2:4e6e761c60f2 304 if(alarm.am_pm)
j3 2:4e6e761c60f2 305 {
j3 2:4e6e761c60f2 306 mask_var |= AM_PM;
j3 2:4e6e761c60f2 307 }
j3 2:4e6e761c60f2 308 }
j3 2:4e6e761c60f2 309 else
j3 2:4e6e761c60f2 310 {
j3 2:4e6e761c60f2 311 max_hour = max_hour - 1;
j3 2:4e6e761c60f2 312 }
j3 2:4e6e761c60f2 313 data[data_length++] = (mask_var | uchar_2_bcd(alarm.hours));
j3 2:4e6e761c60f2 314 mask_var = 0;
j3 2:4e6e761c60f2 315
j3 2:4e6e761c60f2 316 //config day/date register
j3 2:4e6e761c60f2 317 if(alarm.am4)
j3 2:4e6e761c60f2 318 {
j3 2:4e6e761c60f2 319 mask_var |= ALRM_MASK;
j3 2:4e6e761c60f2 320 }
j3 2:4e6e761c60f2 321 if(alarm.dy_dt)
j3 2:4e6e761c60f2 322 {
j3 2:4e6e761c60f2 323 mask_var |= DY_DT;
j3 2:4e6e761c60f2 324 data[data_length++] = (mask_var | uchar_2_bcd(alarm.day));
j3 2:4e6e761c60f2 325 }
j3 2:4e6e761c60f2 326 else
j3 2:4e6e761c60f2 327 {
j3 2:4e6e761c60f2 328 data[data_length++] = (mask_var | uchar_2_bcd(alarm.date));
j3 2:4e6e761c60f2 329 }
j3 2:4e6e761c60f2 330 mask_var = 0;
j3 2:4e6e761c60f2 331 }
j3 2:4e6e761c60f2 332
j3 7:0551d7e7c13f 333 //Make sure data is within range.
j3 7:0551d7e7c13f 334 if((alarm.seconds > 59) || (alarm.minutes > 59) || (alarm.hours > max_hour) ||
j3 7:0551d7e7c13f 335 ((alarm.day < 1) || (alarm.day > 7)) ||
j3 7:0551d7e7c13f 336 ((alarm.date < 1) || (alarm.date > 31)))
j3 2:4e6e761c60f2 337 {
j3 2:4e6e761c60f2 338 return(1);
j3 2:4e6e761c60f2 339 }
j3 2:4e6e761c60f2 340 else
j3 2:4e6e761c60f2 341 {
j3 2:4e6e761c60f2 342 return(write(w_adrs,(const char*) data, data_length));
j3 2:4e6e761c60f2 343 }
j3 0:b00c4699ae6f 344 }
j3 0:b00c4699ae6f 345
j3 0:b00c4699ae6f 346
j3 0:b00c4699ae6f 347 /**********************************************************//**
j3 0:b00c4699ae6f 348 * Set control and status registers of DS3231
j3 0:b00c4699ae6f 349 *
j3 0:b00c4699ae6f 350 * On Entry:
j3 0:b00c4699ae6f 351 * @param[in] data - Struct containing control and status
j3 0:b00c4699ae6f 352 * register data
j3 0:b00c4699ae6f 353 *
j3 0:b00c4699ae6f 354 * On Exit:
j3 0:b00c4699ae6f 355 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 356 *
j3 0:b00c4699ae6f 357 * Example:
j3 0:b00c4699ae6f 358 * @code
j3 0:b00c4699ae6f 359 *
j3 0:b00c4699ae6f 360 * //instantiate rtc object
j3 1:c814af60fdbf 361 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 362 *
j3 0:b00c4699ae6f 363 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 364 * ds3231_cntl_stat_t data = {0xAA, 0xAA};
j3 0:b00c4699ae6f 365 *
j3 0:b00c4699ae6f 366 * rtn_val = rtc.set_cntl_stat_reg(data);
j3 0:b00c4699ae6f 367 *
j3 0:b00c4699ae6f 368 * @endcode
j3 0:b00c4699ae6f 369 **************************************************************/
j3 0:b00c4699ae6f 370 uint16_t Ds3231::set_cntl_stat_reg(ds3231_cntl_stat_t data)
j3 0:b00c4699ae6f 371 {
j3 2:4e6e761c60f2 372 uint8_t local_data[] = {0,0,0};
j3 2:4e6e761c60f2 373 uint8_t data_length = 0;
j3 2:4e6e761c60f2 374
j3 2:4e6e761c60f2 375 local_data[data_length++] = CONTROL;
j3 2:4e6e761c60f2 376 local_data[data_length++] = data.control;
j3 2:4e6e761c60f2 377 local_data[data_length++] = data.status;
j3 0:b00c4699ae6f 378
j3 2:4e6e761c60f2 379 //users responsibility to make sure data is logical
j3 2:4e6e761c60f2 380 return(write(w_adrs,(const char*) local_data, data_length));
j3 0:b00c4699ae6f 381 }
j3 0:b00c4699ae6f 382
j3 0:b00c4699ae6f 383
j3 0:b00c4699ae6f 384 /**********************************************************//**
j3 0:b00c4699ae6f 385 * Gets the time on DS3231
j3 0:b00c4699ae6f 386 *
j3 0:b00c4699ae6f 387 * On Entry:
j3 4:0beb5e9ac927 388 * @param[in] time - pointer to struct for storing time data
j3 0:b00c4699ae6f 389 *
j3 0:b00c4699ae6f 390 * On Exit:
j3 2:4e6e761c60f2 391 * @param[out] time - contains current integrer rtc time
j3 2:4e6e761c60f2 392 * data
j3 0:b00c4699ae6f 393 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 394 *
j3 0:b00c4699ae6f 395 * Example:
j3 0:b00c4699ae6f 396 * @code
j3 0:b00c4699ae6f 397 *
j3 0:b00c4699ae6f 398 * //instantiate rtc object
j3 1:c814af60fdbf 399 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 400 *
j3 4:0beb5e9ac927 401 * //time = 12:00:00 AM 12hr mode
j3 4:0beb5e9ac927 402 * ds3231_time_t time = {12, 0, 0, 0, 1}
j3 0:b00c4699ae6f 403 * uint16_t rtn_val;
j3 0:b00c4699ae6f 404 *
j3 3:312589d8185c 405 * rtn_val = rtc.get_time(&time);
j3 0:b00c4699ae6f 406 *
j3 0:b00c4699ae6f 407 * @endcode
j3 0:b00c4699ae6f 408 **************************************************************/
j3 2:4e6e761c60f2 409 uint16_t Ds3231::get_time(ds3231_time_t* time)
j3 0:b00c4699ae6f 410 {
j3 2:4e6e761c60f2 411 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 412 uint8_t data[3];
j3 2:4e6e761c60f2 413
j3 2:4e6e761c60f2 414 data[0] = SECONDS;
j3 2:4e6e761c60f2 415 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 416
j3 2:4e6e761c60f2 417 if(!rtn_val)
j3 2:4e6e761c60f2 418 {
j3 2:4e6e761c60f2 419 rtn_val = read(r_adrs,(char *) data, 3);
j3 2:4e6e761c60f2 420
j3 2:4e6e761c60f2 421 time->seconds = bcd_2_uchar(data[0]);
j3 2:4e6e761c60f2 422 time->minutes = bcd_2_uchar(data[1]);
j3 4:0beb5e9ac927 423 time->am_pm = (data[2]&AM_PM);
j3 4:0beb5e9ac927 424 time->mode = (data[2]&MODE);
j3 10:3b55ed3f71d3 425
j3 10:3b55ed3f71d3 426 if(time->mode)
j3 10:3b55ed3f71d3 427 {
j3 10:3b55ed3f71d3 428 time->hours = bcd_2_uchar((data[2]&0x1F));
j3 10:3b55ed3f71d3 429 }
j3 10:3b55ed3f71d3 430 else
j3 10:3b55ed3f71d3 431 {
j3 10:3b55ed3f71d3 432 time->hours = bcd_2_uchar((data[2]&0x3F));
j3 10:3b55ed3f71d3 433 }
j3 2:4e6e761c60f2 434 }
j3 2:4e6e761c60f2 435
j3 2:4e6e761c60f2 436 return(rtn_val);
j3 0:b00c4699ae6f 437 }
j3 0:b00c4699ae6f 438
j3 0:b00c4699ae6f 439
j3 0:b00c4699ae6f 440 /**********************************************************//**
j3 0:b00c4699ae6f 441 * Gets the calendar on DS3231
j3 0:b00c4699ae6f 442 *
j3 0:b00c4699ae6f 443 * On Entry:
j3 2:4e6e761c60f2 444 * @param[in] calendar - pointer to struct for storing
j3 4:0beb5e9ac927 445 * calendar data
j3 0:b00c4699ae6f 446 *
j3 0:b00c4699ae6f 447 * On Exit:
j3 2:4e6e761c60f2 448 * @param[out] calendar - contains current integer rtc
j3 2:4e6e761c60f2 449 * calendar data
j3 0:b00c4699ae6f 450 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 451 *
j3 0:b00c4699ae6f 452 * Example:
j3 0:b00c4699ae6f 453 * @code
j3 0:b00c4699ae6f 454 *
j3 0:b00c4699ae6f 455 * //instantiate rtc object
j3 1:c814af60fdbf 456 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 457 *
j3 0:b00c4699ae6f 458 * //see datasheet for calendar format
j3 0:b00c4699ae6f 459 * ds3231_calendar_t calendar = {1, 1, 1, 0};
j3 0:b00c4699ae6f 460 * uint16_t rtn_val;
j3 0:b00c4699ae6f 461 *
j3 3:312589d8185c 462 * rtn_val = rtc.get_calendar(&calendar);
j3 0:b00c4699ae6f 463 *
j3 0:b00c4699ae6f 464 * @endcode
j3 0:b00c4699ae6f 465 **************************************************************/
j3 2:4e6e761c60f2 466 uint16_t Ds3231::get_calendar(ds3231_calendar_t* calendar)
j3 0:b00c4699ae6f 467 {
j3 2:4e6e761c60f2 468 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 469 uint8_t data[4];
j3 2:4e6e761c60f2 470
j3 2:4e6e761c60f2 471 data[0] = DAY;
j3 2:4e6e761c60f2 472 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 473
j3 2:4e6e761c60f2 474 if(!rtn_val)
j3 2:4e6e761c60f2 475 {
j3 2:4e6e761c60f2 476 rtn_val = read(r_adrs,(char *) data, 4);
j3 2:4e6e761c60f2 477
j3 2:4e6e761c60f2 478 calendar->day = bcd_2_uchar(data[0]);
j3 2:4e6e761c60f2 479 calendar->date = bcd_2_uchar(data[1]);
j3 2:4e6e761c60f2 480 calendar->month = bcd_2_uchar((data[2]&0x1F));
j3 2:4e6e761c60f2 481 calendar->year = bcd_2_uchar(data[3]);
j3 2:4e6e761c60f2 482 }
j3 2:4e6e761c60f2 483
j3 2:4e6e761c60f2 484 return(rtn_val);
j3 0:b00c4699ae6f 485 }
j3 0:b00c4699ae6f 486
j3 0:b00c4699ae6f 487
j3 0:b00c4699ae6f 488 /**********************************************************//**
j3 0:b00c4699ae6f 489 * Get either Alarm1 or Alarm2 of DS3231
j3 0:b00c4699ae6f 490 *
j3 0:b00c4699ae6f 491 * On Entry:
j3 2:4e6e761c60f2 492 * @param[in] alarm - pointer to struct for storing alarm
j3 2:4e6e761c60f2 493 * data;
j3 2:4e6e761c60f2 494 *
j3 0:b00c4699ae6f 495 * @param[in] one_r_two - TRUE for Alarm1 and FALSE for
j3 0:b00c4699ae6f 496 * Alarm2
j3 0:b00c4699ae6f 497 *
j3 0:b00c4699ae6f 498 * On Exit:
j3 2:4e6e761c60f2 499 * @param[out] alarm - contains integer alarm data
j3 0:b00c4699ae6f 500 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 501 *
j3 0:b00c4699ae6f 502 * Example:
j3 0:b00c4699ae6f 503 * @code
j3 0:b00c4699ae6f 504 *
j3 0:b00c4699ae6f 505 * //instantiate rtc object
j3 1:c814af60fdbf 506 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 507 *
j3 4:0beb5e9ac927 508 * //see ds3231.h for .members and datasheet for alarm format
j3 4:0beb5e9ac927 509 * ds3231_alrm_t alarm;
j3 0:b00c4699ae6f 510 * uint16_t rtn_val;
j3 0:b00c4699ae6f 511 *
j3 3:312589d8185c 512 * rtn_val = rtc.get_alarm(&alarm, FALSE);
j3 0:b00c4699ae6f 513 *
j3 0:b00c4699ae6f 514 * @endcode
j3 0:b00c4699ae6f 515 **************************************************************/
j3 2:4e6e761c60f2 516 uint16_t Ds3231::get_alarm(ds3231_alrm_t* alarm, bool one_r_two)
j3 0:b00c4699ae6f 517 {
j3 2:4e6e761c60f2 518 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 519 uint8_t data[4];
j3 2:4e6e761c60f2 520
j3 2:4e6e761c60f2 521 if(one_r_two)
j3 2:4e6e761c60f2 522 {
j3 2:4e6e761c60f2 523 data[0] = ALRM1_SECONDS;
j3 2:4e6e761c60f2 524 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 525
j3 2:4e6e761c60f2 526 if(!rtn_val)
j3 2:4e6e761c60f2 527 {
j3 2:4e6e761c60f2 528 rtn_val = read(r_adrs,(char *) data, 4);
j3 2:4e6e761c60f2 529
j3 2:4e6e761c60f2 530 alarm->seconds = bcd_2_uchar(data[0]&0x7F);
j3 4:0beb5e9ac927 531 alarm->am1 = (data[0]&ALRM_MASK);
j3 2:4e6e761c60f2 532 alarm->minutes = bcd_2_uchar(data[1]&0x7F);
j3 4:0beb5e9ac927 533 alarm->am2 = (data[1]&ALRM_MASK);
j3 4:0beb5e9ac927 534 alarm->am3 = (data[2]&ALRM_MASK);
j3 4:0beb5e9ac927 535 alarm->am_pm = (data[2]&AM_PM);
j3 4:0beb5e9ac927 536 alarm->mode = (data[2]&MODE);
j3 2:4e6e761c60f2 537
j3 10:3b55ed3f71d3 538 if(alarm->mode)
j3 10:3b55ed3f71d3 539 {
j3 10:3b55ed3f71d3 540 alarm->hours = bcd_2_uchar((data[2]&0x1F));
j3 10:3b55ed3f71d3 541 }
j3 10:3b55ed3f71d3 542 else
j3 10:3b55ed3f71d3 543 {
j3 10:3b55ed3f71d3 544 alarm->hours = bcd_2_uchar((data[2]&0x3F));
j3 10:3b55ed3f71d3 545 }
j3 10:3b55ed3f71d3 546
j3 2:4e6e761c60f2 547 if(data[3] & DY_DT)
j3 2:4e6e761c60f2 548 {
j3 4:0beb5e9ac927 549 alarm->dy_dt = 1;
j3 2:4e6e761c60f2 550 alarm->day = bcd_2_uchar(data[3]&0x0F);
j3 2:4e6e761c60f2 551 }
j3 2:4e6e761c60f2 552 else
j3 2:4e6e761c60f2 553 {
j3 2:4e6e761c60f2 554 alarm->date = bcd_2_uchar(data[3]&0x3F);
j3 2:4e6e761c60f2 555 }
j3 4:0beb5e9ac927 556 alarm->am4 = (data[3]&ALRM_MASK);
j3 2:4e6e761c60f2 557 }
j3 2:4e6e761c60f2 558 }
j3 2:4e6e761c60f2 559 else
j3 2:4e6e761c60f2 560 {
j3 2:4e6e761c60f2 561 data[0] = ALRM2_MINUTES;
j3 2:4e6e761c60f2 562 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 563
j3 2:4e6e761c60f2 564 if(!rtn_val)
j3 2:4e6e761c60f2 565 {
j3 2:4e6e761c60f2 566 rtn_val = read(r_adrs,(char *) data, 4);
j3 2:4e6e761c60f2 567
j3 2:4e6e761c60f2 568 alarm->minutes = bcd_2_uchar(data[0]&0x7F);
j3 4:0beb5e9ac927 569 alarm->am2 = (data[0]&ALRM_MASK);
j3 4:0beb5e9ac927 570 alarm->am3 = (data[1]&ALRM_MASK);
j3 4:0beb5e9ac927 571 alarm->am_pm = (data[1]&AM_PM);
j3 4:0beb5e9ac927 572 alarm->mode = (data[1]&MODE);
j3 2:4e6e761c60f2 573
j3 10:3b55ed3f71d3 574 if(alarm->mode)
j3 10:3b55ed3f71d3 575 {
j3 10:3b55ed3f71d3 576 alarm->hours = bcd_2_uchar((data[2]&0x1F));
j3 10:3b55ed3f71d3 577 }
j3 10:3b55ed3f71d3 578 else
j3 10:3b55ed3f71d3 579 {
j3 10:3b55ed3f71d3 580 alarm->hours = bcd_2_uchar((data[2]&0x3F));
j3 10:3b55ed3f71d3 581 }
j3 10:3b55ed3f71d3 582
j3 4:0beb5e9ac927 583 if(data[2] & DY_DT)
j3 2:4e6e761c60f2 584 {
j3 4:0beb5e9ac927 585 alarm->dy_dt = 1;
j3 2:4e6e761c60f2 586 alarm->day = bcd_2_uchar(data[2]&0x0F);
j3 2:4e6e761c60f2 587 }
j3 2:4e6e761c60f2 588 else
j3 2:4e6e761c60f2 589 {
j3 2:4e6e761c60f2 590 alarm->date = bcd_2_uchar(data[2]&0x3F);
j3 2:4e6e761c60f2 591 }
j3 4:0beb5e9ac927 592 alarm->am4 = (data[2]&ALRM_MASK);
j3 2:4e6e761c60f2 593 }
j3 2:4e6e761c60f2 594 }
j3 2:4e6e761c60f2 595
j3 2:4e6e761c60f2 596 return(rtn_val);
j3 0:b00c4699ae6f 597 }
j3 0:b00c4699ae6f 598
j3 0:b00c4699ae6f 599
j3 0:b00c4699ae6f 600 /**********************************************************//**
j3 0:b00c4699ae6f 601 * Get control and status registers of DS3231
j3 0:b00c4699ae6f 602 *
j3 0:b00c4699ae6f 603 * On Entry:
j3 2:4e6e761c60f2 604 * @param[in] data - pointer to struct for storing control
j3 4:0beb5e9ac927 605 * and status register data
j3 0:b00c4699ae6f 606 *
j3 0:b00c4699ae6f 607 * On Exit:
j3 0:b00c4699ae6f 608 * @param[out] data - contains control and status registers
j3 0:b00c4699ae6f 609 * data
j3 0:b00c4699ae6f 610 * @return return value = 0 on success, non-0 on failure
j3 0:b00c4699ae6f 611 *
j3 0:b00c4699ae6f 612 * Example:
j3 0:b00c4699ae6f 613 * @code
j3 0:b00c4699ae6f 614 *
j3 0:b00c4699ae6f 615 * //instantiate rtc object
j3 1:c814af60fdbf 616 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 617 *
j3 0:b00c4699ae6f 618 * //do not use 0xAA, see datasheet for appropriate data
j3 0:b00c4699ae6f 619 * ds3231_cntl_stat_t data = {0xAA, 0xAA};
j3 0:b00c4699ae6f 620 *
j3 3:312589d8185c 621 * rtn_val = rtc.get_cntl_stat_reg(&data);
j3 0:b00c4699ae6f 622 *
j3 0:b00c4699ae6f 623 * @endcode
j3 0:b00c4699ae6f 624 **************************************************************/
j3 2:4e6e761c60f2 625 uint16_t Ds3231::get_cntl_stat_reg(ds3231_cntl_stat_t* data)
j3 0:b00c4699ae6f 626 {
j3 2:4e6e761c60f2 627 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 628 uint8_t local_data[2];
j3 2:4e6e761c60f2 629
j3 2:4e6e761c60f2 630 local_data[0] = CONTROL;
j3 2:4e6e761c60f2 631 rtn_val = write(w_adrs, (const char*) local_data, 1);
j3 2:4e6e761c60f2 632
j3 2:4e6e761c60f2 633 if(!rtn_val)
j3 2:4e6e761c60f2 634 {
j3 2:4e6e761c60f2 635 rtn_val = read(r_adrs,(char *) local_data, 2);
j3 2:4e6e761c60f2 636
j3 2:4e6e761c60f2 637 data->control = local_data[0];
j3 2:4e6e761c60f2 638 data->status = local_data[1];
j3 2:4e6e761c60f2 639 }
j3 2:4e6e761c60f2 640
j3 2:4e6e761c60f2 641 return(rtn_val);
j3 0:b00c4699ae6f 642 }
j3 0:b00c4699ae6f 643
j3 0:b00c4699ae6f 644
j3 0:b00c4699ae6f 645 /**********************************************************//**
j3 0:b00c4699ae6f 646 * Get temperature data of DS3231
j3 0:b00c4699ae6f 647 *
j3 0:b00c4699ae6f 648 * On Entry:
j3 0:b00c4699ae6f 649 *
j3 0:b00c4699ae6f 650 * On Exit:
j3 2:4e6e761c60f2 651 * @return return value = raw temperature data
j3 0:b00c4699ae6f 652 *
j3 0:b00c4699ae6f 653 * Example:
j3 0:b00c4699ae6f 654 * @code
j3 0:b00c4699ae6f 655 *
j3 0:b00c4699ae6f 656 * //instantiate rtc object
j3 1:c814af60fdbf 657 * Ds3231 rtc(D14, D15);
j3 0:b00c4699ae6f 658 *
j3 0:b00c4699ae6f 659 * uint16_t temp;
j3 0:b00c4699ae6f 660 *
j3 2:4e6e761c60f2 661 * temp = rtc.get_temperature();
j3 0:b00c4699ae6f 662 *
j3 0:b00c4699ae6f 663 * @endcode
j3 0:b00c4699ae6f 664 **************************************************************/
dexterg 15:9670e0a3aeec 665
j3 2:4e6e761c60f2 666 uint16_t Ds3231::get_temperature(void)
j3 0:b00c4699ae6f 667 {
j3 2:4e6e761c60f2 668 uint16_t rtn_val = 1;
j3 2:4e6e761c60f2 669 uint8_t data[2];
j3 2:4e6e761c60f2 670
j3 2:4e6e761c60f2 671 data[0] = MSB_TEMP;
j3 2:4e6e761c60f2 672 rtn_val = write(w_adrs, (const char*) data, 1);
j3 2:4e6e761c60f2 673
j3 2:4e6e761c60f2 674 if(!rtn_val)
j3 2:4e6e761c60f2 675 {
j3 2:4e6e761c60f2 676 read(r_adrs,(char *) data, 2);
j3 2:4e6e761c60f2 677
j3 2:4e6e761c60f2 678 rtn_val = data[0] << 8;
j3 2:4e6e761c60f2 679 rtn_val |= data[1];
j3 2:4e6e761c60f2 680 }
j3 2:4e6e761c60f2 681
j3 2:4e6e761c60f2 682 return(rtn_val);
j3 0:b00c4699ae6f 683 }
j3 0:b00c4699ae6f 684
j3 2:4e6e761c60f2 685
j3 2:4e6e761c60f2 686 /**********************************************************//**
j3 5:61dfe2690360 687 * Get epoch time based on current RTC time and date.
j3 5:61dfe2690360 688 * DS3231 must be configured and running before this fx is
j3 5:61dfe2690360 689 * called
j3 5:61dfe2690360 690 *
j3 5:61dfe2690360 691 * On Entry:
j3 5:61dfe2690360 692 *
j3 5:61dfe2690360 693 * On Exit:
j3 5:61dfe2690360 694 * @return return value = epoch time
j3 5:61dfe2690360 695 *
j3 5:61dfe2690360 696 * Example:
j3 5:61dfe2690360 697 * @code
j3 5:61dfe2690360 698 *
j3 5:61dfe2690360 699 * //instantiate rtc object
j3 5:61dfe2690360 700 * Ds3231 rtc(D14, D15);
j3 5:61dfe2690360 701 *
j3 5:61dfe2690360 702 * time_t epoch_time;
j3 5:61dfe2690360 703 *
j3 5:61dfe2690360 704 * epoch_time = rtc.get_epoch();
j3 5:61dfe2690360 705 *
j3 5:61dfe2690360 706 * @endcode
j3 5:61dfe2690360 707 **************************************************************/
j3 5:61dfe2690360 708 time_t Ds3231::get_epoch(void)
j3 5:61dfe2690360 709 {
j3 5:61dfe2690360 710 //system vars
j3 5:61dfe2690360 711 struct tm sys_time;
j3 5:61dfe2690360 712
j3 5:61dfe2690360 713 //RTC vars
j3 6:e8850ad15893 714 ds3231_time_t rtc_time = {0,0,0,0,0};
j3 6:e8850ad15893 715 ds3231_calendar_t rtc_calendar = {0,0,0,0};
j3 5:61dfe2690360 716
j3 5:61dfe2690360 717 get_calendar(&rtc_calendar);
j3 5:61dfe2690360 718 get_time(&rtc_time);
j3 5:61dfe2690360 719
j3 5:61dfe2690360 720 sys_time.tm_wday = rtc_calendar.day - 1;
j3 5:61dfe2690360 721 sys_time.tm_mday = rtc_calendar.date;
j3 5:61dfe2690360 722 sys_time.tm_mon = rtc_calendar.month - 1;
j3 5:61dfe2690360 723 sys_time.tm_year = rtc_calendar.year + 100;
j3 5:61dfe2690360 724
j3 5:61dfe2690360 725 //check for 12hr or 24hr mode
j3 5:61dfe2690360 726 if(rtc_time.mode)
j3 5:61dfe2690360 727 {
j3 5:61dfe2690360 728 //check am/pm
j3 5:61dfe2690360 729 if(rtc_time.am_pm && (rtc_time.hours != 12))
j3 5:61dfe2690360 730 {
j3 5:61dfe2690360 731 sys_time.tm_hour = rtc_time.hours + 12;
j3 5:61dfe2690360 732 }
j3 5:61dfe2690360 733 else
j3 5:61dfe2690360 734 {
j3 5:61dfe2690360 735 sys_time.tm_hour = rtc_time.hours;
j3 5:61dfe2690360 736 }
j3 5:61dfe2690360 737
j3 5:61dfe2690360 738 }
j3 5:61dfe2690360 739 else
j3 5:61dfe2690360 740 {
j3 5:61dfe2690360 741 //24hr mode
j3 5:61dfe2690360 742 sys_time.tm_hour = rtc_time.hours;
j3 5:61dfe2690360 743 }
j3 5:61dfe2690360 744
j3 5:61dfe2690360 745 sys_time.tm_min = rtc_time.minutes;
j3 5:61dfe2690360 746 sys_time.tm_sec = rtc_time.seconds;
j3 5:61dfe2690360 747
j3 5:61dfe2690360 748 //make epoch time
j3 5:61dfe2690360 749 return(mktime(&sys_time));
j3 5:61dfe2690360 750 }
j3 5:61dfe2690360 751
j3 5:61dfe2690360 752
j3 5:61dfe2690360 753 /**********************************************************//**
j3 2:4e6e761c60f2 754 * Private mmber fx, converts unsigned char to BCD
j3 2:4e6e761c60f2 755 *
j3 2:4e6e761c60f2 756 * On Entry:
j3 2:4e6e761c60f2 757 * @param[in] data - 0-255
j3 2:4e6e761c60f2 758 *
j3 2:4e6e761c60f2 759 * On Exit:
j3 2:4e6e761c60f2 760 * @return bcd_result = BCD representation of data
j3 2:4e6e761c60f2 761 *
j3 2:4e6e761c60f2 762 **************************************************************/
j3 2:4e6e761c60f2 763 uint16_t Ds3231::uchar_2_bcd(uint8_t data)
j3 2:4e6e761c60f2 764 {
j3 2:4e6e761c60f2 765 uint16_t bcd_result = 0;
j3 2:4e6e761c60f2 766
j3 2:4e6e761c60f2 767 //Get hundreds
j3 2:4e6e761c60f2 768 bcd_result |= ((data/100) << 8);
j3 2:4e6e761c60f2 769 data = (data - (data/100)*100);
j3 2:4e6e761c60f2 770
j3 2:4e6e761c60f2 771 //Get tens
j3 2:4e6e761c60f2 772 bcd_result |= ((data/10) << 4);
j3 2:4e6e761c60f2 773 data = (data - (data/10)*10);
j3 2:4e6e761c60f2 774
j3 2:4e6e761c60f2 775 //Get ones
j3 2:4e6e761c60f2 776 bcd_result |= data;
j3 2:4e6e761c60f2 777
j3 2:4e6e761c60f2 778 return(bcd_result);
j3 2:4e6e761c60f2 779 }
j3 2:4e6e761c60f2 780
j3 2:4e6e761c60f2 781
j3 2:4e6e761c60f2 782 /**********************************************************//**
j3 2:4e6e761c60f2 783 * Private mmber fx, converts BCD to a uint8_t
j3 2:4e6e761c60f2 784 *
j3 2:4e6e761c60f2 785 * On Entry:
j3 2:4e6e761c60f2 786 * @param[in] bcd - 0-99
j3 2:4e6e761c60f2 787 *
j3 2:4e6e761c60f2 788 * On Exit:
j3 2:4e6e761c60f2 789 * @return rtn_val = integer rep. of BCD
j3 2:4e6e761c60f2 790 *
j3 2:4e6e761c60f2 791 **************************************************************/
j3 2:4e6e761c60f2 792 uint8_t Ds3231::bcd_2_uchar(uint8_t bcd)
j3 2:4e6e761c60f2 793 {
j3 2:4e6e761c60f2 794 uint8_t rtn_val = 0;
j3 2:4e6e761c60f2 795
j3 2:4e6e761c60f2 796 rtn_val += ((bcd&0xf0)>>4)*10;
j3 2:4e6e761c60f2 797 rtn_val += (bcd&0x000f);
j3 2:4e6e761c60f2 798
j3 2:4e6e761c60f2 799 return rtn_val;
j3 2:4e6e761c60f2 800 }
j3 2:4e6e761c60f2 801
j3 2:4e6e761c60f2 802