原创 RTC终于成功了啊!

2008-8-27 13:38 5701 4 6 分类: 工程师职场


/* Includes ------------------------------------------------------------------*/
#include "RTC.h"
#include "USART1.h"
#include "rprintf.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define RTCClockOutput_Enable  /* RTC Clock/64 is output on tamper pin(PC.13) */


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/


vu32 TimeDisplay = 0;
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void  RTC_init(u32 y,u32 m,u32 d,u32 h,u32 mm,u32 s)
{
 
  if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  {
    USART1_PutString("RTC 不曾配置....");
    RTC_Configuration();
    USART1_PutString("RTC 开始配置....");
    Time_Adjust( y, m, d, h, mm, s);
   BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
 
  }
  else
  {
   if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {
     USART1_PutString("\r\n电源复位....");
    }
  else if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
     USART1_PutString("\r\n External Reset occurred....");
    }


    USART1_PutString("\r\n 不须要配置 RTC....");
    RTC_WaitForSynchro();
    RTC_ITConfig(RTC_IT_SEC, ENABLE);
    RTC_WaitForLastTask();
  }


#ifdef RTCClockOutput_Enable
 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  PWR_BackupAccessCmd(ENABLE);
  BKP_TamperPinCmd(DISABLE);
 
  BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
 
#endif
    RCC_ClearFlag();
}
void RTC_Configuration(void)
{
 /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);
  /* Reset Backup Domain */
  BKP_DeInit();
  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {}


  /* Select LSE as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
  /* Wait for RTC registers synchronization */
  RTC_WaitForSynchro();
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}


/*******************************************************************************
* Function Name  : Time_Regulate
* Description    : Returns the time entered by user, using Hyperterminal.
* Input          : None
* Output         : None
* Return         : Current time RTC counter value
*******************************************************************************/


u32 Month_Days_Accu_C[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
u32 Month_Days_Accu_L[13] = {0,31,60,91,121,152,182,213,244,274,305,335,366};
#define SecsPerDay (3600*24)


u32 Time_Regulate(u32 y,u32 m,u32 d,u32 h,u32 mm,u32 s)
{
  char data[20];
//#if 1
  u32 Tmp_Year=0xFFFF, Tmp_Month=0xFF, Tmp_Date=0xFF;
  u32 LeapY, ComY, TotSeconds, TotDays;
//#endif
  u32 Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;


  USART1_PutString("\r\n==============Time Settings=====================================");
 
//#if 1
 USART1_PutString("\r\nSet Year");
  while(Tmp_Year == 0xFFFF)
  {
    /*32-bit counter at Second Unit--> 4*1024*1024(s) --> 49710(day) --> 136(year)*/
    Tmp_Year =y;
  }
   sprintf1(data,":  %d", Tmp_Year);
   USART1_PutString(data);
   while(Tmp_Month == 0xFF)
   {
    Tmp_Month = m;
   }
  sprintf1(data,":  %d", Tmp_Month);
  USART1_PutString(data);
  while(Tmp_Date == 0xFF)
  {
    Tmp_Date =d;
   }
  sprintf1(data,":  %d", Tmp_Date);
  USART1_PutString(data);
//#endif
  while(Tmp_HH == 0xFF)
  {
    Tmp_HH = h;
  }
  sprintf1(data,":  %d", Tmp_HH);
  USART1_PutString(data);
  USART1_PutString("\r\nSet Minutes");
  while(Tmp_MM == 0xFF)
  {
    Tmp_MM = mm;
  }
  sprintf1(data,":  %d", Tmp_MM);;
  USART1_PutString(data);
  while(Tmp_SS == 0xFF)
  {
    Tmp_SS = s;
  }
 sprintf1(data,":  %d", Tmp_SS);
 USART1_PutString(data);
//#if 1
  {
  /* change Year-Month-Data-Hour-Minute-Seconds into X(Second) to set RTC->CNTR */
    if(Tmp_Year==2000)
      LeapY = 0;
    else
      LeapY = (Tmp_Year - 2000 -1)/4 +1;
   
  ComY = (Tmp_Year - 2000)-(LeapY);
 
  if (Tmp_Year%4)
    //common year
    TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_C[Tmp_Month-1] + (Tmp_Date-1);
  else
    //leap year
    TotDays = LeapY*366 + ComY*365 + Month_Days_Accu_L[Tmp_Month-1] + (Tmp_Date-1);
 
  TotSeconds = TotDays*SecsPerDay + (Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS);
  }
//#endif
  return TotSeconds;
}


/*******************************************************************************
* Function Name  : Time_Adjust
* Description    : Adjusts time.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Time_Adjust(u32 y,u32 m,u32 d,u32 h,u32 mm,u32 s)
{
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Change the current time */
  RTC_SetCounter(Time_Regulate(y,m,d,h,mm,s));
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}


/*******************************************************************************
* Function Name  : Time_Display
* Description    : Displays the current time.
* Input          : - TimeVar: RTC counter value.
* Output         : None
* Return         : None
*******************************************************************************/
#define SecsPerComYear  3153600//(365*3600*24)
#define SecsPerLeapYear 31622400//(366*3600*24)
#define SecsPerFourYear 126230400//((365*3600*24)*3+(366*3600*24))
#define SecsPerDay      (3600*24)


s32 Year_Secs_Accu[5]={0,
                      31622400,
                      63158400,
                      94694400,
                      126230400};


s32 Month_Secs_Accu_C[13] = { 0,
                            2678400,
                            5097600,
                            7776000,
                            10368000,
                            13046400,
                            15638400,
                            18316800,
                            20995200,
                            23587200,
                            26265600,
                            28857600,
                            31536000};
