#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
void myRtcHandler(void);
void GetTime(void);
u8 isLeapYear(u32 year);
typedef struct
{
vu8 hour;
vu8 min;
vu8 sec;
vu16 w_year;
vu8 w_month;
vu8 w_date;
vu8 week;
}_calendar_obj;
_calendar_obj calendar;
int main(void)
{
u8 x=0;
u8 lcd_id[12];
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
LCD_Init();
myRtcHandler();
while(1)
{
GetTime();
LCD_ShowString(30,40,210,24,24,"WarShip STM32 ^_^");
LED0=!LED0;
delay_ms(500);
LCD_ShowNum(60,130,calendar.w_year,4,16);
LCD_ShowNum(100,130,calendar.w_month,2,16);
LCD_ShowNum(124,130,calendar.w_date,2,16);
delay_ms(500);
LCD_ShowNum(60,162,calendar.hour,2,16);
LCD_ShowNum(84,162,calendar.min,2,16);
LCD_ShowNum(108,162,calendar.sec,2,16);
printf("%d\r\n",RTC_GetCounter());
}
}
void myRtcHandler(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RCC_LSEConfig(RCC_LSE_ON);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_EnterConfigMode();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
RTC_ExitConfigMode();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
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)
{
LED1=~LED1;
RTC_ClearITPendingBit(RTC_IT_SEC);
}
}
void GetTime(void)
{
u32 total_sec=0;
u32 total_day=0;
u32 init_year=1970;
u32 init_month=0;
u32 leav_sec=0;
const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
total_sec=RTC_GetCounter();
total_day=total_sec/86400;
if(total_day>0)
{
while(total_day>=365)
{
if(isLeapYear(init_year))
{
total_day-=366;
}
else
{
total_day-=365;
}
init_year++;
}
calendar.w_year=init_year;
while(total_day>=28)
{
if(isLeapYear(init_year)&&init_month==1)
{
total_day-=29;
}
else{
total_day-=mon_table[init_month];
}
init_month++;
}
calendar.w_month=init_month+1;
calendar.w_date=total_day+1;
}
leav_sec=total_sec%86400;
calendar.hour=(leav_sec)/3600;
calendar.min=(leav_sec%3600)/60;
calendar.sec=(leav_sec%3600)%60;
}
u8 isLeapYear(u32 year)
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)
{
return 1;
}
}
}
return 0;
}
|