RTC学习

RTC简述

思维导图

MM32 RTC学习(兼容STM32)-LMLPHP

RTC框图

MM32 RTC学习(兼容STM32)-LMLPHP

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

MM32 RTC学习(兼容STM32)-LMLPHP

认识理解

配置简单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资料

05-02 06:50