#蓝桥杯嵌入式#RTC的配置

参考资料:
      蓝桥杯嵌入式备赛手册
      蓝桥杯嵌入式开发经验分享(8.实时时钟讲解)
总目录:目录

如果实在官方给的LCD例程基础上写的话,需要加上
STM32固件库代码V3.5版\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver\src
      stm32f10x_rtc.c
      stm32f10x_bkp.c
      stm32f10x_pwr.c
      misc.c

RTC计时

  • 在比赛提供的V3.5库的"Project->STM32F10x_StdPeriph_Examples->RTC->LSI_Calib"文件夹下,打开"main.c

  • 复制NVIC_Configuration()和RTC_Configuration()

  • 最后两行BKP_TamperPinCmd()和BKP_RTCOutputConfig()不要复制,LCD与RTC引脚复用,如果复制会因安全保护导致LCD不能显示
    在这里插入图片描述

  • stm32f10x_it.c下,复制中断操作函数:
    在这里插入图片描述

  • 参考例程2:Project->STM32F10x_StdPeriph_Examples->RTC->Calendar下main.c

  • 复制Time_Adjust()函数,把Time_Regulate()函数中的return代入

  • 复制Time_Display()函数,函数参数用u32变量RTC_GetCounter()代替

  • 把Time_Display()函数中的23:59:59置0写到中断函数中(RTC_GetCounter())

  • 两位整数输出0.2d
    在这里插入图片描述

RTC.c

#include "RTC.h"
#include "lcd.h"
#include "stdio.h"


void RTC_Init()
{
	NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
	//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
	
	/* Enable PWR and BKP clocks */
  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();
}

void RTC_IRQHandler(void)
{
  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    RTC_ClearITPendingBit(RTC_IT_SEC);
		if (RTC_GetCounter() == 0x0001517F)
		{
			RTC_SetCounter(0x0);
			RTC_WaitForLastTask();
		}
  }
}

void Time_Adjust(u8 Tmp_HH,u8 Tmp_MM,u8 Tmp_SS )
{
  RTC_WaitForLastTask();
  RTC_SetCounter((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
  RTC_WaitForLastTask();
}

u8 buffer[20];
void Time_Display()
{
  uint32_t THH = 0, TMM = 0, TSS = 0, TimeVar = RTC_GetCounter();
  
  /* Compute  hours */
  THH = TimeVar / 3600;
  /* Compute minutes */
  TMM = (TimeVar % 3600) / 60;
  /* Compute seconds */
  TSS = (TimeVar % 3600) % 60;

  sprintf((char*)buffer,"Time: %0.2d:%0.2d:%0.2d", THH, TMM, TSS);
	LCD_DisplayStringLine(Line4,buffer);
}

main.c

#include "stm32f10x.h"
#include "lcd.h"
#include "RTC.h"

u32 TimingDelay = 0;

void Delay_Ms(u32 nTime);

//Main Body
int main(void)
{
	SysTick_Config(SystemCoreClock/1000);

	Delay_Ms(200);
	
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	RTC_Init();
	Time_Adjust(23,59,59);
	
	while(1)
	{
		Time_Display();
		Delay_Ms(500);
	}
}

//
void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

显示结果

  • 电路板上LCD显示如下:
    在这里插入图片描述
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值