s32 Month_Secs_Accu_L[13] = {0,
                            2678400,
                            5184000,
                            7862400, 
                            10454400,
                            13132800,
                            15724800,
                            18403200,
                            21081600,
                            23673600,
                            26352000,
                            28944000,
                            31622400};


void Time_Display(u32 TimeVar)
{
  char data[80];
#if 1
  u32 TY = 0, TM = 1, TD = 0;
  s32 Num4Y,NumY, OffSec, Off4Y = 0;
  u32 i;
  s32 NumDay;
#endif
  u32 THH = 0, TMM = 0, TSS = 0;
#if 0
  /* Compute  hours */
  THH = TimeVar/3600;
  /* Compute minutes */
  TMM = (TimeVar % 3600)/60;
  /* Compute seconds */
  TSS = (TimeVar % 3600)% 60;
#endif
#if 1
  {
    Num4Y = TimeVar/SecsPerFourYear;
    OffSec = TimeVar%SecsPerFourYear;


    i="1";
    while(OffSec > Year_Secs_Accu[i++])
      Off4Y++;
 
    /* Numer of Complete Year */
    NumY = Num4Y*4 + Off4Y;
      /* 2000,2001,...~2000+NumY-1 complete year before, so this year is 2000+NumY*/
    TY = 2000+NumY;
   
    OffSec = OffSec - Year_Secs_Accu[i-2];
   
    /* Month (TBD with OffSec)*/
    i="0";
    if(TY%4)
    {// common year
      while(OffSec > Month_Secs_Accu_C[i++]);
      TM = i-1;
      OffSec = OffSec - Month_Secs_Accu_C[i-2];
    }
    else
    {// leap year
      while(OffSec > Month_Secs_Accu_L[i++]);
      TM = i-1;
      OffSec = OffSec - Month_Secs_Accu_L[i-2];
    }
   
    /* Date (TBD with OffSec) */
    NumDay = OffSec/SecsPerDay;
    OffSec = OffSec%SecsPerDay;
    TD = NumDay+1;


      /* Compute  hours */
  THH = OffSec/3600;
  /* Compute minutes */
  TMM = (OffSec % 3600)/60;
  /* Compute seconds */
  TSS = (OffSec % 3600)% 60;
  }
#endif


  sprintf1(data,"Date: %0.4d-%0.2d-%0.2d\r\nTime: %0.2d:%0.2d:%0.2d\r\n",TY, TM, TD,THH, TMM, TSS);
  USART1_PutString(data);
}


/*******************************************************************************
* Function Name  : Time_Show
* Description    : Shows the current time (HH:MM:SS) on the Hyperterminal.
* Input          : None
* Output         : None
* Return         : None
******************************************************************************/
void Time_Show(void)
{
  USART1_PutString("\n\r");
 
  /* Infinite loop */
  while (1)
  {
    // If 1s has paased
    if(TimeDisplay == 1)
    {   
      // Display current time
      Time_Display(RTC_GetCounter());
      TimeDisplay = 0;
    }
  }
}


/*******************************************************************************
* Function Name  : RTC_IRQHandler
* Description    : This function handles RTC global interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_IRQHandler(void)
{
  if(RTC_GetITStatus(RTC_IT_SEC) != RESET)
  {
    /* Clear the RTC Second interrupt */
    RTC_ClearITPendingBit(RTC_IT_SEC);
    /* Enable time update */
    TimeDisplay = 1;
    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    /* Reset RTC Counter when Time is 23:59:59 */
    if(RTC_GetCounter() == 0x00015180)
    {
      RTC_SetCounter(0x0);
      /* Wait until last write operation on RTC registers has finished */
     RTC_WaitForLastTask();
    }
  }
}



 

文章评论2条评论)

登录后参与讨论

用户241153 2009-12-5 15:38

我也在做STM32的RTC #include "RTC.h" #include "USART1.h" #include "rprintf.h" 可以把这3个H文件发给我参考下吗,迫切需要。 邮箱lang1988118@163.com万分感谢。

ilove314_323192455 2008-8-30 20:22

what?
相关推荐阅读
用户158720 2010-01-20 18:55
好久没有写博客了,如果想找请看下边的网址
http://bbs.eeworld.com.cn/index.php?fromuser=daicheng...
用户158720 2008-12-20 18:44
状态机的两种写法
有限状态机FSM思想广泛应用于硬件控制电路设计,也是软件上常用的一种处理方法(软件上称为FMM--有限消息机)。它把复杂的控制逻辑分解成有限个稳定状态,在每个状态上判断事件,变连续处理为离散数字处理,...
用户158720 2008-12-20 18:43
基于状态机控制的面向对象的前后台协从多任务系统设计
一、任务分析    根据题目要求,划分任务如下:    1、键盘扫描线程    2、灯显示线程    3、LED1-LED4四个独立线程    4、后台监视线程    5、串口收发中断    共计7个...
用户158720 2008-12-20 18:42
一种裸奔多任务模型
stateMachine + timerTick + queue。在RTOS环境下的多任务模型:任务通常阻塞在一个OS调用上(比如从消息队列取数据)。外部如果想让该任务运转,就要向消息队列发送消息。任...
用户158720 2008-12-19 20:35
C-51语言程序设计基础
c51设计基础<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />1.7 寄存器组定义8...
用户158720 2008-12-19 20:31
硬件工程师面试试题
模拟电路 1、基尔霍夫定理的内容是什么?(仕兰微电子) 2、平板电容公式(C=εS/4πkd)。(未知) 3、最基本的如三极管曲线特性。(未知) 4、描述反馈电路的概念,列举他们的应用。(仕兰微电子)...
我要评论
2
4
关闭 站长推荐上一条 /2 下一条