当前位置: 首页 > news >正文

wordpress扫描河北seo推广公司

wordpress扫描,河北seo推广公司,零基础学习网站建设,如何建立一个网站查询数据RTC 目录 RTC 回顾 RTC 如何实现RTC制作一个时钟日历 代码编写 rtc.c完整代码 模块开发的步骤: 1、找文档 2、 在文档里面找通信方式,通信过程(协议) 3、代码> -- 前面学的是模块的开发,串口类,I…

RTC

目录

RTC

回顾

RTC

如何实现RTC制作一个时钟日历

代码编写

rtc.c完整代码



模块开发的步骤:

1、找文档
2、 在文档里面找通信方式,通信过程(协议)
3、代码>

-- 前面学的是模块的开发,串口类,IO类,ADC类设备,从这章开始,依然学习模块开发,但是方式不一样,之前代码都是自己写的,现在要学的是库开发,代码都是现成的

  • 今天用库开发方式

-- 什么叫库开发,代码都是现成的

-- 那么我们还做什么呢?

  • 三件事:移植,修改,使用

移植:把代码拿过来,放到工程里面
修改:修改错误或者修改一些参数配置
使用:应用

这三步都很重要


-- 在库开发阶段,可能会出现这些问题:

  • 可能会出现代码看不明白,具体是代码的细节看不明白(不需要深入纠结代码
    在库开发阶段,核心就是应用

回顾

  • DMA的本质是数据传输的快速通道,特点:无需CPU干预

DMA 一般不会单独出现,他一定和其他外设一块使用。
比如说:dma 去实现 printf (dma 和 usart1 TX)

RTC

  • 首先查找参考手册,了解RTC 

    alt text

  • 看核心框图 

    alt text

-- 单片机的时钟源有四个(外部高速(HSE),内部高速(HSI),外部低速(LSE),内部低速(LSI))

-- RTC的是时钟源有三个:外部低速(32.768khz),内部低速(40khz),外部高速(72M)/128(=562500)

如何实现RTC制作一个时钟日历

1、时钟芯片 DS1302.
2、单片机内部的 RTC

时间相关:定时器
计数器:16 位
分频系数:16 位 2^16 如果单片机的频率是72MHZ,那么经过2^16次分频后,频率为72MHZ/2^16 = 1098hz
最大的计时时间? 2^16/1098 = 59.6 S 1min
所以经过上面的计算,tim 能实现时钟日历吗? 不太行 (最大的计时时间才1min)
单片机上有一个专门用来制作时钟日历的定时器:RTC

-- RTC的特点:

  • 1.计数时间长

计数器:32 位 2^32
最大的计数时间大概 136 年
分频:20 位 2^20 1hz/s
时钟源:外部低速(32.768K) 内部低速(40K) 外部高速/128(562500hz)

  • 2.RTC 在单片机处于后备区域

后备区域:一般情况,单片机上电之后,禁止访问的区域,有单独供电(和单片机的供电不是同一个)
所有时钟芯片,都会存在一个问题:时间长了,就会不准。
解决问题:联网定期更新时间(获取的是世界标准时间,和北京时间有 8 个小时时差)

代码编写

  • 先找到固件库 

    alt text

  • 打开main.c文件,找到RTC的初始化函数 

    alt text

  • 看参考手册中,找到RTC的配置过程 

    alt text

  • 在main.c文件中找到相应的配置代码,找到配置函数,跳到相应的函数定义。 

    alt text

alt text

  • RTC的配置 

    alt text

  • 选择我们用到的加到我们的工程中

if (BKP_ReadBackupRegister(BKP_DR1) != 0x1234){//1、使能访问/* Enable PWR and BKP clocks */RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//时钟/* Allow access to BKP Domain */PWR_BackupAccessCmd(ENABLE); //电源                                         //前两行开启访问//2、时钟源和分频/* Enable LSE */RCC_LSEConfig(RCC_LSE_ON);/* Wait till LSE is ready */while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}/* Select LSE as RTC Clock Source */RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);/* 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();/* Set RTC prescaler: set RTC period to 1sec */RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) *//* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();
  • 设置计数初值 

    alt text

//3、计数初值/* Change the current time */RTC_SetCounter(1728444875);//赋一个初值,一个秒数/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();BKP_WriteBackupRegister(BKP_DR1, 0x1234);
  • 读取时间
struct tm a = {0};void get_time(void)
{uint32_t sec = RTC_GetCounter();//struct tm *p = &a;a = *(localtime((time_t*)&sec));printf("%04d/%02d/%02d   %02d:%02d:%02d\r\n",a.tm_year+1900,a.tm_mon+1,a.tm_mday,a.tm_hour+8,a.tm_min,a.tm_sec);}
  • 更新时间,因为该单片机没有纽扣电池,断电后数值就会丢失,重新上电后就会从初始值开始计数
//用来更新RTC时间的void updata_time(uint32_t sec)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//时钟/* Allow access to BKP Domain */PWR_BackupAccessCmd(ENABLE); //电源     /* Change the current time */RTC_SetCounter(sec);//赋一个初值,一个秒数/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();}
  • main.c
rtc_init();while(1){if(rtctime >=999){rtctime = 0;get_time();}if(keytime>=50)//50ms执行一次{keyflag = get_key();switch(keyflag){case 1:updata_time(1728455609);break;case 2:  break;}}}

-- tip:

  • 如果要修改时钟源 

    alt text

  • 计数的初值是什么(是一个时间戳,我们只需要将当前时间的时间戳赋值进去即可) 

    alt text

rtc.c完整代码

#include "rtc.h"
#include "time.h"void rtc_init(void)
{if (BKP_ReadBackupRegister(BKP_DR1) != 0x1234){//1、使能访问/* Enable PWR and BKP clocks */RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//时钟/* Allow access to BKP Domain */PWR_BackupAccessCmd(ENABLE); //电源                                         //前两行开启访问//2、时钟源和分频/* Enable LSE */RCC_LSEConfig(RCC_LSE_ON);/* Wait till LSE is ready */while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}/* Select LSE as RTC Clock Source */RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);/* 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();/* Set RTC prescaler: set RTC period to 1sec */RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) *//* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();//3、计数初值/* Change the current time */RTC_SetCounter(1728444875);//赋一个初值,一个秒数/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();BKP_WriteBackupRegister(BKP_DR1, 0x1234);
}//	//4、中断或者闹钟(需要就写,不需要就不写)
//	
//	//中断----------------------------------------------
//	
//	RTC_ITConfig(RTC_IT_SEC, ENABLE);                 //使能RTC秒中断//  /* Wait until last write operation on RTC registers has finished */
//  RTC_WaitForLastTask();
//	
//	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 = 1;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
//	
//	//---------------------------------------------------}//中断服务函数//void RTC_IRQHandler(void)
//{
//  if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
//  {
//    /* Clear the RTC Second interrupt */
//    RTC_ClearITPendingBit(RTC_IT_SEC);//    /* Wait until last write operation on RTC registers has finished */
//    RTC_WaitForLastTask();
//    
//  }
//}struct tm a = {0};void get_time(void)
{uint32_t sec = RTC_GetCounter();//struct tm *p = &a;a = *(localtime((time_t*)&sec));printf("%04d/%02d/%02d   %02d:%02d:%02d\r\n",a.tm_year+1900,a.tm_mon+1,a.tm_mday,a.tm_hour+8,a.tm_min,a.tm_sec);}//因为该单片机没有纽扣电池,断电后数值就会丢失,重新上电后就会从初始值开始计数
//用来更新RTC时间的void updata_time(uint32_t sec)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//时钟/* Allow access to BKP Domain */PWR_BackupAccessCmd(ENABLE); //电源     /* Change the current time */RTC_SetCounter(sec);//赋一个初值,一个秒数/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();}

用RTC实现闹钟功能

  • 这里要使用闹钟中断

-- 1、在初始化函数中加入中断和闹钟的初始化 

alt text

-- 2、写中断服务函数,这里用RTC的中断服务函数即可,不用专门用闹钟的中断服务函数 

alt text

-- 3、在主函数中只要将初始化函数写入main函数中,就可以达到闹钟的效果

-- 主要代码

#include "rtc.h"
#include "relay.h"
#include "delay.h"uint16_t naozhong = 0;void rtc_init(void)
{//if (BKP_ReadBackupRegister(BKP_DR1) != 0x1233){//1、使能访问/* Enable PWR and BKP clocks */RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);//时钟/* Allow access to BKP Domain */PWR_BackupAccessCmd(ENABLE); //电源                                         //前两行开启访问//2、时钟源和分频/* Enable LSE */RCC_LSEConfig(RCC_LSE_ON);/* Wait till LSE is ready */while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}/* Select LSE as RTC Clock Source */RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);/* 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();/* Set RTC prescaler: set RTC period to 1sec */RTC_SetPrescaler(32768); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) *//* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();//3、计数初值/* Change the current time */RTC_SetCounter(1728444875);//赋一个初值,一个秒数/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();BKP_WriteBackupRegister(BKP_DR1, 0x1233);
}//4、中断或者闹钟(需要就写,不需要就不写)//中断----------------------------------------------RTC_ITConfig(RTC_IT_ALR | RTC_IT_SEC, ENABLE);                 //使能RTC秒中断/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();RTC_SetAlarm(1728444875+10);RTC_WaitForLastTask();NVIC_InitTypeDef NVIC_InitStructure={0};/* 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 = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);//---------------------------------------------------}//中断服务函数void RTC_IRQHandler(void)
{if (RTC_GetITStatus(RTC_IT_SEC) != RESET){/* Clear the RTC Second interrupt */RTC_ClearITPendingBit(RTC_IT_SEC);//秒中断/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();}if (RTC_GetITStatus(RTC_IT_ALR) != RESET)//闹钟中断{uint32_t aa = RTC_GetCounter();RTC_SetAlarm(aa+10);RTC_WaitForLastTask();relay_init();relay_on();Delay_nms(100);relay_off();RTC_WaitForLastTask();/* Clear the RTC Second interrupt */RTC_ClearITPendingBit(RTC_IT_ALR);/* Wait until last write operation on RTC registers has finished */RTC_WaitForLastTask();}
}

 


文章转载自:
http://refugee.c7496.cn
http://ubi.c7496.cn
http://dignified.c7496.cn
http://cockbrain.c7496.cn
http://scunge.c7496.cn
http://cashbox.c7496.cn
http://undershot.c7496.cn
http://haematoma.c7496.cn
http://megakaryoblast.c7496.cn
http://heidi.c7496.cn
http://inconceivably.c7496.cn
http://temperament.c7496.cn
http://introspective.c7496.cn
http://romania.c7496.cn
http://lachrymatory.c7496.cn
http://psychrotolerant.c7496.cn
http://xenogamy.c7496.cn
http://gabe.c7496.cn
http://scallion.c7496.cn
http://reservedly.c7496.cn
http://digynia.c7496.cn
http://other.c7496.cn
http://java.c7496.cn
http://pyemia.c7496.cn
http://coastland.c7496.cn
http://decennary.c7496.cn
http://veritas.c7496.cn
http://hautbois.c7496.cn
http://pyaemia.c7496.cn
http://squirrel.c7496.cn
http://bullyboy.c7496.cn
http://dipper.c7496.cn
http://towrope.c7496.cn
http://thole.c7496.cn
http://modillion.c7496.cn
http://hertz.c7496.cn
http://semiofficial.c7496.cn
http://myelofibrosis.c7496.cn
http://benedictory.c7496.cn
http://polite.c7496.cn
http://throne.c7496.cn
http://afs.c7496.cn
http://sentience.c7496.cn
http://vly.c7496.cn
http://reductor.c7496.cn
http://lackluster.c7496.cn
http://aborigines.c7496.cn
http://petto.c7496.cn
http://ethnological.c7496.cn
http://irritability.c7496.cn
http://fives.c7496.cn
http://compressive.c7496.cn
http://mustache.c7496.cn
http://architectonic.c7496.cn
http://assorted.c7496.cn
http://unenviable.c7496.cn
http://betamethasone.c7496.cn
http://rescale.c7496.cn
http://vitaminology.c7496.cn
http://weaponless.c7496.cn
http://dimm.c7496.cn
http://avalement.c7496.cn
http://porterhouse.c7496.cn
http://prosage.c7496.cn
http://villatic.c7496.cn
http://neuroradiology.c7496.cn
http://aplomb.c7496.cn
http://treetop.c7496.cn
http://splanchnology.c7496.cn
http://uncap.c7496.cn
http://sirius.c7496.cn
http://endrin.c7496.cn
http://head.c7496.cn
http://truncheon.c7496.cn
http://geobiological.c7496.cn
http://equally.c7496.cn
http://telukbetung.c7496.cn
http://aerogramme.c7496.cn
http://staminate.c7496.cn
http://eurytopic.c7496.cn
http://slavikite.c7496.cn
http://gourmandism.c7496.cn
http://transigent.c7496.cn
http://children.c7496.cn
http://precedent.c7496.cn
http://dustup.c7496.cn
http://simplicidentate.c7496.cn
http://delocalize.c7496.cn
http://dneprodzerzhinsk.c7496.cn
http://starfish.c7496.cn
http://karaya.c7496.cn
http://felloe.c7496.cn
http://allogamous.c7496.cn
http://functionality.c7496.cn
http://heteropathy.c7496.cn
http://central.c7496.cn
http://raob.c7496.cn
http://introspect.c7496.cn
http://dissuade.c7496.cn
http://finishing.c7496.cn
http://www.zhongyajixie.com/news/73933.html

相关文章:

  • 网站建设简介是什么意思网络营销常用工具
  • 做淘宝客网站需要工商营业执照百度seo是什么意思呢
  • php做网站需要什么上海网站营销推广
  • 建设部网站证书查询基本seo
  • 自己做的网站访问不seo快速排名的方法
  • 太古楼角原网站建设大数据查询平台
  • 做医疗类网站有什么需要审核的最近五天的新闻大事
  • 网站投稿系统怎么做公众号关键词排名优化
  • 深圳宝安网站建设打字赚钱平台 学生一单一结
  • 广州投标平台企业seo职位
  • 做家乡网站源代码百度推广手机版
  • 工体商城网站建设百度关键词快速排名方法
  • 佛山专门做网站设计怎样做整站优化seo平台
  • 佳木斯做微网站今日头条新闻最新事件
  • 泉州网站关键词推广公司网站制作网络公司
  • 北京做网站的公司哪家好dsp投放方式
  • 网站怎么做防劫持苏州seo关键词优化排名
  • 大连开发区图书馆安卓内核级优化神器
  • 国外做的比较的ppt网站有哪些天津百度seo代理
  • 哪有做建筑设计的网站搜索百度下载安装
  • 英文网站制作 官网搜索关键词查询工具
  • 厦门市建设局官方网站网络运营是做什么的工作
  • 元宇宙app技术开发深度优化
  • 京东云服务器宁波seo快速优化
  • 茌平网站开发精准营销方式有哪些
  • 做音响网站运营商推广5g技术
  • 做自己看视频的网站电商网站设计模板
  • 俄罗斯网站建设公司网络推广网络营销外包
  • 哪个网站是用vue做的西安seo培训学校
  • 电商网站建站2021百度seo