STM32F10x_StdPeriph_Examples/TIM/PWM_Output/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/PWM_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_PWM_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 CCR1_Val = 333;
00039 uint16_t CCR2_Val = 249;
00040 uint16_t CCR3_Val = 166;
00041 uint16_t CCR4_Val = 83;
00042 uint16_t PrescalerValue = 0;
00043 
00044 /* Private function prototypes -----------------------------------------------*/
00045 void RCC_Configuration(void);
00046 void GPIO_Configuration(void);
00047 
00048 /* Private functions ---------------------------------------------------------*/
00049 
00050 /**
00051   * @brief   Main program
00052   * @param  None
00053   * @retval None
00054   */
00055 int main(void)
00056 {
00057   /*!< At this stage the microcontroller clock setting is already configured, 
00058        this is done through SystemInit() function which is called from startup
00059        file (startup_stm32f10x_xx.s) before to branch to application main.
00060        To reconfigure the default setting of SystemInit() function, refer to
00061        system_stm32f10x.c file
00062      */     
00063        
00064   /* System Clocks Configuration */
00065   RCC_Configuration();
00066 
00067   /* GPIO Configuration */
00068   GPIO_Configuration();
00069 
00070   /* -----------------------------------------------------------------------
00071     TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles:
00072     The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter
00073     clock at 24 MHz the Prescaler is computed as following:
00074      - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
00075     SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
00076     and Connectivity line devices and to 24 MHz for Low-Density Value line and
00077     Medium-Density Value line devices
00078 
00079     The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
00080                                                   = 24 MHz / 666 = 36 KHz
00081     TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
00082     TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
00083     TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
00084     TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
00085   ----------------------------------------------------------------------- */
00086   /* Compute the prescaler value */
00087   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
00088   /* Time base configuration */
00089   TIM_TimeBaseStructure.TIM_Period = 665;
00090   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00091   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00092   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00093 
00094   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00095 
00096   /* PWM1 Mode configuration: Channel1 */
00097   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
00098   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00099   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00100   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00101 
00102   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00103 
00104   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
00105 
00106   /* PWM1 Mode configuration: Channel2 */
00107   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00108   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00109 
00110   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
00111 
00112   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
00113 
00114   /* PWM1 Mode configuration: Channel3 */
00115   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00116   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00117 
00118   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
00119 
00120   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
00121 
00122   /* PWM1 Mode configuration: Channel4 */
00123   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00124   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00125 
00126   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
00127 
00128   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
00129 
00130   TIM_ARRPreloadConfig(TIM3, ENABLE);
00131 
00132   /* TIM3 enable counter */
00133   TIM_Cmd(TIM3, ENABLE);
00134 
00135   while (1)
00136   {}
00137 }
00138 
00139 /**
00140   * @brief  Configures the different system clocks.
00141   * @param  None
00142   * @retval None
00143   */
00144 void RCC_Configuration(void)
00145 {
00146   /* TIM3 clock enable */
00147   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00148 
00149   /* GPIOA and GPIOB clock enable */
00150   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
00151                          RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
00152 }
00153 
00154 /**
00155   * @brief  Configure the TIM3 Ouput Channels.
00156   * @param  None
00157   * @retval None
00158   */
00159 void GPIO_Configuration(void)
00160 {
00161   GPIO_InitTypeDef GPIO_InitStructure;
00162 
00163 #ifdef STM32F10X_CL
00164   /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
00165   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
00166   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00167   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00168 
00169   GPIO_Init(GPIOC, &GPIO_InitStructure);
00170 
00171   GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);     
00172 
00173 #else
00174   /* GPIOA Configuration:TIM3 Channel1, 2, 3 and 4 as alternate function push-pull */
00175   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
00176   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00177   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00178 
00179   GPIO_Init(GPIOA, &GPIO_InitStructure);
00180 
00181   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
00182   GPIO_Init(GPIOB, &GPIO_InitStructure);
00183 #endif
00184 }
00185 
00186 #ifdef  USE_FULL_ASSERT
00187 
00188 /**
00189   * @brief  Reports the name of the source file and the source line number
00190   *         where the assert_param error has occurred.
00191   * @param  file: pointer to the source file name
00192   * @param  line: assert_param error line source number
00193   * @retval None
00194   */
00195 void assert_failed(uint8_t* file, uint32_t line)
00196 {
00197   /* User can add his own implementation to report the file name and line number,
00198      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00199 
00200   while (1)
00201   {}
00202 }
00203 #endif
00204 
00205 /**
00206   * @}
00207   */ 
00208 
00209 /**
00210   * @}
00211   */ 
00212 
00213 /******************* (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