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

📄 time.c

📁 STM32 单片机例程
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "..\main\include.h"
//#include "include.h"

//#include "include.h"
//#include "..\main\STM32_Init.h"                            // missing bit definitions

//#include "STM32_Init.h"                            // missing bit definitions
//#include "..\main\STM32_Reg.h"                            // missing bit definitions

//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
//=========================================================================== Real Time Clock Configuration
// <e0> Real Time Clock Configuration
//   <o1.8..9> RTC clock source selection
//   <i> Default: No Clock
//                     <0=> No Clock
//                     <1=> RTCCLK = LSE (32,768kHz)
//                     <2=> RTCCLK = LSI (32 kHz)
//                     <3=> RTCCLK = HSE/128
//   <o2> RTC period [ms] <10-1000:10>
//   <i> Set the timer period for Real Time Clock.
//   <i> Default: 1000  (1s)
//   <h> RTC Time Value
//     <o3> Hour <0-23>
//     <o4> Minute <0-59>
//     <o5> Second <0-59>
//   </h>
//   <h> RTC Alarm Value
//     <o6> Hour <0-23>
//     <o7> Minute <0-59>
//     <o8> Second <0-59>
//   </h>
//   <e9> RTC interrupts
//     <o10.0> RTC_CRH.SECIE: Second interrupt enabled
//     <o10.1> RTC_CRH.ALRIE: Alarm interrupt enabled
//     <o10.2> RTC_CRH.OWIE: Overflow interrupt enabled
//   </e>
// </e>


#define __RTC_SETUP               1
#define __RTC_CLKSRC_VAL          0x00000100
#define __RTC_PERIOD              0x000003E8
#define __RTC_TIME_H              0x0C
#define __RTC_TIME_M              0x00
#define __RTC_TIME_S              0x00
#define __RTC_ALARM_H             0x0C
#define __RTC_ALARM_M             0x00
#define __RTC_ALARM_S             0x14
#define __RTC_INTERRUPTS          0x00000001
#define __RTC_CRH                 0x00000001

/*----------------------------------------------------------------------------
 Define  RTCCLK
 *----------------------------------------------------------------------------*/
#if   ((__RTC_CLKSRC_VAL & 0x00000300) == 0x00000000)
  #define __RTCCLK        (0)
#elif ((__RTC_CLKSRC_VAL & 0x00000300) == 0x00000100)
  #define __RTCCLK        (32768)
#elif ((__RTC_CLKSRC_VAL & 0x00000300) == 0x00000200)
  #define __RTCCLK        (32000)
#elif ((__RTC_CLKSRC_VAL & 0x00000300) == 0x00000300)
  #define __RTCCLK        (__HSE/128)
#endif


/*----------------------------------------------------------------------------
 Define Real Time Clock CNT and ALR settings
 *----------------------------------------------------------------------------*/
#define __RTC_CNT_TICKS  ((__RTC_TIME_H *3600UL)+(__RTC_TIME_M *60UL)+(__RTC_TIME_S) )
#define __RTC_ALR_TICKS  ((__RTC_ALARM_H*3600UL)+(__RTC_ALARM_M*60UL)+(__RTC_ALARM_S))
#define __RTC_CNT        (__RTC_CNT_TICKS*1000UL/__RTC_PERIOD)
#define __RTC_ALR        (__RTC_ALR_TICKS*1000UL/__RTC_PERIOD)

#if __RTC_SETUP
/*----------------------------------------------------------------------------
 STM32 Real Time Clock setup.
 initializes the RTC Prescaler and RTC counter register
 *----------------------------------------------------------------------------*/
