STM32F10x_StdPeriph_Examples/TIM/TimeBase/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TimeBase/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 
00024 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup TIM_TimeBase
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 /* Private macro -------------------------------------------------------------*/
00035 /* Private variables ---------------------------------------------------------*/
00036 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00037 TIM_OCInitTypeDef  TIM_OCInitStructure;
00038 __IO uint16_t CCR1_Val = 40961;
00039 __IO uint16_t CCR2_Val = 27309;
00040 __IO uint16_t CCR3_Val = 13654;
00041 __IO uint16_t CCR4_Val = 6826;
00042 uint16_t PrescalerValue = 0;
00043 
00044 /* Private function prototypes -----------------------------------------------*/
00045 void RCC_Configuration(void);
00046 void GPIO_Configuration(void);
00047 void NVIC_Configuration(void);
00048 
00049 /* Private functions ---------------------------------------------------------*/
00050 
00051 /**
00052   * @brief   Main program
00053   * @param  None
00054   * @retval None
00055   */
00056 int main(void)
00057 {
00058   /*!< At this stage the microcontroller clock setting is already configured, 
00059        this is done through SystemInit() function which is called from startup
00060        file (startup_stm32f10x_xx.s) before to branch to application main.
00061        To reconfigure the default setting of SystemInit() function, refer to
00062        system_stm32f10x.c file
00063      */     
00064        
00065   /* System Clocks Configuration */
00066   RCC_Configuration();
00067 
00068   /* NVIC Configuration */
00069   NVIC_Configuration();
00070 
00071   /* GPIO Configuration */
00072   GPIO_Configuration();
00073 
00074   /* ---------------------------------------------------------------
00075     TIM2 Configuration: Output Compare Timing Mode:
00076     TIM2 counter clock at 6 MHz
00077     CC1 update rate = TIM2 counter clock / CCR1_Val = 146.48 Hz
00078     CC2 update rate = TIM2 counter clock / CCR2_Val = 219.7 Hz
00079     CC3 update rate = TIM2 counter clock / CCR3_Val = 439.4 Hz
00080     CC4 update rate = TIM2 counter clock / CCR4_Val = 878.9 Hz
00081   --------------------------------------------------------------- */
00082 
00083   /* Compute the prescaler value */
00084   PrescalerValue = (uint16_t) (SystemCoreClock / 12000000) - 1;
00085 
00086   /* Time base configuration */
00087   TIM_TimeBaseStructure.TIM_Period = 65535;
00088   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00089   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00090   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00091 
00092   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
00093 
00094   /* Prescaler configuration */
00095   TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);
00096 
00097   /* Output Compare Timing Mode configuration: Channel1 */
00098   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
00099   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00100   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00101   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00102 
00103   TIM_OC1Init(TIM2, &TIM_OCInitStructure);
00104 
00105   TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
00106 
00107   /* Output Compare Timing Mode configuration: Channel2 */
00108   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00109   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00110 
00111   TIM_OC2Init(TIM2, &TIM_OCInitStructure);
00112 
00113   TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
00114 
00115   /* Output Compare Timing Mode configuration: Channel3 */
00116   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00117   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00118 
00119   TIM_OC3Init(TIM2, &TIM_OCInitStructure);
00120 
00121   TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
00122 
00123   /* Output Compare Timing Mode configuration: Channel4 */
00124   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00125   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00126 
00127   TIM_OC4Init(TIM2, &TIM_OCInitStructure);
00128 
00129   TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
00130 
00131   /* TIM IT enable */
00132   TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
00133 
00134   /* TIM2 enable counter */
00135   TIM_Cmd(TIM2, ENABLE);
00136 
00137   while (1);
00138 }
00139 
00140 /**
00141   * @brief  Configures the different system clocks.
00142   * @param  None
00143   * @retval None
00144   */
00145 void RCC_Configuration(void)
00146 {
00147   /* PCLK1 = HCLK/4 */
00148   RCC_PCLK1Config(RCC_HCLK_Div4);
00149 
00150   /* TIM2 clock enable */
00151   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
00152 
00153   /* GPIOC clock enable */
00154   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
00155 }
00156 
00157 /**
00158   * @brief  Configure the GPIOD Pins.
00159   * @param  None
00160   * @retval None
00161   */
00162 void GPIO_Configuration(void)
00163 {
00164   GPIO_InitTypeDef GPIO_InitStructure;
00165 
00166   /* GPIOC Configuration:Pin6, 7, 8 and 9 as alternate function push-pull */
00167   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
00168   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
00169   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00170 
00171   GPIO_Init(GPIOC, &GPIO_InitStructure);
00172 }
00173 
00174 /**
00175   * @brief  Configure the nested vectored interrupt controller.
00176   * @param  None
00177   * @retval None
00178   */
00179 void NVIC_Configuration(void)
00180 {
00181   NVIC_InitTypeDef NVIC_InitStructure;
00182 
00183   /* Enable the TIM2 gloabal Interrupt */
00184   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
00185   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00186   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
00187   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00188 
00189   NVIC_Init(&NVIC_InitStructure);
00190 }
00191 
00192 #ifdef  USE_FULL_ASSERT
00193 
00194 /**
00195   * @brief  Reports the name of the source file and the source line number
00196   *         where the assert_param error has occurred.
00197   * @param  file: pointer to the source file name
00198   * @param  line: assert_param error line source number
00199   * @retval None
00200   */
00201 void assert_failed(uint8_t* file, uint32_t line)
00202 {
00203   /* User can add his own implementation to report the file name and line number,
00204      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00205 
00206   while (1)
00207   {}
00208 }
00209 #endif
00210 
00211 /**
00212   * @}
00213   */ 
00214 
00215 /**
00216   * @}
00217   */ 
00218 
00219 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32