STM32F10x_StdPeriph_Examples/TIM/TIM1_Synchro/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM1_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_TIM1_Synchro
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 TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
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   /* GPIO Configuration */
00064   GPIO_Configuration();
00065 
00066   /* TIM1 and Timers(TIM3 and TIM4) synchronisation in parallel mode -----------
00067      1/TIM1 is configured as Master Timer:
00068      - PWM Mode is used
00069      - The TIM1 Update event is used as Trigger Output
00070     
00071      2/TIM3 and TIM4 are slaves for TIM1,
00072      - PWM Mode is used
00073      - The ITR0(TIM1) is used as input trigger for both slaves
00074      - Gated mode is used, so starts and stops of slaves counters
00075        are controlled by the Master trigger output signal(update event).
00076 
00077   o For Low-density, Medium-density, High-density and Connectivity line devices:
00078     The TIMxCLK is fixed to 72 MHz, Prescaler = 0 so the TIM1 counter clock is 72 MHz.
00079 
00080     The Master Timer TIM1 is running at:
00081     TIM1 frequency = TIM1 counter clock / (TIM1_Period + 1) = 281.250 KHz
00082     and the duty cycle is equal to: TIM1_CCR1/(TIM1_ARR + 1) = 50%
00083 
00084     The TIM3 is running at: 
00085     (TIM1 frequency)/ ((TIM3 period +1)* (Repetion_Counter+1)) = 18.750 KHz and
00086     a duty cycle equal to TIM3_CCR1/(TIM3_ARR + 1) = 33.3%
00087 
00088     The TIM4 is running at:
00089     (TIM1 frequency)/ ((TIM4 period +1)* (Repetion_Counter+1)) = 28.125 KHz and
00090     a duty cycle equal to TIM4_CCR1/(TIM4_ARR + 1) = 50%
00091   
00092   o For Low-Density Value line and Medium-Density Value line devices:
00093     The TIMxCLK is fixed to 24 MHz, Prescaler = 0 so the TIM1 counter clock is 24 MHz.
00094     TIM1 frequency = 93.75 KHz
00095     TIM3 frequency = 6.25 KHz
00096     TIM4 frequency = 9.375 KHz
00097   --------------------------------------------------------------------------- */
00098 
00099   /* TIM3 Peripheral Configuration ----------------------------------------*/
00100   /* TIM3 Slave Configuration: PWM1 Mode */
00101   TIM_TimeBaseStructure.TIM_Period = 2;
00102   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00103   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00104   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00105 
00106   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00107 
00108   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
00109   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00110   TIM_OCInitStructure.TIM_Pulse = 1;
00111 
00112   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00113 
00114   /* Slave Mode selection: TIM3 */
00115   TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);
00116   TIM_SelectInputTrigger(TIM3, TIM_TS_ITR0);
00117   
00118   /* TIM4 Peripheral Configuration ----------------------------------------*/
00119   /* TIM4 Slave Configuration: PWM1 Mode */
00120   TIM_TimeBaseStructure.TIM_Period = 1;
00121   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00122   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00123   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00124 
00125   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
00126 
00127   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
00128   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00129   TIM_OCInitStructure.TIM_Pulse = 1;
00130 
00131   TIM_OC1Init(TIM4, &TIM_OCInitStructure);
00132 
00133   /* Slave Mode selection: TIM4 */
00134   TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Gated);
00135   TIM_SelectInputTrigger(TIM4, TIM_TS_ITR0);
00136   
00137   /* TIM1 Peripheral Configuration ----------------------------------------*/
00138   /* Time Base configuration */
00139   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00140   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00141   TIM_TimeBaseStructure.TIM_Period = 255;
00142   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00143   TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
00144 
00145   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00146 
00147   /* Channel 1 Configuration in PWM mode */
00148   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
00149   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00150   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
00151   TIM_OCInitStructure.TIM_Pulse = 127;
00152   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00153   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
00154   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
00155   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
00156 
00157   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00158 
00159   /* Automatic Output enable, Break, dead time and lock configuration*/
00160   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
00161   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
00162   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
00163   TIM_BDTRInitStructure.TIM_DeadTime = 5;
00164   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
00165   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
00166   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
00167 
00168   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
00169 
00170   /* Master Mode selection */
00171   TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
00172 
00173   /* Select the Master Slave Mode */
00174   TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
00175   
00176   /* TIM1 counter enable */
00177   TIM_Cmd(TIM1, ENABLE);
00178 
00179   /* TIM enable counter */
00180   TIM_Cmd(TIM3, ENABLE);
00181   TIM_Cmd(TIM4, ENABLE);
00182 
00183   /* Main Output Enable */
00184   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00185 
00186   while (1)
00187   {}
00188 }
00189 
00190 /**
00191   * @brief  Configures the different system clocks.
00192   * @param  None
00193   * @retval None
00194   */
00195 void RCC_Configuration(void)
00196 {
00197   /* TIM1, GPIOA and GPIOB clock enable */
00198   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE |
00199                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
00200 
00201   /* TIM3 and TIM4 clock enable */
00202   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);
00203 }
00204 
00205 /**
00206   * @brief  Configures TIM1, TIM3 and TIM4 Pins.
00207   * @param  None
00208   * @retval None
00209   */
00210 void GPIO_Configuration(void)
00211 {
00212   GPIO_InitTypeDef GPIO_InitStructure;
00213 
00214 #ifdef STM32F10X_CL
00215   /*GPIOC Configuration: TIM3 channel1 as alternate function push-pull */
00216   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;
00217   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00218   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00219 
00220   GPIO_Init(GPIOC, &GPIO_InitStructure);
00221 
00222   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     
00223 
00224   /*GPIOE Configuration: TIM1 channel1 as alternate function push-pull */
00225   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8;
00226 
00227   GPIO_Init(GPIOE, &GPIO_InitStructure);
00228 
00229   GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);     
00230 
00231 #else
00232 
00233   /* GPIOA Configuration: TIM1 Channel1 and TIM3 Channel1 as alternate function push-pull */
00234   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_8;
00235   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00236   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00237   GPIO_Init(GPIOA, &GPIO_InitStructure);
00238 #endif
00239 
00240   /* GPIOB Configuration: TIM4 Channel1 as alternate function push-pull */
00241   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00242   GPIO_Init(GPIOB, &GPIO_InitStructure);
00243 }
00244 
00245 #ifdef  USE_FULL_ASSERT
00246 
00247 /**
00248   * @brief  Reports the name of the source file and the source line number
00249   *         where the assert_param error has occurred.
00250   * @param  file: pointer to the source file name
00251   * @param  line: assert_param error line source number
00252   * @retval None
00253   */
00254 void assert_failed(uint8_t* file, uint32_t line)
00255 {
00256   /* User can add his own implementation to report the file name and line number,
00257      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00258 
00259   while (1)
00260   {}
00261 }
00262 #endif
00263 
00264 /**
00265   * @}
00266   */ 
00267 
00268 /**
00269   * @}
00270   */ 
00271 
00272 /******************* (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