STM32
直播中

徐胤

7年用户 1045经验值
私信 关注
[问答]

为什么使用STM32F103的RTC会出现外部的晶振不起振现象呢

为什么使用STM32F103的RTC会出现外部的晶振不起振现象呢?有什么解决方案吗?

回帖(1)

黄欢

2021-11-24 16:08:39
  基于STM32F103VET6的定时器软RTC
  工业环境使用STM32F103的RTC有个问题,经常出现外部的晶振不起振现象,量产产品出现这种情况的话非常头疼,想解决方案的时候想到用定时器模拟RTC秒中断产生时钟计数也许会稳定许多。
  一、程序的编写
  1、软件rtc库程序
  #include “SoftRtc.h”
  #include “usart.h”
  _SoftCalendar_Obj SoftCalendar;
  Monitor_Struct Monitor_Data;
  Network_Struct Network_Data;
  u32 TotalTime;
  //平年的月份日期表
  const u8 Softmon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  //判断是否是闰年函数
  //月份 1 2 3 4 5 6 7 8 9 10 11 12
  //闰年 31 29 31 30 31 30 31 31 30 31 30 31
  //非闰年 31 28 31 30 31 30 31 31 30 31 30 31
  //输入:年份
  //输出:该年份是不是闰年.1,是.0,不是
  u8 SoftIs_Leap_Year(u16 year)
  {
  if(year%4==0) //必须能被4整除
  {
  if(year%100==0)
  {
  if(year%400==0)return 1;//如果以00结尾,还要能被400整除
  else return 0;
  }else return 1;
  }else return 0;
  }
  //得到当前的时间
  //返回值:0,成功;其他:错误代码。
  u8 SoftRTC_Get(u32 STime)
  {
  static u16 daycnt=0;
  u32 timecount=0;
  u32 temp=0;
  u16 temp1=0;
  timecount=STime;
  temp=timecount/86400; //得到天数(秒钟数对应的)
  if(daycnt!=temp)//超过一天了
  {
  daycnt=temp;
  temp1=1970; //从1970年开始
  while(temp》=365)
  {
  if(SoftIs_Leap_Year(temp1))//是闰年
  {
  if(temp》=366)temp-=366;//闰年的秒钟数
  else {temp1++;break;}
  }
  else temp-=365; //平年
  temp1++;
  }
  SoftCalendar.w_year=temp1;//得到年份
  temp1=0;
  while(temp》=28)//超过了一个月
  {
  if(SoftIs_Leap_Year(SoftCalendar.w_year)&&temp1==1)//当年是不是闰年/2月份
  {
  if(temp》=29)temp-=29;//闰年的秒钟数
  else break;
  }
  else
  {
  if(temp》=Softmon_table[temp1])temp-=Softmon_table[temp1];//平年
  else break;
  }
  temp1++;
  }
  SoftCalendar.w_month=temp1+1; //得到月份
  SoftCalendar.w_date=temp+1; //得到日期
  }
  temp=timecount%86400; //得到秒钟数
  SoftCalendar.hour=temp/3600; //小时
  SoftCalendar.min=(temp%3600)/60; //分钟
  SoftCalendar.sec=(temp%3600)%60; //秒钟
  return 0;
  }
  u8 SoftRTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
  {
  u16 t;
  u32 seccount=0;
  if(syear《1970||syear》2099)return 1;
  for(t=1970;t《syear;t++) //把所有年份的秒钟相加
  {
  if(SoftIs_Leap_Year(t))seccount+=31622400;//闰年的秒钟数
  else seccount+=31536000; //平年的秒钟数
  }
  smon-=1;
  for(t=0;t《smon;t++) //把前面月份的秒钟数相加
  {
  seccount+=(u32)Softmon_table[t]*86400;//月份秒钟数相加
  if(SoftIs_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数
  }
  seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加
  seccount+=(u32)hour*3600;//小时秒钟数
  seccount+=(u32)min*60; //分钟秒钟数
  seccount+=sec;//最后的秒钟加上去
  TotalTime = seccount;
  return 0;
  }
  /*
  函数名:软件rtc函数
  形参:无
  返回值:无
  功能:软件rtc函数,负责计算日历,更新时间,只需将本函数放入秒定时器即可
  */
  void SoftRtcFun(void)
  {
  TotalTime ++;
  SoftRTC_Get(TotalTime);//更新时间
  if(Monitor_Data.TimeSetRequest == 1)//时间设置请求
  {
  //Monitor_Data.Network_Hou+=8;
  SoftRTC_Set(Network_Data.Network_Year,Network_Data.Network_Mon,Network_Data.Network_Day,
  Network_Data.Network_Hou,Network_Data.Network_Min,Network_Data.Network_Sec);
  Monitor_Data.TimeSetRequest = 0;
  }
  Monitor_Data.SysTime_Year=SoftCalendar.w_year;
  Monitor_Data.SysTime_Mon=SoftCalendar.w_month;
  Monitor_Data.SysTime_Day=SoftCalendar.w_date;
  Monitor_Data.SysTime_Hou=SoftCalendar.hour;
  Monitor_Data.SysTime_Min=SoftCalendar.min;
  Monitor_Data.SysTime_Sec=SoftCalendar.sec;
  printf(“%d-%d-%d %d:%d:%drn”,Monitor_Data.SysTime_Year,Monitor_Data.SysTime_Mon,Monitor_Data.SysTime_Day,
  Monitor_Data.SysTime_Hou,Monitor_Data.SysTime_Min,Monitor_Data.SysTime_Sec);
  }
  2、定时器中断程序
  //定时器3中断服务程序
  void TIM3_IRQHandler(void) //TIM3中断
  {
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否
  {
  TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx更新中断标志
  SoftRtcFun();
  }
  }
  3、主程序
  int main(void)
  {
  delay_init(); //延时函数初始化
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  uart_init(115200); //串口初始化为115200
  TIM3_Int_Init(9999,7199);//10Khz的计数频率,计数到10000为1s
  SoftRTC_Set(2020,6,11,23,59,55);
  while(1)
  {
  delay_ms(200);
  }
  }
举报

更多回帖

发帖
×
20
完善资料,
赚取积分