myInputCapture.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "myInputCapture.h"
  2. #include "board.h"
  3. #include "stm32f10x.h"
  4. #include "stm32f10x_adc.h"
  5. #include "stm32f10x_tim.h"
  6. __IO uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
  7. __IO uint16_t CaptureNumber = 0;
  8. __IO uint32_t Capture = 0;
  9. __IO uint32_t TIM3Freq = 0;
  10. CaptureCallback inputCaptureCb;
  11. void myInputCaptureTIM2_CH3_init(CaptureCallback cb)
  12. {
  13. NVIC_InitTypeDef NVIC_InitStructure;
  14. TIM_ICInitTypeDef TIM_ICInitStructure;
  15. /* TIM2 clock enable */
  16. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  17. /* Enable the TIM3 global Interrupt */
  18. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  22. NVIC_Init(&NVIC_InitStructure);
  23. /* TIM3 configuration: Input Capture mode ---------------------
  24. The external signal is connected to TIM3 CH2 pin (PA.07)
  25. The Rising edge is used as active edge,
  26. The TIM3 CCR2 is used to compute the frequency value
  27. ------------------------------------------------------------ */
  28. TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
  29. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  30. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  31. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  32. TIM_ICInitStructure.TIM_ICFilter = 0x0;
  33. TIM_ICInit(TIM2, &TIM_ICInitStructure);
  34. /* TIM enable counter */
  35. TIM_Cmd(TIM2, ENABLE);
  36. /* Enable the CC2 Interrupt Request */
  37. TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
  38. inputCaptureCb = cb;
  39. }
  40. /******************************************************************************/
  41. /* STM32F10x Peripherals Interrupt Handlers */
  42. /******************************************************************************/
  43. /**
  44. * @brief This function handles TIM3 global interrupt request.
  45. * @param None
  46. * @retval None
  47. */
  48. void TIM2_IRQHandler(void)
  49. {
  50. if(TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET)
  51. {
  52. /* Clear TIM2 Capture compare interrupt pending bit */
  53. TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
  54. if(CaptureNumber == 0)
  55. {
  56. /* Get the Input Capture value */
  57. IC3ReadValue1 = TIM_GetCapture3(TIM2);
  58. CaptureNumber = 1;
  59. }
  60. else if(CaptureNumber == 1)
  61. {
  62. /* Get the Input Capture value */
  63. IC3ReadValue2 = TIM_GetCapture3(TIM2);
  64. /* Capture computation */
  65. if (IC3ReadValue2 > IC3ReadValue1)
  66. {
  67. Capture = (IC3ReadValue2 - IC3ReadValue1);
  68. }
  69. else
  70. {
  71. Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
  72. }
  73. /* Frequency computation */
  74. TIM3Freq = (uint32_t) SystemCoreClock / Capture;
  75. CaptureNumber = 0;
  76. inputCaptureCb(IC3ReadValue1, IC3ReadValue2, TIM3Freq);
  77. }
  78. }
  79. }
  80. void myInputCaptureTIM3_CH4_init(CaptureCallback cb)
  81. {
  82. NVIC_InitTypeDef NVIC_InitStructure;
  83. TIM_ICInitTypeDef TIM_ICInitStructure;
  84. /* TIM2 clock enable */
  85. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  86. /* Enable the TIM3 global Interrupt */
  87. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  88. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  89. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  90. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  91. NVIC_Init(&NVIC_InitStructure);
  92. /* TIM3 configuration: Input Capture mode ---------------------
  93. The external signal is connected to TIM3 CH2 pin (PA.07)
  94. The Rising edge is used as active edge,
  95. The TIM3 CCR2 is used to compute the frequency value
  96. ------------------------------------------------------------ */
  97. TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
  98. TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  99. TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  100. TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  101. TIM_ICInitStructure.TIM_ICFilter = 0x0;
  102. TIM_ICInit(TIM3, &TIM_ICInitStructure);
  103. /* TIM enable counter */
  104. TIM_Cmd(TIM3, ENABLE);
  105. /* Enable the CC2 Interrupt Request */
  106. TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);
  107. inputCaptureCb = cb;
  108. }
  109. /******************************************************************************/
  110. /* STM32F10x Peripherals Interrupt Handlers */
  111. /******************************************************************************/
  112. /**
  113. * @brief This function handles TIM3 global interrupt request.
  114. * @param None
  115. * @retval None
  116. */
  117. void TIM3_IRQHandler(void)
  118. {
  119. if(TIM_GetITStatus(TIM3, TIM_IT_CC4) == SET)
  120. {
  121. /* Clear TIM3 Capture compare interrupt pending bit */
  122. TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
  123. if(CaptureNumber == 0)
  124. {
  125. /* Get the Input Capture value */
  126. IC3ReadValue1 = TIM_GetCapture4(TIM3);
  127. CaptureNumber = 1;
  128. }
  129. else if(CaptureNumber == 1)
  130. {
  131. /* Get the Input Capture value */
  132. IC3ReadValue2 = TIM_GetCapture4(TIM3);
  133. /* Capture computation */
  134. if (IC3ReadValue2 > IC3ReadValue1)
  135. {
  136. Capture = (IC3ReadValue2 - IC3ReadValue1);
  137. }
  138. else
  139. {
  140. Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
  141. }
  142. /* Frequency computation */
  143. TIM3Freq = (uint32_t) SystemCoreClock / Capture;
  144. CaptureNumber = 0;
  145. inputCaptureCb(IC3ReadValue1, IC3ReadValue2, TIM3Freq);
  146. }
  147. }
  148. }