STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/TIM/TIM_OnePulse/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file TIM/TIM_OnePulse/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>© 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_OnePulse 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 uint16_t PrescalerValue = 0; 00044 00045 /* Private function prototypes -----------------------------------------------*/ 00046 static void TIM_Config(void); 00047 00048 /* Private functions ---------------------------------------------------------*/ 00049 00050 /** 00051 * @brief Main program. 00052 * @param None 00053 * @retval None 00054 */ 00055 int main(void) 00056 { 00057 /*!< At this stage the microcontroller clock setting is already configured, 00058 this is done through SystemInit() function which is called from startup 00059 file (startup_stm32f0xx.s) before to branch to application main. 00060 To reconfigure the default setting of SystemInit() function, refer to 00061 system_stm32f0xx.c file 00062 */ 00063 00064 /* TIM Configuration */ 00065 TIM_Config(); 00066 00067 /* Infinite loop */ 00068 while (1) 00069 { 00070 } 00071 } 00072 00073 /** 00074 * @brief Configure the TIM2 Pins. 00075 * @param None 00076 * @retval None 00077 */ 00078 static void TIM_Config(void) 00079 { 00080 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 00081 TIM_ICInitTypeDef TIM_ICInitStructure; 00082 TIM_OCInitTypeDef TIM_OCInitStructure; 00083 GPIO_InitTypeDef GPIO_InitStructure; 00084 00085 /* TIM4 clock enable */ 00086 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 00087 00088 /* GPIOA clock enable */ 00089 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); 00090 00091 /* TIM2_CH1 pin (PA.05) and TIM2_CH2 pin (PA.01) configuration */ 00092 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5; 00093 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00094 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00095 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 00096 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 00097 GPIO_Init(GPIOA, &GPIO_InitStructure); 00098 00099 /* Connect TIM pins to AF2 */ 00100 GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_2); 00101 GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_2); 00102 00103 /* -------------------------------------------------------------------------- 00104 TIM2 configuration: One Pulse mode 00105 The external signal is connected to TIM2_CH2 pin (PA.01), 00106 The Rising edge is used as active edge, 00107 The One Pulse signal is output on TIM2_CH1 pin (PA.05) 00108 The TIM_Pulse defines the delay value 00109 The (TIM_Period - TIM_Pulse) defines the One Pulse value. 00110 00111 TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1) 00112 TIM2CLK = PCLK1 00113 PCLK1 = HCLK 00114 => TIM2CLK = HCLK = SystemCoreClock 00115 00116 TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz: 00117 Prescaler = (TIM2CLK / TIM2 counter clock) - 1 00118 Prescaler = (SystemCoreClock /24 MHz) - 1 00119 00120 The Autoreload value is 65535 (TIM2->ARR), so the maximum frequency value 00121 to trigger the TIM2 input is 24000000/65535 = 366.2 Hz. 00122 00123 The TIM_Pulse defines the delay value, this value is fixed to: 00124 delay = CCR1/TIM2 counter clock = 682.6 us. 00125 00126 The (TIM_Period - TIM_Pulse) defines the One Pulse value, this value is fixed to: 00127 One Pulse value = (TIM_Period - TIM_Pulse) / TIM2 counter clock = 2.04 ms. 00128 00129 Note: 00130 SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f0xx.c file. 00131 Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() 00132 function to update SystemCoreClock variable value. Otherwise, any configuration 00133 based on this variable will be incorrect. 00134 00135 --------------------------------------------------------------------------- */ 00136 00137 /* Compute the prescaler value */ 00138 PrescalerValue = (uint16_t) ((SystemCoreClock ) / 24000000) - 1; 00139 00140 /* Time base configuration */ 00141 TIM_TimeBaseStructure.TIM_Period = 65535; 00142 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; 00143 TIM_TimeBaseStructure.TIM_ClockDivision = 0; 00144 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 00145 00146 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 00147 00148 /* TIM2 PWM2 Mode configuration: Channel1 */ 00149 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 00150 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 00151 TIM_OCInitStructure.TIM_Pulse = 16383; 00152 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 00153 00154 TIM_OC1Init(TIM2, &TIM_OCInitStructure); 00155 00156 /* TIM4 configuration in Input Capture Mode */ 00157 00158 TIM_ICStructInit(&TIM_ICInitStructure); 00159 00160 TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; 00161 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; 00162 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 00163 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 00164 TIM_ICInitStructure.TIM_ICFilter = 0; 00165 00166 TIM_ICInit(TIM2, &TIM_ICInitStructure); 00167 00168 /* One Pulse Mode selection */ 00169 TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single); 00170 00171 /* Input Trigger selection */ 00172 TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2); 00173 00174 /* Slave Mode selection: Trigger Mode */ 00175 TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger); 00176 00177 } 00178 00179 #ifdef USE_FULL_ASSERT 00180 00181 /** 00182 * @brief Reports the name of the source file and the source line number 00183 * where the assert_param error has occurred. 00184 * @param file: pointer to the source file name 00185 * @param line: assert_param error line source number 00186 * @retval None 00187 */ 00188 void assert_failed(uint8_t* file, uint32_t line) 00189 { 00190 /* User can add his own implementation to report the file name and line number, 00191 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00192 00193 /* Infinite loop */ 00194 while (1) 00195 { 00196 } 00197 } 00198 #endif 00199 00200 /** 00201 * @} 00202 */ 00203 00204 /** 00205 * @} 00206 */ 00207 00208 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/