void stm32_RtcSetup (void) {

  RCC->APB1ENR |= RCC_APB1ENR_PWREN;                            // enable clock for Power interface
  PWR->CR      |= PWR_CR_DBP;                                   // enable access to RTC, BDC registers

  if ((__RTC_CLKSRC_VAL & RCC_BDCR_RTCSEL) == 0x00000100) {     // LSE is RTC clock source
    RCC->BDCR |= RCC_BDCR_LSEON;                                // enable LSE
    while ((RCC->BDCR & RCC_BDCR_LSERDY) == 0);                 // Wait for LSERDY = 1 (LSE is ready)
  }

  if ((__RTC_CLKSRC_VAL & RCC_BDCR_RTCSEL) == 0x00000200) {     // LSI is RTC clock source
    RCC->CSR |= RCC_CSR_LSION;                                  // enable LSI
    while ((RCC->CSR & RCC_CSR_LSIRDY) == 0);                   // Wait for LSERDY = 1 (LSE is ready)
  }

  RCC->BDCR |= (__RTC_CLKSRC_VAL | RCC_BDCR_RTCEN);             // set RTC clock source, enable RTC clock

//  RTC->CRL   &= ~(1<<3);                                        // reset Registers Synchronized Flag
//  while ((RTC->CRL & (1<<3)) == 0);                             // wait until registers are synchronized

  RTC->CRL  |=  RTC_CRL_CNF;                                    // set configuration mode
  RTC->PRLH  = ((__RTC_PERIOD*__RTCCLK/1000-1)>>16) & 0x00FF;   // set prescaler load register high
  RTC->PRLL  = ((__RTC_PERIOD*__RTCCLK/1000-1)    ) & 0xFFFF;   // set prescaler load register low
  RTC->CNTH  = ((__RTC_CNT)>>16) & 0xFFFF;                      // set counter high
  RTC->CNTL  = ((__RTC_CNT)    ) & 0xFFFF;                      // set counter low
  RTC->ALRH  = ((__RTC_ALR)>>16) & 0xFFFF;                      // set alarm high
  RTC->ALRL  = ((__RTC_ALR)    ) & 0xFFFF;                      // set alarm low
  
  if (__RTC_INTERRUPTS) {                                       // RTC interrupts used
    RTC->CRH = __RTC_CRH;                                       // enable RTC interrupts
    NVIC->ISER[0] |= (1 << (RTC_IRQChannel & 0x1F));            // enable interrupt
  }
  RTC->CRL  &= ~RTC_CRL_CNF;                                    // reset configuration mode
  while ((RTC->CRL & RTC_CRL_RTOFF) == 0);                      // wait until write is finished

  PWR->CR   &= ~PWR_CR_DBP;                                     // disable access to RTC registers
} // end of stm32_RtcSetup
#endif


uint8 volatile UpdataTimeS=0x11;//秒更新标志
uint32 gSec=0;
struct tm *gTm_t;

//设置更新年月日时分秒标志
void SetUpdataTimeS(void)
{
   UpdataTimeS=0x11;
}

//返回秒更新标志
uint8 GetUpdataTimeS(void)
{uint8 temp;
  
   temp=UpdataTimeS;
   UpdataTimeS=0;
   return(temp);
}

void timebasic(void)
{
   /* Wait until last write operation on RTC registers has finished */
   RTC_WaitForLastTask();
   
   gSec=RTC_GetCounter();
   if( (gSec%60)==0 )//分更新
      UpdataTimeS |= 0x11;
   else//秒更新
      UpdataTimeS |= 0x01;
   
   /* Wait until last write operation on RTC registers has finished */
   RTC_WaitForLastTask();
}

/*******************************************************************************
* 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);
    
    //UpdataTimeS |= 0x01;
    timebasic();
    
    /* 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();
//    }
  }
}

/*******************************************************************************
* Function Name  : RTC_Configuration
* Description    : Configures the RTC.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RTC_Configuration(uint8 reset)
{
  /* Enable PWR and BKP clocks */
  //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);//// enable access to RTC, BDC registers

  /* Reset Backup Domain */
//  if(reset==1)//这个版本这里要引起复位
//    BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */
  
  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  {
     //RCC_LSEConfig(RCC_LSE_ON);//不能要这句,否则RTC不起振
     //Watchdog();
  }

  /* 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();
  /* 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();
  
  RTC_EnterConfigMode();  //RTC->CRL  |=  RTC_CRL_CNF;                                    // set configuration mode
  
  /* Enable the RTC Second 中断需要开启 */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);                           //
  //NVIC->ISER[0] |= (1 << (RTC_IRQChannel & 0x1F));            // enable interrupt
  
  RTC_ExitConfigMode();  //RTC->CRL  &= ~RTC_CRL_CNF;                                    // reset configuration mode
  
  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
  
  PWR_BackupAccessCmd(DISABLE); //// disable access to RTC registers
}

void InitTime(void)
{struct tm tm;
 //uint8 buf[16];
 
  
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  
  //cpyU8fU16(buf, BKP_ReadBackupRegister(BKP_DR1));
  //DebugSendBuf(buf, 4);
  
  RTC_Configuration(0);
  
  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) */
    
//    DebugSendBuf("\xef", 1);

⌨️ 快捷键说明

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