STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_TimeBase/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_TimeBase/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 TIM_TimeBase
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 __IO uint16_t CCR1_Val = 40961;
00044 __IO uint16_t CCR2_Val = 27309;
00045 __IO uint16_t CCR3_Val = 13654;
00046 __IO uint16_t CCR4_Val = 6826;
00047 uint16_t PrescalerValue = 0;
00048 
00049 /* Private function prototypes -----------------------------------------------*/
00050 static void TIM_Config(void);
00051 
00052 /* Private functions ---------------------------------------------------------*/
00053 
00054 /**
00055   * @brief  Main program.
00056   * @param  None
00057   * @retval None
00058   */
00059 int main(void)
00060 {
00061   /*!< At this stage the microcontroller clock setting is already configured, 
00062        this is done through SystemInit() function which is called from startup
00063        file (startup_stm32f0xx.s) before to branch to application main.
00064        To reconfigure the default setting of SystemInit() function, refer to
00065        system_stm32f0xx.c file
00066      */ 
00067 
00068   /* TIM Configuration */
00069   TIM_Config();
00070 
00071   /* Infinite loop */
00072   while (1)
00073   {
00074   }
00075 }
00076 
00077 /**
00078   * @brief  Configure the TIM IRQ Handler.
00079   * @param  None
00080   * @retval None
00081   */
00082 static void TIM_Config(void)
00083 {
00084   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00085   TIM_OCInitTypeDef  TIM_OCInitStructure;
00086   NVIC_InitTypeDef NVIC_InitStructure;
00087 
00088   /* TIM3 clock enable */
00089   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00090 
00091   /* Enable the TIM3 gloabal Interrupt */
00092   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
00093   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00094   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00095   NVIC_Init(&NVIC_InitStructure);
00096 
00097   /* Initialize Leds mounted on STM320518-EVAL board */
00098   STM_EVAL_LEDInit(LED1);
00099   STM_EVAL_LEDInit(LED2);
00100   STM_EVAL_LEDInit(LED3);
00101   STM_EVAL_LEDInit(LED4);
00102 
00103   /* Turn on LED1, LED2, LED3 and LED4 */
00104   STM_EVAL_LEDOn(LED1);
00105   STM_EVAL_LEDOn(LED2);
00106   STM_EVAL_LEDOn(LED3);
00107   STM_EVAL_LEDOn(LED4);
00108   
00109     /* -----------------------------------------------------------------------
00110     TIM3 Configuration: Output Compare Timing Mode:
00111     
00112     In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1),  
00113       => TIM3CLK = PCLK1 = SystemCoreClock = 48 MHz
00114           
00115     To get TIM3 counter clock at 6 MHz, the prescaler is computed as follows:
00116        Prescaler = (TIM3CLK / TIM3 counter clock) - 1
00117        Prescaler = (PCLK1 /6 MHz) - 1
00118                                               
00119     CC1 update rate = TIM3 counter clock / CCR1_Val = 146.48 Hz
00120     ==> Toggling frequency = 73.24 Hz
00121     
00122     C2 update rate = TIM3 counter clock / CCR2_Val = 219.7 Hz
00123     ==> Toggling frequency = 109.8 Hz
00124     
00125     CC3 update rate = TIM3 counter clock / CCR3_Val = 439.4 Hz
00126     ==> Toggling frequency = 219.7 Hz
00127     
00128     CC4 update rate = TIM3 counter clock / CCR4_Val = 878.9 Hz
00129     ==> Toggling frequency = 439.4 Hz
00130 
00131     Note: 
00132      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
00133      Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
00134      function to update SystemCoreClock variable value. Otherwise, any configuration
00135      based on this variable will be incorrect.    
00136   ----------------------------------------------------------------------- */   
00137 
00138 
00139   /* Compute the prescaler value */
00140   PrescalerValue = (uint16_t) (SystemCoreClock  / 6000000) - 1;
00141 
00142   /* Time base configuration */
00143   TIM_TimeBaseStructure.TIM_Period = 65535;
00144   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00145   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00146   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00147 
00148   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00149 
00150   /* Prescaler configuration */
00151   TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
00152 
00153   /* Output Compare Timing Mode configuration: Channel1 */
00154   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
00155   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00156   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00157   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00158 
00159   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00160 
00161   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
00162 
00163   /* Output Compare Timing Mode configuration: Channel2 */
00164   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00165   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00166 
00167   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
00168 
00169   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
00170 
00171   /* Output Compare Timing Mode configuration: Channel3 */
00172   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00173   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00174 
00175   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
00176 
00177   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
00178 
00179   /* Output Compare Timing Mode configuration: Channel4 */
00180   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00181   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00182 
00183   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
00184 
00185   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
00186    
00187   /* TIM Interrupts enable */
00188   TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
00189 
00190   /* TIM3 enable counter */
00191   TIM_Cmd(TIM3, ENABLE);
00192 }
00193 
00194 #ifdef  USE_FULL_ASSERT
00195 
00196 /**
00197   * @brief  Reports the name of the source file and the source line number
00198   *         where the assert_param error has occurred.
00199   * @param  file: pointer to the source file name
00200   * @param  line: assert_param error line source number
00201   * @retval None
00202   */
00203 void assert_failed(uint8_t* file, uint32_t line)
00204 { 
00205   /* User can add his own implementation to report the file name and line number,
00206      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00207 
00208   /* Infinite loop */
00209   while (1)
00210   {
00211   }
00212 }
00213 #endif
00214 
00215 /**
00216   * @}
00217   */
00218 
00219 /**
00220   * @}
00221   */
00222 
00223 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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