IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> MCU的RTC功能(32位计数器)(亲测有用包括年月日星期时分秒) -> 正文阅读

[嵌入式]MCU的RTC功能(32位计数器)(亲测有用包括年月日星期时分秒)

GD32F303的RTC功能(包括闰年平年的年月日星期时分秒)(时间设置和获取正确,适用于使用32位的计数器的MCU)

针对RTC的设置和获取,应该测试一些特殊值如:闰年的最后一天2020.12.31,闰年的2月28.29,平年的2月28,1月30,7月30.31,8月30.31等,如果这些日期没有问题,基本确定是没有问题的;

以下为我的代码,已测试全部,没有问题,除了上面日期,还包括测试这些上面日期在(1)设置后断电后,等到下一日再上电时间;(2)设置后断电后,未到下一日的再上电时间;(3)设置后断电,等到过了第二日再上电时间;功能全部正常;

#include “gd32f30x.h”
#include “RTC.h”
#include “LCD.h”

//enter the second interruption,set the second interrupt flag to 1
volatile uint32_t timedisplay;

//月份数据表
uint8_t const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表
//平年的月份日期表
const uint8_t mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};

void RTC_Init(void)
{

	if (bkp_read_data(BKP_DATA_0) != 0xA5A5){
		//RTC configuration
		rtc_configuration();
		//adjust time by values entred by the user on the hyperterminal
		//time_adjust();
		RTC_Set(2011,01,01,0,0,0);//默认时间
			
		bkp_write_data(BKP_DATA_0, 0xA5A5);
	}else{
		/* check if the power on reset flag is set */
  if (rcu_flag_get(RCU_FLAG_PORRST) != RESET){
  //    printf("\r\n\n Power On Reset occurred....");
  }else if (rcu_flag_get(RCU_FLAG_SWRST) != RESET){
        /* check if the pin reset flag is set */
  //    printf("\r\n\n External Reset occurred....");
  }
			
		/* allow access to BKP domain */
  rcu_periph_clock_enable(RCU_PMU);
  pmu_backup_write_enable();
    
  //printf("\r\n No need to configure RTC....");
  /* wait for RTC registers synchronization */
  rtc_register_sync_wait();
  rtc_lwoff_wait();
  /* enable the RTC second */
  rtc_interrupt_enable(RTC_INT_SECOND);
  //rtc_interrupt_enable(RTC_INT_ALARM);
  /* wait until last write operation on RTC registers has finished */
  rtc_lwoff_wait();
	}
		
	//clear reset flags
	rcu_all_reset_flag_clear();

	RTC_Get();
	
	
	nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
	nvic_irq_enable(RTC_IRQn,1,0);

}

//功能描述:判断是否是闰年函数
//输入:年份
//输出:该年份是不是闰年.1:是, 0:不是
uint8_t Is_Leap_Year(uint16_t year)
{

	if(((year%4==0)&&(year%100!=0))||((year%100==0)&&(year%400==0))){
			return 1;
	}else{
			return 0;
	}

}

//功能描述:设置时钟
//把输入的时钟转换为秒钟
//以1970年1月1日为基准
//1970~2099为合法年份
//返回值:0:成功,其他:错误
uint8_t RTC_Set(uint16_t syear,uint8_t smon,uint8_t sday,uint8_t hour,uint8_t min,uint8_t sec)
{
uint16_t t;
uint32_t seccount=0;

if(syear<2000||syear>2099)return 1;//syear范围1970-2099,此处设置范围为2000-2099       

for(t=1970;t<syear;t++) //所有年份的秒钟相加
{
			if(Is_Leap_Year(t)){
				seccount+=31622400;//闰年的秒钟数
			}else{
				seccount+=31536000;                    //平年的秒钟数
			}
}

	smon-=1;
	for(t=0;t<smon;t++)         //把前面月份的秒钟数相加
{
			seccount+=(uint32_t)mon_table[t]*86400;//月份秒钟数相加
    if((Is_Leap_Year(syear)) && (t==1)){
					seccount+=86400;//闰年2月份增加一天的秒钟数        
			}
}

seccount+=(uint32_t)(sday-1)*86400;//把前面日期的秒钟数相加
seccount+=(uint32_t)hour*3600;//小时秒钟数
	seccount+=(uint32_t)min*60;      //分钟秒钟数
seccount+=sec;//最后的秒钟加上去

/* change the current time */
  rtc_counter_set(seccount);
  rtc_lwoff_wait();

return 0;     

}

