(需要修改)文章来源:https://www.toymoban.com/news/detail-677355.html
源码文章来源地址https://www.toymoban.com/news/detail-677355.html
while(1)
{
//rtc唤醒事件
if(g_rtc_wakeup_event)
{
//获取日期
RTC_GetDate(RTC_Format_BCD,&RTC_DateStructure);
printf("20%02x/%02x/%02xWeek:%x\r\n",RTC_DateStructure.RTC_Year,RTC_DateStructure.RTC_Month,RTC_DateStructure.RTC_Date,RTC_DateStructure.RTC_WeekDay);
//获取时间
RTC_GetTime(RTC_Format_BCD,&RTC_TimeStructure);
printf("%02x:%02x:%02x\r\n",RTC_TimeStructure.RTC_Hours,RTC_TimeStructure.RTC_Minutes,RTC_TimeStructure.RTC_Seconds);
g_rtc_wakeup_event=0;
}
//rtc闹钟事件
if(g_rtc_alarm_event)
{
//模拟闹钟滴滴的声音,并闪烁LED灯
if(time_sum<=6)
{
time_cnt++;
delay_ms(1);
if(time_cnt==80)
{
PFout(8)=1;
PFout(10)=0;
}
else if(time_cnt==160)
{
PFout(8)=0;
PFout(10)=1;
}
else if(time_cnt==240)
{
PFout(8)=1;
PFout(10)=0;
}
else if(time_cnt==320)
{
PFout(8)=0;
PFout(10)=1;
}
else if(time_cnt>=1000)
{
time_sum+=1;
time_cnt=0;
}
}
else
{
//清空标志位
g_rtc_alarm_event=0;
//清零蜂鸣器鸣响的总时间值
time_sum=0;
//清零1毫秒时间计数值
time_cnt=0;
}
}
//串口1事件
if(g_usart1_event)
{
//判断接收到的字符串为DATE SET
//示例:DATE SET-2017-10-12-4\n
if(strstr((char *)g_usart1_recv_buf,"DATE SET"))
{
//以等号分割字符串
strtok((char *)g_usart1_recv_buf,"-");
//获取年
p=strtok(NULL,"-");
//2017-2000=17
i = atoi(p)-2000;
//转换为16进制 17 ->0x17
i= (i/10)*16+i%10;
RTC_DateStructure.RTC_Year = i;
//获取月
p=strtok(NULL,"-");
i=atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_DateStructure.RTC_Month=i;
//获取日
p=strtok(NULL,"-");
i=atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_DateStructure.RTC_Date = i;
//获取星期
p=strtok(NULL,"-");
i=atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_DateStructure.RTC_WeekDay = i;
//设置日期
RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
}
//判断接收到的字符串为TIME SET
//示例:TIME SET-14-20-10\n
if(strstr((char *)g_usart1_recv_buf,"TIME SET"))
{
//以等号分割字符串
strtok((char *)g_usart1_recv_buf,"-");
//获取时
p=strtok(NULL,"-");
i = atoi(p);
//通过时,判断是AM还是PM
if(i<12)
RTC_TimeStructure.RTC_H12 = RTC_H12_AM;
else
RTC_TimeStructure.RTC_H12 = RTC_H12_PM;
//转换为16进制
i= (i/10)*16+i%10;
RTC_TimeStructure.RTC_Hours = i;
//获取分
p=strtok(NULL,"-");
i = atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_TimeStructure.RTC_Minutes = i;
//获取秒
p=strtok(NULL,"-");
i = atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_TimeStructure.RTC_Seconds = i;
RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
}
//判断接收到的字符串为ALARM SET
//示例:ALARM SET-14-20-10\n
if(strstr((char *)g_usart1_recv_buf,"ALARM SET"))
{
//以等号分割字符串
strtok((char *)g_usart1_recv_buf,"-");
//获取时
p=strtok(NULL,"-");
i = atoi(p);
//通过时,判断是AM还是PM
if(i<12)
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
else
RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_PM;
//转换为16进制
i= (i/10)*16+i%10;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = i;
//获取分
p=strtok(NULL,"-");
i = atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = i;
//获取秒
p=strtok(NULL,"-");
i = atoi(p);
//转换为16进制
i= (i/10)*16+i%10;
RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = i;
#if 1 //每天闹钟都会生效
//RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; //若RTC_AlarmDateWeekDaySel_Date,当前为日期,数值范围是0x1~0x31
//若RTC_AlarmDateWeekDaySel_WeekDay,当前为星期几,数值范围:0x1~0x7
//RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; //选择日期
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay; //屏蔽星期/日期,也就说闹钟每天都会生效
#else //指定闹钟在某一天生效
RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31; //若RTC_AlarmDateWeekDaySel_Date,当前为日期,数值范围是0x1~0x31
//若RTC_AlarmDateWeekDaySel_WeekDay,当前为星期几,数值范围:0x1~0x7
RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date; //选择日期
RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_None; //不屏蔽星期/日期,也就说闹钟会指定某一天会生效
#endif
rtc_alarm_set(RTC_AlarmStructure);
printf("%s ok,please wait rtc alarm...\r\n",g_usart1_recv_buf);
}
//清空串口1数据计数值
g_usart1_recv_cnt = 0;
//清空串口1接收数据事件
g_usart1_event = 0;
//清空串口1接收数据缓冲区
memset((char *)g_usart1_recv_buf,0,sizeof g_usart1_recv_buf);
}
}
}
void RTC_WKUP_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_WUT) != RESET)
{
g_rtc_wakeup_event = 1;
RTC_ClearITPendingBit(RTC_IT_WUT);
}
EXTI_ClearITPendingBit(EXTI_Line22);
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
//从串口1接收数据
g_usart1_recv_buf[g_usart1_recv_cnt]=USART_ReceiveData(USART1);
//记录多少个数据
g_usart1_recv_cnt++;
//检测到'#'符或接收的数据满的时候则发送数据
if(g_usart1_recv_buf[g_usart1_recv_cnt-1]=='#' || g_usart1_recv_cnt>=(sizeof g_usart1_recv_buf)-1)
{
g_usart1_event = 1;
}
}
}
void RTC_Alarm_IRQHandler(void)
{
if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)
{
//添加用户代码
g_rtc_alarm_event=1;
printf("rtc alarm trig\r\n");
RTC_ClearITPendingBit(RTC_IT_ALRA);
EXTI_ClearITPendingBit(EXTI_Line17);
}
}
到了这里,关于stm32之24.RTC闹钟usart端口修改配置的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!