• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C语言 > ST17H26移植软时钟代码

ST17H26移植软时钟代码

作者:qinrenzhi 字体:[增加 减小] 来源:互联网 时间:2017-10-15

qinrenzhi通过本文主要向大家介绍了等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

在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;
}

 

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2017-05-28C++读取INI配置文件类实例详解
  • 2017-05-28vector与map的erase()函数详细解析
  • 2017-05-28C语言的指针类型详细解析
  • 2017-05-28c语言线程终止练习示例
  • 2017-05-28C++判断pe文件实例
  • 2017-05-28C++删除指定文件夹下N天及之前日志文件的方法
  • 2017-05-28c++ 尽量不要使用#define 而是用const、enum、inline替换。
  • 2022-04-30C语言代码中的空白符
  • 2017-05-28C语言数据结构中数制转换实例代码
  • 2017-05-28C++调试追踪class成员变量的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 显示任何进程加载的DLL文件的代码
    • C/C++数据对齐详细解析
    • VC++实现文件与应用程序关联的方法(注册表修改)
    • C++中基类和派生类之间的转换实例教程
    • C语言采用文本方式和二进制方式打开文件的区别分析
    • 如何在程序中判断VS的版本(实现方法详解)
    • C++中的常对象与常对象成员详解
    • C语言 数据结构中求解迷宫问题实现方法
    • C++中4种强制类型转换的区别总结
    • C语言之栈和堆(Stack && Heap)的优缺点及其使用区别

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有