打印
[MM32软件]

MM32 RTC学习(兼容STM32)

[复制链接]
1143|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
杨寅辉|  楼主 | 2019-11-30 21:25 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
RTC学习
RTC简述
实时时钟是一个独立的定时器。
RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。
修改计数器的值可以重新设置系统当前的时间和日期。
RTC模块和时钟配置系统(RCC_BDCR寄存器)处于后备区域,即在系统复位或从待机模式唤醒后, RTC的设置和时间维持不变。

使用特权

评论回复
沙发
杨寅辉|  楼主 | 2019-11-30 21:26 | 只看该作者
思维导图



使用特权

评论回复
板凳
杨寅辉|  楼主 | 2019-11-30 21:26 | 只看该作者
RTC框图


使用特权

评论回复
地板
杨寅辉|  楼主 | 2019-11-30 21:27 | 只看该作者
RTC电源框图(详细请看电源控制(PWM)章节)

使用特权

评论回复
5
杨寅辉|  楼主 | 2019-11-30 21:27 | 只看该作者
认识理解
RTC在相应软件配置下,可以提供日历功能。
有独立的时钟源与电源(RTC处在备份域)
RTC与计算机的时钟相似,系统断电(关闭Vdd电源),Vbak电源供电(可以是纽扣电池),这样重启计算机时钟依旧可以显示正确。

使用特权

评论回复
6
杨寅辉|  楼主 | 2019-11-30 21:28 | 只看该作者
配置简单RTC(寄存器版)(注意修改头文件)
#include "all.h"

void delay(uint32_t num)
{
    uint32_t i;
    for(i=0;i<num;i++);
}

void rtc_work_cfg()
{
    uint32_t    scaler;
    uint32_t    cnt;

    RCC->APB1ENR |= 1<<28;   //Enable the PWREN clock.
    RCC->APB1ENR |= 1<<27;   //Enable the PWREN clock.
    PWR->CR |= 1<<8;         //Enable access to the RTC and backup registers.

    RCC->BDCR |= 1<<16;    //Force the Backup domain reset.
    RCC->BDCR &= ~(1<<16); //Release the Backup domain reset.
    RCC->BDCR |= 1<<15;    //Enable RTC clock.
    RCC->BDCR |= 1<<8;     //select LES as RTC clock source.
    RCC->BDCR |= 1<<0;     //External low-speed oscillar enable.

    while(!(RCC->BDCR & 0x1<<1));  //External low-speed clock ready flag.
    while(!(RTC->CRL & 1<<5)); //Wait until last write operation on RTC registers has finished.
   
    RTC->CRL |= 1<<4;     //Enter the RTC configuration mode.
    RTC->ALRH = 0x0;      //Set the RTC alarm value.
    RTC->ALRL = 0x300;   
    RTC->PRLH = 0x0;      //Set the RTC prescaler value.
    RTC->PRLL = 0x10;
    RTC->CNTH = 0x0;      //Set the RTC counter value.
    RTC->CNTL = 0x50;
    RTC->CRL &= ~(1<<4);  //Exit from the RTC configuration mode.
   
    while(!(RTC->CRL & 1<<5));  //Wait until last write operation on RTC registers has finished.
    while(!(RTC->CRL & 1<<3));  //wait until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) are synchronized with RTC APB clock.

    delay(1000);
    cnt  = RTC->CNTL;
    cnt |= RTC->CNTH << 16;
    scaler  = RTC->PRLL;
    scaler |= RTC->PRLH << 16;

    delay(100);
    printf_info("Prescaler = %x,cnt = %x\n",scaler,cnt);
}

void main()
{
    rtc_work_cfg();
}


使用特权

评论回复
7
杨寅辉|  楼主 | 2019-11-30 21:28 | 只看该作者
配置简单RTC(库函数版)(注意修改头文件)
#include "all.h"

void delay(uint32_t num)
{
    uint32_t i;
    for(i=0;i<num;i++);
}

void rtc_work_cfg()
{
    uint32_t    scaler;
    uint32_t    cnt;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);
    PWR_BackupAccessCmd(ENABLE);  // Enable or disables access to the RTC and backup registers.

    RCC_BackupResetCmd(ENABLE);
    RCC_BackupResetCmd(DISABLE);
    RCC_RTCCLKCmd(ENABLE);        //Enable or disables the RTC clock.
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //Configure the RTC clock (RTCCLK).
    RCC_LSEConfig(RCC_LSE_ON);    //Configure the External Low Speed oscillator (LSE).

    while(!(RCC->BDCR & 0x1<<1)); // //External low-speed clock ready flag.
   
    RTC_WaitForLastTask();   //Wait until last write operation on RTC registers has finished.
    RTC_EnterConfigMode();   //Enter the RTC configuration mode.

    RTC_SetPrescaler(0x80);  //Set the RTC prescaler value.
    RTC_SetCounter(0x50);    //Set the RTC counter value.
    RTC_SetAlarm(0x150);     //Set the RTC alarm value.
   
    RTC_ExitConfigMode();     //Exit from the RTC configuration mode.
    RTC_WaitForLastTask();   //Wait until last write operation on RTC registers has finished.
    RTC_WaitForSynchro();    //wait until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) are synchronized with RTC APB clock.

    delay(8000);
//***************************************************  
    RTC_WaitForLastTask();   //Wait until last write operation on RTC registers has finished.
    RTC_EnterConfigMode();   //Enter the RTC configuration mode.
    RTC_SetCounter(0x500);   //Set the RTC counter value.
    RTC_ExitConfigMode();    //Exit from the RTC configuration mode.
    RTC_WaitForLastTask();   //Wait until last write operation on RTC registers has finished.
    RTC_WaitForSynchro();    //wait until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) are synchronized with RTC APB clock.
//***************************************************  

    delay(8000);
    cnt = RTC_GetCounter();
    scaler = RTC_GetDivider();

    delay(100);
    printf_info("Prescaler = %x,cnt = %x\n",scaler,cnt);
}

void main()
{
    rtc_work_cfg();
}


使用特权

评论回复
8
labasi| | 2019-12-4 15:28 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
9
keaibukelian| | 2019-12-4 15:31 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
10
heimaojingzhang| | 2019-12-4 15:35 | 只看该作者
非常感谢楼主分享

使用特权

评论回复
11
tfqi| | 2019-12-16 13:14 | 只看该作者
非常感谢分享

使用特权

评论回复
12
wiba| | 2019-12-16 13:19 | 只看该作者
非常感谢分享

使用特权

评论回复
13
zljiu| | 2019-12-16 13:22 | 只看该作者
非常感谢分享

使用特权

评论回复
14
Sunriver_Yao| | 2020-3-21 12:07 | 只看该作者
不能叫做“兼容”!是能说是个子集!洒家需要的RTC高频中断(128Hz,用于心率采样),加上本身的1Hz中断(用于收发数据定时),灵动就不能实现。然,ST的MCU可以!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

295

帖子

2

粉丝