STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_7PWMOutputs/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_7PWMOutputs/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_7PWMOutputs
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 uint16_t TimerPeriod = 0;
00044 uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
00045 
00046 /* Private function prototypes -----------------------------------------------*/
00047 static void TIM_Config(void);
00048 
00049 /* Private functions ---------------------------------------------------------*/
00050 
00051 /**
00052   * @brief  Main program.
00053   * @param  None
00054   * @retval None
00055   */
00056 int main(void)
00057 {
00058   /*!< At this stage the microcontroller clock setting is already configured, 
00059        this is done through SystemInit() function which is called from startup
00060        file (startup_stm32f0xx.s) before to branch to application main.
00061        To reconfigure the default setting of SystemInit() function, refer to
00062        system_stm32f0xx.c file
00063      */ 
00064 
00065   /* TIM Configuration */
00066   TIM_Config();
00067 
00068   /* Infinite loop */
00069   while (1)
00070   {
00071   }
00072 }
00073 
00074 /**
00075   * @brief  Configure the TIM1 Pins.
00076   * @param  None
00077   * @retval None
00078   */
00079 static void TIM_Config(void)
00080 {
00081   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00082   TIM_OCInitTypeDef  TIM_OCInitStructure;
00083   GPIO_InitTypeDef GPIO_InitStructure;
00084 
00085   /* GPIOA, GPIOB and GPIOE Clocks enable */
00086   RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
00087   
00088   /* GPIOA Configuration: Channel 1, 2, 3, 4 and Channel 1N as alternate function push-pull */
00089   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_7;
00090   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00091   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00092   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00093   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
00094   GPIO_Init(GPIOA, &GPIO_InitStructure);
00095   
00096   GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
00097   GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_2);
00098   GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_2);
00099   GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);
00100   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2);
00101     
00102   /* GPIOB Configuration: Channel 2N and 3N as alternate function push-pull */
00103   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
00104   GPIO_Init(GPIOB, &GPIO_InitStructure); 
00105 
00106   GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_2); 
00107   GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_2);
00108   
00109   /* TIM1 Configuration ---------------------------------------------------
00110    Generate 7 PWM signals with 4 different duty cycles:
00111    TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2)    
00112     => TIM1CLK = PCLK2 = SystemCoreClock
00113    TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
00114    SystemCoreClock is set to 48 MHz for STM32F0xx devices
00115    
00116    The objective is to generate 7 PWM signal at 17.57 KHz:
00117      - TIM1_Period = (SystemCoreClock / 17570) - 1
00118    The channel 1 and channel 1N duty cycle is set to 50%
00119    The channel 2 and channel 2N duty cycle is set to 37.5%
00120    The channel 3 and channel 3N duty cycle is set to 25%
00121    The channel 4 duty cycle is set to 12.5%
00122    The Timer pulse is calculated as follows:
00123      - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
00124    
00125    Note: 
00126     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
00127     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
00128     function to update SystemCoreClock variable value. Otherwise, any configuration
00129     based on this variable will be incorrect. 
00130   ----------------------------------------------------------------------- */
00131   /* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */
00132   TimerPeriod = (SystemCoreClock / 17570 ) - 1;
00133   /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */
00134   Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
00135   /* Compute CCR2 value to generate a duty cycle at 37.5%  for channel 2 and 2N */
00136   Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
00137   /* Compute CCR3 value to generate a duty cycle at 25%  for channel 3 and 3N */
00138   Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
00139   /* Compute CCR4 value to generate a duty cycle at 12.5%  for channel 4 */
00140   Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
00141 
00142   /* TIM1 clock enable */
00143   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
00144   
00145   /* Time Base configuration */
00146   TIM_TimeBaseStructure.TIM_Prescaler = 0;
00147   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00148   TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
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_PWM2;
00156   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00157   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
00158   TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
00159   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
00160   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
00161   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
00162   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
00163 
00164   TIM_OC1Init(TIM1, &TIM_OCInitStructure);
00165 
00166   TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
00167   TIM_OC2Init(TIM1, &TIM_OCInitStructure);
00168 
00169   TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
00170   TIM_OC3Init(TIM1, &TIM_OCInitStructure);
00171 
00172   TIM_OCInitStructure.TIM_Pulse = Channel4Pulse;
00173   TIM_OC4Init(TIM1, &TIM_OCInitStructure);
00174 
00175   /* TIM1 counter enable */
00176   TIM_Cmd(TIM1, ENABLE);
00177 
00178   /* TIM1 Main Output Enable */
00179   TIM_CtrlPWMOutputs(TIM1, ENABLE);
00180 }
00181 
00182 #ifdef  USE_FULL_ASSERT
00183 
00184 /**
00185   * @brief  Reports the name of the source file and the source line number
00186   *         where the assert_param error has occurred.
00187   * @param  file: pointer to the source file name
00188   * @param  line: assert_param error line source number
00189   * @retval None
00190   */
00191 void assert_failed(uint8_t* file, uint32_t line)
00192 { 
00193   /* User can add his own implementation to report the file name and line number,
00194      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00195 
00196   /* Infinite loop */
00197   while (1)
00198   {
00199   }
00200 }
00201 #endif
00202 
00203 /**
00204   * @}
00205   */
00206 
00207 /**
00208   * @}
00209   */
00210 
00211 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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