//得到当前的时间
//返回值:0:成功,其他:错误
uint8_t RTC_Get(void)
{
static uint16_t daycnt=0;
uint32_t timecount=0;
uint32_t temp=0;
uint16_t temp1=0;

timecount=rtc_counter_get();
	Current_Clock = timecount;         

	temp=timecount/86400;   

if(daycnt!=temp)
{      
			daycnt=temp;
			temp1=1970;  //1970年开始

    while(temp>=365)
			{                         
					if(Is_Leap_Year(temp1))//闰年
					{
							if(temp>=366){
								temp-=366;//闰年的秒钟数
							}else {
								break;
							} 
        }
					else{ 
							temp-=365;       //平年
					}
        temp1++; 

    }  
			Current_year=temp1;//得到年份
			temp1=0;
			
			while(temp>=28)
    {
					if(Is_Leap_Year(Current_year)&&temp1==1)
        {
							if(temp>=29){
								temp-=29;//闰年的秒钟数
							}else{
								break;
							}
        }
        else
        {
            if(temp>=mon_table[temp1]){
								temp-=mon_table[temp1];//平年
							}else{
								break;
							}
        }

        temp1++; 
    }
			Current_month=temp1+1;//得到月份
    Current_date=temp+1;  //得到日期
	}
temp=timecount%86400;     //得到秒钟数    
Current_hour=temp/3600;     //小时
Current_min=(temp%3600)/60; //分钟     
Current_sec=(temp%3600)%60; //秒钟
	Current_week = CaculateWeekDay(Current_year,Current_month,Current_date)+1;

return 0;

}

/*
Return 0~6: 分别对应星期一~星期日
*/
uint8_t CaculateWeekDay(int y,int m,int d)
{
int week;

	if((m==1)||(m==2)){
		m+=12;
		y--;
	}
	week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
	
	return week;

}

/*!
\brief configure the RTC
\param[in] none
\param[out] none
\retval none
/
void rtc_configuration(void)
{
/
enable PMU and BKPI clocks /
rcu_periph_clock_enable(RCU_BKPI);
rcu_periph_clock_enable(RCU_PMU);
/
allow access to BKP domain */
pmu_backup_write_enable();

/* reset backup domain */
bkp_deinit();

/* enable LXTAL */
rcu_osci_on(RCU_LXTAL);
/* wait till LXTAL is ready */
rcu_osci_stab_wait(RCU_LXTAL);

/* select RCU_LXTAL as RTC clock source */
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);

/* enable RTC Clock */
rcu_periph_clock_enable(RCU_RTC);

/* wait for RTC registers synchronization */
rtc_register_sync_wait();

/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();

/* enable the RTC second interrupt*/
rtc_interrupt_enable(RTC_INT_SECOND);
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();

/* set RTC prescaler: set RTC period to 1s */
rtc_prescaler_set(32767); 

/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();

}
/!
\brief adjust time
\param[in] none
\param[out] none
\retval none
/
void time_adjust(void)
{
uint32_t temp = 0;
/
wait until last write operation on RTC registers has finished /
rtc_lwoff_wait();
//temp = time_regulate();
temp = 21
31104000+5
2592000+2886400+93600 + 4660 + 33;
/
change the current time /
rtc_counter_set(temp);
rtc_lwoff_wait();
/
set the alarm time = currenttime + 10 second*/

/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();

}

