STM32F10x_StdPeriph_Examples/TIM/ComplementarySignals/main.c

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