|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
用的是正点原子mini开发板stm32f103RCT6
************main函数**************
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "usart.h"
#include "exti.h"
#include "key.h"
#include "time.h"
#include "pwm.h"
#include "capture.h"
#include "rtc.h"
int main(void)
{
u16 i=0;
// u8 dir=0;
// LED0=0; //µãÁÁLED
// u32 temp=0;
delay_init();
LED_Init();
KEY_Init();
//My_EXTIX_Init();
//USART1_Init(9600);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÖжÏÓÅÏȼ¶·Ö×é ·Ö2×é
RTC_Init();
while(1)
{
i++;
if(i%20==0)
{
LED1=!LED1;
}
delay_ms(10);
}
}
********rtc.c************
#include "rtc.h"
#include "delay.h"
//#include "usart.h" //Óõ½ÁËprintf
_calendar calendar;
void RTC_NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//ÇÀÕ¼ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//×ÓÓÅÏȼ¶
NVIC_Init(&NVIC_InitStructure);
}
void RTC_Get()//»ñȡʱ¼ä
{
u32 timedata=0;
timedata=RTC_GetCounter();
calendar.hour=timedata/3600;
calendar.min=(timedata%3600)/60;
calendar.sec=timedata%60;
}
u8 RTC_Init()
{
u8 temp;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);
PWR_BackupAccessCmd(ENABLE);//ʹÄܺ󱸼ĴæÆ÷·ÃÎÊ
if (BKP_ReadBackupRegister(BKP_DR1)!=0x5050)//µÚÒ»´Î½øÈëRTC£¬²»µÈÓÚ£¬Ôò˵Ã÷ÊǵÚÒ»´Î³õʼ»¯
{
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET&&temp<250)//µÈ´ý¾§ÕñÎȶ¨
{
temp++;
delay_ms(10);
}//2.5ÃëºóÍ˳öwhileÑ»·
if(temp>=250)
{
return 1;//·µ»Ø1£º³õʼ»¯Ê§°Ü ·µ»Ø0£º³õʼ»¯³É¹¦
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);//RTCʱÖÓÅäÖÃ
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForLastTask(); //µÈ´ýRTC×î½üÒ»´Îд²Ù×÷Íê³É
RTC_WaitForSynchro(); //µÈ´ýRTCͬ²½
RTC_ITConfig(RTC_IT_SEC,ENABLE);//ÉèÖÃΪÃëÖжϣ¬ºóÐøÅäÖÃÖжÏÓÅÏȼ¶
RTC_WaitForLastTask(); //µÈ´ýд²Ù×÷Íê³É
RTC_EnterConfigMode();//RTCÔÊÐíÅäÖ㬽øÈëÅäÖÃģʽ
RTC_SetPrescaler(32767);//32.768k¾§Õñ
RTC_WaitForLastTask();
RTC_SetCounter(0xf73f);//ÅäÖÃΪ17:34:55 ʱ·ÖÃë
RTC_ExitConfigMode();//Í˳öÅäÖÃ
BKP_WriteBackupRegister(BKP_DR1,0X5050);
}
else//µÚ¶þ´Î
{
RTC_WaitForSynchro(); //µÈ´ýRTCͬ²½
RTC_ITConfig(RTC_IT_SEC, ENABLE);//ÉèÖÃΪÃëÖжÏ
RTC_WaitForLastTask(); //µÈ´ýRTC×î½üÒ»´Îд²Ù×÷Íê³É
}
RTC_NVIC_Config();
RTC_Get();
return 0;//·µ»Ø0£º³õʼ»¯³É¹
}
void RTC_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_SEC)!=RESET)//¼´¡Ù0
{
RTC_Get();
//printf("RTC Time:%d:%d:%d",calendar.hour,calendar.min,calendar.sec);
}
RTC_ClearITPendingBit(RTC_IT_SEC);
}
*********rtc.h***********
#ifndef _rtc_H
#define _rtc_H
#include "sys.h"
typedef struct
{
u8 hour;
u8 min;
u8 sec;
}_calendar;
extern _calendar calendar;
void RTC_NVIC_Config(void);
void RTC_Get(void);
u8 RTC_Init(void);
void RTC_IRQHandler(void);
#endif |
|