/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

  1. /**
  2. ******************************************************************************
  3. * @file TIM/PWM_Input/main.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 13-April-2012
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32l1xx.h"
  29. /** @addtogroup STM32L1xx_StdPeriph_Examples
  30. * @{
  31. */
  32. /** @addtogroup TIM_PWM_Input
  33. * @{
  34. */
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. TIM_ICInitTypeDef TIM_ICInitStructure;
  40. GPIO_InitTypeDef GPIO_InitStructure;
  41. NVIC_InitTypeDef NVIC_InitStructure;
  42. /* Private function prototypes -----------------------------------------------*/
  43. /* Private functions ---------------------------------------------------------*/
  44. /**
  45. * @brief Main program
  46. * @param None
  47. * @retval None
  48. */
  49. int main(void)
  50. {
  51. /*!< At this stage the microcontroller clock setting is already configured,
  52. this is done through SystemInit() function which is called from startup
  53. file (startup_stm32l1xx_xx.s) before to branch to application main.
  54. To reconfigure the default setting of SystemInit() function, refer to
  55. system_stm32l1xx.c file
  56. */
  57. /* --------------------------- System Clocks Configuration ---------------------*/
  58. /* TIM3 clock enable */
  59. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  60. /* GPIOA clock enable */
  61. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  62. /*--------------------------------- GPIO Configuration -------------------------*/
  63. /* GPIOA Configuration: Pin 7 */
  64. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  65. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  66. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  67. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  68. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
  69. GPIO_Init(GPIOA, &GPIO_InitStructure);
  70. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
  71. /*-------------------------------- NVIC Configuration --------------------------*/
  72. /* Enable the TIM3 global Interrupt */
  73. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  74. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  75. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  76. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  77. NVIC_Init(&NVIC_InitStructure);
  78. /* TIM3 configuration: PWM Input mode ------------------------
  79. The external signal is connected to TIM3 CH2 pin (PA.07),
  80. The Rising edge is used as active edge,
  81. The TIM3 CCR2 is used to compute the frequency value
  82. The TIM3 CCR1 is used to compute the duty cycle value
  83. ------------------------------------------------------------ */
  84. TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  85. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  86. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  87. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  88. TIM_ICInitStructure.TIM_ICFilter = 0x0;
  89. TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
  90. /* Input Trigger selection */
  91. TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);
  92. /* Select the slave Mode: Reset Mode */
  93. TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
  94. /* Enable the Master/Slave Mode */
  95. TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
  96. /* TIM3 enable counter */
  97. TIM_Cmd(TIM3, ENABLE);
  98. /* Enable the CC2 Interrupt Request */
  99. TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
  100. while (1);
  101. }
  102. #ifdef USE_FULL_ASSERT
  103. /**
  104. * @brief Reports the name of the source file and the source line number
  105. * where the assert_param error has occurred.
  106. * @param file: pointer to the source file name
  107. * @param line: assert_param error line source number
  108. * @retval None
  109. */
  110. void assert_failed(uint8_t* file, uint32_t line)
  111. {
  112. /* User can add his own implementation to report the file name and line number,
  113. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  114. while (1)
  115. {}
  116. }
  117. #endif
  118. /**
  119. * @}
  120. */
  121. /**
  122. * @}
  123. */
  124. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/