j

Dependents:   LPC-SD-35

Fork of ds3231 by Maxim Integrated

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ds3231.cpp Source File

ds3231.cpp

Go to the documentation of this file.
00001 /******************************************************************//**
00002 * @file ds3231.cpp
00003 *
00004 * @author Justin Jordan
00005 *
00006 * @version 1.0
00007 *
00008 * Started: 11NOV14
00009 *
00010 * Updated: 
00011 *
00012 * @brief Source file for DS3231 class
00013 *
00014 ***********************************************************************
00015 *
00016 * @copyright 
00017 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
00018 *
00019 * Permission is hereby granted, free of charge, to any person obtaining a
00020 * copy of this software and associated documentation files (the "Software"),
00021 * to deal in the Software without restriction, including without limitation
00022 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00023 * and/or sell copies of the Software, and to permit persons to whom the
00024 * Software is furnished to do so, subject to the following conditions:
00025 *
00026 * The above copyright notice and this permission notice shall be included
00027 * in all copies or substantial portions of the Software.
00028 *
00029 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00030 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00031 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00032 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00033 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00034 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00035 * OTHER DEALINGS IN THE SOFTWARE.
00036 *
00037 * Except as contained in this notice, the name of Maxim Integrated
00038 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00039 * Products, Inc. Branding Policy.
00040 *
00041 * The mere transfer of this software does not imply any licenses
00042 * of trade secrets, proprietary technology, copyrights, patents,
00043 * trademarks, maskwork rights, or any other form of intellectual
00044 * property whatsoever. Maxim Integrated Products, Inc. retains all
00045 * ownership rights.
00046 **********************************************************************/
00047 
00048 
00049 #include "ds3231.h"
00050 
00051 
00052 /**********************************************************//**
00053 * Constructor for Ds3231 Class
00054 *
00055 * On Entry:
00056 *     @param[in] sda - sda pin of I2C bus
00057 *     @param[in] scl - scl pin of I2C bus
00058 *
00059 * On Exit:
00060 *    @return none
00061 *
00062 * Example:
00063 * @code
00064 * 
00065 * //instantiate rtc object
00066 * Ds3231 rtc(D14, D15); 
00067 *
00068 * @endcode
00069 **************************************************************/
00070 Ds3231::Ds3231(PinName sda, PinName scl) : I2C(sda, scl)
00071 {
00072     w_adrs = ((DS3231_I2C_ADRS << 1) | I2C_WRITE);
00073     r_adrs = ((DS3231_I2C_ADRS << 1) | I2C_READ);
00074 }
00075 
00076 
00077 /**********************************************************//**
00078 * Sets the time on DS3231
00079 * Struct data is in integrer format, not BCD.  Fx will convert
00080 * to BCD for you.
00081 *
00082 * On Entry:
00083 *     @param[in] time - struct cotaining time data; 
00084 *
00085 * On Exit:
00086 *     @return return value = 0 on success, non-0 on failure
00087 *
00088 * Example:
00089 * @code
00090 * 
00091 * //instantiate rtc object
00092 * Ds3231 rtc(D14, D15); 
00093 * 
00094 * //time = 12:00:00 AM 12hr mode
00095 * ds3231_time_t time = {12, 0, 0, 0, 1}
00096 * uint16_t rtn_val;
00097 *
00098 * rtn_val = rtc.set_time(time);
00099 *
00100 * @endcode
00101 **************************************************************/
00102 uint16_t Ds3231::set_time(ds3231_time_t time)
00103 {
00104     uint8_t data[] = {0,0,0,0};
00105     uint8_t data_length = 0;
00106     uint8_t max_hour = 24;
00107     
00108     data[data_length++] = SECONDS;
00109     data[data_length++] = uchar_2_bcd(time.seconds);
00110     data[data_length++] = uchar_2_bcd(time.minutes);
00111     
00112     //format Hours register
00113     data[data_length] = uchar_2_bcd(time.hours);
00114     if(time.mode)
00115     {
00116         max_hour = max_hour/2;
00117         
00118         data[data_length] |= MODE;
00119         if(time.am_pm)
00120         {
00121             data[data_length] |= AM_PM;
00122         }
00123         
00124     }
00125     else
00126     {
00127         max_hour = max_hour - 1;
00128     }
00129     data_length++;
00130     
00131     //Make sure data is within range.  
00132     if((time.seconds > 59) || (time.minutes > 59) || (time.hours > max_hour))
00133     {
00134         return(1);
00135     }
00136     else
00137     {
00138         return(write(w_adrs,(const char*) data, data_length));
00139     }
00140 }
00141 
00142 
00143 /**********************************************************//**
00144 * Sets the calendar on DS3231
00145 *
00146 * On Entry:
00147 *     @param[in] calendar - struct cotaining calendar data 
00148 *
00149 * On Exit:
00150 *     @return return value = 0 on success, non-0 on failure
00151 *
00152 * Example:
00153 * @code
00154 * 
00155 * //instantiate rtc object
00156 * Ds3231 rtc(D14, D15); 
00157 * 
00158 * //see datasheet for calendar format
00159 * ds3231_calendar_t calendar = {1, 1, 1, 0}; 
00160 * uint16_t rtn_val;
00161 *
00162 * rtn_val = rtc.set_calendar(calendar);
00163 *
00164 * @endcode
00165 **************************************************************/
00166 uint16_t Ds3231::set_calendar(ds3231_calendar_t calendar)
00167 {
00168     uint8_t data[] = {0,0,0,0,0};
00169     uint8_t data_length = 0;
00170     
00171     data[data_length++] = DAY;
00172     data[data_length++] = uchar_2_bcd(calendar.day);
00173     data[data_length++] = uchar_2_bcd(calendar.date);
00174     data[data_length++] = uchar_2_bcd(calendar.month);
00175     data[data_length++] = uchar_2_bcd(calendar.year);
00176 
00177     //Make sure data is within range.  
00178     if(((calendar.day < 1) || (calendar.day > 7)) || 
00179        ((calendar.date < 1) || (calendar.date > 31)) || 
00180        ((calendar.month < 1) || (calendar.month > 12)) || 
00181        (calendar.year > 99))
00182     {
00183         return(1);
00184     }
00185     else
00186     {
00187         return(write(w_adrs,(const char*) data, data_length));
00188     }
00189 }
00190 
00191 
00192 /**********************************************************//**
00193 * Set either Alarm1 or Alarm2 of DS3231
00194 *
00195 * On Entry:
00196 *     @param[in] alarm - struct cotaining alarm data 
00197 *                        
00198 *     @param[in] one_r_two - TRUE for Alarm1 and FALSE for 
00199 *                            Alarm2
00200 *
00201 * On Exit:
00202 *     @return return value = 0 on success, non-0 on failure
00203 *
00204 * Example:
00205 * @code
00206 * 
00207 * //instantiate rtc object
00208 * Ds3231 rtc(D14, D15); 
00209 * 
00210 * //see ds3231.h for .members and datasheet for alarm format
00211 * ds3231_alrm_t alarm; 
00212 * uint16_t rtn_val;
00213 *
00214 * rtn_val = rtc.set_alarm(alarm, FALSE);
00215 *
00216 * @endcode
00217 **************************************************************/
00218 uint16_t Ds3231::set_alarm(ds3231_alrm_t alarm, bool one_r_two)
00219 {
00220     uint8_t data[] = {0,0,0,0,0};
00221     uint8_t data_length = 0;
00222     uint8_t max_hour = 24;
00223     uint8_t mask_var = 0;
00224     
00225     //setting alarm 1 or 2?
00226     if(one_r_two)
00227     {
00228         data[data_length++] = ALRM1_SECONDS;
00229         
00230         //config seconds register
00231         if(alarm.am1)
00232         {
00233            mask_var |= ALRM_MASK;
00234         }
00235         data[data_length++] =  (mask_var | uchar_2_bcd(alarm.seconds));
00236         mask_var = 0;
00237         
00238         //config minutes register
00239         if(alarm.am2)
00240         {
00241            mask_var |= ALRM_MASK;
00242         }
00243         data[data_length++] =  (mask_var | uchar_2_bcd(alarm.minutes));
00244         mask_var = 0;
00245         
00246         //config hours register
00247         if(alarm.am3)
00248         {
00249            mask_var |= ALRM_MASK;
00250         }
00251         if(alarm.mode)
00252         {
00253             max_hour = max_hour/2;
00254             mask_var |= MODE;
00255             if(alarm.am_pm)
00256             {
00257                 mask_var |= AM_PM;
00258             }
00259         } 
00260         else
00261         {
00262             max_hour = max_hour - 1;
00263         }
00264         data[data_length++] =  (mask_var | uchar_2_bcd(alarm.hours));
00265         mask_var = 0;
00266         
00267         //config day/date register
00268         if(alarm.am4)
00269         {
00270            mask_var |= ALRM_MASK;
00271         }
00272         if(alarm.dy_dt)
00273         {
00274             mask_var |= DY_DT;
00275             data[data_length++] =  (mask_var | uchar_2_bcd(alarm.day));
00276         } 
00277         else
00278         {
00279             data[data_length++] =  (mask_var | uchar_2_bcd(alarm.date));
00280         }
00281         mask_var = 0;
00282     }
00283     else
00284     {
00285         data[data_length++] = ALRM2_MINUTES;
00286         
00287         //config minutes register
00288         if(alarm.am2)
00289         {
00290            mask_var |= ALRM_MASK;
00291         }
00292         data[data_length++] =  (mask_var | uchar_2_bcd(alarm.minutes));
00293         mask_var = 0;
00294         
00295         //config hours register
00296         if(alarm.am3)
00297         {
00298            mask_var |= ALRM_MASK;
00299         }
00300         if(alarm.mode)
00301         {
00302             max_hour = max_hour/2;
00303             mask_var |= MODE;
00304             if(alarm.am_pm)
00305             {
00306                 mask_var |= AM_PM;
00307             }
00308         } 
00309         else
00310         {
00311             max_hour = max_hour - 1;
00312         }
00313         data[data_length++] =  (mask_var | uchar_2_bcd(alarm.hours));
00314         mask_var = 0;
00315         
00316         //config day/date register
00317         if(alarm.am4)
00318         {
00319            mask_var |= ALRM_MASK;
00320         }
00321         if(alarm.dy_dt)
00322         {
00323             mask_var |= DY_DT;
00324             data[data_length++] =  (mask_var | uchar_2_bcd(alarm.day));
00325         } 
00326         else
00327         {
00328             data[data_length++] =  (mask_var | uchar_2_bcd(alarm.date));
00329         }
00330         mask_var = 0;
00331     }  
00332     
00333     //Make sure data is within range.
00334     if((alarm.seconds > 59) || (alarm.minutes > 59) || (alarm.hours > max_hour) || 
00335        ((alarm.day < 1) || (alarm.day > 7)) || 
00336        ((alarm.date < 1) || (alarm.date > 31)))
00337     {
00338         return(1);
00339     }
00340     else
00341     {
00342         return(write(w_adrs,(const char*) data, data_length));
00343     }
00344 }
00345 
00346 
00347 /**********************************************************//**
00348 * Set control and status registers of DS3231
00349 *
00350 * On Entry:
00351 *     @param[in] data - Struct containing control and status 
00352 *                       register data
00353 *
00354 * On Exit:
00355 *     @return return value = 0 on success, non-0 on failure
00356 *
00357 * Example:
00358 * @code
00359 * 
00360 * //instantiate rtc object
00361 * Ds3231 rtc(D14, D15);  
00362 * 
00363 * //do not use 0xAA, see datasheet for appropriate data 
00364 * ds3231_cntl_stat_t data = {0xAA, 0xAA}; 
00365 *
00366 * rtn_val = rtc.set_cntl_stat_reg(data);
00367 *
00368 * @endcode
00369 **************************************************************/
00370 uint16_t Ds3231::set_cntl_stat_reg(ds3231_cntl_stat_t data)
00371 {
00372     uint8_t local_data[] = {0,0,0};
00373     uint8_t data_length = 0;
00374     
00375     local_data[data_length++] = CONTROL;
00376     local_data[data_length++] = data.control;
00377     local_data[data_length++] = data.status;
00378 
00379     //users responsibility to make sure data is logical
00380     return(write(w_adrs,(const char*) local_data, data_length));
00381 }
00382 
00383 
00384 /**********************************************************//**
00385 * Gets the time on DS3231
00386 *
00387 * On Entry:
00388 *     @param[in] time - pointer to struct for storing time data
00389 *
00390 * On Exit:
00391 *     @param[out] time - contains current integrer rtc time 
00392 *                        data
00393 *     @return return value = 0 on success, non-0 on failure
00394 *
00395 * Example:
00396 * @code
00397 * 
00398 * //instantiate rtc object
00399 * Ds3231 rtc(D14, D15); 
00400 * 
00401 * //time = 12:00:00 AM 12hr mode
00402 * ds3231_time_t time = {12, 0, 0, 0, 1} 
00403 * uint16_t rtn_val;
00404 *
00405 * rtn_val = rtc.get_time(&time);
00406 *
00407 * @endcode
00408 **************************************************************/
00409 uint16_t Ds3231::get_time(ds3231_time_t* time)
00410 {
00411     uint16_t rtn_val = 1;
00412     uint8_t data[3];
00413     
00414     data[0] = SECONDS;
00415     rtn_val = write(w_adrs, (const char*) data, 1);
00416     
00417     if(!rtn_val)
00418     {
00419         rtn_val = read(r_adrs,(char *) data, 3);
00420         
00421         time->seconds = bcd_2_uchar(data[0]);
00422         time->minutes = bcd_2_uchar(data[1]);
00423         time->am_pm = (data[2]&AM_PM);
00424         time->mode = (data[2]&MODE);
00425         
00426         if(time->mode)
00427         {
00428             time->hours = bcd_2_uchar((data[2]&0x1F));
00429         }
00430         else
00431         {
00432             time->hours = bcd_2_uchar((data[2]&0x3F));
00433         }  
00434     } 
00435   
00436     return(rtn_val);
00437 }
00438 
00439 
00440 /**********************************************************//**
00441 * Gets the calendar on DS3231
00442 *
00443 * On Entry:
00444 *     @param[in] calendar - pointer to struct for storing 
00445 *                           calendar data
00446 *
00447 * On Exit:
00448 *     @param[out] calendar - contains current integer rtc 
00449 *                            calendar data
00450 *     @return return value = 0 on success, non-0 on failure
00451 *
00452 * Example:
00453 * @code
00454 * 
00455 * //instantiate rtc object
00456 * Ds3231 rtc(D14, D15); 
00457 * 
00458 * //see datasheet for calendar format
00459 * ds3231_calendar_t calendar = {1, 1, 1, 0}; 
00460 * uint16_t rtn_val;
00461 *
00462 * rtn_val = rtc.get_calendar(&calendar);
00463 *
00464 * @endcode
00465 **************************************************************/
00466 uint16_t Ds3231::get_calendar(ds3231_calendar_t* calendar)
00467 {
00468     uint16_t rtn_val = 1;
00469     uint8_t data[4];
00470     
00471     data[0] = DAY;
00472     rtn_val = write(w_adrs, (const char*) data, 1);
00473     
00474     if(!rtn_val)
00475     {
00476         rtn_val = read(r_adrs,(char *) data, 4);
00477         
00478         calendar->day = bcd_2_uchar(data[0]);
00479         calendar->date = bcd_2_uchar(data[1]);
00480         calendar->month = bcd_2_uchar((data[2]&0x1F));
00481         calendar->year = bcd_2_uchar(data[3]);
00482     } 
00483   
00484     return(rtn_val);
00485 }
00486 
00487 
00488 /**********************************************************//**
00489 * Get either Alarm1 or Alarm2 of DS3231
00490 *
00491 * On Entry:
00492 *     @param[in] alarm - pointer to struct for storing alarm 
00493 *                        data; 
00494 *
00495 *     @param[in] one_r_two - TRUE for Alarm1 and FALSE for 
00496 *                            Alarm2
00497 *
00498 * On Exit:
00499 *     @param[out] alarm - contains integer alarm data
00500 *     @return return value = 0 on success, non-0 on failure
00501 *
00502 * Example:
00503 * @code
00504 * 
00505 * //instantiate rtc object
00506 * Ds3231 rtc(D14, D15); 
00507 * 
00508 * //see ds3231.h for .members and datasheet for alarm format
00509 * ds3231_alrm_t alarm; 
00510 * uint16_t rtn_val;
00511 *
00512 * rtn_val = rtc.get_alarm(&alarm, FALSE);
00513 *
00514 * @endcode
00515 **************************************************************/
00516 uint16_t Ds3231::get_alarm(ds3231_alrm_t* alarm, bool one_r_two)
00517 {
00518     uint16_t rtn_val = 1;
00519     uint8_t data[4];
00520     
00521     if(one_r_two)
00522     {
00523         data[0] = ALRM1_SECONDS;
00524         rtn_val = write(w_adrs, (const char*) data, 1);
00525         
00526         if(!rtn_val)
00527         {
00528             rtn_val = read(r_adrs,(char *) data, 4);
00529             
00530             alarm->seconds = bcd_2_uchar(data[0]&0x7F);
00531             alarm->am1 = (data[0]&ALRM_MASK);
00532             alarm->minutes = bcd_2_uchar(data[1]&0x7F);
00533             alarm->am2 = (data[1]&ALRM_MASK);
00534             alarm->am3 = (data[2]&ALRM_MASK);
00535             alarm->am_pm = (data[2]&AM_PM);
00536             alarm->mode = (data[2]&MODE);
00537             
00538             if(alarm->mode)
00539             {
00540                 alarm->hours = bcd_2_uchar((data[2]&0x1F));
00541             }
00542             else
00543             {
00544                 alarm->hours = bcd_2_uchar((data[2]&0x3F));
00545             }  
00546             
00547             if(data[3] & DY_DT)
00548             {
00549                 alarm->dy_dt = 1;
00550                 alarm->day = bcd_2_uchar(data[3]&0x0F);
00551             }
00552             else
00553             {
00554                 alarm->date = bcd_2_uchar(data[3]&0x3F);
00555             }
00556             alarm->am4 = (data[3]&ALRM_MASK);
00557         } 
00558     }
00559     else
00560     {
00561         data[0] = ALRM2_MINUTES;
00562         rtn_val = write(w_adrs, (const char*) data, 1);
00563         
00564         if(!rtn_val)
00565         {
00566             rtn_val = read(r_adrs,(char *) data, 4);
00567             
00568             alarm->minutes = bcd_2_uchar(data[0]&0x7F);
00569             alarm->am2 = (data[0]&ALRM_MASK);
00570             alarm->am3 = (data[1]&ALRM_MASK);
00571             alarm->am_pm = (data[1]&AM_PM);
00572             alarm->mode = (data[1]&MODE);
00573             
00574             if(alarm->mode)
00575             {
00576                 alarm->hours = bcd_2_uchar((data[2]&0x1F));
00577             }
00578             else
00579             {
00580                 alarm->hours = bcd_2_uchar((data[2]&0x3F));
00581             }  
00582             
00583             if(data[2] & DY_DT)
00584             {
00585                 alarm->dy_dt = 1;
00586                 alarm->day = bcd_2_uchar(data[2]&0x0F);
00587             }
00588             else
00589             {
00590                 alarm->date = bcd_2_uchar(data[2]&0x3F);
00591             }
00592             alarm->am4 = (data[2]&ALRM_MASK);
00593         } 
00594     }
00595     
00596     return(rtn_val);
00597 }
00598 
00599 
00600 /**********************************************************//**
00601 * Get control and status registers of DS3231
00602 *
00603 * On Entry:
00604 *     @param[in] data - pointer to struct for storing control 
00605 *                       and status register data
00606 *
00607 * On Exit:
00608 *     @param[out] data - contains control and status registers
00609 *                        data
00610 *     @return return value = 0 on success, non-0 on failure
00611 *
00612 * Example:
00613 * @code
00614 * 
00615 * //instantiate rtc object
00616 * Ds3231 rtc(D14, D15);  
00617 * 
00618 * //do not use 0xAA, see datasheet for appropriate data 
00619 * ds3231_cntl_stat_t data = {0xAA, 0xAA}; 
00620 *
00621 * rtn_val = rtc.get_cntl_stat_reg(&data);
00622 *
00623 * @endcode
00624 **************************************************************/
00625 uint16_t Ds3231::get_cntl_stat_reg(ds3231_cntl_stat_t* data)
00626 {
00627     uint16_t rtn_val = 1;
00628     uint8_t local_data[2];
00629     
00630     local_data[0] = CONTROL;
00631     rtn_val = write(w_adrs, (const char*) local_data, 1);
00632     
00633     if(!rtn_val)
00634     {
00635         rtn_val = read(r_adrs,(char *) local_data, 2);
00636         
00637         data->control = local_data[0];
00638         data->status = local_data[1];
00639     } 
00640   
00641     return(rtn_val);
00642 }
00643 
00644 
00645 /**********************************************************//**
00646 * Get temperature data of DS3231
00647 *
00648 * On Entry:
00649 *
00650 * On Exit:
00651 *     @return return value = raw temperature data
00652 *
00653 * Example:
00654 * @code
00655 * 
00656 * //instantiate rtc object
00657 * Ds3231 rtc(D14, D15); 
00658 * 
00659 * uint16_t temp; 
00660 *
00661 * temp = rtc.get_temperature();
00662 *
00663 * @endcode
00664 **************************************************************/
00665 
00666 uint16_t Ds3231::get_temperature(void)
00667 {
00668     uint16_t rtn_val = 1;
00669     uint8_t data[2];
00670     
00671     data[0] = MSB_TEMP;
00672     rtn_val = write(w_adrs, (const char*) data, 1);
00673     
00674     if(!rtn_val)
00675     {
00676         read(r_adrs,(char *) data, 2);
00677         
00678         rtn_val = data[0] << 8;
00679         rtn_val |= data[1];
00680     } 
00681   
00682     return(rtn_val);    
00683 }
00684 
00685 
00686 /**********************************************************//**
00687 * Get epoch time based on current RTC time and date.  
00688 * DS3231 must be configured and running before this fx is 
00689 * called
00690 *
00691 * On Entry:
00692 *
00693 * On Exit:
00694 *     @return return value = epoch time
00695 *
00696 * Example:
00697 * @code
00698 * 
00699 * //instantiate rtc object
00700 * Ds3231 rtc(D14, D15); 
00701 * 
00702 * time_t epoch_time; 
00703 *
00704 * epoch_time = rtc.get_epoch();
00705 *
00706 * @endcode
00707 **************************************************************/
00708 time_t Ds3231::get_epoch(void)
00709 {
00710     //system vars
00711     struct tm sys_time;
00712     
00713     //RTC vars
00714     ds3231_time_t rtc_time = {0,0,0,0,0};
00715     ds3231_calendar_t rtc_calendar = {0,0,0,0};
00716     
00717     get_calendar(&rtc_calendar);
00718     get_time(&rtc_time);
00719     
00720     sys_time.tm_wday = rtc_calendar.day - 1;
00721     sys_time.tm_mday = rtc_calendar.date;
00722     sys_time.tm_mon = rtc_calendar.month - 1;
00723     sys_time.tm_year = rtc_calendar.year + 100;
00724     
00725     //check for 12hr or 24hr mode
00726     if(rtc_time.mode)
00727     {
00728         //check am/pm
00729         if(rtc_time.am_pm  && (rtc_time.hours != 12))
00730         {
00731             sys_time.tm_hour = rtc_time.hours + 12;
00732         }
00733         else
00734         {
00735             sys_time.tm_hour = rtc_time.hours;
00736         }
00737         
00738     }
00739     else
00740     {
00741         //24hr mode
00742         sys_time.tm_hour = rtc_time.hours;
00743     }
00744     
00745     sys_time.tm_min = rtc_time.minutes;
00746     sys_time.tm_sec = rtc_time.seconds;
00747     
00748     //make epoch time
00749     return(mktime(&sys_time));
00750 }
00751 
00752 
00753 /**********************************************************//**
00754 * Private mmber fx, converts unsigned char to BCD
00755 *
00756 * On Entry:
00757 *     @param[in] data - 0-255
00758 *
00759 * On Exit:
00760 *     @return bcd_result = BCD representation of data
00761 *
00762 **************************************************************/
00763 uint16_t Ds3231::uchar_2_bcd(uint8_t data)
00764 {
00765    uint16_t bcd_result = 0;
00766    
00767    //Get hundreds
00768    bcd_result |= ((data/100) << 8);
00769    data = (data - (data/100)*100);
00770    
00771    //Get tens
00772    bcd_result |= ((data/10) << 4);
00773    data = (data - (data/10)*10);
00774    
00775    //Get ones
00776    bcd_result |= data;
00777    
00778    return(bcd_result);   
00779 }
00780 
00781 
00782 /**********************************************************//**
00783 * Private mmber fx, converts BCD to a uint8_t
00784 *
00785 * On Entry:
00786 *     @param[in] bcd - 0-99
00787 *
00788 * On Exit:
00789 *     @return rtn_val = integer rep. of BCD
00790 *
00791 **************************************************************/
00792 uint8_t Ds3231::bcd_2_uchar(uint8_t bcd)
00793 {
00794     uint8_t rtn_val = 0;
00795 
00796     rtn_val += ((bcd&0xf0)>>4)*10;
00797     rtn_val += (bcd&0x000f);   
00798 
00799     return rtn_val;
00800 }
00801 
00802