STM32F10x_StdPeriph_Examples/TIM/Parallel_Synchro/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/Parallel_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_Parallel_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 
00039 /* Private function prototypes -----------------------------------------------*/
00040 void RCC_Configuration(void);
00041 void GPIO_Configuration(void);
00042 
00043 /* Private functions ---------------------------------------------------------*/
00044 
00045 /**
00046   * @brief   Main program
00047   * @param  None
00048   * @retval None
00049   */
00050 int main(void)
00051 {
00052   /*!< At this stage the microcontroller clock setting is already configured, 
00053        this is done through SystemInit() function which is called from startup
00054        file (startup_stm32f10x_xx.s) before to branch to application main.
00055        To reconfigure the default setting of SystemInit() function, refer to
00056        system_stm32f10x.c file
00057      */     
00058        
00059   /* System Clocks Configuration */
00060   RCC_Configuration();
00061 
00062   /* GPIO Configuration */
00063   GPIO_Configuration();
00064 
00065   /* Timers synchronisation in parallel mode ----------------------------
00066      1/TIM2 is configured as Master Timer:
00067      - PWM Mode is used
00068      - The TIM2 Update event is used as Trigger Output  
00069      2/TIM3 and TIM4 are slaves for TIM2,
00070      - PWM Mode is used
00071      - The ITR1(TIM2) is used as input trigger for both slaves
00072      - Gated mode is used, so starts and stops of slaves counters
00073       are controlled by the Master trigger output signal(update event).
00074 
00075   * For Low-density, Medium-density, High-density and Connectivity line devices:
00076      The TIMxCLK is fixed to 72 MHz, the TIM2 counter clock is 72 MHz.
00077      The Master Timer TIM2 is running at 281.250 KHz and the duty cycle
00078      is equal to 25%
00079      The TIM3 is running:
00080      - At (TIM2 frequency)/ (TIM3 period + 1) = 28.125 KHz and a duty cycle
00081      equal to TIM3_CCR1/(TIM3_ARR + 1) = 30%
00082      The TIM4 is running:
00083      - At (TIM2 frequency)/ (TIM4 period + 1) = 56.250 KHz and a duty cycle
00084      equal to TIM4_CCR1/(TIM4_ARR + 1) = 60%
00085      
00086   * For Value line devices:
00087      The TIMxCLK is fixed to 24 MHz, the TIM2 counter clock is 24 MHz.
00088      TIM2 frequency = 93.750 KHz,
00089      TIM3 frequency = 23.437 KHz,
00090      TIM4 frequency = 18.75 KHz     
00091   -------------------------------------------------------------------- */
00092 
00093   /* Time base configuration */
00094   TIM_TimeBaseStructure.TIM_Period = 255;
00095   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00096   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00097   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00098 
00099   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
00100 
00101   TIM_TimeBaseStructure.TIM_Period = 9;
00102   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00103 
00104   TIM_TimeBaseStructure.TIM_Period = 4;
00105   TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
00106 
00107   /* Master Configuration in PWM1 Mode */
00108   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
00109   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00110   TIM_OCInitStructure.TIM_Pulse = 64;
00111   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00112 
00113   TIM_OC1Init(TIM2, &TIM_OCInitStructure);
00114 
00115   /* Select the Master Slave Mode */
00116   TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
00117 
00118   /* Master Mode selection */
00119   TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
00120 
00121   /* Slaves Configuration: PWM1 Mode */
00122   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
00123   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00124   TIM_OCInitStructure.TIM_Pulse = 3;
00125 
00126   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00127 
00128   TIM_OC1Init(TIM4, &TIM_OCInitStructure);
00129 
00130   /* Slave Mode selection: TIM3 */
00131   TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);
00132   TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1);
00133   
00134   /* Slave Mode selection: TIM4 */
00135   TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Gated);
00136   TIM_SelectInputTrigger(TIM4, TIM_TS_ITR1);
00137   
00138   /* TIM enable counter */
00139   TIM_Cmd(TIM3, ENABLE);
00140   TIM_Cmd(TIM2, ENABLE);
00141   TIM_Cmd(TIM4, ENABLE);
00142 
00143   while (1)
00144   {}
00145 }
00146 
00147 /**
00148   * @brief  Configures the different system clocks.
00149   * @param  None
00150   * @retval None
00151   */
00152 void RCC_Configuration(void)
00153 {  
00154   /* TIM2, TIM3 and TIM4 clock enable */
00155   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
00156                          RCC_APB1Periph_TIM4, ENABLE);
00157 
00158   /* GPIOA, GPIOB, GPIOC and AFIO clocks enable */
00159   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
00160                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
00161 }
00162 
00163 /**
00164   * @brief  Configure the GPIOD Pins.
00165   * @param  None
00166   * @retval None
00167   */
00168 void GPIO_Configuration(void)
00169 {
00170   GPIO_InitTypeDef GPIO_InitStructure;
00171 
00172 #ifdef STM32F10X_CL
00173   /*GPIOB Configuration:  PC6(TIM3 CH1) as alternate function push-pull */
00174   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 ;
00175   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00176   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00177 
00178   GPIO_Init(GPIOC, &GPIO_InitStructure);
00179 
00180   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     
00181 
00182 #else
00183 /* GPIOA Configuration: PA6(TIM3 CH1) as alternate function push-pull */
00184   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00185   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00186   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00187 
00188   GPIO_Init(GPIOA, &GPIO_InitStructure);
00189 #endif
00190   /* GPIOA Configuration: PA0(TIM2 CH1) as alternate function push-pull */
00191   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
00192   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00193   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00194 
00195   GPIO_Init(GPIOA, &GPIO_InitStructure);
00196 
00197   /* GPIOB Configuration: PB6(TIM4 CH1) as alternate function push-pull */
00198   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00199 
00200   GPIO_Init(GPIOB, &GPIO_InitStructure);
00201 }
00202 
00203 #ifdef  USE_FULL_ASSERT
00204 
00205 /**
00206   * @brief  Reports the name of the source file and the source line number
00207   *         where the assert_param error has occurred.
00208   * @param  file: pointer to the source file name
00209   * @param  line: assert_param error line source number
00210   * @retval None
00211   */
00212 void assert_failed(uint8_t* file, uint32_t line)
00213 {
00214   /* User can add his own implementation to report the file name and line number,
00215      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00216 
00217   while (1)
00218   {}
00219 }
00220 #endif
00221 
00222 /**
00223   * @}
00224   */ 
00225 
00226 /**
00227   * @}
00228   */ 
00229 
00230 /******************* (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