久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7340|回復: 11
打印 上一主題 下一主題
收起左側

stm32超強日歷源碼(含24節氣和閏平年)

  [復制鏈接]
跳轉到指定樓層
樓主
ID:342296 發表于 2018-6-1 09:38 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本歷程使用STM32F103VET6編寫的電子日歷,除了常有的日期和星期外,領加了24節氣;

單片機源程序如下:
  1. /******************** (C) COPYRIGHT 2011 野火嵌入式開發工作室 ********************
  2. * 文件名  :main.c
  3. * 描述    :超強的日歷,支持農歷,24節氣幾乎所有日歷的功能 日歷時間以1970年為元年,
  4. *           用32bit的時間寄存器可以運行到2100年左右。         
  5. * 實驗平臺:野火STM32開發板
  6. * 庫版本  :ST3.0.0
  7. **********************************************************************************/
  8. #include "stm32f10x.h"
  9. #include "stdio.h"
  10. #include "calendar.h"
  11. #include "date.h"

  12. __IO uint32_t TimeDisplay = 0;

  13. void RCC_Configuration(void);
  14. void NVIC_Configuration(void);
  15. void GPIO_Configuration(void);
  16. void USART_Configuration(void);
  17. int fputc(int ch, FILE *f);
  18. void RTC_Configuration(void);
  19. //u32 Time_Regulate(void);
  20. void Time_Regulate(struct rtc_time *tm);
  21. void Time_Adjust(void);
  22. void Time_Display(uint32_t TimeVar);
  23. void Time_Show(void);
  24. u8 USART_Scanf(u32 value);


  25. #define  RTCClockSource_LSE        


  26. u8 const *WEEK_STR[] = {"日", "一", "二", "三", "四", "五", "六"};
  27. u8 const *zodiac_sign[] = {"豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗"};

  28. struct rtc_time systmtime;

  29. int main()
  30. {
  31.           /* System Clocks Configuration */
  32.           RCC_Configuration();
  33.         
  34.           /* NVIC configuration */
  35.           NVIC_Configuration();
  36.         
  37.           GPIO_Configuration();
  38.         
  39.           USART_Configuration();
  40.         
  41.           /*在啟動時檢查備份寄存器BKP_DR1,如果內容不是0xA5A5,則需重新配置時間并詢問用戶調整時間*/
  42.         if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  43.         {
  44.             printf("\r\n\n RTC not yet configured....");
  45.                
  46.             /* RTC Configuration */
  47.             RTC_Configuration();
  48.                 printf("\r\n RTC configured....");
  49.                
  50.                 /* Adjust time by users typed on the hyperterminal */
  51.             Time_Adjust();
  52.                
  53.                 BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  54.         }
  55.         else
  56.         {
  57.             /*啟動無需設置新時鐘*/
  58.                 /*檢查是否掉電重啟*/
  59.                 if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
  60.                 {
  61.                     printf("\r\n\n Power On Reset occurred....");
  62.                 }
  63.                 /*檢查是否Reset復位*/
  64.                 else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
  65.             {
  66.               printf("\r\n\n External Reset occurred....");
  67.             }
  68.                
  69.                 printf("\r\n No need to configure RTC....");
  70.                
  71.                 /*等待寄存器同步*/
  72.                 RTC_WaitForSynchro();
  73.                
  74.                 /*允許RTC秒中斷*/
  75.                 RTC_ITConfig(RTC_IT_SEC, ENABLE);
  76.                
  77.                 /*等待上次RTC寄存器寫操作完成*/
  78.                 RTC_WaitForLastTask();
  79.         }

  80.         #ifdef RTCClockOutput_Enable
  81.           /* Enable PWR and BKP clocks */
  82.           RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  83.         
  84.           /* Allow access to BKP Domain */
  85.           PWR_BackupAccessCmd(ENABLE);
  86.         
  87.           /* Disable the Tamper Pin */
  88.           BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamper
  89.                                          functionality must be disabled */
  90.         
  91.           /* Enable RTC Clock Output on Tamper Pin */
  92.           BKP_RTCOutputConfig(BKP_RTCOutputSource_CalibClock);
  93.         #endif
  94.         
  95.           /* Clear reset flags */
  96.           RCC_ClearFlag();
  97.         
  98.           /* Display time in infinite loop */
  99.           Time_Show();
  100. }

  101. void RCC_Configuration()
  102. {
  103.                 SystemInit();
  104.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  105. }

  106. void NVIC_Configuration()
  107. {
  108.           NVIC_InitTypeDef NVIC_InitStructure;
  109.         
  110.           /* Configure one bit for preemption priority */
  111.           NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  112.         
  113.           /* Enable the RTC Interrupt */
  114.           NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  115.           NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  116.           NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  117.           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  118.           NVIC_Init(&NVIC_InitStructure);
  119. }

  120. void GPIO_Configuration()
  121. {
  122.           GPIO_InitTypeDef GPIO_InitStructure;
  123.         
  124.            /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  125.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  126.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  127.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  128.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  129.             
  130.           /* Configure USART1 Rx (PA.10) as input floating */
  131.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  132.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  133.           GPIO_Init(GPIOA, &GPIO_InitStructure);

  134. }

  135. /*******************************************************************************
  136. * Function Name  : USART_Configuration
  137. * Description    : Configures the USART1.
  138. * Input          : None
  139. * Output         : None
  140. * Return         : None
  141. *******************************************************************************/
  142. void USART_Configuration()
  143. {
  144.             USART_InitTypeDef USART_InitStructure;
  145.         
  146.                 USART_InitStructure.USART_BaudRate = 115200;
  147.                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  148.                 USART_InitStructure.USART_StopBits = USART_StopBits_1;
  149.                 USART_InitStructure.USART_Parity = USART_Parity_No ;
  150.                 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  151.             USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  152.         
  153.                 USART_Init(USART1, &USART_InitStructure);
  154.             USART_Cmd(USART1, ENABLE);
  155. }

  156. /*******************************************************************************
  157. * Function Name  : fputc
  158. * Description    : Retargets the C library printf function to the USART or ITM Viewer.
  159. * Input          : None
  160. * Output         : None
  161. * Return         : None
  162. *******************************************************************************/
  163. int fputc(int ch, FILE *f)
  164. {
  165.           /* 將Printf內容發往串口 */
  166.           USART_SendData(USART1, (unsigned char) ch);

  167.           while (!(USART1->SR & USART_FLAG_TXE));
  168.          
  169.           return (ch);
  170. }

  171. /***********************************************************************
  172. *函數名:RTC_Configuration
  173. *描述:配置RTC
  174. *輸入:無
  175. *輸出:無
  176. *返回:無
  177. ************************************************************************/
  178. void RTC_Configuration()
  179. {
  180.      /*允許PWR和BKP時鐘*/
  181.          RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  182.          
  183.          /*允許訪問BKP域*/
  184.          PWR_BackupAccessCmd(ENABLE);
  185.          
  186.          /*復位備份域*/
  187.          BKP_DeInit();
  188.          
  189.          #ifdef RTCClockSource_LSI
  190.          
  191.          /*允許LSI*/
  192.          RCC_LSICmd(ENABLE);
  193.          
  194.          /*等待LSI準備好*/
  195.          while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET)
  196.          {
  197.          }
  198.          
  199.          /*選擇LSI作為RTC時鐘源*/
  200.          RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  201.          
  202.          #elif defined  RTCClockSource_LSE
  203.          
  204.          /*允許LSE*/
  205.          RCC_LSEConfig(RCC_LSE_ON);
  206.          
  207.          /*等待LSE準備好*/
  208.          while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET)
  209.          {
  210.          }
  211.          
  212.          /*選擇LSE作為RTC時鐘源*/
  213.          RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  214.          #endif
  215.          
  216.          /* Enable RTC Clock */
  217.      RCC_RTCCLKCmd(ENABLE);
  218.          
  219.          #ifdef RTCClockOutput_Enable
  220.          /*禁止Tamper引腳*/
  221.          BKP_TamperPinCmd(DISABLE);/*為了將RTCCLK/64在Tamper引腳輸出,Tamper功能必須被禁止*/
  222.          
  223.          /*允許RTC時鐘在Tamper引腳上輸出*/
  224.          BKP_RTCCalibrationClockOutputCmd(ENABLE);
  225.          #endif
  226.          
  227.          /*等待寄存器同步*/
  228.          RTC_WaitForSynchro();
  229.          
  230.          /*等待上次RTC寄存器寫操作完成*/
  231.          RTC_WaitForLastTask();
  232.          
  233.          /*允許RTC秒中斷*/
  234.          RTC_ITConfig(RTC_IT_SEC, ENABLE);
  235.          
  236.          /*等待上次RTC寄存器寫操作完成*/
  237.          RTC_WaitForLastTask();
  238.          
  239.          #ifdef RTCClockSource_LSI
  240.          /*設置分頻系數*/
  241.          RTC_SetPrescaler(31999); /*RTC周期=RTCCLK/RTC_PR=(32.000kHz/(31999+1))*/
  242.          
  243.          #elif defined  RTCClockSource_LSE
  244.          RTC_SetPrescaler(32767); /*RTC周期=RTCCLK/RTC_PR=(32.768kHz/(31767+1))*/
  245.          #endif
  246.          
  247.          /*等待上次RTC寄存器寫操作完成*/
  248.          RTC_WaitForLastTask();
  249.         
  250. }

  251. /*******************************************************************************
  252. * Function Name  : Time_Regulate
  253. * Description    : Returns the time entered by user, using Hyperterminal.
  254. * Input          : None
  255. * Output         : None
  256. * Return         : Current time RTC counter value
  257. *******************************************************************************/
  258. //u32 Time_Regulate()
  259. //{
  260. //          u32 Tmp_HH = 0xFF, Tmp_MM = 0xFF, Tmp_SS = 0xFF;
  261. //        
  262. //          printf("\r\n==============Time Settings=====================================");
  263. //          printf("\r\n  Please Set Hours");
  264. //        
  265. //          while (Tmp_HH == 0xFF)
  266. //          {
  267. //            Tmp_HH = USART_Scanf(23);
  268. //          }
  269. //          printf(":  %d", Tmp_HH);
  270. //          printf("\r\n  Please Set Minutes");
  271. //          while (Tmp_MM == 0xFF)
  272. //          {
  273. //            Tmp_MM = USART_Scanf(59);
  274. //          }
  275. //          printf(":  %d", Tmp_MM);
  276. //          printf("\r\n  Please Set Seconds");
  277. //          while (Tmp_SS == 0xFF)
  278. //          {
  279. //            Tmp_SS = USART_Scanf(59);
  280. //          }
  281. //          printf(":  %d", Tmp_SS);
  282. //        
  283. //          /* 返回用戶輸入值,以便存入RTC計數寄存器 */
  284. //          return((Tmp_HH*3600 + Tmp_MM*60 + Tmp_SS));
  285. //}

  286. void Time_Regulate(struct rtc_time *tm)
  287. {
  288.           u32 Tmp_YY = 0xFF, Tmp_MM = 0xFF, Tmp_DD = 0xFF, Tmp_HH = 0xFF, Tmp_MI = 0xFF, Tmp_SS = 0xFF;
  289.         
  290.           printf("\r\n=========================Time Settings==================");
  291.         
  292.           printf("\r\n  請輸入年份(Please Set Years):  20");

  293.           while (Tmp_YY == 0xFF)
  294.           {
  295.             Tmp_YY = USART_Scanf(99);
  296.           }

  297.           printf("\n\r  年份被設置為:  20%0.2d\n\r", Tmp_YY);

  298.           tm->tm_year = Tmp_YY+2000;
  299.         
  300.           Tmp_MM = 0xFF;

  301.           printf("\r\n  請輸入月份(Please Set Months):  ");

  302.           while (Tmp_MM == 0xFF)
  303.           {
  304.             Tmp_MM = USART_Scanf(12);
  305.           }

  306.           printf("\n\r  月份被設置為:  %d\n\r", Tmp_MM);

  307.           tm->tm_mon= Tmp_MM;
  308.         
  309.           Tmp_DD = 0xFF;

  310.           printf("\r\n  請輸入日期(Please Set Dates):  ");

  311.           while (Tmp_DD == 0xFF)
  312.           {
  313.             Tmp_DD = USART_Scanf(31);
  314.           }

  315.           printf("\n\r  日期被設置為:  %d\n\r", Tmp_DD);

  316.           tm->tm_mday= Tmp_DD;
  317.         
  318.           Tmp_HH  = 0xFF;

  319.           printf("\r\n  請輸入時鐘(Please Set Hours):  ");

  320.           while (Tmp_HH == 0xFF)
  321.           {
  322.             Tmp_HH = USART_Scanf(23);
  323.           }

  324.           printf("\n\r  時鐘被設置為:  %d\n\r", Tmp_HH );

  325.           tm->tm_hour= Tmp_HH;
  326.             
  327.           Tmp_MI = 0xFF;

  328.           printf("\r\n  請輸入分鐘(Please Set Minutes):  ");

  329.           while (Tmp_MI == 0xFF)
  330.           {
  331.             Tmp_MI = USART_Scanf(59);
  332.           }

  333.           printf("\n\r  分鐘被設置為:  %d\n\r", Tmp_MI);

  334.           tm->tm_min= Tmp_MI;
  335.          
  336.           Tmp_SS = 0xFF;

  337.           printf("\r\n  請輸入秒鐘(Please Set Seconds):  ");

  338.           while (Tmp_SS == 0xFF)
  339.           {
  340.             Tmp_SS = USART_Scanf(59);
  341.           }

  342.           printf("\n\r  秒鐘被設置為:  %d\n\r", Tmp_SS);

  343.           tm->tm_sec= Tmp_SS;
  344. }

  345. /*******************************************************************************
  346. * Function Name  : Time_Adjust
  347. * Description    : Adjusts time.
  348. * Input          : None
  349. * Output         : None
  350. * Return         : None
  351. *******************************************************************************/
  352. //void Time_Adjust()
  353. //{
  354. //          /* Wait until last write operation on RTC registers has finished */
  355. //          RTC_WaitForLastTask();
  356. //         
  357. //          /* 修改當前RTC計數寄存器內容 */
  358. //          RTC_SetCounter(Time_Regulate());
  359. //         
  360. //          /* Wait until last write operation on RTC registers has finished */
  361. //          RTC_WaitForLastTask();
  362. //}

  363. void Time_Adjust()
  364. {
  365.           /* Wait until last write operation on RTC registers has finished */
  366.           RTC_WaitForLastTask();
  367.         
  368.           /* Get time entred by the user on the hyperterminal */
  369.           Time_Regulate(&systmtime);
  370.          
  371.           /* Get wday */
  372.           GregorianDay(&systmtime);

  373.           /* 修改當前RTC計數寄存器內容 */
  374.           RTC_SetCounter(mktimev(&systmtime));

  375.           /* Wait until last write operation on RTC registers has finished */
  376.           RTC_WaitForLastTask();
  377. }

  378. /*******************************************************************************
  379. * Function Name  : Time_Display
  380. * Description    : Displays the current time.
  381. * Input          : - TimeVar: RTC counter value.
  382. * Output         : None
  383. * Return         : None
  384. *******************************************************************************/
  385. //void Time_Display(u32 TimeVar)
  386. //{
  387. //          u32 THH = 0, TMM = 0, TSS = 0;
  388. //        
  389. //          /* Compute  hours */
  390. //          THH = TimeVar / 3600;
  391. //          /* Compute minutes */
  392. //          TMM = (TimeVar % 3600) / 60;
  393. //          /* Compute seconds */
  394. //          TSS = (TimeVar % 3600) % 60;
  395. //        
  396. //          printf("Time: %0.2d:%0.2d:%0.2d\r", THH, TMM, TSS);
  397. //}

  398. void Time_Display(uint32_t TimeVar)
  399. {
  400.            static uint32_t FirstDisplay = 1;
  401.            u8 str[15]; // 字符串暫存
  402.            
  403.            to_tm(TimeVar, &systmtime);
  404.         
  405.           if((!systmtime.tm_hour && !systmtime.tm_min && !systmtime.tm_sec)  || (FirstDisplay))
  406.           {
  407.               
  408.               GetChinaCalendar((u16)systmtime.tm_year, (u8)systmtime.tm_mon, (u8)systmtime.tm_mday, str);
  409.         
  410.               printf("\n\r\n\r  今天農歷:%0.2d%0.2d,%0.2d,%0.2d", str[0], str[1], str[2],  str[3]);
  411.         
  412.               GetChinaCalendarStr((u16)systmtime.tm_year,(u8)systmtime.tm_mon,(u8)systmtime.tm_mday,str);
  413.               printf("  %s", str);
  414.         
  415.              if(GetJieQiStr((u16)systmtime.tm_year, (u8)systmtime.tm_mon, (u8)systmtime.tm_mday, str))
  416.                   printf("  %s\n\r", str);
  417.         
  418.               FirstDisplay = 0;
  419.           }

  420.          

  421.           /* 輸出公歷時間 */
  422.           printf("\r   當前時間為: %d年(%s年) %d月 %d日 (星期%s)  %0.2d:%0.2d:%0.2d",
  423.                             systmtime.tm_year, zodiac_sign[(systmtime.tm_year-3)%12], systmtime.tm_mon, systmtime.tm_mday,
  424.                             WEEK_STR[systmtime.tm_wday], systmtime.tm_hour,
  425.                             systmtime.tm_min, systmtime.tm_sec);

  426. }

  427. /*******************************************************************************
  428. * Function Name  : Time_Show
  429. * Description    : Shows the current time (HH:MM:SS) on the Hyperterminal.
  430. * Input          : None
  431. * Output         : None
  432. * Return         : None
  433. ******************************************************************************/
  434. void Time_Show()
  435. {
  436.           printf("\n\r");
  437.         
  438.           /* Infinite loop */
  439.           while (1)
  440.           {
  441.             /* 每過1s */
  442. ……………………

  443. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:

超強日歷(含24節氣和閏平年).rar (255.87 KB, 下載次數: 245)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏8 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:389200 發表于 2019-3-23 17:59 | 只看該作者
很想要這資源
回復

使用道具 舉報

板凳
ID:78947 發表于 2019-11-15 13:50 | 只看該作者
贊,跑得很6,感謝樓主分享,,,之前在論壇下載別人的f103cb_FreeRTOS跑到硬件故障中斷了
回復

使用道具 舉報

地板
ID:443840 發表于 2019-11-30 10:53 | 只看該作者
想要這程序可黑幣還差一顆星,我籌齊了過來哈
回復

使用道具 舉報

5#
ID:638375 發表于 2019-11-30 23:32 | 只看該作者
收藏學習了,謝謝分享!!
回復

使用道具 舉報

6#
ID:300101 發表于 2019-12-4 23:44 | 只看該作者
可算找到了,收藏學習,謝謝分享!
回復

使用道具 舉報

7#
ID:574789 發表于 2019-12-23 07:55 | 只看該作者
戰艦的板子能跑嗎
回復

使用道具 舉報

8#
ID:268151 發表于 2020-2-26 14:30 | 只看該作者
收藏學習了,謝謝分享
回復

使用道具 舉報

9#
ID:91165 發表于 2020-2-26 18:44 | 只看該作者
下載了,樓主辛苦了
回復

使用道具 舉報

10#
ID:793756 發表于 2020-12-7 16:47 | 只看該作者
MiniSTM32開發板能跑嗎?
回復

使用道具 舉報

11#
ID:866625 發表于 2021-6-26 21:38 | 只看該作者
proteus誰有能分享學習一下嗎?
回復

使用道具 舉報

12#
ID:216265 發表于 2021-6-28 11:04 | 只看該作者
我也開發了一個呢,還修正了天之地干表
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲人免费视频 | 欧美日韩中文在线 | 91亚洲一区| 国产精品成人av | 国产三区在线观看视频 | 免费1区2区3区 | 欧美综合一区二区 | 五月综合久久 | 久久精品超碰 | 日日骚av | 国产精品久久网 | 亚洲精品乱码久久久久久9色 | 国产精品一区二区三区在线 | 亚洲一区二区在线 | 91在线观看 | 亚洲一区中文字幕 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 日本电影网站 | 精品视频一区二区三区在线观看 | 欧美不卡视频一区发布 | 91在线观看| 99久久99久久精品国产片果冰 | 一级片av | 精品在线一区 | 人人擦人人| 在线一区视频 | 国产欧美一区二区三区在线看 | 成年人的视频免费观看 | 欧美日韩亚洲成人 | 成人av网站在线观看 | 国产精品成人国产乱一区 | 午夜激情影院 | 91在线精品一区二区 | 欧美人妇做爰xxxⅹ性高电影 | 亚洲麻豆 | 三级av免费 | 国产一区二区三区精品久久久 | 日韩不卡三区 | 精品99久久久久久 | 九九国产| 免费视频中文字幕 |