STM32F10x_StdPeriph_Examples/TIM/7PWM_Output/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/7PWM_Output/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_7PWM_Output
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 TimerPeriod = 0;
00039 uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
00040 
00041 /* Private function prototypes -----------------------------------------------*/
00042 void RCC_Configuration(void);
00043 void GPIO_Configuration(void);
00044 
00045 /* Private functions ---------------------------------------------------------*/
00046 
00047 /**
00048   * @brief   Main program
00049   * @param  None
00050   * @retval None
00051   */
00052 int main(void)
00053 {
00054   /*!< At this stage the microcontroller clock setting is already configured, 
00055        this is done through SystemInit() function which is called from startup
00056        file (startup_stm32f10x_xx.s) before to branch to application main.
00057        To reconfigure the default setting of SystemInit() function, refer to
00058        system_stm32f10x.c file
00059      */     
00060        
00061   /* System Clocks Configuration */
00062   RCC_Configuration();
00063 
00064   /* GPIO Configuration */
00065   GPIO_Configuration();
00066 
00067   /* TIM1 Configuration ---------------------------------------------------
00068    Generate 7 PWM signals with 4 different duty cycles:
00069    TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
00070    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
00071    and Connectivity line devices and to 24 MHz for Low-Density Value line and
00072    Medium-Density Value line devices
00073    
00074    The objective is to generate 7 PWM signal at 17.57 KHz:
00075      - TIM1_Period = (SystemCoreClock / 17570) - 1
00076    The channel 1 and channel 1N duty cycle is set to 50%
00077    The channel 2 and channel 2N duty cycle is set to 37.5%
00078    The channel 3 and channel 3N duty cycle is set to 25%
00079    The channel 4 duty cycle is set to 12.5%
00080    The Timer pulse is calculated as follows:
00081      - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
00082   ----------------------------------------------------------------------- */
00083   /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
00084   TimerPeriod = (SystemCoreClock / 17570 ) - 1;
00085   /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
00086   Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
00087   /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
00088   Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
00089   /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
00090   Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
00091   /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
00092   Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
00093 
00094   /* Time Base configuration */
00095   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00096   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00097   TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
00098   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00099   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
00100 
00101   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00102 
00103   /* Channel 1, 2,3 and 4 Configuration in PWM mode */
00104   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
00105   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00106   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
00107   TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
00108   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00109   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
00110   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
00111   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
00112 
00113   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00114 
00115   TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
00116   TIM_OC2Init(TIM1, &TIM_OCInitStructure);
00117 
00118   TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
00119   TIM_OC3Init(TIM1, &TIM_OCInitStructure);
00120 
00121   TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
00122   TIM_OC4Init(TIM1, &TIM_OCInitStructure);
00123 
00124   /* TIM1 counter enable */
00125   TIM_Cmd(TIM1, ENABLE);
00126 
00127   /* TIM1 Main Output Enable */
00128   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00129 
00130   while (1)
00131   {}
00132 }
00133 
00134 /**
00135   * @brief  Configures the different system clocks.
00136   * @param  None
00137   * @retval None
00138   */
00139 void RCC_Configuration(void)
00140 {
00141   /* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */
00142   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|
00143                          RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
00144 }
00145 
00146 /**
00147   * @brief  Configure the TIM1 Pins.
00148   * @param  None
00149   * @retval None
00150   */
00151 void GPIO_Configuration(void)
00152 {
00153   GPIO_InitTypeDef GPIO_InitStructure;
00154 
00155 #ifdef STM32F10X_CL
00156   /* GPIOE Configuration: Channel 1/1N, 2/2N, 3/3N and 4 as alternate function push-pull */
00157   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14|
00158                                 GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_12;
00159   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00160   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00161 
00162   GPIO_Init(GPIOE, &GPIO_InitStructure);
00163 
00164   /* TIM1 Full remapping pins */
00165   GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); 
00166 
00167 #else
00168   /* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */
00169   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
00170   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00171   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00172   GPIO_Init(GPIOA, &GPIO_InitStructure);
00173 
00174   /* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */
00175   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
00176   GPIO_Init(GPIOB, &GPIO_InitStructure);
00177 
00178 #endif
00179 }
00180 
00181 #ifdef  USE_FULL_ASSERT
00182 
00183 /**
00184   * @brief  Reports the name of the source file and the source line number
00185   *         where the assert_param error has occurred.
00186   * @param  file: pointer to the source file name
00187   * @param  line: assert_param error line source number
00188   * @retval None
00189   */
00190 void assert_failed(uint8_t* file, uint32_t line)
00191 {
00192   /* User can add his own implementation to report the file name and line number,
00193      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00194 
00195   while (1)
00196   {}
00197 }
00198 #endif
00199 
00200 /**
00201   * @}
00202   */ 
00203 
00204 /**
00205   * @}
00206   */ 
00207 
00208 /******************* (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