STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/TIM/TIM_PWMInput/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    TIM/TIM_PWMInput/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_PWMInput
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 
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_stm32f0xx.s) before to branch to application main.
00058        To reconfigure the default setting of SystemInit() function, refer to
00059        system_stm32f0xx.c file
00060      */ 
00061 
00062   /* TIM Configuration */
00063   TIM_Config();
00064 
00065   /* Infinite loop */
00066   while (1)
00067   {
00068   }
00069 }
00070 
00071 /**
00072   * @brief  Configure the TIM2 Pins.
00073   * @param  None
00074   * @retval None
00075   */
00076 static void TIM_Config(void)
00077 {
00078   TIM_ICInitTypeDef  TIM_ICInitStructure;
00079   GPIO_InitTypeDef GPIO_InitStructure;
00080   NVIC_InitTypeDef NVIC_InitStructure;
00081 
00082   /* TIM2 clock enable */
00083   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
00084 
00085   /* GPIOB clock enable */
00086   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
00087   
00088   /* TIM2 chennel2 configuration : PA.01 */
00089   GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;
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   /* Connect TIM pin to AF2 */
00097   GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2);
00098   
00099     /* Enable the TIM2 global Interrupt */
00100   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
00101   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00102   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00103   NVIC_Init(&NVIC_InitStructure);
00104   
00105  /* --------------------------------------------------------------------------- 
00106     TIM2 configuration: PWM Input mode
00107      The external signal is connected to TIM2 CH2 pin (PA.01)
00108      TIM2 CCR2 is used to compute the frequency value 
00109      TIM2 CCR1 is used to compute the duty cycle value
00110 
00111     In this example TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1), since
00112     APB1 prescaler is set to 1.
00113       TIM2CLK = PCLK1 = HCLK = SystemCoreClock
00114 
00115     External Signal Frequency = SystemCoreClock / TIM2_CCR2 in Hz.
00116     External Signal DutyCycle = (TIM2_CCR1*100)/(TIM2_CCR2) in %.
00117   Note: 
00118   SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file.
00119   Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
00120   function to update SystemCoreClock variable value. Otherwise, any configuration
00121   based on this variable will be incorrect.
00122   --------------------------------------------------------------------------- */
00123   
00124   TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
00125   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
00126   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
00127   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
00128   TIM_ICInitStructure.TIM_ICFilter = 0x0;
00129 
00130   TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
00131 
00132   /* Select the TIM2 Input Trigger: TI2FP2 */
00133   TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
00134 
00135   /* Select the slave Mode: Reset Mode */
00136   TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
00137   TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
00138 
00139   /* TIM enable counter */
00140   TIM_Cmd(TIM2, ENABLE);
00141 
00142   /* Enable the CC2 Interrupt Request */
00143    TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
00144 
00145 }
00146 
00147 #ifdef  USE_FULL_ASSERT
00148 
00149 /**
00150   * @brief  Reports the name of the source file and the source line number
00151   *         where the assert_param error has occurred.
00152   * @param  file: pointer to the source file name
00153   * @param  line: assert_param error line source number
00154   * @retval None
00155   */
00156 void assert_failed(uint8_t* file, uint32_t line)
00157 { 
00158   /* User can add his own implementation to report the file name and line number,
00159      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00160 
00161   /* Infinite loop */
00162   while (1)
00163   {
00164   }
00165 }
00166 #endif
00167 
00168 /**
00169   * @}
00170   */
00171 
00172 /**
00173   * @}
00174   */
00175 
00176 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

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