STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/RTC/RTC_TimeStamp/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/RTC_TimeStamp/main.c 
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "main.h"
00030 
00031 /** @addtogroup STM32F0xx_StdPeriph_Examples
00032   * @{
00033   */
00034 
00035 /** @addtogroup RTC_TimeStamp
00036   * @{
00037   */ 
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 uint32_t AsynchPrediv = 0, SynchPrediv = 0;
00044 uint32_t Secondfraction = 0;
00045 __IO uint8_t Button_State = 0;
00046 /* Private function prototypes -----------------------------------------------*/
00047 static void RTC_Time_display(uint8_t Line,__IO uint16_t Color_x, Table_TypeDef table);
00048 static Table_TypeDef RTC_Get_Time(uint32_t Secondfraction , RTC_TimeTypeDef* RTC_TimeStructure);
00049 static Table_TypeDef RTC_Get_Date(RTC_DateTypeDef* RTC_DateStructure );
00050 static void RTC_Config(void);
00051 static void RTC_TimeRegulate(void);
00052 static void RTC_TimeShow(void);
00053 static void RTC_DateShow(void);
00054 static void RTC_TimeStampShow(void);
00055 
00056 /* Private functions ---------------------------------------------------------*/
00057 
00058 /**
00059   * @brief  Main program
00060   * @param  None
00061   * @retval None
00062   */
00063 int main(void)
00064 {
00065   /*!< At this stage the microcontroller clock setting is already configured, 
00066        this is done through SystemInit() function which is called from startup
00067        file (startup_stm32f4xx.s) before to branch to application main.
00068        To reconfigure the default setting of SystemInit() function, refer to
00069        system_stm32f4xx.c file
00070      */     
00071 #if defined (RTC_CLOCK_SOURCE_LSI) 
00072   /* Enable the LSI OSC */ 
00073   RCC_LSICmd(ENABLE);
00074 #endif /* RTC_CLOCK_SOURCE_LSI */
00075   
00076   /* Configure the external interrupt "WAKEUP" and "TAMPER" buttons */ 
00077   STM_EVAL_PBInit(BUTTON_TAMPER , BUTTON_MODE_EXTI);
00078   STM_EVAL_PBInit(BUTTON_SEL , BUTTON_MODE_EXTI);
00079 
00080   /* Configure LEDs */
00081   STM_EVAL_LEDInit(LED1);
00082   STM_EVAL_LEDInit(LED2);
00083   STM_EVAL_LEDOn(LED1);
00084   
00085   /* Initialize the LCD */
00086 #ifdef USE_STM320518_EVAL
00087     STM320518_LCD_Init();
00088 #else
00089     STM32072B_LCD_Init();
00090 #endif /* USE_STM320518_EVAL */
00091 
00092   /* Clear the LCD */ 
00093   LCD_Clear(White);
00094   
00095   /* Enable The Display */
00096   LCD_DisplayOn(); 
00097   
00098   /* Set the LCD Back Color and Text Color*/
00099   LCD_SetBackColor(Blue);
00100   LCD_SetTextColor(White);
00101 
00102  
00103   LCD_DisplayStringLine(LCD_LINE_0,(uint8_t *) "  TimeStamp Example " );
00104   
00105   /* Set the LCD Text size */
00106   LCD_SetFont(&Font16x24);
00107   
00108   if (RTC_ReadBackupRegister(RTC_BKP_DR0) != 0x32F2)
00109   {
00110     /* RTC configuration  */
00111     RTC_Config();
00112 
00113     /* Configure the time&date register */
00114     RTC_TimeRegulate(); 
00115     
00116     /* Display the Date */
00117     RTC_DateShow();
00118     
00119     /* Display the Time */
00120     RTC_TimeShow();
00121     
00122   }
00123   else
00124   {
00125     /* Check if the Power On Reset flag is set */
00126     if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
00127     {
00128       /* Set the Back Color */
00129       LCD_SetBackColor(LCD_COLOR_CYAN);
00130       
00131       /* Set the Text Color */
00132       LCD_SetTextColor(LCD_COLOR_BLACK);
00133       LCD_SetFont(&Font12x12);
00134       LCD_DisplayStringLine(LCD_LINE_2,(uint8_t *) "  Power On Reset occurred        " );
00135     }
00136     /* Check if the Pin Reset flag is set */
00137     else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
00138     {
00139       /* Set the Back Color */
00140       LCD_SetBackColor(LCD_COLOR_CYAN);
00141       
00142       /* Set the Text Color */
00143       LCD_SetTextColor(LCD_COLOR_BLACK);
00144       LCD_SetFont(&Font12x12);
00145       LCD_DisplayStringLine(LCD_LINE_2,(uint8_t *) "  External Reset occurred       " );
00146     }
00147 
00148     /* Set the Back Color */
00149     LCD_SetBackColor(LCD_COLOR_CYAN);
00150     
00151     /* Set the Text Color */
00152     LCD_SetTextColor(LCD_COLOR_BLACK);
00153     LCD_SetFont(&Font12x12);
00154     LCD_DisplayStringLine(LCD_LINE_3,(uint8_t *) " No need to configure RTC     " );
00155     
00156     /* Enable the PWR clock */
00157     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00158 
00159     /* Allow access to RTC */
00160     PWR_BackupAccessCmd(ENABLE);
00161 
00162     /* Wait for RTC APB registers synchronisation */
00163     RTC_WaitForSynchro();
00164 
00165     /* Clear the RTC Alarm Flag */
00166     RTC_ClearFlag(RTC_FLAG_ALRAF);
00167 
00168     /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
00169     EXTI_ClearITPendingBit(EXTI_Line17);
00170 
00171     /* Display the RTC Time/Date and TimeStamp Time/Date */ 
00172     RTC_DateShow();
00173     RTC_TimeShow();
00174 
00175   }
00176    
00177   while (1)
00178   {
00179     if (Button_State == TAMPER_ON)
00180     {
00181       /* Turn LED4 ON */
00182       STM_EVAL_LEDOn(LED2);
00183       LCD_SetFont(&Font12x12);
00184       LCD_DisplayStringLine(LCD_LINE_5,(uint8_t *) " TimeStamp Event Occurred      " );
00185       LCD_SetFont(&Font16x24);
00186       LCD_ClearLine(LCD_LINE_4);
00187       LCD_ClearLine(LCD_LINE_5);
00188       LCD_ClearLine(LCD_LINE_6);
00189       LCD_ClearLine(LCD_LINE_7);
00190       LCD_ClearLine(LCD_LINE_8);
00191       /* Display the TimeStamp */
00192       RTC_TimeStampShow();
00193       Button_State = 0;
00194     }
00195     else if (Button_State == SEL_ON)
00196     {
00197       /* Turn LED1 ON and LED2 OFF */
00198       STM_EVAL_LEDOn(LED1);
00199       STM_EVAL_LEDOff(LED2);
00200       
00201       /* Clear The TSF Flag (Clear TimeStamp Registers) */
00202       RTC_ClearFlag(RTC_FLAG_TSF);
00203       LCD_SetFont(&Font12x12);
00204       LCD_ClearLine(LCD_LINE_5);
00205       LCD_SetFont(&Font12x12);
00206       LCD_DisplayStringLine(LCD_LINE_5,(uint8_t *) " TimeStamp Event Cleared        " );
00207       Button_State = 0;
00208     }
00209   }
00210 }
00211 
00212 /**
00213   * @brief  Configure the RTC peripheral by selecting the clock source.
00214   * @param  None
00215   * @retval None
00216   */
00217 static void RTC_Config(void)
00218 {
00219   RTC_InitTypeDef RTC_InitStructure;
00220   
00221   /* Enable the PWR clock */
00222   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00223 
00224   /* Allow access to RTC */
00225   PWR_BackupAccessCmd(ENABLE);
00226   
00227     RCC_BackupResetCmd(ENABLE);
00228   RCC_BackupResetCmd(DISABLE);
00229   
00230 #if defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
00231   /* Enable the LSE OSC */
00232   RCC_LSEConfig(RCC_LSE_ON);
00233 
00234   /* Wait till LSE is ready */  
00235   while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00236   {
00237   }
00238 
00239   /* Select the RTC Clock Source */
00240   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00241   /* ck_spre(1Hz) = RTCCLK(LSE) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/
00242   SynchPrediv = 0xFF;
00243   AsynchPrediv = 0x7F;
00244   
00245 #elif defined (RTC_CLOCK_SOURCE_LSI)  /* LSI used as RTC source clock*/
00246  /* The RTC Clock may varies due to LSI frequency dispersion. */
00247   /* Enable the LSI OSC */ 
00248   RCC_LSICmd(ENABLE);
00249 
00250   /* Wait till LSI is ready */  
00251   while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00252   {
00253   }
00254 
00255   /* Select the RTC Clock Source */
00256   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00257   
00258   /* ck_spre(1Hz) = RTCCLK(LSI) /(AsynchPrediv + 1)*(SynchPrediv + 1)*/
00259   SynchPrediv = 0xFF;
00260   AsynchPrediv = 0x7F;
00261   
00262 #else
00263   #error Please select the RTC Clock source inside the main.c file
00264 #endif /* RTC_CLOCK_SOURCE_LSE */
00265 
00266    /* Configure the RTC data register and RTC prescaler */
00267   RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
00268   RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
00269   RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
00270   
00271   /* Check on RTC init */
00272   if (RTC_Init(&RTC_InitStructure) == ERROR)
00273   {
00274     /* Set the Back Color */
00275     LCD_SetBackColor(LCD_COLOR_WHITE);
00276     
00277     /* Set the Text Color */
00278     LCD_SetTextColor(LCD_COLOR_RED);
00279     LCD_SetFont(&Font12x12);
00280     LCD_DisplayStringLine(LCD_LINE_3,(uint8_t *) " RTC Prescaler Conf failed       " );
00281     LCD_SetFont(&Font16x24);
00282   }
00283   
00284   /* Enable the RTC Clock */
00285   RCC_RTCCLKCmd(ENABLE);
00286   
00287   /* Wait for RTC APB registers synchronisation */
00288   RTC_WaitForSynchro();
00289   
00290   /* Enable The TimeStamp */
00291   RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE);    
00292 }
00293 
00294 /**
00295   * @brief  Returns the time entered by user, using Hyperterminal.
00296   * @param  None
00297   * @retval None
00298   */
00299 static void RTC_TimeRegulate(void)
00300 {
00301   RTC_TimeTypeDef RTC_TimeStructure;
00302   RTC_DateTypeDef RTC_DateStructure;
00303   
00304   /* Set Time hh:mm:ss */
00305   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
00306   RTC_TimeStructure.RTC_Hours   = 0x08;  
00307   RTC_TimeStructure.RTC_Minutes = 0x10;
00308   RTC_TimeStructure.RTC_Seconds = 0x00;
00309   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure);
00310 
00311   /* Set Date Week/Date/Month/Year */
00312   RTC_DateStructure.RTC_WeekDay = 01;
00313   RTC_DateStructure.RTC_Date = 0x31;
00314   RTC_DateStructure.RTC_Month = 0x12;
00315   RTC_DateStructure.RTC_Year = 0x12;
00316   RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure);
00317   
00318   /* Write BkUp DR0 */
00319   RTC_WriteBackupRegister(RTC_BKP_DR0, 0x32F2);
00320 }
00321 
00322 /**
00323   * @brief  Display the current time on the Hyperterminal.
00324   * @param  None
00325   * @retval None
00326   */
00327 static void RTC_TimeShow(void)
00328 {
00329   RTC_TimeTypeDef RTC_TimeStructure;
00330   /* Get the current Time and Date */
00331   RTC_GetTime(RTC_Format_BCD, &RTC_TimeStructure);
00332    
00333   /* Set the Back Color */
00334   LCD_SetBackColor(LCD_COLOR_WHITE);
00335   
00336   /* Set the Text Color */
00337   LCD_SetTextColor(LCD_COLOR_BLUE);
00338   LCD_SetFont(&Font16x24);
00339   LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"Current Time Display");
00340   /* Display the curent time and the sub second on the LCD */
00341   RTC_Time_display(LCD_LINE_5, Black , RTC_Get_Time(Secondfraction , &RTC_TimeStructure));
00342 }
00343 
00344 /**
00345   * @brief  Display the current date on the Hyperterminal.
00346   * @param  None
00347   * @retval None
00348   */
00349 static void RTC_DateShow(void)
00350 {
00351   RTC_DateTypeDef RTC_DateStructure;
00352   /* Get the current Date */
00353   RTC_GetDate(RTC_Format_BCD, &RTC_DateStructure);
00354   /* Set the Back Color */
00355   LCD_SetBackColor(LCD_COLOR_WHITE);
00356   
00357   /* Set the Text Color */
00358   LCD_SetTextColor(LCD_COLOR_BLUE);
00359   LCD_SetFont(&Font16x24);
00360   LCD_DisplayStringLine(LCD_LINE_6, (uint8_t *)"Current Date Display");
00361   /* Set the Text Color */
00362   LCD_SetTextColor(LCD_COLOR_BLACK);
00363   RTC_Time_display( LCD_LINE_7, Black,  RTC_Get_Date( &RTC_DateStructure));
00364 }
00365 
00366 /**
00367   * @brief  Display the current TimeStamp (time and date) on the Hyperterminal.
00368   * @param  None
00369   * @retval None
00370   */
00371 static void RTC_TimeStampShow(void)
00372 {
00373   RTC_TimeTypeDef  RTC_TimeStampStructure;
00374   RTC_DateTypeDef  RTC_TimeStampDateStructure;
00375   /* Get the current TimeStamp */
00376   RTC_GetTimeStamp(RTC_Format_BCD, &RTC_TimeStampStructure, &RTC_TimeStampDateStructure);
00377 
00378   /* Set the Back Color */
00379   LCD_SetBackColor(LCD_COLOR_WHITE);
00380   
00381   /* Set the Text Color */
00382   LCD_SetTextColor(LCD_COLOR_BLUE);
00383   LCD_SetFont(&Font16x24);
00384   LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"TimeStamp Display");
00385   /* Display the curent time and the sub second on the LCD */
00386   LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *) "Time");
00387   RTC_Time_display(LCD_LINE_5, Black , RTC_Get_Time(Secondfraction , &RTC_TimeStampStructure));
00388   /* Set the Text Color */
00389   LCD_SetTextColor(LCD_COLOR_BLUE);
00390   LCD_DisplayStringLine(LCD_LINE_6, (uint8_t *) "Date");
00391   RTC_Time_display(LCD_LINE_6, Black,  RTC_Get_Date( &RTC_TimeStampDateStructure));
00392 }
00393 
00394 /**
00395   * @brief  Displays the current Time on the LCD.
00396   * @param  Line:  the Line where to display the Current time .
00397   *           This parameter can be one of the following values:
00398   *             @arg Linex: where x can be 0..9
00399   * @param  Color_x: specifies the Background Color.
00400   * @param  table: the Current time and sub second.
00401   * @retval None
00402   */
00403 static void RTC_Time_display(uint8_t Line,__IO uint16_t Color_x, Table_TypeDef table )
00404 {   
00405   uint8_t i = 0;
00406 
00407   /* Initialize table */
00408   LCD_SetTextColor(Color_x);
00409   
00410   for (i=0;i<8;i++)
00411   {
00412     /* Display char on the LCD */
00413     LCD_DisplayChar(Line, (250 - (20 *i)), table.tab[i]);
00414   }  
00415 }
00416 
00417 /**
00418   * @brief  Returns the current time and sub second.
00419   * @param  Secondfraction: the sub second fraction.
00420   * @param  RTC_TimeStructure : pointer to a RTC_TimeTypeDef structure that 
00421   *         contains the current time values. 
00422   * @retval table : return current time and sub second in a table form
00423   */
00424 static Table_TypeDef RTC_Get_Time(uint32_t Secondfraction , RTC_TimeTypeDef* RTC_TimeStructure )
00425 {
00426   Table_TypeDef table2;
00427 
00428   /* Fill the table2 fields with the current Time*/
00429   table2.tab[0] = (((uint8_t)(RTC_TimeStructure->RTC_Hours & 0xF0) >> 0x04) + 0x30);
00430   table2.tab[1]  = (((uint8_t)(RTC_TimeStructure->RTC_Hours & 0x0F))+ 0x30);
00431   table2.tab[2]  = 0x3A;
00432   
00433   table2.tab[3]  = (((uint8_t)(RTC_TimeStructure->RTC_Minutes & 0xF0) >> 0x04) + 0x30);
00434   table2.tab[4]  =(((uint8_t)(RTC_TimeStructure->RTC_Minutes & 0x0F))+ (uint8_t)0x30);
00435   table2.tab[5]  = 0x3A;
00436 
00437   table2.tab[6]   = (((uint8_t)(RTC_TimeStructure->RTC_Seconds & 0xF0) >> 0x04)+ 0x30);
00438   table2.tab[7]   = (((uint8_t)(RTC_TimeStructure->RTC_Seconds & 0x0F)) + 0x30);
00439   table2.tab[8]   = 0xA0;
00440   
00441   /* return table2 */
00442   return table2;
00443 }
00444 
00445 /**
00446   * @brief  Returns the current time and sub second.
00447   * @param  Secondfraction: the sub second fraction.
00448   * @param  RTC_TimeStructure : pointer to a RTC_TimeTypeDef structure that 
00449   *         contains the current time values. 
00450   * @retval table : return current time and sub second in a table form
00451   */
00452 static Table_TypeDef RTC_Get_Date(RTC_DateTypeDef* RTC_DateStructure )
00453 {
00454   Table_TypeDef table2;
00455 
00456   /* Fill the table2 fields with the current Time*/
00457   table2.tab[0] = (((uint8_t)(RTC_DateStructure->RTC_Date & 0xF0) >> 0x04) + 0x30);
00458   table2.tab[1]  = (((uint8_t)(RTC_DateStructure->RTC_Date & 0x0F))+ 0x30);
00459   table2.tab[2]  = 0x2F;
00460   
00461   table2.tab[3]  = (((uint8_t)(RTC_DateStructure->RTC_Month & 0xF0) >> 0x04) + 0x30);
00462   table2.tab[4]  =(((uint8_t)(RTC_DateStructure->RTC_Month & 0x0F))+ (uint8_t)0x30);
00463   table2.tab[5]  = 0x2F;
00464 
00465   table2.tab[6]   = (((uint8_t)(RTC_DateStructure->RTC_Year & 0xF0) >> 0x04)+ 0x30);
00466   table2.tab[7]   = (((uint8_t)(RTC_DateStructure->RTC_Year & 0x0F)) + 0x30);
00467   table2.tab[8]   = 0xA0;
00468  
00469   /* return table2 */
00470   return table2;
00471 }
00472 
00473 #ifdef  USE_FULL_ASSERT
00474 
00475 /**
00476   * @brief  Reports the name of the source file and the source line number
00477   *         where the assert_param error has occurred.
00478   * @param  file: pointer to the source file name
00479   * @param  line: assert_param error line source number
00480   * @retval None
00481   */
00482 void assert_failed(uint8_t* file, uint32_t line)
00483 { 
00484   /* User can add his own implementation to report the file name and line number,
00485      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00486 
00487   /* Infinite loop */
00488   while (1)
00489   {
00490   }
00491 }
00492 #endif
00493 
00494 /**
00495   * @}
00496   */ 
00497 
00498 /**
00499   * @}
00500   */ 
00501 
00502 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32