util.c File Reference

#include <inttypes.h>
#include "util.h"
Include dependency graph for util.c:

Functions

uint8_t frombcd (uint8_t x)
uint8_t month_length (uint8_t m, uint8_t leap)
uint8_t days_in_month_bcd (uint8_t year, uint8_t month)
uint8_t bcd_increment (uint8_t x)
 Increment BCD value by 1.
uint8_t day_of_week (uint8_t y, uint8_t m, uint8_t d)
 Get day of week, 0 = Sunday. Input parameters are binary (not BCD).
uint16_t tobcd16 (uint16_t x)

Function Documentation

uint8_t bcd_increment ( uint8_t  x  ) 

Increment BCD value by 1.

00023                                  {
00024     x++;
00025     if ((x & 0x0f) >= 0x0a)
00026         x += 0x10 - 0x0a;
00027     if (x >= 0xa0) 
00028        x = 0;
00029     return x;
00030 }

uint8_t day_of_week ( uint8_t  y,
uint8_t  m,
uint8_t  d 
)

Get day of week, 0 = Sunday. Input parameters are binary (not BCD).

00033                                                      {
00034     uint8_t leap = y%4 == 0;
00035     uint16_t centurydays = 6 + y * 365 + (y+3)/4; // year 2000 started on Saturday
00036     for (; --m >= 1; ) {
00037         centurydays += month_length(m,leap);
00038     }
00039     
00040     centurydays += d-1;
00041     
00042     return (centurydays % 7);
00043 }

Here is the call graph for this function:

uint8_t days_in_month_bcd ( uint8_t  year,
uint8_t  month 
)

Return BCD count of days in month for given BCD year and month. Valid only for years 2000-2099.

00013                                                        {
00014     uint8_t y = frombcd(year);//(year & 7) + ((year & 070)>>3) * 10;
00015     uint8_t m = frombcd(month);//(month & 7) + ((month & 070)>>3) * 10;
00016     uint8_t leap = y % 4 == 0; // non y2k1-compliant, but should be correct for the next 91 years or so :)
00017     
00018     uint8_t knuckle = m < 8 ? (m & 1) == 1 : (m & 1) == 0;
00019     return knuckle ? 0x31 : m != 2 ? 0x30 : leap ? 0x29 : 0x28;
00020 }

Here is the call graph for this function:

uint8_t frombcd ( uint8_t   ) 

Convert to binary from BCD representation as a function.

See also:
_frombcd

00004                            {
00005     return _frombcd(x);
00006 }

uint8_t month_length ( uint8_t  m,
uint8_t  leap 
)

00008                                               {
00009     uint8_t knuckle = m < 8 ? (m & 1) == 1 : (m & 1) == 0;
00010     return knuckle ? 31 : m != 2 ? 30 : leap ? 29 : 28;
00011 }

uint16_t tobcd16 ( uint16_t  x  ) 

00045                              {
00046     uint16_t y;
00047     
00048     y  = x % 10;        x /= 10;       
00049     y |= (x % 10)<<4;   x /= 10;
00050     y |= (x % 100)<<8;  x /= 10; 
00051     y |= 0xf000;
00052     
00053     return y;
00054 }


Generated on Wed Jan 27 00:29:59 2010 for Patashnik by  doxygen 1.6.1