123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "myInputCapture.h"
- #include "board.h"
- #include "stm32f10x.h"
- #include "stm32f10x_adc.h"
- #include "stm32f10x_tim.h"
- __IO uint16_t IC3ReadValue1 = 0, IC3ReadValue2 = 0;
- __IO uint16_t CaptureNumber = 0;
- __IO uint32_t Capture = 0;
- __IO uint32_t TIM3Freq = 0;
- CaptureCallback inputCaptureCb;
- void myInputCaptureTIM2_CH2_init(CaptureCallback cb)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- /* TIM2 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- /* Enable the TIM3 global Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* TIM3 configuration: Input Capture 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
- ------------------------------------------------------------ */
- 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_ICInit(TIM2, &TIM_ICInitStructure);
-
- /* TIM enable counter */
- TIM_Cmd(TIM2, ENABLE);
- /* Enable the CC2 Interrupt Request */
- TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
- inputCaptureCb = cb;
- }
- void myInputCaptureTIM2_CH3_init(CaptureCallback cb)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- /* TIM2 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- /* Enable the TIM3 global Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- /* TIM3 configuration: Input Capture 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
- ------------------------------------------------------------ */
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
- 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_ICInit(TIM2, &TIM_ICInitStructure);
-
- /* TIM enable counter */
- TIM_Cmd(TIM2, ENABLE);
- /* Enable the CC2 Interrupt Request */
- TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
- inputCaptureCb = cb;
- }
- /******************************************************************************/
- /* STM32F10x Peripherals Interrupt Handlers */
- /******************************************************************************/
- /**
- * @brief This function handles TIM3 global interrupt request.
- * @param None
- * @retval None
- */
- void TIM2_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM2, TIM_IT_CC3) == SET)
- {
- /* Clear TIM2 Capture compare interrupt pending bit */
- TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
- if(CaptureNumber == 0)
- {
- /* Get the Input Capture value */
- IC3ReadValue1 = TIM_GetCapture3(TIM2);
- CaptureNumber = 1;
- }
- else if(CaptureNumber == 1)
- {
- /* Get the Input Capture value */
- IC3ReadValue2 = TIM_GetCapture3(TIM2);
-
- /* Capture computation */
- if (IC3ReadValue2 > IC3ReadValue1)
- {
- Capture = (IC3ReadValue2 - IC3ReadValue1);
- }
- else
- {
- Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
- }
- /* Frequency computation */
- TIM3Freq = (uint32_t) SystemCoreClock / Capture;
- CaptureNumber = 0;
- inputCaptureCb(IC3ReadValue1, IC3ReadValue2, TIM3Freq);
- }
- }
- if(TIM_GetITStatus(TIM2, TIM_IT_CC2) == SET)
- {
- /* Clear TIM2 Capture compare interrupt pending bit */
- TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
- if(CaptureNumber == 0)
- {
- /* Get the Input Capture value */
- IC3ReadValue1 = TIM_GetCapture2(TIM2);
- CaptureNumber = 1;
- }
- else if(CaptureNumber == 1)
- {
- /* Get the Input Capture value */
- IC3ReadValue2 = TIM_GetCapture2(TIM2);
-
- /* Capture computation */
- if (IC3ReadValue2 > IC3ReadValue1)
- {
- Capture = (IC3ReadValue2 - IC3ReadValue1);
- }
- else
- {
- Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
- }
- /* Frequency computation */
- TIM3Freq = (uint32_t) SystemCoreClock / Capture;
- CaptureNumber = 0;
- inputCaptureCb(IC3ReadValue1, IC3ReadValue2, TIM3Freq);
- }
- }
- }
- void myInputCaptureTIM3_CH4_init(CaptureCallback cb)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- /* TIM2 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* 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: Input Capture 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
- ------------------------------------------------------------ */
- TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
- 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_ICInit(TIM3, &TIM_ICInitStructure);
-
- /* TIM enable counter */
- TIM_Cmd(TIM3, ENABLE);
- /* Enable the CC2 Interrupt Request */
- TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);
- inputCaptureCb = cb;
- }
- /******************************************************************************/
- /* STM32F10x Peripherals Interrupt Handlers */
- /******************************************************************************/
- /**
- * @brief This function handles TIM3 global interrupt request.
- * @param None
- * @retval None
- */
- void TIM3_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM3, TIM_IT_CC4) == SET)
- {
- /* Clear TIM3 Capture compare interrupt pending bit */
- TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
- if(CaptureNumber == 0)
- {
- /* Get the Input Capture value */
- IC3ReadValue1 = TIM_GetCapture4(TIM3);
- CaptureNumber = 1;
- }
- else if(CaptureNumber == 1)
- {
- /* Get the Input Capture value */
- IC3ReadValue2 = TIM_GetCapture4(TIM3);
-
- /* Capture computation */
- if (IC3ReadValue2 > IC3ReadValue1)
- {
- Capture = (IC3ReadValue2 - IC3ReadValue1);
- }
- else
- {
- Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
- }
- /* Frequency computation */
- TIM3Freq = (uint32_t) SystemCoreClock / Capture;
- CaptureNumber = 0;
- inputCaptureCb(IC3ReadValue1, IC3ReadValue2, TIM3Freq);
- }
- }
- }
|