新手入门
- 积分
- 28
- 金钱
- 28
- 注册时间
- 2016-4-11
- 在线时间
- 6 小时
|
1金钱
下面贴代码#include "stm32f10x.h"
#include "stdio.h"
#include "time.h"
#define PRINTF_ON 1
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void RTC_Configuration(void);
void TimeShow(void);
void SetAlarm(struct tm t);
void SetCalendarTime(struct tm t);
void SetUnixTime(time_t);
struct tm ConvUnixToCalendar(time_t t);
u32 ConvCalendarToUnix(struct tm t);
u32 GetUnixTime(void);
vu32 Display;
struct tm CurrentTime = {0,34,23,9,4,2016};
struct tm AlarmTime = {0,35,23,9,4,2016};
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
USART_Configuration();
RTC_Configuration();
SetCalendarTime(CurrentTime);
SetAlarm(AlarmTime);
while(1){ TimeShow(); }
}
void TimeShow(void)
{
u32 Time = 0;
if(Display)
{
Time = GetUnixTime();
CurrentTime = ConvUnixToCalendar(Time);
printf("\r\n Time : %d - %d - %d,%d : %d : %d \r\n",
CurrentTime.tm_year,
CurrentTime.tm_mon,
CurrentTime.tm_mday,
CurrentTime.tm_hour,
CurrentTime.tm_min,
CurrentTime.tm_sec);
Display = 0;
}
}
void SetCalendarTime(struct tm t)
{
SetUnixTime(ConvCalendarToUnix(t));
}
void SetUnixTime(time_t t)
{
RTC_WaitForLastTask();
RTC_SetCounter((u32)t);
RTC_WaitForLastTask();
}
void SetAlarm(struct tm t)
{
RTC_WaitForLastTask();
RTC_SetAlarm(ConvCalendarToUnix(t));
RTC_WaitForLastTask();
}
u32 GetUnixTime(void)
{
return (u32)RTC_GetCounter();
}
u32 ConvCalendarToUnix(struct tm t)
{
t.tm_year -=1900;
return mktime(&t);
}
struct tm ConvUnixToCalendar(time_t t)
{
struct tm *t_tm;
t_tm = localtime(&t);
t_tm->tm_year += 1900;
return *t_tm;
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
void RTC_Configuration(void)
{
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_SEC|RTC_IT_ALR,ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}
void RCC_Configuration(void)
{
/* ???????? HSEStartUpStatus */
ErrorStatus HSEStartUpStatus;
/* ????????*/
RCC_DeInit();
/* ??HSE*/
RCC_HSEConfig(RCC_HSE_ON);
/* ??HSE?????*/
HSEStartUpStatus = RCC_WaitForHSEStartUp();
/* ??HSE??????,????if()?? */
if(HSEStartUpStatus == SUCCESS)
{
/* ??HCLK(AHB)????SYSCLK 1?? */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* ??PCLK2???? HCLK(AHB) 1?? */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* ??PCLK1???? HCLK(AHB) 2?? */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* ??FLASH??????2 */
FLASH_SetLatency(FLASH_Latency_2);
/* ??FLASH???? */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* ?????(PLL)????HSE 1??,????9,?PLL????? 8MHz * 9 = 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* ??PLL */
RCC_PLLCmd(ENABLE);
/* ??PLL???? */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* ??SYSCLK????PLL */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* ??PLL??SYSCLK??? */
while(RCC_GetSYSCLKSource() != 0x08);
}
/* ??APB2????GPIOA??*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1 , &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
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);
}
#include "stm32f10x_it.h"
#include "stdio.h"
extern vu32 Display;
void RTC_IRQHandler(void)
{
if(RTC_GetFlagStatus(RTC_FLAG_ALR) != RESET){
printf("\r\nIt's time to do sth.\r\n");
}else{
Display =1 ;
}
RTC_ClearITPendingBit(RTC_IT_ALR|RTC_IT_SEC);
}
|
|