/STM32L1xx_StdPeriph_Lib_V1.1.1/Project/STM32L1xx_StdPeriph_Examples/TIM/PWM_Input/main.c
https://github.com/rurume/stm32-test · C · 149 lines · 40 code · 25 blank · 84 comment · 2 complexity · af791a753fbb6f67bc6dbd111a0c6d38 MD5 · raw file
- /**
- ******************************************************************************
- * @file TIM/PWM_Input/main.c
- * @author MCD Application Team
- * @version V1.1.1
- * @date 13-April-2012
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32l1xx.h"
- /** @addtogroup STM32L1xx_StdPeriph_Examples
- * @{
- */
- /** @addtogroup TIM_PWM_Input
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- TIM_ICInitTypeDef TIM_ICInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- file (startup_stm32l1xx_xx.s) before to branch to application main.
- To reconfigure the default setting of SystemInit() function, refer to
- system_stm32l1xx.c file
- */
-
- /* --------------------------- System Clocks Configuration ---------------------*/
- /* TIM3 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* GPIOA clock enable */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- /*--------------------------------- GPIO Configuration -------------------------*/
- /* GPIOA Configuration: Pin 7 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
- /*-------------------------------- NVIC Configuration --------------------------*/
- /* Enable the TIM3 global Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* TIM3 configuration: PWM Input mode ------------------------
- The external signal is connected to TIM3 CH2 pin (PA.07),
- The Rising edge is used as active edge,
- The TIM3 CCR2 is used to compute the frequency value
- The TIM3 CCR1 is used to compute the duty cycle value
- ------------------------------------------------------------ */
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
- TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
- TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
- TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
- TIM_ICInitStructure.TIM_ICFilter = 0x0;
- TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
- /* Input Trigger selection */
- TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
-
- /* Select the slave Mode: Reset Mode */
- TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
- /* Enable the Master/Slave Mode */
- TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE);
- /* Enable the CC2 Interrupt Request */
- TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
- while (1);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- while (1)
- {}
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/