MCU中的RTC

第1章 RTC的作用

配置好MCU的外设RTC,MCU就可以产生一个年月日-时分秒的时钟;

第2章 时间戳

时间戳的定义:是指从 1970 年 1 月 1 日0时0分0秒开始,所经过的秒数(不考虑闰秒);

通俗点讲就是,从1970 年 1 月 1 日0时0分0秒开始按下一个秒表,秒表开始计数。计数的数值就是时间戳。

时间戳和时间是可以相互转换的。

北京的时区是东八区(UTC+8),属于中国标准时间(CST)

伦敦的时区是零时区(UTC+0),但在夏令时期间会调整为西一区(UTC+1),称为英国夏令时(BST)。因此:

  • 北京是东八区,比伦敦快 8小时 (冬季)或 7小时(夏季)。

2.1 获取系统时间

cpp 复制代码
#include <stdio.h>
#include <time.h>

int main() {
    time_t current_time;
    time(&current_time); // 获取当前时间,单位是秒

    printf("Current time in seconds since epoch: %ld\n", current_time); 
    return 0;
}

Current time in seconds since epoch: 1734007378

2.2 秒计数器转换为字符串

cpp 复制代码
#include <stdio.h>
#include <time.h>

int main() {
    time_t current_time;
    time(&current_time);

    char *time_str = ctime(&current_time); // 转换为可读字符串
    printf("Current time: %s", time_str);
    return 0;
}

Current time: Thu Dec 12 20:47:23 2024

2.3 秒计数器转换为日期时间

cpp 复制代码
#include <stdio.h>
#include <time.h>

int main() {
    time_t current_time;
    time(&current_time);

    struct tm *local_time = localtime(&current_time); // 转换为本地时间
    printf("Year: %d, Month: %d, Day: %d\n",
           local_time->tm_year + 1900,
           local_time->tm_mon + 1,
           local_time->tm_mday);
    return 0;
}

Year: 2024, Month: 12, Day: 12

2.4 日期时间转换为秒

cpp 复制代码
#include <stdio.h>
#include <time.h>

int main() {
    struct tm time_spec = {0};

    time_spec.tm_year = 2024 - 1900; // 年份从1900开始
    time_spec.tm_mon = 11;          // 12月(从0开始)
    time_spec.tm_mday = 31;
    time_spec.tm_hour = 23;
    time_spec.tm_min = 59;
    time_spec.tm_sec = 59;

    time_t specific_time = mktime(&time_spec);
    printf("Seconds since epoch: %ld\n", specific_time);
    return 0;
}

Seconds since epoch: 1735660799

2.5 日期时间转换为字符串

cpp 复制代码
#include <stdio.h>
#include <time.h>

int main() {
    time_t current_time;
    struct tm *local_time;

    // 获取当前时间
    time(&current_time);

    // 转换为本地时间结构
    local_time = localtime(&current_time);

    // 使用 asctime 转换为字符串
    char *time_str = asctime(local_time);

    printf("Current local time: %s", time_str);

    return 0;
}

Current local time: Thu Dec 12 20:52:31 2024

第3章 RTC的详细描述

配置好RTC后,MUC就会产生一个日期时间。

stm32F4的RTC可以由LSE和LSI供给时钟频率

3.1 RTC的硬件结构

  • 分频器分频后的频率为1HZ,也就是1s。

3.2 RTC的时钟体系

  1. 使用LSE时钟(32.768KHZ)给RTC供给时钟频率;
  2. 通过128分频,得到的时钟频率为256HZ;
  3. 通过预分配器再进行256分频,得到1Hz;
  4. 经过多路选择器选择哪个时钟源;
  5. 配置自动重装载寄存器,决定定时时间,定时时间到产生一个中断标志位(时间唤醒中断标志位);
  6. 时间唤醒中断标志位;

3.3 RTC的电源供电

第4章 RTC驱动

相关推荐
文火冰糖的硅基工坊3 分钟前
[嵌入式系统-146]:五次工业革命对应的机器人形态的演进、主要功能的演进以及操作系统的演进
前端·网络·人工智能·嵌入式硬件·机器人
老六哥_AI助理指南14 分钟前
为什么AI会改变单片机的未来?
人工智能·单片机·嵌入式硬件
点灯小铭15 分钟前
基于单片机的智能家居多参数环境监测与联动报警系统设计
单片机·mongodb·毕业设计·智能家居·课程设计·期末大作业
点灯小铭20 分钟前
基于单片机与上位机的智能宠物喂食管理系统设计
单片机·嵌入式硬件·毕业设计·课程设计·宠物
Lester_110121 分钟前
嵌入式学习笔记 - 瑞萨单片机
单片机·嵌入式硬件
麻辣长颈鹿Sir22 分钟前
单片机中的机器周期、指令周期、总线周期的联系和区别
单片机·嵌入式硬件·时钟周期·指令周期·机器周期·总线周期·嵌入式指令时间
蜀黍@猿25 分钟前
【GD32】输出时钟配置
stm32·单片机·嵌入式硬件
二进制coder2 小时前
深入浅出:I²C多路复用器PCA9546详解 - 解决地址冲突,扩展你的I²C总线
c语言·开发语言·单片机
Lhj06163 小时前
stm32 can错误中断不处理
stm32·单片机·嵌入式硬件
啃硬骨头3 小时前
AURIX-TC3xx-GTM详解六-DPLL(Digital PLL Module)
单片机·嵌入式硬件