[MM32软件] MM32 RTC学习(兼容STM32)

[复制链接]
 楼主| 杨寅辉 发表于 2019-11-30 21:25 | 显示全部楼层 |阅读模式
RTC学习
RTC简述
实时时钟是一个独立的定时器。
RTC模块拥有一组连续计数的计数器,在相应软件配置下,可提供时钟日历的功能。
修改计数器的值可以重新设置系统当前的时间和日期。
RTC模块和时钟配置系统(RCC_BDCR寄存器)处于后备区域,即在系统复位或从待机模式唤醒后, RTC的设置和时间维持不变。

 楼主| 杨寅辉 发表于 2019-11-30 21:26 | 显示全部楼层
思维导图
847278-20161212083833839-360530369.png


 楼主| 杨寅辉 发表于 2019-11-30 21:26 | 显示全部楼层
RTC框图

847278-20161212083758854-1872867150.png
 楼主| 杨寅辉 发表于 2019-11-30 21:27 | 显示全部楼层
RTC电源框图(详细请看电源控制(PWM)章节)
847278-20161212090543933-1561297255.png
 楼主| 杨寅辉 发表于 2019-11-30 21:27 | 显示全部楼层
认识理解
RTC在相应软件配置下,可以提供日历功能。
有独立的时钟源与电源(RTC处在备份域)
RTC与计算机的时钟相似,系统断电(关闭Vdd电源),Vbak电源供电(可以是纽扣电池),这样重启计算机时钟依旧可以显示正确。

 楼主| 杨寅辉 发表于 2019-11-30 21:28 | 显示全部楼层
配置简单RTC(寄存器版)(注意修改头文件)
  1. #include "all.h"

  2. void delay(uint32_t num)
  3. {
  4.     uint32_t i;
  5.     for(i=0;i<num;i++);
  6. }

  7. void rtc_work_cfg()
  8. {
  9.     uint32_t    scaler;
  10.     uint32_t    cnt;

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

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

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

  33.     delay(1000);
  34.     cnt  = RTC->CNTL;
  35.     cnt |= RTC->CNTH << 16;
  36.     scaler  = RTC->PRLL;
  37.     scaler |= RTC->PRLH << 16;

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

  41. void main()
  42. {
  43.     rtc_work_cfg();
  44. }


 楼主| 杨寅辉 发表于 2019-11-30 21:28 | 显示全部楼层
配置简单RTC(库函数版)(注意修改头文件)
  1. #include "all.h"

  2. void delay(uint32_t num)
  3. {
  4.     uint32_t i;
  5.     for(i=0;i<num;i++);
  6. }

  7. void rtc_work_cfg()
  8. {
  9.     uint32_t    scaler;
  10.     uint32_t    cnt;

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

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

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

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

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

  39.     delay(8000);
  40.     cnt = RTC_GetCounter();
  41.     scaler = RTC_GetDivider();

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

  45. void main()
  46. {
  47.     rtc_work_cfg();
  48. }


labasi 发表于 2019-12-4 15:28 | 显示全部楼层
非常感谢楼主分享
keaibukelian 发表于 2019-12-4 15:31 | 显示全部楼层
非常感谢楼主分享
heimaojingzhang 发表于 2019-12-4 15:35 | 显示全部楼层
非常感谢楼主分享
tfqi 发表于 2019-12-16 13:14 | 显示全部楼层
非常感谢分享
wiba 发表于 2019-12-16 13:19 | 显示全部楼层
非常感谢分享
zljiu 发表于 2019-12-16 13:22 | 显示全部楼层
非常感谢分享
Sunriver_Yao 发表于 2020-3-21 12:07 | 显示全部楼层
不能叫做“兼容”!是能说是个子集!洒家需要的RTC高频中断(128Hz,用于心率采样),加上本身的1Hz中断(用于收发数据定时),灵动就不能实现。然,ST的MCU可以!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

39

主题

295

帖子

2

粉丝
快速回复 返回顶部 返回列表