STM32F10x_StdPeriph_Examples/TIM/TIM10_PWMOutput/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM10_PWMOutput/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 TIM10_PWMOutput
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 CCR1Val = 249;
00039 uint16_t PrescalerValue = 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   /* GPIO Configuration */
00062   GPIO_Configuration();
00063 
00064   /* -----------------------------------------------------------------------
00065     TIM10 Configuration: generate 1 PWM signal.
00066     The TIM10CLK frequency is set to SystemCoreClock (72 MHz), to get TIM10 counter
00067     clock at 24 MHz the Prescaler is computed as following:
00068      - Prescaler = (TIM10CLK / TIM10 counter clock) - 1
00069 
00070     The TIM10 is running at 36 KHz: TIM10 Frequency = TIM10 counter clock/(ARR + 1)
00071                                                   = 24 MHz / 666 = 36 KHz
00072     TIM10 Channel1 duty cycle = (TIM10_CCR1/ TIM10_ARR)* 100 = 37.5%
00073   ----------------------------------------------------------------------- */
00074   /* Compute the prescaler value */
00075   PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
00076   /* Time base configuration */
00077   TIM_TimeBaseStructure.TIM_Period = 665;
00078   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00079   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00080   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00081 
00082   TIM_TimeBaseInit(TIM10, &TIM_TimeBaseStructure);
00083 
00084   /* PWM1 Mode configuration: Channel1 */
00085   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
00086   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00087   TIM_OCInitStructure.TIM_Pulse = CCR1Val;
00088   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00089 
00090   TIM_OC1Init(TIM10, &TIM_OCInitStructure);
00091 
00092   TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);
00093 
00094   TIM_ARRPreloadConfig(TIM10, ENABLE);
00095 
00096   /* TIM10 enable counter */
00097   TIM_Cmd(TIM10, ENABLE);
00098 
00099   while (1)
00100   {}
00101 }
00102 
00103 /**
00104   * @brief  Configure TIM10 pin.
00105   * @param  None
00106   * @retval None
00107   */
00108 void GPIO_Configuration(void)
00109 {
00110   GPIO_InitTypeDef GPIO_InitStructure;
00111 
00112   /* Enable TIM10 and GPIOF clock */
00113   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10 | RCC_APB2Periph_GPIOF |
00114                          RCC_APB2Periph_AFIO, ENABLE);
00115 
00116   /* Remap TIM10_CH1 on PF6 pin */
00117   GPIO_PinRemapConfig(GPIO_Remap_TIM10, ENABLE);        
00118   
00119   /* GPIOF Configuration: TIM10 Channel1 as alternate function push-pull */
00120   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
00121   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00122   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00123 
00124   GPIO_Init(GPIOF, &GPIO_InitStructure);
00125 }
00126 
00127 #ifdef  USE_FULL_ASSERT
00128 
00129 /**
00130   * @brief  Reports the name of the source file and the source line number
00131   *         where the assert_param error has occurred.
00132   * @param  file: pointer to the source file name
00133   * @param  line: assert_param error line source number
00134   * @retval None
00135   */
00136 void assert_failed(uint8_t* file, uint32_t line)
00137 {
00138   /* User can add his own implementation to report the file name and line number,
00139      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00140 
00141   while (1)
00142   {}
00143 }
00144 #endif
00145 
00146 /**
00147   * @}
00148   */ 
00149 
00150 /**
00151   * @}
00152   */ 
00153 
00154 /******************* (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