STM32F10x_StdPeriph_Examples/TIM/OCInactive/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/OCInactive/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_OCInactive
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 uint16_t CCR1_Val = 1000;
00039 uint16_t CCR2_Val = 500;
00040 uint16_t CCR3_Val = 250;
00041 uint16_t CCR4_Val = 125;
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: 
00076     TIM2CLK = SystemCoreClock / 2,
00077     The objective is to get TIM2 counter clock at 1 KHz:
00078      - Prescaler = (TIM2CLK / TIM2 counter clock) - 1
00079     And generate 4 signals with 4 different delays:
00080     TIM2_CH1 delay = CCR1_Val/TIM2 counter clock = 1000 ms
00081     TIM2_CH2 delay = CCR2_Val/TIM2 counter clock = 500 ms
00082     TIM2_CH3 delay = CCR3_Val/TIM2 counter clock = 250 ms
00083     TIM2_CH4 delay = CCR4_Val/TIM2 counter clock = 125 ms
00084 
00085   * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
00086     and Connectivity line devices and to 24 MHz for Low-Density Value line and
00087     Medium-Density Value line devices
00088   --------------------------------------------------------------- */
00089   /* Compute the prescaler value */
00090   PrescalerValue = (uint16_t) (SystemCoreClock / 2000) - 1;
00091   /* Time base configuration */
00092   TIM_TimeBaseStructure.TIM_Period = 65535;
00093   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00094   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00095   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00096 
00097   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
00098 
00099   /* Output Compare Active Mode configuration: Channel1 */
00100   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
00101   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00102   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00103   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00104 
00105   TIM_OC1Init(TIM2, &TIM_OCInitStructure);
00106 
00107   TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
00108 
00109   /* Output Compare Active Mode configuration: Channel2 */
00110   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00111   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00112 
00113   TIM_OC2Init(TIM2, &TIM_OCInitStructure);
00114 
00115   TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
00116 
00117   /* Output Compare Active Mode configuration: Channel3 */
00118   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00119   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00120 
00121   TIM_OC3Init(TIM2, &TIM_OCInitStructure);
00122 
00123   TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
00124 
00125   /* Output Compare Active Mode configuration: Channel4 */
00126   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00127   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00128 
00129   TIM_OC4Init(TIM2, &TIM_OCInitStructure);
00130 
00131   TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
00132 
00133   TIM_ARRPreloadConfig(TIM2, ENABLE);
00134 
00135   /* TIM IT enable */
00136   TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
00137 
00138   /* Set PC.06, PC.07, PC.08 and PC.09 pins */
00139   GPIO_SetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9);
00140 
00141   /* TIM2 enable counter */
00142   TIM_Cmd(TIM2, ENABLE);
00143 
00144   while (1)
00145   {}
00146 }
00147 
00148 /**
00149   * @brief  Configures the different system clocks.
00150   * @param  None
00151   * @retval None
00152   */
00153 void RCC_Configuration(void)
00154 {
00155   /* PCLK1 = HCLK/4 */
00156   RCC_PCLK1Config(RCC_HCLK_Div4);
00157 
00158   /* TIM2 clock enable */
00159   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
00160 
00161   /* GPIOC clock enable */
00162   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
00163 }
00164 
00165 /**
00166   * @brief  Configure the GPIOD Pins.
00167   * @param  None
00168   * @retval None
00169   */
00170 void GPIO_Configuration(void)
00171 {
00172   GPIO_InitTypeDef GPIO_InitStructure;
00173 
00174   /* GPIOC Configuration: Pin6, 7, 8 and 9 as output push-pull */
00175   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
00176   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
00177   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00178 
00179   GPIO_Init(GPIOC, &GPIO_InitStructure);
00180 }
00181 
00182 /**
00183   * @brief  Configure the nested vectored interrupt controller.
00184   * @param  None
00185   * @retval None
00186   */
00187 void NVIC_Configuration(void)
00188 {
00189   NVIC_InitTypeDef NVIC_InitStructure;
00190 
00191   /* Enable the TIM2 Interrupt */
00192   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
00193   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00194   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
00195   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00196 
00197   NVIC_Init(&NVIC_InitStructure);
00198 
00199 }
00200 
00201 #ifdef  USE_FULL_ASSERT
00202 
00203 /**
00204   * @brief  Reports the name of the source file and the source line number
00205   *         where the assert_param error has occurred.
00206   * @param  file: pointer to the source file name
00207   * @param  line: assert_param error line source number
00208   * @retval None
00209   */
00210 void assert_failed(uint8_t* file, uint32_t line)
00211 {
00212   /* User can add his own implementation to report the file name and line number,
00213      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00214 
00215   while (1)
00216   {}
00217 }
00218 #endif
00219 /**
00220   * @}
00221   */ 
00222 
00223 /**
00224   * @}
00225   */ 
00226 
00227 /******************* (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