STM8S/A Standard Peripherals Firmware Library: main.c Source File

STM8S/A

STM8S_StdPeriph_Examples/TIM4/TIM4_TimeBase/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file     TIM4_TimeBase\main.c
00004   * @author   MCD Application Team
00005   * @version  V2.2.0
00006   * @date     30-September-2014
00007   * @brief    This file contains the main function for TIM4 Time Base Configuration example.
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 "stm8s.h"
00030 #include "stm8s_eval.h"
00031 
00032 /**
00033   * @addtogroup TIM4_TimeBase
00034   * @{
00035   */
00036 
00037 /* Private typedef -----------------------------------------------------------*/
00038 /* Private define ------------------------------------------------------------*/
00039 #define TIM4_PERIOD       124
00040 /* Private macro -------------------------------------------------------------*/
00041 /* Private variables ---------------------------------------------------------*/
00042 __IO uint32_t TimingDelay = 0;
00043 /* Private function prototypes -----------------------------------------------*/
00044 void Delay(__IO uint32_t nTime);
00045 void TimingDelay_Decrement(void);
00046 static void CLK_Config(void);
00047 static void TIM4_Config(void);
00048 static void GPIO_Config(void);
00049 /* Private functions ---------------------------------------------------------*/
00050 /* Public functions ----------------------------------------------------------*/
00051 /**
00052   * @brief  Main program.
00053   * @param  None
00054   * @retval None
00055   */
00056 void main(void)
00057 {
00058   
00059   /* Clock configuration -----------------------------------------*/
00060   CLK_Config();  
00061 
00062   /* GPIO configuration -----------------------------------------*/
00063   GPIO_Config();  
00064 
00065   /* TIM4 configuration -----------------------------------------*/
00066   TIM4_Config();    
00067   
00068   while (1)
00069   {
00070     /* Toggle LED2 and LED4 */
00071     STM_EVAL_LEDToggle(LED2);
00072     STM_EVAL_LEDToggle(LED4);
00073 
00074     /* Insert 50 ms delay */
00075     Delay(50);
00076 
00077     /* Toggle LED1 and LED3 */
00078     STM_EVAL_LEDToggle(LED1);
00079     STM_EVAL_LEDToggle(LED3);
00080 
00081     /* Insert 100 ms delay */
00082     Delay(100);
00083   }
00084 }
00085 
00086 /**
00087   * @brief  Configure system clock to run at 16Mhz
00088   * @param  None
00089   * @retval None
00090   */
00091 static void CLK_Config(void)
00092 {
00093     /* Initialization of the clock */
00094     /* Clock divider to HSI/1 */
00095     CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
00096 }
00097 
00098 /**
00099   * @brief  Configure TIM4 to generate an update interrupt each 1ms 
00100   * @param  None
00101   * @retval None
00102   */
00103 static void TIM4_Config(void)
00104 {
00105   /* TIM4 configuration:
00106    - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
00107    clock used is 16 MHz / 128 = 125 000 Hz
00108   - With 125 000 Hz we can generate time base:
00109       max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
00110       min time base is 0.016 ms if TIM4_PERIOD = 1   --> (  1 + 1) / 125000 = 0.016 ms
00111   - In this example we need to generate a time base equal to 1 ms
00112    so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */
00113 
00114   /* Time base configuration */
00115   TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
00116   /* Clear TIM4 update flag */
00117   TIM4_ClearFlag(TIM4_FLAG_UPDATE);
00118   /* Enable update interrupt */
00119   TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
00120   
00121   /* enable interrupts */
00122   enableInterrupts();
00123 
00124   /* Enable TIM4 */
00125   TIM4_Cmd(ENABLE);
00126 }
00127 
00128 /**
00129   * @brief  Configure GPIO for LEDs available on the evaluation board
00130   * @param  None
00131   * @retval None
00132   */
00133 static void GPIO_Config(void)
00134 {
00135     /* Initialize LEDs mounted on STM8-128 EVAL board */
00136     STM_EVAL_LEDInit(LED1);
00137     STM_EVAL_LEDInit(LED2);
00138     STM_EVAL_LEDInit(LED3);
00139     STM_EVAL_LEDInit(LED4);
00140     
00141     /* Switch LED2 & LED4 Off */
00142     STM_EVAL_LEDOff(LED2);
00143     STM_EVAL_LEDOff(LED4);
00144     
00145     /* Switch LED1 & LED3 On */
00146     STM_EVAL_LEDOn(LED1);
00147     STM_EVAL_LEDOn(LED3);
00148     
00149 }
00150 
00151 /**
00152   * @brief  Inserts a delay time.
00153   * @param  nTime: specifies the delay time length, in milliseconds.
00154   * @retval None
00155   */
00156 void Delay(__IO uint32_t nTime)
00157 {
00158   TimingDelay = nTime;
00159 
00160   while (TimingDelay != 0);
00161 }
00162 
00163 /**
00164   * @brief  Decrements the TimingDelay variable.
00165   * @param  None
00166   * @retval None
00167   */
00168 void TimingDelay_Decrement(void)
00169 {
00170   if (TimingDelay != 0x00)
00171   {
00172     TimingDelay--;
00173   }
00174 }
00175 
00176 #ifdef USE_FULL_ASSERT
00177 
00178 /**
00179   * @brief  Reports the name of the source file and the source line number
00180   *   where the assert_param error has occurred.
00181   * @param file: pointer to the source file name
00182   * @param line: assert_param error line source number
00183   * @retval None
00184   */
00185 void assert_failed(uint8_t* file, uint32_t line)
00186 { 
00187   /* User can add his own implementation to report the file name and line number,
00188      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00189 
00190   /* Infinite loop */
00191   while (1)
00192   {
00193   }
00194 }
00195 #endif
00196 
00197 /**
00198   * @}
00199   */
00200 
00201 
00202 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com