/*!
\brief display the current time
\param[in] timeVar: RTC counter value
\param[out] none
\retval none
*/
void time_display(uint32_t timevar)
{
uint32_t thh = 0, tmm = 0, tss = 0;

	/*Current_year	= (timevar / 31104000);
	Current_month	= ((timevar % 31104000) / 2592000);
	Current_date	=	((timevar % 2592000) / 86400);
	Current_hour = (timevar % 86400) / 3600;
	Current_min = (timevar % 3600) / 60;
	Current_sec = (timevar % 3600) % 60;
	Current_week = CaculateWeekDay(Current_year+2000,Current_month,Current_date)+1;
	*/
/* compute  hours */
thh = (timevar % 86400) / 3600;
/* compute minutes */
tmm = (timevar % 3600) / 60;
/* compute seconds */
tss = (timevar % 3600) % 60;

//printf(" Time: %0.2d:%0.2d:%0.2d\r\n", thh, tmm, tss);
	if((PageScrnPoint==0x01)&&(!Display_Watch_Logo)){
		Watch_Show_Time(thh,tmm,tss);
	}

}
void time_show(void)
{
//printf("\n\r");

/* infinite loop */
while (1){
    /* if 1s has paased */
    if (timedisplay == 1){
        /* display current time */
					Current_Clock=rtc_counter_get();
				
					//temp = 5*31104000+12*2592000+30*86400+23*3600 + 59*60 + 3;
					
					Current_year	= Current_Clock / 31104000;
					Current_month	= (Current_Clock % 31104000) / 2592000;
					Current_date	=	(Current_Clock % 2592000) / 86400;
					Current_hour = (Current_Clock % 86400) / 3600;
					Current_min = (Current_Clock % 3600) / 60;
					Current_sec = (Current_Clock % 3600) % 60;
				
        time_display(Current_Clock);
        timedisplay = 0;
    }
}

}
/!
\brief this function handles RTC global interrupt request
\param[in] none
\param[out] none
\retval none
/
void RTC_IRQHandler(void)
{
if (rtc_flag_get(RTC_FLAG_SECOND) != RESET){
/
clear the RTC second interrupt flag
/
rtc_flag_clear(RTC_FLAG_SECOND);
/* enable time update */
timedisplay = 1;

			Current_year_old=Current_year;
			Current_month_old=Current_month;
			Current_date_old=Current_date;
			//Current_Clock=rtc_counter_get();
			RTC_Get();
			timer_disable(TIMER1);
			nvic_irq_disable(EXTI5_9_IRQn);
    time_display(Current_Clock);
			timer_enable(TIMER1);
			nvic_irq_enable(EXTI5_9_IRQn, 0U, 0U);
		
    
			if((Current_hour==0)&&(Current_min==0)&&(Current_sec==0)){
						//watch_set_date(Current_year+2000,Current_month,Current_date,Current_week);
						watch_set_date(Current_year,Current_month,Current_date,Current_week);
			}

}

}

Alt

带尺寸的图片: Alt

居中的图片: Alt

居中并且带尺寸的图片: Alt

当然,我们为了让用户更加便捷,我们增加了图片拖拽功能。

如何插入一段漂亮的代码片

博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片.

// An highlighted block
var foo = 'bar';

生成一个适合你的列表

  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

创建一个表格

一个简单的表格是这么创建的:

项目Value
电脑$1600
手机$12
导管$1

设定内容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列第二列第三列
第一列文本居中第二列文本居右第三列文本居左

SmartyPants

SmartyPants将ASCII标点字符转换为“智能”印刷标点HTML实体。例如:

TYPEASCIIHTML
Single backticks'Isn't this fun?'‘Isn’t this fun?’
Quotes"Isn't this fun?"“Isn’t this fun?”
Dashes-- is en-dash, --- is em-dash– is en-dash, — is em-dash

创建一个自定义列表

Markdown
Text-to- HTML conversion tool
Authors
John
Luke

如何创建一个注脚

一个具有注脚的文本。1

注释也是必不可少的

Markdown将文本转换为 HTML

KaTeX数学公式

您可以使用渲染LaTeX数学表达式 KaTeX:

Gamma公式展示 Γ ( n ) = ( n ? 1 ) ! ? n ∈ N \Gamma(n) = (n-1)!\quad\forall n\in\mathbb N Γ(n)=(n?1)!?nN 是通过欧拉积分

Γ ( z ) = ∫ 0 ∞ t z ? 1 e ? t d t ? . \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,. Γ(z)=0?tz?1e?tdt.

你可以找到更多关于的信息 LaTeX 数学表达式here.

新的甘特图功能,丰富你的文章

Mon 06 Mon 13 Mon 20 已完成 进行中 计划一 计划二 现有任务 Adding GANTT diagram functionality to mermaid
  • 关于 甘特图 语法,参考 这儿,

UML 图表

可以使用UML图表进行渲染。 Mermaid. 例如下面产生的一个序列图:

张三 李四 王五 你好!李四, 最近怎么样? 你最近怎么样,王五? 我很好,谢谢! 我很好,谢谢! 李四想了很长时间, 文字太长了 不适合放在一行. 打量着王五... 很好... 王五, 你怎么样? 张三 李四 王五

这将产生一个流程图。:

链接
长方形
圆角长方形
菱形
  • 关于 Mermaid 语法,参考 这儿,

FLowchart流程图

我们依旧会支持flowchart的流程图:

Created with Rapha?l 2.3.0 开始 我的操作 确认? 结束 yes no
  • 关于 Flowchart流程图 语法,参考 这儿.

导出与导入

导出

如果你想尝试使用此编辑器, 你可以在此篇文章任意编辑。当你完成了一篇文章的写作, 在上方工具栏找到 文章导出 ,生成一个.md文件或者.html文件进行本地保存。

导入

如果你想加载一篇你写过的.md文件,在上方工具栏可以选择导入功能进行对应扩展名的文件导入,
继续你的创作。


  1. 注脚的解释 ??

  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-08-05 17:31:05  更:2021-08-05 17:32:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 15:43:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码