STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/RTC/RTC_Calendar/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    RTC/RTC_Calendar/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_Calendar
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Uncomment the corresponding line to select the RTC Clock source */
00042 #define RTC_CLOCK_SOURCE_LSE   /* LSE used as RTC source clock */
00043 //#define RTC_CLOCK_SOURCE_LSI  // LSI used as RTC source clock. The RTC Clock
00044                                 // may varies due to LSI frequency dispersion
00045 
00046 #define BKP_VALUE    0x32F0 
00047 /* Private macro -------------------------------------------------------------*/
00048 /* Private variables ---------------------------------------------------------*/
00049 __IO uint32_t AsynchPrediv = 0, SynchPrediv = 0;
00050 
00051 /* Private function prototypes -----------------------------------------------*/
00052 static void RTC_Config(void);
00053 /* Private functions ---------------------------------------------------------*/
00054 
00055 /**
00056   * @brief  Main program.
00057   * @param  None
00058   * @retval None
00059   */
00060 int main(void)
00061 {  
00062   /*!< At this stage the microcontroller clock setting is already configured, 
00063        this is done through SystemInit() function which is called from startup
00064        file (startup_stm32f0xx.s) before to branch to application main.
00065        To reconfigure the default setting of SystemInit() function, refer to
00066        system_stm32f0xx.c file
00067      */ 
00068 
00069   RTC_InitTypeDef RTC_InitStructure;
00070   NVIC_InitTypeDef  NVIC_InitStructure;
00071   EXTI_InitTypeDef  EXTI_InitStructure;
00072   USART_InitTypeDef USART_InitStructure;
00073      
00074   /* USARTx configured as follow:
00075         - BaudRate = 115200 baud  
00076         - Word Length = 8 Bits
00077         - One Stop Bit
00078         - No parity
00079         - Hardware flow control disabled (RTS and CTS signals)
00080         - Receive and transmit enabled
00081   */
00082   USART_InitStructure.USART_BaudRate = 115200;
00083   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
00084   USART_InitStructure.USART_StopBits = USART_StopBits_1;
00085   USART_InitStructure.USART_Parity = USART_Parity_No;
00086   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
00087   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
00088 
00089   STM_EVAL_COMInit(COM1, &USART_InitStructure);
00090  
00091   /* Output a message on Hyperterminal using printf function */
00092   printf("\n\r  *********************** RTC Hardware Calendar Example ***********************\n\r");
00093   
00094   if (RTC_ReadBackupRegister(RTC_BKP_DR0) != BKP_VALUE)
00095   {  
00096     /* RTC configuration  */
00097     RTC_Config();
00098 
00099     /* Configure the RTC data register and RTC prescaler */
00100     RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
00101     RTC_InitStructure.RTC_SynchPrediv = SynchPrediv;
00102     RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
00103    
00104     /* Check on RTC init */
00105     if (RTC_Init(&RTC_InitStructure) == ERROR)
00106     {
00107       printf("\n\r        /!\\***** RTC Prescaler Config failed ********/!\\ \n\r");
00108     }
00109 
00110     /* Configure the time register */
00111     RTC_TimeRegulate(); 
00112   }
00113   else
00114   {
00115     /* Check if the Power On Reset flag is set */
00116     if (RCC_GetFlagStatus(RCC_FLAG_PORRST) != RESET)
00117     {
00118       printf("\r\n Power On Reset occurred....\n\r");
00119     }
00120     /* Check if the Pin Reset flag is set */
00121     else if (RCC_GetFlagStatus(RCC_FLAG_PINRST) != RESET)
00122     {
00123       printf("\r\n External Reset occurred....\n\r");
00124     }
00125 
00126     printf("\n\r No need to configure RTC....\n\r");
00127     
00128     /* Enable the PWR clock */
00129     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00130 
00131     /* Allow access to RTC */
00132     PWR_BackupAccessCmd(ENABLE);
00133 
00134  #ifdef RTC_CLOCK_SOURCE_LSI
00135     /* Enable the LSI OSC */ 
00136     RCC_LSICmd(ENABLE);
00137  #endif /* RTC_CLOCK_SOURCE_LSI */
00138 
00139     /* Wait for RTC APB registers synchronisation */
00140     RTC_WaitForSynchro();
00141 
00142     /* Clear the RTC Alarm Flag */
00143     RTC_ClearFlag(RTC_FLAG_ALRAF);
00144 
00145     /* Clear the EXTI Line 17 Pending bit (Connected internally to RTC Alarm) */
00146     EXTI_ClearITPendingBit(EXTI_Line17);
00147 
00148     /* Display the RTC Time and Alarm */
00149     RTC_TimeShow();
00150     RTC_AlarmShow();
00151   }
00152    
00153   /* Configure the external interrupt "KEY", "SEL" and "UP" buttons */
00154   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI); 
00155   STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_EXTI);
00156   STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_EXTI);
00157 
00158   /* Configure LEDs */
00159   STM_EVAL_LEDInit(LED1);
00160   STM_EVAL_LEDInit(LED2);
00161 
00162   /* Turn LED2 ON */
00163   STM_EVAL_LEDOn(LED2);
00164 
00165   /* RTC Alarm A Interrupt Configuration */
00166   /* EXTI configuration *********************************************************/
00167   EXTI_ClearITPendingBit(EXTI_Line17);
00168   EXTI_InitStructure.EXTI_Line = EXTI_Line17;
00169   EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
00170   EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
00171   EXTI_InitStructure.EXTI_LineCmd = ENABLE;
00172   EXTI_Init(&EXTI_InitStructure);
00173   
00174   /* Enable the RTC Alarm Interrupt */
00175   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
00176   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00177   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00178   NVIC_Init(&NVIC_InitStructure);
00179 
00180   /* Infinite loop */
00181   while (1)
00182   {
00183   }
00184 }
00185 
00186 /**
00187   * @brief  Configure the RTC peripheral by selecting the clock source.
00188   * @param  None
00189   * @retval None
00190   */
00191 static void RTC_Config(void)
00192 {
00193   /* Enable the PWR clock */
00194   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
00195 
00196   /* Allow access to RTC */
00197   PWR_BackupAccessCmd(ENABLE);
00198     
00199 #if defined (RTC_CLOCK_SOURCE_LSI)  /* LSI used as RTC source clock*/
00200 /* The RTC Clock may varies due to LSI frequency dispersion. */   
00201   /* Enable the LSI OSC */ 
00202   RCC_LSICmd(ENABLE);
00203 
00204   /* Wait till LSI is ready */  
00205   while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
00206   {
00207   }
00208 
00209   /* Select the RTC Clock Source */
00210   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
00211   
00212   SynchPrediv = 0x18F;
00213   AsynchPrediv = 0x63;
00214 
00215 #elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */
00216   /* Enable the LSE OSC */
00217   RCC_LSEConfig(RCC_LSE_ON);
00218 
00219   /* Wait till LSE is ready */  
00220   while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
00221   {
00222   }
00223 
00224   /* Select the RTC Clock Source */
00225   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
00226   
00227   SynchPrediv = 0xFF;
00228   AsynchPrediv = 0x7F;
00229 
00230 #else
00231   #error Please select the RTC Clock source inside the main.c file
00232 #endif /* RTC_CLOCK_SOURCE_LSI */
00233   
00234   /* Enable the RTC Clock */
00235   RCC_RTCCLKCmd(ENABLE);
00236 
00237   /* Wait for RTC APB registers synchronisation */
00238   RTC_WaitForSynchro();
00239 }
00240 
00241 /**
00242   * @brief  Returns the time entered by user, using Hyperterminal.
00243   * @param  None
00244   * @retval None
00245   */
00246 void RTC_TimeRegulate(void)
00247 {
00248   RTC_TimeTypeDef RTC_TimeStructure;
00249   RTC_AlarmTypeDef  RTC_AlarmStructure;
00250   uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
00251 
00252   printf("\n\r==============Time Settings=====================================\n\r");
00253   RTC_TimeStructure.RTC_H12     = RTC_H12_AM;
00254   printf("  Please Set Hours:\n\r");
00255   while (tmp_hh == 0xFF)
00256   {
00257     tmp_hh = USART_Scanf(23);
00258     RTC_TimeStructure.RTC_Hours = tmp_hh;
00259   }
00260   printf("  %0.2d\n\r", tmp_hh);
00261   
00262   printf("  Please Set Minutes:\n\r");
00263   while (tmp_mm == 0xFF)
00264   {
00265     tmp_mm = USART_Scanf(59);
00266     RTC_TimeStructure.RTC_Minutes = tmp_mm;
00267   }
00268   printf("  %0.2d\n\r", tmp_mm);
00269   
00270   printf("  Please Set Seconds:\n\r");
00271   while (tmp_ss == 0xFF)
00272   {
00273     tmp_ss = USART_Scanf(59);
00274     RTC_TimeStructure.RTC_Seconds = tmp_ss;
00275   }
00276   printf("  %0.2d\n\r", tmp_ss);
00277 
00278   /* Configure the RTC time register */
00279   if(RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure) == ERROR)
00280   {
00281     printf("\n\r>> !! RTC Set Time failed. !! <<\n\r");
00282   } 
00283   else
00284   {
00285     printf("\n\r>> !! RTC Set Time success. !! <<\n\r");
00286     RTC_TimeShow();
00287     /* Indicator for the RTC configuration */
00288     RTC_WriteBackupRegister(RTC_BKP_DR0, BKP_VALUE);
00289   }
00290 
00291   tmp_hh = 0xFF;
00292   tmp_mm = 0xFF;
00293   tmp_ss = 0xFF;
00294 
00295   /* Disable the Alarm A */
00296   RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
00297 
00298   printf("\n\r==============Alarm A Settings=====================================\n\r");
00299   RTC_AlarmStructure.RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
00300   printf("  Please Set Alarm Hours:\n\r");
00301   while (tmp_hh == 0xFF)
00302   {
00303     tmp_hh = USART_Scanf(23);
00304     RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours = tmp_hh;
00305   }
00306   printf("  %0.2d\n\r", tmp_hh);
00307   
00308   printf("  Please Set Alarm Minutes:\n\r");
00309   while (tmp_mm == 0xFF)
00310   {
00311     tmp_mm = USART_Scanf(59);
00312     RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = tmp_mm;
00313   }
00314   printf("  %0.2d\n\r", tmp_mm);
00315   
00316   printf("  Please Set Alarm Seconds:\n\r");
00317   while (tmp_ss == 0xFF)
00318   {
00319     tmp_ss = USART_Scanf(59);
00320     RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = tmp_ss;
00321   }
00322   printf("  %0.2d", tmp_ss);
00323 
00324   /* Set the Alarm A */
00325   RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
00326   RTC_AlarmStructure.RTC_AlarmDateWeekDay = RTC_Weekday_Monday;    
00327   RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay;
00328 
00329   /* Configure the RTC Alarm A register */
00330   RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
00331   printf("\n\r>> !! RTC Set Alarm success. !! <<\n\r");
00332   RTC_AlarmShow();
00333 
00334   /* Enable the RTC Alarm A Interrupt */
00335   RTC_ITConfig(RTC_IT_ALRA, ENABLE);
00336    
00337   /* Enable the alarm  A */
00338   RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
00339 }
00340 
00341 /**
00342   * @brief  Display the current time on the Hyperterminal.
00343   * @param  None
00344   * @retval None
00345   */
00346 void RTC_TimeShow(void)
00347 {
00348   RTC_TimeTypeDef RTC_TimeStructure;
00349   /* Get the current Time */
00350   RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
00351   printf("\n\r  The current time is :  %0.2d:%0.2d:%0.2d \n\r", RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
00352 }
00353 
00354 /**
00355   * @brief  Display the current time on the Hyperterminal.
00356   * @param  None
00357   * @retval None
00358   */
00359 void RTC_AlarmShow(void)
00360 {
00361   RTC_AlarmTypeDef  RTC_AlarmStructure;
00362   /* Get the current Alarm */
00363   RTC_GetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);
00364   printf("\n\r  The current alarm is :  %0.2d:%0.2d:%0.2d \n\r", RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours, RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes, RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds);
00365 }
00366 
00367 
00368 /**
00369   * @brief  Gets numeric values from the hyperterminal.
00370   * @param  None
00371   * @retval None
00372   */
00373 uint8_t USART_Scanf(uint32_t value)
00374 {
00375   uint32_t index = 0;
00376   uint32_t tmp[2] = {0, 0};
00377 
00378   while (index < 2)
00379   {
00380     /* Loop until RXNE = 1 */
00381     while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)
00382     {}
00383     tmp[index++] = (USART_ReceiveData(EVAL_COM1));
00384     if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
00385     {
00386       printf("\n\r Please enter valid number between 0 and 9 \n\r");
00387       index--;
00388     }
00389   }
00390   /* Calculate the Corresponding value */
00391   index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
00392   /* Checks */
00393   if (index > value)
00394   {
00395     printf("\n\r Please enter valid number between 0 and %d \n\r", value);
00396     return 0xFF;
00397   }
00398   return index;
00399 }
00400 
00401 /**
00402   * @brief  Retargets the C library printf function to the USART.
00403   * @param  None
00404   * @retval None
00405   */
00406 PUTCHAR_PROTOTYPE
00407 {
00408   /* Place your implementation of fputc here */
00409   /* e.g. write a character to the USART */
00410   USART_SendData(EVAL_COM1, (uint8_t) ch);
00411 
00412   /* Loop until the end of transmission */
00413   while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
00414   {}
00415 
00416   return ch;
00417 }
00418 
00419 #ifdef  USE_FULL_ASSERT
00420 
00421 /**
00422   * @brief  Reports the name of the source file and the source line number
00423   *         where the assert_param error has occurred.
00424   * @param  file: pointer to the source file name
00425   * @param  line: assert_param error line source number
00426   * @retval None
00427   */
00428 void assert_failed(uint8_t* file, uint32_t line)
00429 { 
00430   /* User can add his own implementation to report the file name and line number,
00431      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00432 
00433   /* Infinite loop */
00434   while (1)
00435   {
00436   }
00437 }
00438 #endif
00439 
00440 /**
00441   * @}
00442   */
00443 
00444 /**
00445   * @}
00446   */
00447 
00448 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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