2013-03-02 01:52 PM
Hello,
In my project i need to measure the frequency of 8 external signals with the STM32F4 Discovery Board. So i begin with the first signal by using Timer 1 in Input Capture mode and it works. Now i'm trying to do this with Timer 2 by applying the same configuration but it don't work.The code of Timer 1 is the following:
&sharpinclude ''stm32f4xx.h''
&sharpinclude ''stm32f4_discovery.h''
TIM_ICInitTypeDef TIM_ICInitStructure;
void TIM_Config(void);
int main(void)
{ TIM_Config(); 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(TIM1, &TIM_ICInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
while (1); } void TIM_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOE, &GPIO_InitStructure);GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }The code of the IRQHandler :
void TIM1_CC_IRQHandler(void)
{ if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET) { /* Clear TIM1 Capture compare interrupt pending bit */ TIM_ClearITPendingBit(TIM1, TIM_IT_CC2); if(CaptureNumber == 0) { /* Get the Input Capture value */ IC3ReadValue1 = TIM_GetCapture2(TIM1); CaptureNumber = 1; } else if(CaptureNumber == 1) { /* Get the Input Capture value */ IC3ReadValue2 = TIM_GetCapture2(TIM1); /* Capture computation */ if (IC3ReadValue2 > IC3ReadValue1) { Capture = (IC3ReadValue2 - IC3ReadValue1); } else if (IC3ReadValue2 < IC3ReadValue1) { Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); } else { Capture = 0; } /* Frequency computation */ TIM1Freq = (uint32_t) SystemCoreClock / Capture; CaptureNumber = 0; } } }The same code with Timer 2 is:
&sharpinclude ''stm32f4xx.h''
&sharpinclude ''stm32f4_discovery.h'' TIM_ICInitTypeDef TIM_ICInitStructure; void TIM_Config(void); int main(void) { TIM_Config(); 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_Cmd(TIM2, ENABLE); TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); while (1); } void TIM_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2); 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); }The IRQHandler of Timer 2 is:
void TIM2_IRQHandler(void)
{ 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 if (IC3ReadValue2 < IC3ReadValue1) { Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2); } else { Capture = 0; } /* Frequency computation */ TIM1Freq = (uint32_t) SystemCoreClock / Capture; CaptureNumber = 0; } } } I don't know what's wrong and what i have to do to make the Timer 2 working. Thanks. #stm32f4discovery #stm32-timer #pulse-input2013-09-16 06:53 AM
2013-09-16 07:02 AM
2013-09-16 07:19 AM
The key point to understand is that the CCRx registers latch the value in CNT at the edge event(s). The tick units of the timebase are defined by the Prescaler value divided against the APBx clock of the TIM unit.
Both examples should illustrate the modes in question, and be used in collaboration with the Reference Manual descriptions.2015-07-02 01:04 AM
Hi.
I use TIM2 in encoder mode (pins PA0, PA1).I want to use a second timer in capture mode to measure the impulse time on PA0, or PA1.How can I connect another timer (in capture mode) to PA0 or PA1 ?Thanks.2015-07-02 08:31 AM
Woefully off topic.
Don't know, perhaps you can configure CC1 or CC2 as the Trigger output, and slave one of the other timers off those. Or trigger a DMA action against another free-running timer/timebase?Review the manuals, experiment.2015-07-03 01:52 AM
Thankyou Clive.
I think I will use a separate I/O for TIM input capture, and I connect this to encoder output.2015-11-06 07:42 PM
2015-11-07 06:50 AM
Misspelled the name of the handler, and referencing TIM6?
void TIM2_IQRHandler(){ if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET){ TIM_ClearITPendingBit(TIM2,TIM_IT_Update); counter=TIM_GetCounter(TIM6);