STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_OCActive/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_OCActive/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_OCActive
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 uint16_t CCR1_Val = 1000;
00044 uint16_t CCR2_Val = 500;
00045 uint16_t CCR3_Val = 250;
00046 uint16_t CCR4_Val = 125;
00047 uint16_t PrescalerValue = 0;
00048 
00049 /* Private function prototypes -----------------------------------------------*/
00050 static void TIM_Config(void);
00051 
00052 /* Private functions ---------------------------------------------------------*/
00053 
00054 /**
00055   * @brief  Main program.
00056   * @param  None
00057   * @retval None
00058   */
00059 int main(void)
00060 {
00061   /*!< At this stage the microcontroller clock setting is already configured, 
00062        this is done through SystemInit() function which is called from startup
00063        file (startup_stm32f0xx.s) before to branch to application main.
00064        To reconfigure the default setting of SystemInit() function, refer to
00065        system_stm32f0xx.c file
00066      */ 
00067 
00068   /* TIM Configuration */
00069   TIM_Config();
00070 
00071   /* Turn on LED1 */
00072   STM_EVAL_LEDOn(LED1);
00073 
00074   /* Infinite loop */
00075   while (1)
00076   {
00077   }
00078 }
00079 
00080 /**
00081   * @brief  Configure the TIM3 pins.
00082   * @param  None
00083   * @retval None
00084   */
00085 static void TIM_Config(void)
00086 {
00087   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
00088   TIM_OCInitTypeDef  TIM_OCInitStructure;
00089   GPIO_InitTypeDef GPIO_InitStructure;
00090 
00091   /* TIM3 clock enable */
00092   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
00093 
00094   /* GPIOC clock enable */
00095   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
00096    
00097   /* GPIOA Configuration: TIM3 CH1 (PA6) and TIM3 CH2 (PA7) */
00098   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
00099   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00100   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00101   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
00102   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
00103   GPIO_Init(GPIOA, &GPIO_InitStructure); 
00104 
00105   /* GPIOB Configuration: TIM3 CH3 (PB0) and TIM3 CH4 (PB1) */
00106   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
00107   GPIO_Init(GPIOB, &GPIO_InitStructure);
00108     
00109   /* Connect TIM Channels to AF2 */
00110   GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
00111   GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1); 
00112   GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);
00113   GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
00114 
00115   /* Initialize Leds mounted on STM320518-EVAL board */
00116   STM_EVAL_LEDInit(LED1);  
00117   
00118   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
00119 
00120   TIM_OCStructInit(&TIM_OCInitStructure);
00121 
00122   /* ---------------------------------------------------------------------------
00123     TIM3 Configuration: Output Compare Active Mode:
00124     In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1)    
00125       TIM3CLK = PCLK1  
00126       PCLK1 = HCLK 
00127       => TIM3CLK = HCLK = SystemCoreClock 
00128           
00129     To get TIM3 counter clock at 1 KHz, the prescaler is computed as follows:
00130        Prescaler = (TIM3CLK / TIM3 counter clock) - 1
00131        Prescaler = (SystemCoreClock /1 KHz) - 1
00132        
00133     Generate 4 signals with 4 different delays:
00134     TIM3_CH1 delay = CCR1_Val/TIM3 counter clock = 1000 ms
00135     TIM3_CH2 delay = CCR2_Val/TIM3 counter clock = 500 ms
00136     TIM3_CH3 delay = CCR3_Val/TIM3 counter clock = 250 ms
00137     TIM3_CH4 delay = CCR4_Val/TIM3 counter clock = 125 ms
00138 
00139     Note: 
00140      SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
00141      Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
00142      function to update SystemCoreClock variable value. Otherwise, any configuration
00143      based on this variable will be incorrect. 
00144      
00145   --------------------------------------------------------------------------- */
00146   
00147   /*Compute the prescaler value */
00148   PrescalerValue = (uint16_t) (SystemCoreClock / 1000) - 1;
00149 
00150   /* Time base configuration */
00151   TIM_TimeBaseStructure.TIM_Period = 65535;
00152   TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
00153   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
00154   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
00155 
00156   TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
00157 
00158   /* Output Compare Active Mode configuration: Channel1 */
00159   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
00160   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
00161   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
00162   TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
00163   TIM_OC1Init(TIM3, &TIM_OCInitStructure);
00164 
00165   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
00166   TIM_ARRPreloadConfig(TIM3, DISABLE); 
00167   /* Output Compare Active Mode configuration: Channel2 */
00168   TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
00169   TIM_OC2Init(TIM3, &TIM_OCInitStructure);
00170 
00171   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
00172 
00173   /* Output Compare Active Mode configuration: Channel3 */
00174   TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
00175   TIM_OC3Init(TIM3, &TIM_OCInitStructure);
00176 
00177   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
00178 
00179   /* Output Compare Active Mode configuration: Channel4 */
00180   TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
00181   TIM_OC4Init(TIM3, &TIM_OCInitStructure);
00182 
00183   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
00184  
00185   /* TIM3 enable counter */
00186   TIM_Cmd(TIM3, ENABLE);
00187   
00188   TIM_GenerateEvent(TIM3, TIM_EventSource_Update);
00189 }
00190 
00191 #ifdef  USE_FULL_ASSERT
00192 
00193 /**
00194   * @brief  Reports the name of the source file and the source line number
00195   *         where the assert_param error has occurred.
00196   * @param  file: pointer to the source file name
00197   * @param  line: assert_param error line source number
00198   * @retval None
00199   */
00200 void assert_failed(uint8_t* file, uint32_t line)
00201 { 
00202   /* User can add his own implementation to report the file name and line number,
00203      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00204 
00205   /* Infinite loop */
00206   while (1)
00207   {
00208   }
00209 }
00210 #endif
00211 
00212 /**
00213   * @}
00214   */
00215 
00216 /**
00217   * @}
00218   */
00219 
00220 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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