PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/taxi_v1.2/Project/Examples/TIM/OnePulse/main.c

https://github.com/ymqqqqdx/n2_taximeter
C | 186 lines | 57 code | 35 blank | 94 comment | 2 complexity | ff659a41c5e305f3cbff88fbb8327987 MD5 | raw file
  1. /**
  2. ******************************************************************************
  3. * @file TIM/OnePulse/main.c
  4. * @author MCD Application Team
  5. * @version V3.0.0
  6. * @date 04/06/2009
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x.h"
  22. /** @addtogroup StdPeriph_Examples
  23. * @{
  24. */
  25. /** @addtogroup TIM_OnePulse
  26. * @{
  27. */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* Private define ------------------------------------------------------------*/
  30. /* Private macro -------------------------------------------------------------*/
  31. /* Private variables ---------------------------------------------------------*/
  32. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  33. TIM_ICInitTypeDef TIM_ICInitStructure;
  34. TIM_OCInitTypeDef TIM_OCInitStructure;
  35. /* Private function prototypes -----------------------------------------------*/
  36. void RCC_Configuration(void);
  37. void GPIO_Configuration(void);
  38. /* Private functions ---------------------------------------------------------*/
  39. /**
  40. * @brief Main program
  41. * @param None
  42. * @retval : None
  43. */
  44. int main(void)
  45. {
  46. /* System Clocks Configuration */
  47. RCC_Configuration();
  48. /* Configure the GPIO ports */
  49. GPIO_Configuration();
  50. /* TIM2 configuration: One Pulse mode ------------------------
  51. The external signal is connected to TIM2_CH2 pin (PA.01),
  52. The Rising edge is used as active edge,
  53. The One Pulse signal is output on TIM2_CH1 pin (PA.00)
  54. The TIM_Pulse defines the delay value
  55. The (TIM_Period - TIM_Pulse) defines the One Pulse value.
  56. The TIM2CLK is fixed to 72 MHz, the Prescaler is 1, so the
  57. TIM2 counter clock is 36 MHz.
  58. The Autoreload value is 65535 (TIM2->ARR), so the maximum
  59. frequency value to trigger the TIM2 input is 500 Hz.
  60. The TIM_Pulse defines the delay value, the delay value is fixed
  61. to 455.08 us:
  62. delay = CCR1/TIM2 counter clock = 455.08 us.
  63. The (TIM_Period - TIM_Pulse) defines the One Pulse value,
  64. the pulse value is fixed to 1.365ms:
  65. One Pulse value = (TIM_Period - TIM_Pulse)/TIM2 counter clock = 1.365 ms.
  66. ------------------------------------------------------------ */
  67. /* Time base configuration */
  68. TIM_TimeBaseStructure.TIM_Period = 65535;
  69. TIM_TimeBaseStructure.TIM_Prescaler = 1;
  70. TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  71. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  72. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  73. /* TIM2 PWM2 Mode configuration: Channel1 */
  74. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  75. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  76. TIM_OCInitStructure.TIM_Pulse = 16383;
  77. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  78. TIM_OC1Init(TIM2, &TIM_OCInitStructure);
  79. /* TIM2 configuration in Input Capture Mode */
  80. TIM_ICStructInit(&TIM_ICInitStructure);
  81. TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  82. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  83. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  84. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  85. TIM_ICInitStructure.TIM_ICFilter = 0;
  86. TIM_ICInit(TIM2, &TIM_ICInitStructure);
  87. /* One Pulse Mode selection */
  88. TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
  89. /* Input Trigger selection */
  90. TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
  91. /* Slave Mode selection: Trigger Mode */
  92. TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);
  93. while (1)
  94. {}
  95. }
  96. /**
  97. * @brief Configures the different system clocks.
  98. * @param None
  99. * @retval : None
  100. */
  101. void RCC_Configuration(void)
  102. {
  103. /* Setup the microcontroller system. Initialize the Embedded Flash Interface,
  104. initialize the PLL and update the SystemFrequency variable. */
  105. SystemInit();
  106. /* TIM2 clock enable */
  107. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  108. /* GPIOA clock enable */
  109. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  110. }
  111. /**
  112. * @brief Configure the GPIOD Pins.
  113. * @param None
  114. * @retval : None
  115. */
  116. void GPIO_Configuration(void)
  117. {
  118. GPIO_InitTypeDef GPIO_InitStructure;
  119. /* TIM2_CH1 pin (PA.00) configuration */
  120. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  121. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  122. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  123. GPIO_Init(GPIOA, &GPIO_InitStructure);
  124. /* TIM2_CH2 pin (PA.01) configuration */
  125. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  126. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  127. GPIO_Init(GPIOA, &GPIO_InitStructure);
  128. }
  129. #ifdef USE_FULL_ASSERT
  130. /**
  131. * @brief Reports the name of the source file and the source line number
  132. * where the assert_param error has occurred.
  133. * @param file: pointer to the source file name
  134. * @param line: assert_param error line source number
  135. * @retval : None
  136. */
  137. void assert_failed(uint8_t* file, uint32_t line)
  138. {
  139. /* User can add his own implementation to report the file name and line number,
  140. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  141. while (1)
  142. {}
  143. }
  144. #endif
  145. /**
  146. * @}
  147. */
  148. /**
  149. * @}
  150. */
  151. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/