STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/WWDG/WWDG_Example/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    WWDG/WWDG_Example/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 WWDG_Example
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 __IO uint32_t TimingDelay = 0;
00044 
00045 /* Private function prototypes -----------------------------------------------*/
00046 void Delay(__IO uint32_t nTime);
00047 /* Private functions ---------------------------------------------------------*/
00048 static void WWDG_Config(void);
00049 
00050 /**
00051   * @brief  Main program.
00052   * @param  None
00053   * @retval None
00054   */
00055 int main(void)
00056 {
00057   /*!< At this stage the microcontroller clock setting is already configured, 
00058        this is done through SystemInit() function which is called from startup
00059        file (startup_stm32f0xx.s) before to branch to application main.
00060        To reconfigure the default setting of SystemInit() function, refer to
00061        system_stm32f0xx.c file
00062      */ 
00063   /* Initialize LEDs and Tamper Button mounted on EVAL board */       
00064   STM_EVAL_LEDInit(LED1);
00065   STM_EVAL_LEDInit(LED2);
00066   STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_EXTI);
00067 
00068   /* Check if the system has resumed from WWDG reset */
00069   if (RCC_GetFlagStatus(RCC_FLAG_WWDGRST) != RESET)
00070   { 
00071     /* WWDGRST flag set */
00072     /* Turn on LED1 */
00073     STM_EVAL_LEDOn(LED1);
00074 
00075     /* Clear reset flags */
00076     RCC_ClearFlag();
00077   }
00078   else
00079   {
00080     /* WWDGRST flag is not set */
00081     /* Turn off LED1 */
00082     STM_EVAL_LEDOff(LED1);
00083    }
00084   
00085   /* Setup SysTick Timer for 1 msec interrupts  */
00086   if (SysTick_Config(SystemCoreClock / 1000))
00087   { 
00088     /* Capture error */ 
00089     while (1)
00090     {}
00091   }
00092   
00093   /* Configure WWDG */
00094   WWDG_Config();
00095    
00096   while (1)
00097   {
00098     /* Toggle LED2 */
00099     STM_EVAL_LEDToggle(LED2);
00100 
00101     /* Insert 33 ms delay */
00102     Delay(33);
00103 
00104     /* Update WWDG counter */
00105     WWDG_SetCounter(127);
00106   }
00107 }
00108 
00109 /**
00110   * @brief  WWDG configuration
00111   * @param  None
00112   * @retval None
00113   */
00114 static void WWDG_Config(void)
00115 {
00116   /* Enable WWDG clock */
00117   RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
00118 
00119   /* WWDG clock counter = (PCLK1 (48MHz)/4096)/8 = 1464Hz (~683 us)  */
00120   WWDG_SetPrescaler(WWDG_Prescaler_8);
00121 
00122   /* Set Window value to 80; WWDG counter should be refreshed only when the counter
00123     is below 80 (and greater than 64) otherwise a reset will be generated */
00124   WWDG_SetWindowValue(80);
00125 
00126   /* Enable WWDG and set counter value to 127, WWDG timeout = ~683 us * 64 = 43.7 ms 
00127      In this case the refresh window is: ~683 * (127-80)= 32.1ms < refresh window < ~683 * 64 = 43.7ms
00128      */
00129   WWDG_Enable(127);
00130   
00131 }
00132 
00133 /**
00134   * @brief  Inserts a delay time.
00135   * @param  nTime: specifies the delay time length, in 10 ms.
00136   * @retval None
00137   */
00138 void Delay(__IO uint32_t nTime)
00139 {
00140   TimingDelay = nTime;
00141 
00142   while(TimingDelay != 0);
00143 }
00144 
00145 /**
00146   * @brief  Decrements the TimingDelay variable.
00147   * @param  None
00148   * @retval None
00149   */
00150 void TimingDelay_Decrement(void)
00151 {
00152   if (TimingDelay != 0x00)
00153   { 
00154     TimingDelay--;
00155   }
00156 }
00157 
00158 #ifdef  USE_FULL_ASSERT
00159 
00160 /**
00161   * @brief  Reports the name of the source file and the source line number
00162   *         where the assert_param error has occurred.
00163   * @param  file: pointer to the source file name
00164   * @param  line: assert_param error line source number
00165   * @retval None
00166   */
00167 void assert_failed(uint8_t* file, uint32_t line)
00168 { 
00169   /* User can add his own implementation to report the file name and line number,
00170      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00171 
00172   /* Infinite loop */
00173   while (1)
00174   {
00175   }
00176 }
00177 #endif
00178 
00179 /**
00180   * @}
00181   */
00182 
00183 /**
00184   * @}
00185   */
00186 
00187 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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