MM32 RTC学习(兼容STM32)

RTC学习

RTC简述

实时时钟是一个独立的定时器。
RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。
修改计数器的值可以重新设置系统当前的时间和日期。
RTC模块和时钟配置系统(RCC_BDCR寄存器)处于后备区域,即在系统复位或从待机模式唤醒后, RTC的设置和时间维持不变。

思维导图

RTC框图

RTC电源框图(详细请看电源控制(PWM)章节)

认识理解

  • RTC在相应软件配置下,可以提供日历功能。
  • 有独立的时钟源与电源(RTC处在备份域)
  • RTC与计算机的时钟相似,系统断电(关闭Vdd电源),Vbak电源供电(可以是纽扣电池),这样重启计算机时钟依旧可以显示正确。

配置简单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();
}

配置简单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();
}

参考资料

[1]. MM32 miniboard资料

posted @ 2016-12-12 08:40  乔_木  阅读(1031)  评论(0编辑  收藏  举报