STM32F10x_StdPeriph_Examples/TIM/OCToggle/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/OCToggle/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_OCToggle
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 = 32768;
00039 __IO uint16_t CCR2_Val = 16384;
00040 __IO uint16_t CCR3_Val = 8192;
00041 __IO uint16_t CCR4_Val = 4096;
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     TIM3 Configuration: Output Compare Toggle Mode:
00076     TIM3CLK = SystemCoreClock / 2,
00077     The objective is to get TIM3 counter clock at 12 MHz:
00078      - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
00079     CC1 update rate = TIM3 counter clock / CCR1_Val = 366.2 Hz
00080     CC2 update rate = TIM3 counter clock / CCR2_Val = 732.4 Hz
00081     CC3 update rate = TIM3 counter clock / CCR3_Val = 1464.8 Hz
00082     CC4 update rate = TIM3 counter clock / CCR4_Val = 2929.6 Hz
00083   ----------------------------------------------------------------------------*/
00084   /* Compute the prescaler value */
00085   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
00086 
00087   /* Time base configuration */
00088   TIM_TimeBaseStructure.TIM_Period = 65535;
00089   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00090   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00091   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00092 
00093   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00094 
00095   /* Output Compare Toggle Mode configuration: Channel1 */
00096   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
00097   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00098   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00099   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00100   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00101 
00102   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
00103 
00104   /* Output Compare Toggle Mode configuration: Channel2 */
00105   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00106   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00107 
00108   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
00109 
00110   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
00111 
00112   /* Output Compare Toggle Mode configuration: Channel3 */
00113   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00114   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00115 
00116   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
00117 
00118   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
00119 
00120   /* Output Compare Toggle Mode configuration: Channel4 */
00121   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00122   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00123 
00124   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
00125 
00126   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
00127 
00128   /* TIM enable counter */
00129   TIM_Cmd(TIM3, ENABLE);
00130 
00131   /* TIM IT enable */
00132   TIM_ITConfig(TIM3, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
00133 
00134   while (1)
00135   {}
00136 }
00137 
00138 /**
00139   * @brief  Configures the different system clocks.
00140   * @param  None
00141   * @retval None
00142   */
00143 void RCC_Configuration(void)
00144 {
00145   /* PCLK1 = HCLK/4 */
00146   RCC_PCLK1Config(RCC_HCLK_Div4);
00147 
00148   /* TIM3 clock enable */
00149   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00150 
00151   /* GPIOA clock enable */
00152   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB|
00153                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
00154 }
00155 
00156 /**
00157   * @brief  Configure the TIM3 Pins.
00158   * @param  None
00159   * @retval None
00160   */
00161 void GPIO_Configuration(void)
00162 {
00163   GPIO_InitTypeDef GPIO_InitStructure;
00164 
00165 #ifdef STM32F10X_CL
00166   /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
00167   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
00168   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00169   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00170 
00171   GPIO_Init(GPIOC, &GPIO_InitStructure);
00172 
00173   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     
00174 
00175 #else
00176   /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
00177   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
00178   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00179   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00180 
00181   GPIO_Init(GPIOA, &GPIO_InitStructure);
00182 
00183   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
00184   GPIO_Init(GPIOB, &GPIO_InitStructure);
00185 #endif
00186 }
00187 
00188 /**
00189   * @brief  Configure the nested vectored interrupt controller.
00190   * @param  None
00191   * @retval None
00192   */
00193 void NVIC_Configuration(void)
00194 {
00195   NVIC_InitTypeDef NVIC_InitStructure;
00196 
00197   /* Enable the TIM3 global Interrupt */
00198   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
00199   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00200   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00201   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00202   NVIC_Init(&NVIC_InitStructure);
00203 }
00204 
00205 #ifdef  USE_FULL_ASSERT
00206 
00207 /**
00208   * @brief  Reports the name of the source file and the source line number
00209   *         where the assert_param error has occurred.
00210   * @param  file: pointer to the source file name
00211   * @param  line: assert_param error line source number
00212   * @retval None
00213   */
00214 void assert_failed(uint8_t* file, uint32_t line)
00215 {
00216   /* User can add his own implementation to report the file name and line number,
00217      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00218 
00219   while (1)
00220   {}
00221 }
00222 #endif
00223 /**
00224   * @}
00225   */ 
00226 
00227 /**
00228   * @}
00229   */ 
00230 
00231 /******************* (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