RTC相关设置与代码配置

如图设置,并设置年月日,时分秒 

void RTC_CalendarRead(void)
{
	RTC_DateTypeDef sdatestructureget;
	RTC_TimeTypeDef stimestructureget;
	
	HAL_RTC_GetTime(&hrtc,&stimestructureget,RTC_FORMAT_BIN);
	HAL_RTC_GetDate(&hrtc,&sdatestructureget,RTC_FORMAT_BIN);
	RTC_Year=sdatestructureget.Year;
	RTC_Month=sdatestructureget.Month;
	RTC_Day=sdatestructureget.Date;
	RTC_Hour=stimestructureget.Hours;
	RTC_Min=stimestructureget.Minutes;
	RTC_Sec=stimestructureget.Seconds;
	
}

void RTC_SetTime(uint8_t HH,uint8_t MM,uint8_t SS)
{
	RTC_TimeTypeDef sTime = {0};
	sTime.Hours = HH;
	sTime.Minutes = MM;
	sTime.Seconds = SS;
	
	if(HAL_RTC_SetTime(&hrtc,&sTime,RTC_FORMAT_BIN)!=HAL_OK)
	{
		Error_Handler();
	}
	
}

void RTC_SetDate(uint8_t YY,uint8_t MM,uint8_t DD)
{
	RTC_DateTypeDef sDate = {0};
	sDate.Month = MM;
	sDate.Date = DD;
	sDate.Year = YY;
	
	if(HAL_RTC_SetDate(&hrtc,&sDate,RTC_FORMAT_BIN)!=HAL_OK)
	{
		Error_Handler();
	}
	
}

 在主程序中使用上述三个函数

1.设置日期及时间

	/* RTC Set */
	RTC_SetDate(24,3,18);
	RTC_SetTime(23,59,30);

 2.读取时间

if(RTC_ref==1)
	  {
		RTC_ref=0;
		RTC_CalendarRead();
	  }

 !!!注意!!!

RTC中相关时间及日期设置在RTC.c中有相关提示,在编写过程中可以复制参考其中代码,但是要关注相关变量引用是否正确的问题,相关结构体的编写问题也要注意

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值