STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_6Steps/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_6Steps/main.c  
00004   * @author  MCD Application Team
00005   * @version V1.4.0
00006   * @date    24-July-2014
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "main.h"
00030 
00031 /** @addtogroup STM32F0xx_StdPeriph_Examples
00032   * @{
00033   */
00034 
00035 /** @addtogroup TIM_6Steps
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 /* Private function prototypes -----------------------------------------------*/
00044 static void TIM_Config(void);
00045 static void SysTickConfig(void);
00046 
00047 /* Private functions ---------------------------------------------------------*/
00048 
00049 /**
00050   * @brief  Main program.
00051   * @param  None
00052   * @retval None
00053   */
00054 int main(void)
00055 {
00056   /*!< At this stage the microcontroller clock setting is already configured, 
00057        this is done through SystemInit() function which is called from startup
00058        file (startup_stm32f0xx.s) before to branch to application main.
00059        To reconfigure the default setting of SystemInit() function, refer to
00060        system_stm32f0xx.c file
00061      */ 
00062 
00063   /* SysTick Configuration */
00064   SysTickConfig();
00065 
00066   /* TIM1 Configuration */
00067   TIM_Config();
00068 
00069   /* Infinite loop */
00070   while (1)
00071   {
00072   }
00073 }
00074 
00075 /**
00076   * @brief  Configure the TIM1 Pins.
00077   * @param  None
00078   * @retval None
00079   */
00080 static void TIM_Config(void)
00081 {
00082   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00083   TIM_OCInitTypeDef  TIM_OCInitStructure;
00084   TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
00085   GPIO_InitTypeDef GPIO_InitStructure;
00086   NVIC_InitTypeDef NVIC_InitStructure;
00087   
00088   /* GPIOA and GPIOB clocks enable */
00089   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
00090 
00091   /* TIM1 clock enable */
00092   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
00093 
00094   /* GPIOA Configuration: Channel 1, 2, 1N and 3 as alternate function push-pull */
00095   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
00096   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00097   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00098   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00099   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
00100   GPIO_Init(GPIOA, &GPIO_InitStructure);
00101   
00102   /* GPIOB Configuration: Channel 2N and 3N as alternate function push-pull */
00103   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_12;
00104   GPIO_Init(GPIOB, &GPIO_InitStructure);
00105 
00106   /* Connect TIM pins to AF2 */
00107   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);
00108   GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
00109   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
00110   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
00111   GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_2);
00112   GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_2);
00113   GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_2);
00114   
00115   /*----------------------------------------------------------------------------
00116   The STM32F0xx TIM1 peripheral offers the possibility to program in advance the 
00117   configuration for the next TIM1 outputs behaviour (step) and change the configuration
00118   of all the channels at the same time. This operation is possible when the COM 
00119   (commutation) event is used.
00120   The COM event can be generated by software by setting the COM bit in the TIM1_EGR
00121   register or by hardware (on TRC rising edge).
00122   In this example, a software COM event is generated each 100 ms: using the SysTick 
00123   interrupt.
00124   The TIM1 is configured in Timing Mode, each time a COM event occurs, a new TIM1 
00125   configuration will be set in advance.
00126 
00127   The following Table  describes the TIM1 Channels states:
00128               -----------------------------------------------
00129              | Step1 | Step2 | Step3 | Step4 | Step5 | Step6 |
00130    ----------------------------------------------------------
00131   |Channel1  |   1   |   0   |   0   |   0   |   0   |   1   |
00132    ----------------------------------------------------------
00133   |Channel1N |   0   |   0   |   1   |   1   |   0   |   0   |
00134    ----------------------------------------------------------
00135   |Channel2  |   0   |   0   |   0   |   1   |   1   |   0   |
00136    ----------------------------------------------------------
00137   |Channel2N |   1   |   1   |   0   |   0   |   0   |   0   |
00138    ----------------------------------------------------------
00139   |Channel3  |   0   |   1   |   1   |   0   |   0   |   0   |
00140    ----------------------------------------------------------
00141   |Channel3N |   0   |   0   |   0   |   0   |   1   |   1   |
00142    ----------------------------------------------------------
00143   ----------------------------------------------------------------------------*/
00144 
00145   /* Time Base configuration */
00146   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00147   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00148   TIM_TimeBaseStructure.TIM_Period = 4095;
00149   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00150   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
00151 
00152   TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
00153 
00154   /* Channel 1, 2,3 and 4 Configuration in PWM mode */
00155   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
00156   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00157   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
00158   TIM_OCInitStructure.TIM_Pulse = 2047;
00159   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00160   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
00161   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
00162   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
00163 
00164   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00165 
00166   TIM_OCInitStructure.TIM_Pulse = 1023;
00167   TIM_OC2Init(TIM1, &TIM_OCInitStructure);
00168 
00169   TIM_OCInitStructure.TIM_Pulse = 511;
00170   TIM_OC3Init(TIM1, &TIM_OCInitStructure);
00171 
00172   /* Automatic Output enable, Break, dead time and lock configuration*/
00173   TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
00174   TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
00175   TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
00176   TIM_BDTRInitStructure.TIM_DeadTime = 1;
00177   TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
00178   TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
00179   TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
00180 
00181   TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
00182 
00183   TIM_CCPreloadControl(TIM1, ENABLE);
00184 
00185   TIM_ITConfig(TIM1, TIM_IT_COM, ENABLE);
00186 
00187   /* TIM1 counter enable */
00188   TIM_Cmd(TIM1, ENABLE);
00189 
00190   /* Main Output Enable */
00191   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00192 
00193   /* Enable the TIM1 Trigger and commutation interrupt */
00194   NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;
00195   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00196   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00197   NVIC_Init(&NVIC_InitStructure);   
00198 }
00199 
00200 /**
00201   * @brief  Configures the SysTick.
00202   * @param  None
00203   * @retval None
00204   */
00205 static void SysTickConfig(void)
00206 {
00207   /* Setup SysTick Timer for 100 msec interrupts  */
00208   if (SysTick_Config((SystemCoreClock) / 10))
00209   { 
00210     /* Capture error */ 
00211     while (1);
00212   }
00213 
00214   NVIC_SetPriority(SysTick_IRQn, 0x0);
00215 }
00216 
00217 #ifdef  USE_FULL_ASSERT
00218 
00219 /**
00220   * @brief  Reports the name of the source file and the source line number
00221   *         where the assert_param error has occurred.
00222   * @param  file: pointer to the source file name
00223   * @param  line: assert_param error line source number
00224   * @retval None
00225   */
00226 void assert_failed(uint8_t* file, uint32_t line)
00227 { 
00228   /* User can add his own implementation to report the file name and line number,
00229      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00230 
00231   /* Infinite loop */
00232   while (1)
00233   {
00234   }
00235 }
00236 #endif
00237 
00238 /**
00239   * @}
00240   */
00241 
00242 /**
00243   * @}
00244   */
00245 
00246 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32