STM32F10x_StdPeriph_Examples/TIM/ExtTrigger_Synchro/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/ExtTrigger_Synchro/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_ExtTrigger_Synchro
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 /* Private macro -------------------------------------------------------------*/
00035 /* Private variables ---------------------------------------------------------*/
00036 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00037 TIM_ICInitTypeDef  TIM_ICInitStructure;
00038 TIM_OCInitTypeDef  TIM_OCInitStructure;
00039 
00040 /* Private function prototypes -----------------------------------------------*/
00041 void RCC_Configuration(void);
00042 void GPIO_Configuration(void);
00043 
00044 /* Private functions ---------------------------------------------------------*/
00045 
00046 /**
00047   * @brief   Main program
00048   * @param  None
00049   * @retval None
00050   */
00051 int main(void)
00052 {
00053   /*!< At this stage the microcontroller clock setting is already configured, 
00054        this is done through SystemInit() function which is called from startup
00055        file (startup_stm32f10x_xx.s) before to branch to application main.
00056        To reconfigure the default setting of SystemInit() function, refer to
00057        system_stm32f10x.c file
00058      */     
00059        
00060   /* System Clocks Configuration */
00061   RCC_Configuration();
00062 
00063   /* Configure the GPIO ports */
00064   GPIO_Configuration();
00065 
00066   /* Timers synchronisation in cascade mode with an external trigger -----
00067     1/TIM1 is configured as Master Timer:
00068      - Toggle Mode is used
00069      - The TIM1 Enable event is used as Trigger Output 
00070 
00071     2/TIM1 is configured as Slave Timer for an external Trigger connected
00072      to TIM1 TI2 pin (TIM1 CH2 configured as input pin):
00073      - The TIM1 TI2FP2 is used as Trigger Input
00074      - Rising edge is used to start and stop the TIM1: Gated Mode.
00075 
00076     3/TIM3 is slave for TIM1 and Master for TIM4,
00077      - Toggle Mode is used
00078      - The ITR1(TIM1) is used as input trigger 
00079      - Gated mode is used, so start and stop of slave counter
00080        are controlled by the Master trigger output signal(TIM1 enable event).
00081      - The TIM3 enable event is used as Trigger Output. 
00082 
00083     4/TIM4 is slave for TIM3,
00084      - Toggle Mode is used
00085      - The ITR2(TIM3) is used as input trigger
00086      - Gated mode is used, so start and stop of slave counter
00087        are controlled by the Master trigger output signal(TIM3 enable event).
00088 
00089     * For Low-density, Medium-density, High-density and Connectivity line devices:
00090       The TIMxCLK is fixed to 72 MHZ, the Prescaler is equal to 2 so the TIMx clock 
00091       counter is equal to 24 MHz.
00092       The Three Timers are running at: 
00093       TIMx frequency = TIMx clock counter/ 2*(TIMx_Period + 1) = 162.1 KHz.
00094 
00095     * For Low-Density Value line and Medium-Density Value line devices:
00096       The TIMxCLK is fixed to 24 MHz, the Prescaler is equal to 2 so the TIMx clock 
00097       counter is equal to 8 MHz.
00098       TIMx frequency = TIMx clock counter/ 2*(TIMx_Period + 1) = 54 KHz.
00099 
00100     The starts and stops of the TIM1 counters are controlled by the 
00101     external trigger.
00102     The TIM3 starts and stops are controlled by the TIM1, and the TIM4 
00103     starts and stops are controlled by the TIM3.  
00104   -------------------------------------------------------------------- */
00105 
00106   /* Time base configuration */
00107   TIM_TimeBaseStructure.TIM_Period = 73;
00108   TIM_TimeBaseStructure.TIM_Prescaler = 2;
00109   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00110   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00111 
00112   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00113 
00114   TIM_TimeBaseStructure.TIM_Period = 73;
00115   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00116 
00117   TIM_TimeBaseStructure.TIM_Period = 73;
00118   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
00119 
00120   /* Master Configuration in Toggle Mode */
00121   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
00122   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00123   TIM_OCInitStructure.TIM_Pulse = 64;
00124   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00125 
00126   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00127 
00128   /* TIM1 Input Capture Configuration */
00129   TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
00130   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
00131   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
00132   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
00133   TIM_ICInitStructure.TIM_ICFilter = 0;
00134 
00135   TIM_ICInit(TIM1, &TIM_ICInitStructure);
00136 
00137   /* TIM1 Input trigger configuration: External Trigger connected to TI2 */
00138   TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);
00139   TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);
00140 
00141   /* Select the Master Slave Mode */
00142   TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
00143 
00144   /* Master Mode selection: TIM1 */
00145   TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Enable);
00146 
00147   /* Slaves Configuration: Toggle Mode */
00148   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
00149   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00150 
00151   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00152 
00153   TIM_OC1Init(TIM4, &TIM_OCInitStructure);
00154 
00155   /* Slave Mode selection: TIM3 */
00156   TIM_SelectInputTrigger(TIM3, TIM_TS_ITR0);
00157   TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);
00158 
00159   /* Select the Master Slave Mode */
00160   TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
00161 
00162   /* Master Mode selection: TIM3 */
00163   TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Enable);
00164 
00165   /* Slave Mode selection: TIM4 */
00166   TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);
00167   TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Gated);
00168   
00169   /* TIM1 Main Output Enable */
00170   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00171 
00172   /* TIM enable counter */
00173   TIM_Cmd(TIM1, ENABLE);
00174   TIM_Cmd(TIM3, ENABLE);
00175   TIM_Cmd(TIM4, ENABLE);
00176 
00177   while (1)
00178   {}
00179 }
00180 
00181 /**
00182   * @brief  Configures the different system clocks.
00183   * @param  None
00184   * @retval None
00185   */
00186 void RCC_Configuration(void)
00187 {
00188   /* TIM1, TIM3 and TIM4 clock enable */
00189   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 |
00190                          RCC_APB1Periph_TIM4, ENABLE);
00191 
00192   /* TIM1, GPIOA, GPIOE, GPIOC and GPIOB clocks enable */
00193   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
00194                          RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
00195 }
00196 
00197 /**
00198   * @brief  Configure the GPIO Pins.
00199   * @param  None
00200   * @retval None
00201   */
00202 void GPIO_Configuration(void)
00203 {
00204   GPIO_InitTypeDef GPIO_InitStructure;
00205 #ifdef STM32F10X_CL
00206   /* GPIOE Configuration: Channel 1 as alternate function push-pull */
00207   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
00208   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00209   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00210 
00211   GPIO_Init(GPIOE, &GPIO_InitStructure);
00212 
00213    /* GPIOE Configuration: Channel 2 as Input floating */
00214   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
00215   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00216 
00217   GPIO_Init(GPIOE, &GPIO_InitStructure);
00218 
00219   /* TIM1 Full remapping pins */
00220   GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); 
00221 
00222  /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
00223   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
00224   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00225   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00226 
00227   GPIO_Init(GPIOC, &GPIO_InitStructure);
00228 
00229   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     
00230 
00231   /* GPIOB Configuration: PB.06(TIM4 CH1) as alternate function push-pull */
00232   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00233 
00234   GPIO_Init(GPIOB, &GPIO_InitStructure);
00235 
00236 #else
00237   /* GPIOA Configuration: PA.08(TIM1 CH1) and PA.06(TIM3 CH1) as alternate function push-pull */
00238   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_6;
00239   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00240   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00241 
00242   GPIO_Init(GPIOA, &GPIO_InitStructure);
00243 
00244   /* GPIOB Configuration: PB.06(TIM4 CH1) as alternate function push-pull */
00245   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00246 
00247   GPIO_Init(GPIOB, &GPIO_InitStructure);
00248 
00249   /* GPIOA Configuration: PA.09(TIM1 CH2) */
00250   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
00251   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00252 
00253   GPIO_Init(GPIOA, &GPIO_InitStructure);
00254 
00255 #endif
00256 
00257 }
00258 
00259 #ifdef  USE_FULL_ASSERT
00260 
00261 /**
00262   * @brief  Reports the name of the source file and the source line number
00263   *         where the assert_param error has occurred.
00264   * @param  file: pointer to the source file name
00265   * @param  line: assert_param error line source number
00266   * @retval None
00267   */
00268 void assert_failed(uint8_t* file, uint32_t line)
00269 {
00270   /* User can add his own implementation to report the file name and line number,
00271      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00272 
00273   while (1)
00274   {}
00275 }
00276 #endif
00277 
00278 /**
00279   * @}
00280   */ 
00281 
00282 /**
00283   * @}
00284   */ 
00285 
00286 /******************* (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