在dev-c++上编译测试过
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef unsigned char u8 ;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef int s32;
typedef unsigned int u32;
typedef long long s64;
typedef unsigned long long u64;
typedef unsigned char U8 ;
typedef signed char S8;
typedef unsigned short U16;
typedef signed short S16;
typedef int S32;
typedef unsigned int U32;
typedef long long S64;
typedef unsigned long long U64;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long int uint32_t;
typedef signed char int8_t;
typedef signed short int16_t;
typedef signed long int int32_t;
struct doorTm
{
S32 tm_sec; /* seconds after the minute, 0 to 60
(0 - 60 allows for the occasional leap second) */
S32 tm_min; /* minutes after the hour, 0 to 59 */
S32 tm_hour; /* hours since midnight, 0 to 23 */
S32 tm_mday; /* day of the month, 1 to 31 */
S32 tm_mon; /* months since January, 0 to 11 */
S32 tm_year; /* years since 1900 */
S32 tm_wday; /* days since Sunday, 0 to 6 */
S32 tm_yday; /* days since January 1, 0 to 365 */
S32 tm_isdst; /* Daylight Savings Time flag */
};
#include <string.h>
//#define NULL 0
static const S8 mon_list[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const S8 leap_mon_list[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
S32 SoftRtcCounter;
S32 GetSoftRtcCounter(void)
{
return SoftRtcCounter;
}
void SetSoftRtcCounter(S32 counter)
{
SoftRtcCounter = counter;
}
U8 DoorGetWeekDay(U8 c, U8 y, U8 m, U8 d)
{
U8 w;//weekday
//printf("c:%d y:%d m:%d d:%d\n", c, y, m, d);
if(m <= 2)
{
}
else
{
w = y + y/4 + c/4 - 2*c + 26*(m+1)/10 + d - 1;
w = w % 7;
return w;
}
m = m+12;//month
if(y == 0)
{
y = 99;
c = c - 1;//century
}
else
{
y = y - 1;//year
}
//printf("c:%d y:%d m:%d d:%d\n", c, y, m, d);
w = y + y/4 + c/4 - 2*c + 26*(m+1)/10 + d - 1;
w = w % 7;
return w;
}
S32 DoorMktime(struct doorTm *pT)
{
const S8 *pDays = 0;
S32 tmp = 0;
S16 i = 0;
//计算总共有多少个闰年
tmp = (pT->tm_year / 4 - pT->tm_year / 100 + pT->tm_year / 400) - (1970 / 4 - 1970 / 100 + 1970 / 400);
//如果当年是闰年,需要减去当年的闰年
if((pT->tm_year % 4 == 0) && ((pT->tm_year % 100 != 0) || (pT->tm_year % 400 == 0)))
{
tmp = tmp - 1 + (pT->tm_year - 1970) * 365;
pDays = leap_mon_list;
}
else
{
tmp = tmp + (pT->tm_year - 1970) * 365;
pDays = mon_list;
}
for(i = 0; i < pT->tm_mon - 1; i++)
{
tmp += pDays[i];
}
tmp = tmp + pT->tm_mday - 1;
tmp = tmp * 24 + pT->tm_hour;
tmp = tmp * 60 + pT->tm_min;
tmp = tmp * 60 + pT->tm_sec;
return tmp;
}
void DoorLocaltime(struct doorTm *pT, S32 tim)
{
const S8 *pDays = NULL;
U16 index = 0;
memset(pT, 0, sizeof(*pT));
//year initialization
if(tim > 0x5685C180L) // 2016-1-1 0:0:0
{
pT->tm_year = 2016;
tim -= 0x5685C180L;
}
else if(tim > 0x4B3D3B00L) // 2010-1-1 0:0:0
{
pT->tm_year = 2010;
tim -= 0x4B3D3B00L;
}
else if(tim > 0x386D4380L) // 2000-1-1 0:0:0
{
pT->tm_year = 2000;
tim -= 0x386D4380L;
}
else
{
pT->tm_year = 1970;
}
//now have year
while(tim >= 366L * 24 * 60 * 60)
{
if((pT->tm_year % 4 == 0) && ((pT->tm_year % 100 != 0) || (pT->tm_year % 400 == 0)))
{
tim -= 366L * 24 * 60 * 60;
}
else
{
tim -= 365L * 24 * 60 * 60;
}
pT->tm_year++;
}
// then 365 * 24 * 60 * 60 < tim < 366 * 24 * 60 * 60
if(!(((pT->tm_year % 4 == 0) && ((pT->tm_year % 100 != 0) || (pT->tm_year % 400 == 0)))) && (tim > 365L * 24 * 60 * 60))
{
tim -= 365L * 24 * 60 * 60;
pT->tm_year++;
}
// this year is a leap year?
if(((pT->tm_year % 4 == 0) && ((pT->tm_year % 100 != 0) || (pT->tm_year % 400 == 0))))
{
pDays = leap_mon_list;
}
else
{
pDays = mon_list;
}
pT->tm_mon = 1;
// now have mon
while(tim > pDays[index] * 24L * 60 * 60)
{
tim -= pDays[index] * 24L * 60 * 60;
index++;
pT->tm_mon++;
}
// now have days
pT->tm_mday = tim / (24L * 60 * 60) + 1;
tim = tim % (24L * 60 * 60);
// now have hour
pT->tm_hour = tim / (60 * 60);
tim = tim % (60 * 60);
// now have min
pT->tm_min = tim / 60;
tim = tim % 60;
pT->tm_sec = tim;
}
static U8 HexToBcd(U8 hexValue)
{
U8 bcdValue;
bcdValue = hexValue / 10;
bcdValue = (hexValue % 10) | (bcdValue << 4);
return bcdValue;
}
static U8 BcdToHex(U8 bcdValue)
{
U8 hexValue;
hexValue = bcdValue & 0x0f;
hexValue = hexValue + (bcdValue >> 4) * 10;
return hexValue;
}
/*修改RTC时间,BCD格式*/
void UpdateRtcSoftTime(U8 *time)
{
U8 i;
struct doorTm *pT = {0};
struct doorTm ptTest;
pT = &ptTest;
S32 timeCounter = 0;
for(i=0; i<7; i++)
{
time[i] = BcdToHex(time[i]);
}
pT->tm_year = time[0] + 2000;
pT->tm_mon = time[1];
pT->tm_mday = time[2];
pT->tm_hour = time[3];
pT->tm_min = time[4];
pT->tm_sec = time[5];
timeCounter = DoorMktime(pT);
SetSoftRtcCounter(timeCounter);
}
#include <stdio.h>
/*读取RTC时间,BCD格式*/
void ReadSoftRtcTime(U8 *time)
{
U8 i;
struct doorTm *pT = {0};
S32 timeCounter = 0;
timeCounter = GetSoftRtcCounter();
struct doorTm ptTest;
pT = &ptTest;
DoorLocaltime(pT, timeCounter);
time[0] = pT->tm_year-2000;//<2000?
time[1] = pT->tm_mon;
time[2] = pT->tm_mday;
time[3] = pT->tm_hour;
time[4] = pT->tm_min;
time[5] = pT->tm_sec;
time[6] = pT->tm_wday;
for(i=0; i<7; i++)
{
time[i] = HexToBcd(time[i]);
printf("%02X ", time[i]);
}
printf("\r\n");
}
S32 SoftRtcTest1(void)
{
U8 time[16];
ReadSoftRtcTime(time);
return 0;
}
S32 SoftRtcTest2(void)
{
U8 time[16];
time[0] = 0x17;
time[1] = 0x02;
time[2] = 0x13;
time[3] = 0x12;
time[4] = 0x41;
time[5] = 0x55;
UpdateRtcSoftTime(time);
return 0;
}
int main(int argc, char** argv)
{
SoftRtcTest2();
SoftRtcTest1();
int weekDay = DoorGetWeekDay(20, 17, 10, 16);
printf("weekDay:%d\n", weekDay);
return 0;
}