大学生
最后登录1970-1-1
在线时间 小时
注册时间2013-8-6
|
楼主 |
发表于 2013-8-27 14:07:52
|
显示全部楼层
Main.c文件 2
[code=cpp]
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
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 GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
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 USART_Configuration()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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);
}
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
void RTC_Configuration()
{
/*允许PWR和BKP时钟*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/*允许访问BKP域*/
PWR_BackupAccessCmd(ENABLE);
/*复位备份域*/
BKP_DeInit();
#ifdef RTCClockSource_LSI
/*允许LSI*/
RCC_LSICmd(ENABLE);
/*等待LSI准备好*/
while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET)
{
}
/*选择LSI作为RTC时钟源*/
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#elif defined RTCClockSource_LSE
/*允许LSE*/
RCC_LSEConfig(RCC_LSE_ON);
/*等待LSE准备好*/
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET)
{
}
/*选择LSE作为RTC时钟源*/
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
#endif
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
#ifdef RTCClockOutput_Enable
/*禁止Tamper引脚*/
BKP_TamperPinCmd(DISABLE);/*为了将RTCCLK/64在Tamper引脚输出,Tamper功能必须被禁止*/
/*允许RTC时钟在Tamper引脚上输出*/
BKP_RTCCalibrationClockOutputCmd(ENABLE);
#endif
/*等待寄存器同步*/
RTC_WaitForSynchro();
/*等待上次RTC寄存器写操作完成*/
RTC_WaitForLastTask();
/*允许RTC秒中断*/
RTC_ITConfig(RTC_IT_SEC, ENABLE);
/*等待上次RTC寄存器写操作完成*/
RTC_WaitForLastTask();
#ifdef RTCClockSource_LSI
/*设置分频系数*/
RTC_SetPrescaler(31999); /*RTC周期=RTCCLK/RTC_PR=(32.000kHz/(31999+1))*/
#elif defined RTCClockSource_LSE
RTC_SetPrescaler(32767); /*RTC周期=RTCCLK/RTC_PR=(32.768kHz/(31767+1))*/
#endif
/*等待上次RTC寄存器写操作完成*/
RTC_WaitForLastTask();
}
void Time_Regulate(struct rtc_time *tm)
{
u32 Tmp_YY = 0xFF, Tmp_MM = 0xFF, Tmp_DD = 0xFF, Tmp_HH = 0xFF, Tmp_MI = 0xFF, Tmp_SS = 0xFF;
printf("\r\n=========================Time Settings==================");
printf("\r\n 请输入年份(Please Set Years): 20");
while (Tmp_YY == 0xFF)
{
Tmp_YY = USART_Scanf(99);
}
printf("\n\r 年份被设置为: 20%0.2d\n\r", Tmp_YY);
tm->tm_year = Tmp_YY+2000;
Tmp_MM = 0xFF;
printf("\r\n 请输入月份(Please Set Months): ");
while (Tmp_MM == 0xFF)
{
Tmp_MM = USART_Scanf(12);
}
printf("\n\r 月份被设置为: %d\n\r", Tmp_MM);
tm->tm_mon= Tmp_MM;
Tmp_DD = 0xFF;
printf("\r\n 请输入日期(Please Set Dates): ");
while (Tmp_DD == 0xFF)
{
Tmp_DD = USART_Scanf(31);
}
printf("\n\r 日期被设置为: %d\n\r", Tmp_DD);
tm->tm_mday= Tmp_DD;
Tmp_HH = 0xFF;
printf("\r\n 请输入时钟(Please Set Hours): ");
while (Tmp_HH == 0xFF)
{
Tmp_HH = USART_Scanf(23);
}
printf("\n\r 时钟被设置为: %d\n\r", Tmp_HH );
tm->tm_hour= Tmp_HH;
Tmp_MI = 0xFF;
printf("\r\n 请输入分钟(Please Set Minutes): ");
while (Tmp_MI == 0xFF)
{
Tmp_MI = USART_Scanf(59);
}
printf("\n\r 分钟被设置为: %d\n\r", Tmp_MI);
tm->tm_min= Tmp_MI;
Tmp_SS = 0xFF;
printf("\r\n 请输入秒钟(Please Set Seconds): ");
while (Tmp_SS == 0xFF)
{
Tmp_SS = USART_Scanf(59);
}
printf("\n\r 秒钟被设置为: %d\n\r", Tmp_SS);
tm->tm_sec= Tmp_SS;
}
void Time_Adjust()
{
RTC_WaitForLastTask();
Time_Regulate(&systmtime);
GregorianDay(&systmtime);
RTC_SetCounter(mktimev(&systmtime));
RTC_WaitForLastTask();
}
void Time_Display(uint32_t TimeVar)
{
to_tm(TimeVar, &systmtime);
printf("\r 当前时间为: %d年 %d月 %d日 (星期%s) %0.2d:%0.2d:%0.2d",
systmtime.tm_year, systmtime.tm_mon, systmtime.tm_mday,
WEEK_STR[systmtime.tm_wday], systmtime.tm_hour,
systmtime.tm_min, systmtime.tm_sec);
}
void Time_Show()
{
printf("\n\r");
/* Infinite loop */
while (1)
{
/* 每过1s */
if (TimeDisplay == 1)
{
Time_Display(RTC_GetCounter());
TimeDisplay = 0;
}
}
}
u8 USART_Scanf(u32 value)
{
u32 index = 0;
u32 tmp[2] = {0, 0};
while (index < 2)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{
}
tmp[index++] = (USART_ReceiveData(USART1));
if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)) /*数字0到9的ASCII码为0x30至0x39*/
{
if((index == 2) && (tmp[index - 1] == '\r'))
{
tmp[1] = tmp[0];
tmp[0] = 0x30;
}
else
{
printf("\n\rPlease enter valid number between 0 and 9 -->: ");
index--;
}
}
}
/* 计算输入字符的相应ASCII值*/
index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
/* Checks */
if (index > value)
{
printf("\n\rPlease enter valid number between 0 and %d", value);
return 0xFF;
}
return index;
}
[/code]
|
|