STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/RTC/RTC_StopWatch/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/RTC_StopWatch/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_StopWatch
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 #define MESSAGE1   " **** STOPWATCH ****" 
00042 #define MESSAGE2   " LEFT         |      RIGHT  " 
00043 #define MESSAGE3   " START        |       GET   " 
00044 #define MESSAGE4   "  Press and hold TAMPER   " 
00045 #define MESSAGE5   " to reset Backup registers" 
00046 /* Private macro -------------------------------------------------------------*/
00047 /* Private variables ---------------------------------------------------------*/
00048 __IO uint32_t SecondFraction = 0;
00049 uint32_t BackupIndex = 0;
00050 __IO uint32_t SubSecFrac = 0;
00051 __IO uint8_t StartEvent = 0; 
00052 __IO uint8_t Button_State =0;
00053 __IO uint8_t Button_RTC =0;
00054 uint8_t i =0;
00055 extern __IO uint32_t CurrentTimeSec;
00056 /* Define the backup register */
00057 uint32_t BKPDataReg[5] = { RTC_BKP_DR0, RTC_BKP_DR1, RTC_BKP_DR2, RTC_BKP_DR3, RTC_BKP_DR4
00058                           };
00059 __IO uint32_t CurrentTimeSec = 0;
00060 /* Private function prototypes -----------------------------------------------*/
00061 /* Private functions ---------------------------------------------------------*/
00062 static void RTC_Config(void);
00063 static void RTC_TamperConfig(void);
00064 static void Display_Init(void);
00065 
00066 /**
00067   * @brief  Main program.
00068   * @param  None
00069   * @retval None
00070   */
00071 int main(void)
00072 {
00073   /*!< At this stage the microcontroller clock setting is already configured, 
00074        this is done through SystemInit() function which is called from startup
00075        file (startup_stm32f0xx.s) before to branch to application main.
00076        To reconfigure the default setting of SystemInit() function, refer to
00077        system_stm32f0xx.c file
00078      */ 
00079 
00080   RTC_TimeTypeDef  RTC_TimeStruct;   
00081   RTC_TimeTypeDef  RTC_TimeStructureInit;
00082   RTC_TimeTypeDef  RTC_StampTimeStruct;
00083   
00084   /* Configure the external interrupt "RIGHT" and "LEFT" buttons */
00085   STM_EVAL_PBInit(BUTTON_RIGHT,BUTTON_MODE_EXTI);
00086   STM_EVAL_PBInit(BUTTON_LEFT,BUTTON_MODE_EXTI);
00087   
00088   /* Configure the RTC peripheral by selecting the clock source.*/
00089   RTC_Config();
00090   
00091   /* LCD Display init  */
00092   Display_Init();
00093   
00094   /* Configure the RTC tamper register : To Clear all the Backup data register */
00095   RTC_TamperConfig();
00096 
00097   /* Initialize time Stucture */
00098   RTC_TimeStructInit(&RTC_TimeStruct); 
00099 
00100   /* Infinite loop */
00101   while (1)
00102   {
00103     /* Set the LCD Back Color and text size */
00104     LCD_SetFont(&Font16x24);
00105     LCD_SetBackColor(White);
00106     
00107     /* Check on the event 'start' */
00108     if(StartEvent != 0x0)
00109     {  
00110       /* Get the RTC sub second fraction */
00111       SecondFraction = (((256 - (uint32_t)RTC_GetSubSecond()) * 1000) / 256);
00112       
00113       /* Get the Curent time */
00114       RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
00115       
00116       /* Refresh : Display the curent time and the sub second on the LCD  */
00117       RTC_Time_Display(37, Black , RTC_Get_Time(SecondFraction , &RTC_TimeStruct));
00118     }
00119     else
00120     {
00121       /* Re-initialize the Display time on the LCD */
00122       RTC_Time_InitDisplay();
00123     }  
00124     
00125     /* Left button is pressed */
00126       if (Button_State == LEFT_ON)
00127       {
00128         /* Enable Tamper interrupt */
00129       RTC_ITConfig(RTC_IT_TAMP, ENABLE);
00130       /* Enabale the tamper 1 */
00131       RTC_TamperCmd(RTC_Tamper_1 , ENABLE);
00132       
00133       /* Set the LCD Back Color */
00134       LCD_SetBackColor(White);
00135       
00136       /* Clear the LCD line 5 */
00137       LCD_ClearLine(Line5);
00138       
00139       /* Get the current time */
00140       RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructureInit);
00141       CurrentTimeSec = (RTC_TimeStructureInit.RTC_Hours * 3600) + (RTC_TimeStructureInit.RTC_Minutes * 60) +
00142         RTC_TimeStructureInit.RTC_Seconds;     
00143       
00144       /* start count */
00145       StartEvent = 0x1;
00146       /* Reinitialize Button_State variable */
00147       Button_State = 0; 
00148       }
00149     /* Right button is pressed */
00150       else if (Button_State == RIGHT_ON)
00151       {
00152         uint16_t Colorx;
00153       
00154       if (BackupIndex < 5)
00155       {
00156         if((uint8_t)(BackupIndex% 2) != 0x0)
00157         { 
00158           /* Set LCD backcolor*/
00159           LCD_SetBackColor(Blue2);
00160           Colorx = White;
00161         }
00162         else
00163         {
00164           /* Set LCD backcolor*/
00165           LCD_SetBackColor(Cyan);
00166           Colorx = Black;
00167         }
00168         SubSecFrac = 0;
00169         /* Get the Current sub second and time */
00170         SubSecFrac = (((256 - (uint32_t)RTC_GetSubSecond()) * 1000) / 256);
00171         
00172         RTC_GetTime(RTC_Format_BIN, &RTC_StampTimeStruct);
00173         
00174         LCD_SetFont(&Font16x24);
00175         /* Display result on the LCD */
00176         RTC_Time_Display( LINE(3 + BackupIndex), Colorx, RTC_Get_Time(SubSecFrac , &RTC_StampTimeStruct) ); 
00177         
00178         if (BackupIndex < 2)
00179         {
00180           /* Save time register  to Backup register ( the first 5 register is reserved for time) */
00181           RTC_WriteBackupRegister(BKPDataReg[BackupIndex],(uint32_t)RTC->TR);
00182         
00183           /* Save sub second time stamp register ( the latest 6 register is reserved for time) */
00184           RTC_WriteBackupRegister(BKPDataReg[BackupIndex + 2], SubSecFrac);
00185         }
00186         
00187       }
00188       else
00189       {
00190         /* the backup register is full with 10trials */
00191         /* Set the LCD Back Color */
00192         LCD_SetBackColor(White);
00193         LCD_SetFont(&Font12x12);
00194         /* Set the LCD Text Color */
00195         LCD_SetTextColor(Red); 
00196         LCD_DisplayStringLine(LINE(16), (uint8_t *)MESSAGE4);
00197         LCD_DisplayStringLine(LINE(17), (uint8_t *)MESSAGE5);
00198         
00199       }  
00200       BackupIndex++;
00201       /* Set the LCD Back Color */
00202       LCD_SetBackColor(White);  
00203       /* Reinitialize Button_State variable */
00204       Button_State = 0; 
00205       }
00206    
00207    /* TAMPER button is pressed */
00208    else if (Button_RTC == RTC_TAMP)
00209    {
00210      /* Set the LCD Back Color */
00211     LCD_SetBackColor(White);
00212     LCD_SetFont(&Font16x24);
00213     /* Clear LCD line 5 to 9 */
00214     for (i=0; i < 5; i++)
00215     {  
00216       /* Clear all the LCD lignes from 3 to 7 */
00217       LCD_ClearLine(LINE(3+i));
00218     }
00219     
00220     /* reset Counter */
00221     BackupIndex = 0 ;
00222     
00223     /* Enetr to idle */
00224     StartEvent =0x0;
00225     
00226     LCD_SetFont(&Font12x12);
00227     RTC_Time_InitDisplay();
00228     
00229     LCD_SetFont(&Font12x12);
00230     LCD_ClearLine(LINE(16));
00231     LCD_ClearLine(LINE(17));
00232     
00233     /* Enable Tamper interrupt */
00234     RTC_ITConfig(RTC_IT_TAMP, DISABLE);
00235     /* Enabale the tamper 1 */
00236     RTC_TamperCmd(RTC_Tamper_1 , DISABLE);
00237     /* Reinitialize Button_RTC variable */
00238     Button_RTC = 0; 
00239    }
00240   }
00241 }
00242 
00243 /**
00244 * @brief  RTC Tamper Configuration..
00245 * @param  None
00246 * @retval None
00247 */
00248 static void RTC_TamperConfig(void)
00249 {
00250   EXTI_InitTypeDef EXTI_InitStructure;
00251   NVIC_InitTypeDef NVIC_InitStructure;
00252   
00253   /* EXTI configuration *********************************************************/
00254   EXTI_ClearITPendingBit(EXTI_Line19);
00255   EXTI_InitStructure.EXTI_Line = EXTI_Line19;
00256   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00257   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00258   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00259   EXTI_Init(&EXTI_InitStructure);
00260   
00261   /* Enable RTC_IRQn */
00262   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00263   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00264   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00265   NVIC_Init(&NVIC_InitStructure);
00266   
00267   /* determines the number of active pulse for the specific level */
00268   RTC_TamperFilterConfig(RTC_TamperFilter_2Sample);
00269   
00270   /* Determines the frequency at which each of the tamper inputs are sampled */
00271   RTC_TamperSamplingFreqConfig(RTC_TamperSamplingFreq_RTCCLK_Div32768);
00272   
00273   RTC_TamperPullUpCmd(DISABLE);
00274   
00275   /* Select the tamper 1 with High level */
00276   RTC_TamperTriggerConfig(RTC_Tamper_1, RTC_TamperTrigger_LowLevel );
00277   
00278   /* Clear tamper 1 flag */
00279   RTC_ClearFlag(RTC_FLAG_TAMP1F);
00280 }
00281 
00282 /**
00283 * @brief  load the current time and sub second on a table form.
00284 * @param   SecondFraction: the sub second fraction .
00285 * @param  RTC_TimeStructure : pointer to a RTC_TimeTypeDef structure that 
00286 *                             contains the current time values. 
00287 * @retval table : return current time and sub second in a table form
00288 */
00289 Table_TypeDef RTC_Get_Time(uint32_t SecondFrac , RTC_TimeTypeDef* RTC_TimeStructure )
00290 {
00291   Table_TypeDef timetab;
00292   uint32_t currenttimesec = 0;
00293   
00294   /* Get the Current time in second */ 
00295   currenttimesec = ((RTC_TimeStructure->RTC_Hours * 3600) +(RTC_TimeStructure->RTC_Minutes * 60) +
00296                     RTC_TimeStructure->RTC_Seconds) - CurrentTimeSec; 
00297   
00298   /* Fill the table2 fields with the current Time*/
00299   timetab.tab[0]  = ((uint8_t)((currenttimesec/3600)/10) + 0x30);
00300   timetab.tab[1]  = ((uint8_t)((currenttimesec/3600) %10)+ 0x30); 
00301   timetab.tab[2]  = 0x3A;
00302   timetab.tab[3]  = (((uint8_t)(((currenttimesec % 3600)/60) /10)) + 0x30);
00303   timetab.tab[4]  = (((uint8_t)(((currenttimesec % 3600)/60) %10)) + 0x30);
00304   timetab.tab[5]  = 0x3A;
00305   timetab.tab[6]   = (((uint8_t)((currenttimesec% 60) /10))+ 0x30);
00306   timetab.tab[7]   = (((uint8_t)((currenttimesec% 60) %10)) + 0x30);
00307   timetab.tab[8]   = 0x2E;
00308   timetab.tab[9]   = (uint8_t)((SecondFrac / 100) + 0x30);
00309   timetab.tab[10]  = (uint8_t)(((SecondFrac % 100 ) / 10) +0x30);
00310   timetab.tab[11]  =  (uint8_t)((SecondFrac % 10) +0x30);
00311   
00312   /* return timetab */
00313   return timetab;
00314 }
00315 
00316 /**
00317 * @brief  Configure the RTC peripheral by selecting the clock source.
00318 * @param  None
00319 * @retval None
00320 */
00321 static void RTC_Config(void)
00322 {
00323   RTC_InitTypeDef RTC_InitStructure;
00324   RTC_TimeTypeDef  RTC_TimeStruct;
00325     
00326   /* Enable the PWR clock */
00327   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00328   
00329   /* Allow access to RTC */
00330   PWR_BackupAccessCmd(ENABLE);
00331   
00332   /* Enable the LSE OSC */
00333   RCC_LSEConfig(RCC_LSE_ON);
00334   
00335   /* Wait till LSE is ready */  
00336   while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00337   {
00338   }
00339   
00340   /* Select the RTC Clock Source */
00341   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00342   
00343   RTC_DeInit();
00344   
00345   /* Configure the RTC data register and RTC prescaler */
00346   RTC_InitStructure.RTC_AsynchPrediv = 0x7F;
00347   RTC_InitStructure.RTC_SynchPrediv  = 0xFF;
00348   RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;
00349   RTC_Init(&RTC_InitStructure);
00350   
00351   /* Set the time to 00h 00mn 00s AM */
00352   RTC_TimeStruct.RTC_H12     = RTC_H12_AM;
00353   RTC_TimeStruct.RTC_Hours   = 0x00;
00354   RTC_TimeStruct.RTC_Minutes = 0x00;
00355   RTC_TimeStruct.RTC_Seconds = 0x00;  
00356   RTC_SetTime(RTC_Format_BCD, &RTC_TimeStruct);
00357   
00358   
00359   /* Enable the RTC Clock */
00360   RCC_RTCCLKCmd(ENABLE);
00361   
00362   /* Wait for RTC APB registers synchronisation */
00363   RTC_WaitForSynchro();
00364 }
00365 
00366 /**
00367   * @brief  Display Init (LCD)
00368   * @param  None
00369   * @retval None
00370   */
00371 static void Display_Init(void)
00372 {
00373   /* Initialize the LCD */
00374 #ifdef USE_STM320518_EVAL
00375     STM320518_LCD_Init();
00376 #else
00377     STM32072B_LCD_Init();
00378 #endif /* USE_STM320518_EVAL */
00379 
00380   /* Clear the LCD */
00381   LCD_Clear(LCD_COLOR_WHITE);
00382   
00383   /* Set the LCD Back Color */
00384   LCD_SetBackColor(Blue);
00385   
00386   /* Set the LCD Text Color */
00387   LCD_SetTextColor(White);
00388   
00389   /* Displays MESSAGE1 on line 1 */
00390   LCD_DisplayStringLine(LINE(0), (uint8_t *)MESSAGE1);
00391   
00392   /* Set the LCD Text Color */
00393   LCD_SetTextColor(Red);
00394   
00395   /* Set the LCD Back Color */
00396   LCD_SetBackColor(Red);
00397   LCD_DrawFullRect(31, 292,264,34);
00398   
00399   /* Set the LCD Back Color */
00400   LCD_SetBackColor(White);
00401   LCD_DrawFullRect(33, 290 ,260,30);
00402   
00403   
00404   /* Set the LCD Text, Back Colors and Text size */
00405   LCD_SetTextColor(Black); 
00406   LCD_SetBackColor(Cyan);
00407   LCD_SetFont(&Font12x12);
00408   
00409   LCD_DisplayStringLine(LINE(18), (uint8_t *)MESSAGE2);
00410   
00411   /* Set the LCD Back Color */
00412   LCD_SetBackColor(Blue);
00413   LCD_SetTextColor(White);
00414   
00415   LCD_DisplayStringLine(LINE(19), (uint8_t *)MESSAGE3);
00416   
00417   /* Set text size */
00418   LCD_SetFont(&Font16x24);
00419   
00420   /* Set the LCD Text Color */
00421   LCD_SetTextColor(Blue);
00422   
00423 }
00424 
00425 /**
00426 * @brief  Initialize the time displays  on the LCD.
00427 * @param  None.
00428 * @retval None
00429 */
00430 void RTC_Time_InitDisplay(void)
00431 {
00432   uint8_t i = 0;
00433   for (i=0;i<12;i++)
00434   {
00435     if ((i==2) || (i==5))
00436     {
00437       /* Display char on the LCD */
00438       LCD_DisplayChar(37, (280 - (20 *i)), ':');
00439     }
00440     else if ((i==8) )
00441     {
00442       /* Display char on the LCD */
00443       LCD_DisplayChar(37, (280 - (20 *i)), '.');
00444     }
00445     else
00446     {
00447       /* Display char on the LCD */
00448       LCD_DisplayChar(37, (280 - (20 *i)), '0');
00449     }
00450   }
00451 }
00452 
00453 /**
00454 * @brief  Displays the current Time on the LCD.
00455 * @param   Line:  the Line where to display the Current time .
00456 *   This parameter can be one of the following values:
00457 *     @arg Linex: where x can be 0..9
00458 * @param  Color_x : specifies the Background Color.
00459 * @param  table   : the Current time and sub second.
00460 * @retval None
00461 */
00462 void RTC_Time_Display(uint8_t Line, __IO uint16_t Colorx, Table_TypeDef timetable)
00463 {   
00464   uint8_t i = 0;
00465   
00466   /* Initialize table */
00467   LCD_SetTextColor(Colorx);
00468   
00469   for (i=0;i<12;i++)
00470   {
00471     /* Display char on the LCD */
00472     LCD_DisplayChar(Line, (280 - (20 *i)), timetable.tab[i]);
00473     
00474   }  
00475 }
00476 
00477 #ifdef  USE_FULL_ASSERT
00478 
00479 /**
00480   * @brief  Reports the name of the source file and the source line number
00481   *         where the assert_param error has occurred.
00482   * @param  file: pointer to the source file name
00483   * @param  line: assert_param error line source number
00484   * @retval None
00485   */
00486 void assert_failed(uint8_t* file, uint32_t line)
00487 { 
00488   /* User can add his own implementation to report the file name and line number,
00489      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00490 
00491   /* Infinite loop */
00492   while (1)
00493   {
00494   }
00495 }
00496 #endif
00497 
00498 /**
00499   * @}
00500   */
00501 
00502 /**
00503   * @}
00504   */
00505 
00506 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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