新聞中心

        基于 STM32 RTC的萬年歷

        作者: 時(shí)間:2016-12-03 來源:網(wǎng)絡(luò) 收藏
        例子基本是照抄官方的 萬年歷算法也沒深入研究 主要是大賽 都要求會用DS1302 若我用STM32來做 肯定不用那個片子了。

        這個用的是 LSE (片外低速時(shí)鐘)配合 掉電寄存器來確定是否配置時(shí)鐘。

        本文引用地址:http://www.104case.com/article/201612/325211.htm

        注釋很全 話不多說了。

        u8 TimeDisplay;
        int main(void)
        {
        SystemInit();

        stm32_Init ();//GPIO PA8 Init
        USART_Configuration();//USART2 9600-8-N-1
        NVIC_Configuration();//Enable the RTC Interrupt
        RTC_Configuration();//RTC的啟動

        start_rct();//檢測是否配置時(shí)鐘
        Time_Show();//不斷地時(shí)鐘串口輸出

        }


        void LEDToggle(void)
        {
        GPIOA->ODR=GPIOA->ODR^GPIO_Pin_8 ;
        }

        RTC.C///////////////////////////////////////////////////////////////////////////////

        #include "stm32f10x.h"
        #include //用到printf函數(shù)的串口的輸出函數(shù) 注意勾選MicroLIB

        u32 Time_Regulate(void);
        void Time_Adjust(void);
        void Time_Show(void);
        void Time_Display(u32 TimeVar);
        u32 USART_Scanf(u32 value);
        extern u8 TimeDisplay;

        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(); //記錄0XA5A5 來確定是否重置時(shí)間

        /* 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[13] = {0,31,28,31,30, 31, 30, 31, 31, 30, 31, 30, 31};

        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(void)
        {
        #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;

        printf("rn==============Time Settings=====================================");

        #if 1
        printf("rn Please Set Year");
        while(Tmp_Year == 0xFFFF)
        {
        /*32-bit counter at Second Unit--> 4*1024*1024(s) --> 49710(day) --> 136(year)*/
        Tmp_Year = USART_Scanf(2136);
        }
        printf(": %d", Tmp_Year);

        printf("rn Please Set Month");
        while(Tmp_Month == 0xFF)
        {
        Tmp_Month = USART_Scanf(12);
        }
        printf(": %d", Tmp_Month);

        printf("rn Please Set Date");
        while(Tmp_Date == 0xFF)
        {
        Tmp_Date = USART_Scanf(31);
        }
        printf(": %d", Tmp_Date);
        #endif

        printf("rn Please Set Hours");
        while(Tmp_HH == 0xFF)
        {
        Tmp_HH = USART_Scanf(23);
        }
        printf(": %d", Tmp_HH);

        printf("rn Please Set Minutes");
        while(Tmp_MM == 0xFF)
        {
        Tmp_MM = USART_Scanf(59);
        }
        printf(": %d", Tmp_MM);;

        printf("rn 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.2dr",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("nr");

        /* Infinite loop */
        while (1)
        {
        /* If 1s has paased */
        if(TimeDisplay == 1)
        {
        /* Display current time */
        Time_Display(RTC_GetCounter());
        TimeDisplay = 0;
        }
        }
        }

        /*******************************************************************************
        * 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(USART2, USART_FLAG_RXNE) == RESET)
        {
        }
        tmp[index++] = (USART_ReceiveData(USART2));
        if((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
        {
        printf("nrPlease 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("nrPlease enter valid number between 0 and %d", value);
        return 0xFF;
        }
        return index;
        }

        void start_rct(void)
        {
        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) */

        printf("rnn RTC not yet configured....");

        /* RTC Configuration */
        RTC_Configuration();

        printf("rn RTC configured....");

        /* 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)
        {
        printf("rnn Power On Reset occurred....");
        }
        /* Check if the Pin Reset flag is set */
        else if(RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
        {
        printf("rnn External Reset occurred....");
        }

        printf("rn No need to configure RTC....");
        /*一下都是可以省略的 RTC_Configuration 已有啟用 RTC_IT_SEC */
        /* Wait for RTC registers synchronization */
        RTC_WaitForSynchro();

        /* Enable the RTC Second */
        RTC_ITConfig(RTC_IT_SEC, ENABLE);
        /* Wait until last write operation on RTC registers has finished */
        RTC_WaitForLastTask();
        }

        }

        其中這個函數(shù)決定了 printf 函數(shù)的輸出目標(biāo) 一定要有的。
        int fputc(int ch)
        {
        USART_SendData(USART2, (u8) ch);
        while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
        return ch;
        }

        秒中斷;

        /******************************************************************************/
        /* STM32F10x Peripherals Interrupt Handlers */
        /******************************************************************************/

        /**
        * @brief This function handles RTC global interrupt request.
        * @param None
        * @retval None
        */
        extern u8 TimeDisplay;
        void LEDToggle(void);
        void RTC_IRQHandler(void)
        {
        if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
        {
        /* Clear the RTC Second interrupt */
        RTC_ClearITPendingBit(RTC_IT_SEC);

        /* Toggle LED1 閃燈*/
        LEDToggle();

        /* Enable time update */
        TimeDisplay = 1;
        }
        }

        不要使用串口助手 推薦使用超級終端。



        關(guān)鍵詞: STM32RTC萬年

        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 黄冈市| 定襄县| 东平县| 永顺县| 虎林市| 泸西县| 莱阳市| 巍山| 汶川县| 独山县| 新晃| 崇礼县| 巴林左旗| 泰顺县| 蚌埠市| 孟州市| 麻城市| 吉林市| 江西省| 景宁| 屏东市| 屯昌县| 海门市| 侯马市| 谢通门县| 句容市| 贡觉县| 泗阳县| 水城县| 正蓝旗| 江永县| 台北市| 商水县| 中阳县| 会同县| 麻城市| 淮滨县| 惠来县| 东城区| 黄大仙区| 拜城县|