⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 用stm32设计的rtc万年历程序
💻 C
📖 第 1 页 / 共 2 页
字号:
  printf("\r\n  Please Set Date");
  while(Tmp_Date == 0xFF)
  {
    Tmp_Date = USART_Scanf(31);
  }
  printf(":  %d", Tmp_Date);
#endif
  
  printf("\r\n  Please Set Hours"); 
  while(Tmp_HH == 0xFF)
  {
    Tmp_HH = USART_Scanf(23);
  }
  printf(":  %d", Tmp_HH);
  
  printf("\r\n  Please Set Minutes");
  while(Tmp_MM == 0xFF)
  {
    Tmp_MM = USART_Scanf(59);
  }
  printf(":  %d", Tmp_MM);;
  
  printf("\r\n  Please Set Seconds");
  while(Tmp_SS == 0xFF)
  {
    Tmp_SS = USART_Scanf(59);
  }
  printf(":  %d", Tmp_SS);
  
#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 the value to store in RTC counter register */
  //return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
  return TotSeconds;
}

/*******************************************************************************
* Function Name  : Time_Adjust
* Description    : Adjusts time.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Time_Adjust(void)
{
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  /* Change the current time */
  RTC_SetCounter(Time_Regulate());
  /* 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)
{ 
#if 1
  u32 TY = 0, TM = 1, TD = 0;
  s32 Num4Y,NumY, OffSec, Off4Y = 0;
  u32 i;
  s32 NumDay, OffDay;
#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

  printf("Date: %0.4d-%0.2d-%0.2d Time: %0.2d:%0.2d:%0.2d\r",TY, TM, TD,THH, TMM, TSS);
}

/*******************************************************************************
* 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)
{
  printf("\n\r");
  
  /* Infinite loop */ 
  while (1)
  {
    /* If 1s has paased */
    if(TimeDisplay == 1)
    {    
      /* Display current time */
      Time_Display(RTC_GetCounter());
      TimeDisplay = 0;
    }
  }
}

/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configures the USART1.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USART_Configuration(void)
{
  USART_InitTypeDef USART_InitStructure;

/* USART1 configuration ------------------------------------------------------*/
  /* USART1 configured as follow:
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        - USART Clock disabled
        - USART CPOL: Clock is active low
        - USART CPHA: Data is captured on the middle 
        - USART LastBit: The clock pulse of the last data bit is not output to 
                         the SCLK pin
  */
  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_InitStructure.USART_Clock = USART_Clock_Disable;
  USART_InitStructure.USART_CPOL = USART_CPOL_Low;
  USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

  USART_Init(USART1, &USART_InitStructure);

  /* Enable USART1 */
  USART_Cmd(USART1, ENABLE);
}

/*******************************************************************************
* Function Name  : PUTCHAR_PROTOTYPE
* Description    : Retargets the C library printf function to the USART.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (u8) ch);

  /* Loop until the end of transmission */
  while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {
  }

  return ch;
}

void USART_OUTString(char * str)
{
  while((*str)!=NULL)
  {
    USART_SendData(USART1, (u8) (*str));
    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
    str++;
  }
}

/*******************************************************************************
* Function Name  : USART_Scanf
* Description    : Gets numeric values from the hyperterminal.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
u32 USART_Scanf(u32 value)
{
  u32 index = 0;
  u32 tmp[4] = {0, 0};
  u32 Num;
  
  if (value==2136)
    Num = 4;
  else
    Num = 2;
    
  while(index < Num)
  {
    /* Loop until RXNE = 1 */
    while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
    {
    }
    tmp[index++] = (USART_ReceiveData(USART1));
    if((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
    {
      printf("\n\rPlease enter valid number between 0 and 9");
      index--;
    }
  }
  /* Calculate the Corresponding value */
  if (value!=2136)
    index = ((tmp[0] - 0x30) * 10) + (tmp[1] - 0x30);
  else
    index = ((tmp[0] - 0x30) * 1000) + ((tmp[1] - 0x30) * 100) + ((tmp[2] - 0x30) * 10) + (tmp[3] - 0x30); 
  /* Checks */
  if(index > value)
  {
    printf("\n\rPlease enter valid number between 0 and %d", value);
    return 0xFF;
  }
  return index;
}

#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -