生成一个关于STM32L431的RTC程序,程序主频80MHZ,采用 24 小时制,使用 LSI作为 RTC 的时钟源
时间: 2024-03-02 17:51:28 浏览: 117
好的,以下是一个关于STM32L431的RTC程序,程序主频80MHZ,采用24小时制,使用LSI作为RTC的时钟源:
```c
#include "stm32l431xx.h"
int main(void) {
// 使能PWR和RTC时钟
RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;
RCC->APB1ENR1 |= RCC_APB1ENR1_RTCAPBEN;
// 使能LSI时钟并等待其稳定
RCC->CSR |= RCC_CSR_LSION;
while(!(RCC->CSR & RCC_CSR_LSIRDY));
// 关闭RTC写保护
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
RTC->ISR |= RTC_ISR_INIT;
while(!(RTC->ISR & RTC_ISR_INITF));
// 初始化RTC寄存器
RTC->PRER = (uint32_t)(((uint32_t)799 << 16) | (uint32_t)1249); // LSI时钟预分频器
RTC->TR = (uint32_t)(((uint32_t)0 << RTC_TR_SU_Pos) | ((uint32_t)0 << RTC_TR_ST_Pos) | ((uint32_t)0 << RTC_TR_MNU_Pos) | ((uint32_t)0 << RTC_TR_MNT_Pos) | ((uint32_t)0 << RTC_TR_HU_Pos) | ((uint32_t)0 << RTC_TR_HT_Pos)); // 时间寄存器
RTC->DR = (uint32_t)(((uint32_t)1 << RTC_DR_WDU_Pos) | ((uint32_t)1 << RTC_DR_YU_Pos) | ((uint32_t)2 << RTC_DR_YT_Pos) | ((uint32_t)0 << RTC_DR_MU_Pos) | ((uint32_t)1 << RTC_DR_MT_Pos) | ((uint32_t)2 << RTC_DR_DU_Pos) | ((uint32_t)8 << RTC_DR_DT_Pos)); // 日期寄存器
RTC->CR &= ~RTC_CR_FMT; // 24小时制
RTC->ISR &= ~RTC_ISR_INIT;
// 启用RTC
RTC->CR |= RTC_CR_BYPSHAD; // 关闭后备寄存器
RTC->CR |= RTC_CR_WUTE; // 使能Wakeup定时器
RTC->CR |= RTC_CR_RSF; // 使能时间和日期的同步
RTC->ISR &= ~RTC_ISR_INITS;
// 等待RTC同步完成
while(!(RTC->ISR & RTC_ISR_RSF));
while(1) {
// 在此处编写其他代码
}
}
```
这个程序使用LSI作为RTC的时钟源,并且采用24小时制。你可以在程序中添加其他代码来实现你的需求。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)