在蓝桥杯中,RTC也是有时会常考的一个考察点,这个RTC也不难。主要也就是时钟的配置。时钟的话,也有很多方法去配置。但是考虑到有时会要求一定要用RTC去配置,这个RTC的配置也还是要会配置的。
这个配置,官方给的固件库中代码还是给出来的。只要把关键部分CTRL+C、CTRL+V
就可以了的。
例如:配置一个初始时间为23:59:30
.h文件
#include "stm32f10x.h"
#define SS 23
#define FF 59
#define MM 30
void rtc_init(void);
void window(void);
.c
#include "rtc.h"
#include "lcd.h"
#include "stdio.h"
unsigned long time=SS*3600+FF*60+MM;
void rtc_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable the LSI OSC */
RCC_LSICmd(ENABLE);
/* Wait till LSI is ready */
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
{}
/* Select the RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Enable the RTC Second */
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(40000);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
RTC_SetCounter(time);
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void window()
{
u8 str[20];
time = RTC_GetCounter();
sprintf(str1,"%0.2d-%0.2d-%0.2d",time/3600,(time%3600)/60,(time%3600)%60);
LCD_DisplayStringLine(Line2, str); //第二行显示
}
/*中断服务函数*/
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
if(RTC_GetCounter()==86400) //检测到24:00:00
{
RTC_SetCounter(0x0); //清零
}
/* Clear Interrupt pending bit */
RTC_ClearITPendingBit(RTC_FLAG_SEC);
}
}
快速配置:打开固件库V3.5:6-STM32固件库代码V3.5版\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\RTC\LSI_Calib的main文件
直接粘贴复制这部分
在加上RTC_SetCounte();
函数
中断服务函数直接打开stm32f10x_it这个c文件,复制里面的
在加上里面的中断条件,即:到24:00:00时清零。
最后就是对于rtc显示在lcd上的处理的了。