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

📄 stm32rtc.txt

📁 STM32具有32位秒计数器
💻 TXT
📖 第 1 页 / 共 2 页
字号:
#include "stm32f10x_lib.h"


#define RTCClockSource_LSE   /* Use the external 32.768 KHz oscillator as RTC clock source */

vu32 TimeDisplay = 0;
/* Date Structure definition */
struct date_t
{
  u8 month;
  u8 day;
  u16 year;
};
struct date_t date_s;
/**************************************/
const u8 MaxDayArray[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //
u8 RTHour=11,RTMinute=6,RTSecond=50;

void rtc_start(void)
{
    /* Initialize Date structure */
  date_s.month = 2;
  date_s.day = 27;
  date_s.year =8;

  if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  {
    /* Backup data register value is not correct or not yet programmed (when
       the first time the program is executed) */
    /* RTC Configuration */
   RTC_Configuration();
    /* Adjust time by values entred by the user on the hyperterminal */
    Time_Adjust();
    BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  }
  else
  {
    /* Check if the Power On Reset flag is set */
    if(RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
    {}
    /* Check if the Pin Reset flag is set */
    else if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
    {
    }
    /* PWR and BKP clocks selection ------------------------------------------*/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);

    /* 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();
    /* Initialize Date structure */
    date_s.month = (BKP_ReadBackupRegister(BKP_DR3) & 0xFF00) >> 8;
    date_s.day = (BKP_ReadBackupRegister(BKP_DR3) & 0x00FF);
    date_s.year = BKP_ReadBackupRegister(BKP_DR2);
  }
}
/*******************************************************************************
* Function Name  : RTC_Configuration
* Description    : Configures the RTC.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
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();

#ifdef RTCClockSource_LSI
  /* Enable LSI */
  RCC_LSICmd(ENABLE);
  /* Wait till LSI is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {
  }

  /* Select LSI as RTC Clock Source */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
#elif defined	RTCClockSource_LSE
  /* 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);
#endif


  /* Enable RTC Clock */
  RCC_RTCCLKCmd(ENABLE);


#ifdef RTCClockOutput_Enable
  /* Disable the Tamper Pin */
  BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
                               functionality must be disabled */

  /* Enable RTC Clock Output on Tamper Pin */
  BKP_RTCCalibrationClockOutputCmd(ENABLE);
#endif

  /* 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 */
#ifdef RTCClockSource_LSI
  RTC_SetPrescaler(31999); /* RTC period = RTCCLK/RTC_PR = (32.000 KHz)/(31999+1) */
#elif defined	RTCClockSource_LSE
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
#endif

  /* 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 Time_Regulate(void)
{
  u32 Tmp_HH = RTHour, Tmp_MM = RTMinute, Tmp_SS = RTSecond;
  while(Tmp_HH == 24)
  {
    Tmp_HH = 0;
  }
  while(Tmp_MM == 60)
  {
    Tmp_MM = 0;
  }
  while(Tmp_SS == 60)
  {
    Tmp_SS =0;
  }
  /* Return the value to store in RTC counter register */
  return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
}

/*******************************************************************************
* 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
*******************************************************************************/
void Time_Display(u32 TimeVar)
{
  u8 THH = 0, TMM = 0, TSS = 0;

  /* Compute  hours */
  THH = TimeVar/3600;
  /* Compute minutes */
  TMM = (TimeVar % 3600)/60;
  /* Compute seconds */
  TSS = (TimeVar % 3600)% 60;

  RTHour=(u8)THH;
  RTMinute=(u8)TMM;
  RTSecond=(u8)TSS;


}
 /*******************************************************************************
* Function Name  : Time_PreAdjust
* Description    : Returns the time entered by user, using demoboard keys.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void Time_PreAdjust(void)
{

   /* Check which key is pressed */
  /* Regulate hour */
  line_h(75,144,174,h_blue);
  Regulatehour();
  line_h(75,144,174,black);
  line_h(75,194,224,h_blue);
  /* Regulate minute */
  RegulateMinute();
  line_h(75,194,224,black);
  if(BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);
    /* RTC Configuration */
    RTC_Configuration();
    /* Wait until last write operation on RTC registers has finished */
   Time_Adjust();
   BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  }
  else
  {
    /* Change the current time */
     Time_Adjust();
  }
}
/*******************************************************************************
* Function Name  : Regulatehour
* Description    : Regulates the hour.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void Regulatehour(void)
{
  u8 tmpValue = 0;
  u8 MyKey = 0;

  /* Initialize tmpValue */
  tmpValue = RTHour;

  /* Endless loop */
  while(1)
  {
    /* Check which key is pressed */
    MyKey = ReadKey();

    /* If "UP" pushbutton is pressed */
    if(MyKey == UP)
    {
       ++tmpValue;
      /* Increase the value of the digit */
      if(tmpValue == 24)
      {
        tmpValue = 0;
      }
       Writetime(tmpValue,50,144);
    }
    /* If "DOWN" pushbutton is pressed */
    if(MyKey == DOWN)
    {
      /* Decrease the value of the digit */
      if(tmpValue == 0)
      {

        tmpValue =24;
      }
      /* Display new value */
       Writetime(--tmpValue,50,144);
    }
    /* If "SEL" pushbutton is pressed */
    if(MyKey == SEL)
    {
      /* Display new value */
      Writetime(tmpValue,50,144);
      /* Return the digit value and exit */
      RTHour = tmpValue;
      return;
    }
  }
}

/*******************************************************************************
* Function Name  : RegulateMinute
* Description    : Regulates Minute.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
static void RegulateMinute(void)
{
  u8 tmpValue = 0;
  u8 MyKey = 0;

  /* Initialize tmpValue */
  tmpValue =RTMinute;

  /* Endless loop */

⌨️ 快捷键说明

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