#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); } } }