STM32F10x_StdPeriph_Examples/TIM/6Steps/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/6Steps/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_6Steps
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 CCR1_Val = 32767;
00040 uint16_t CCR2_Val = 24575;
00041 uint16_t CCR3_Val = 16383;
00042 uint16_t CCR4_Val = 8191;
00043 
00044 /* Private function prototypes -----------------------------------------------*/
00045 void RCC_Configuration(void);
00046 void GPIO_Configuration(void);
00047 void SysTick_Configuration(void);
00048 void NVIC_Configuration(void);
00049 
00050 /* Private functions ---------------------------------------------------------*/
00051 
00052 /**
00053   * @brief   Main program
00054   * @param  None
00055   * @retval None
00056   */
00057 int main(void)
00058 {
00059   /*!< At this stage the microcontroller clock setting is already configured, 
00060        this is done through SystemInit() function which is called from startup
00061        file (startup_stm32f10x_xx.s) before to branch to application main.
00062        To reconfigure the default setting of SystemInit() function, refer to
00063        system_stm32f10x.c file
00064      */     
00065            
00066   /* System Clocks Configuration */
00067   RCC_Configuration();
00068 
00069   /* NVIC Configuration */
00070   NVIC_Configuration();
00071 
00072   /* GPIO Configuration */
00073   GPIO_Configuration();
00074 
00075   /* SysTick Configuration */
00076   SysTick_Configuration();
00077 
00078   /*-----------------------------------------------------------------------------
00079   The STM32F10x TIM1 peripheral offers the possibility to program in advance the 
00080   configuration for the next TIM1 outputs behaviour (step) and change the configuration
00081   of all the channels at the same time. This operation is possible when the COM 
00082   (commutation) event is used.
00083   The COM event can be generated by software by setting the COM bit in the TIM1_EGR
00084   register or by hardware (on TRC rising edge).
00085   In this example, a software COM event is generated each 100 ms: using the Systick 
00086   interrupt.
00087   The TIM1 is configured in Timing Mode, each time a COM event occurs, 
00088   a new TIM1 configuration will be set in advance.
00089   The following Table  describes the TIM1 Channels states:
00090               -----------------------------------------------
00091              | Step1 | Step2 | Step3 | Step4 | Step5 | Step6 |
00092    ----------------------------------------------------------
00093   |Channel1  |   1   |   0   |   0   |   0   |   0   |   1   |
00094    ----------------------------------------------------------
00095   |Channel1N |   0   |   0   |   1   |   1   |   0   |   0   |
00096    ----------------------------------------------------------
00097   |Channel2  |   0   |   0   |   0   |   1   |   1   |   0   |
00098    ----------------------------------------------------------
00099   |Channel2N |   1   |   1   |   0   |   0   |   0   |   0   |
00100    ----------------------------------------------------------
00101   |Channel3  |   0   |   1   |   1   |   0   |   0   |   0   |
00102    ----------------------------------------------------------
00103   |Channel3N |   0   |   0   |   0   |   0   |   1   |   1   |
00104    ----------------------------------------------------------
00105   -----------------------------------------------------------------------------*/
00106 
00107   /* Time Base configuration */
00108   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00109   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00110   TIM_TimeBaseStructure.TIM_Period = 4095;
00111   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00112   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
00113 
00114   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00115 
00116   /* Channel 1, 2,3 and 4 Configuration in PWM mode */
00117   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
00118   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00119   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
00120   TIM_OCInitStructure.TIM_Pulse = 2047;
00121   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00122   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
00123   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
00124   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
00125 
00126   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00127 
00128   TIM_OCInitStructure.TIM_Pulse = 1023;
00129   TIM_OC2Init(TIM1, &TIM_OCInitStructure);
00130 
00131   TIM_OCInitStructure.TIM_Pulse = 511;
00132   TIM_OC3Init(TIM1, &TIM_OCInitStructure);
00133 
00134   /* Automatic Output enable, Break, dead time and lock configuration*/
00135   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
00136   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
00137   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
00138   TIM_BDTRInitStructure.TIM_DeadTime = 1;
00139   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
00140   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
00141   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
00142 
00143   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
00144 
00145   TIM_CCPreloadControl(TIM1, ENABLE);
00146 
00147   TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);
00148 
00149   /* TIM1 counter enable */
00150   TIM_Cmd(TIM1, ENABLE);
00151 
00152   /* Main Output Enable */
00153   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00154 
00155   while (1)
00156   {}
00157 }
00158 
00159 /**
00160   * @brief  Configures the different system clocks.
00161   * @param  None
00162   * @retval None
00163   */
00164 void RCC_Configuration(void)
00165 {  
00166   /* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */
00167   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|
00168                          RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
00169 }
00170 
00171 /**
00172   * @brief  Configure the TIM1 Pins.
00173   * @param  None
00174   * @retval None
00175   */
00176 void GPIO_Configuration(void)
00177 {
00178   GPIO_InitTypeDef GPIO_InitStructure;
00179 
00180 #ifdef STM32F10X_CL
00181   /* GPIOE Configuration: Channel 1/1N, 2/2N, 3/3N as alternate function push-pull */
00182   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|
00183                                 GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_12;
00184   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00185   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00186 
00187   GPIO_Init(GPIOE, &GPIO_InitStructure);
00188 
00189   /* GPIOE Configuration: BKIN pin */
00190   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
00191   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00192   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00193 
00194   GPIO_Init(GPIOE, &GPIO_InitStructure);
00195 
00196   /* TIM1 Full remapping pins */
00197   GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); 
00198 
00199 #else
00200   /* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */
00201   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
00202   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
00203   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00204   GPIO_Init(GPIOA, &GPIO_InitStructure);
00205 
00206   /* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */
00207   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
00208   GPIO_Init(GPIOB, &GPIO_InitStructure);
00209 
00210   /* GPIOB Configuration: BKIN pin */
00211   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
00212   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
00213   GPIO_Init(GPIOB, &GPIO_InitStructure);  
00214 #endif
00215 }
00216 
00217 /**
00218   * @brief  Configures the SysTick.
00219   * @param  None
00220   * @retval None
00221   */
00222 void SysTick_Configuration(void)
00223 {
00224   /* Setup SysTick Timer for 100 msec interrupts  */
00225   if (SysTick_Config((SystemCoreClock) / 10))
00226   { 
00227     /* Capture error */ 
00228     while (1);
00229   }
00230 
00231    NVIC_SetPriority(SysTick_IRQn, 0x0);
00232 }
00233 
00234 /**
00235   * @brief  Configures the nested vectored interrupt controller.
00236   * @param  None
00237   * @retval None
00238   */
00239 void NVIC_Configuration(void)
00240 {
00241   NVIC_InitTypeDef NVIC_InitStructure;
00242 
00243   /* Enable the TIM1 Interrupt */
00244 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00245   NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_TIM17_IRQn;
00246 #else
00247   NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;
00248 #endif
00249   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00250   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
00251   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00252   NVIC_Init(&NVIC_InitStructure); 
00253 
00254 }
00255 
00256 #ifdef  USE_FULL_ASSERT
00257 
00258 /**
00259   * @brief  Reports the name of the source file and the source line number
00260   *         where the assert_param error has occurred.
00261   * @param  file: pointer to the source file name
00262   * @param  line: assert_param error line source number
00263   * @retval None
00264   */
00265 void assert_failed(uint8_t* file, uint32_t line)
00266 {
00267   /* User can add his own implementation to report the file name and line number,
00268      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00269 
00270   while (1)
00271   {}
00272 }
00273 #endif
00274 /**
00275   * @}
00276   */ 
00277 
00278 /**
00279   * @}
00280   */ 
00281 
00282 /******************* (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