Question
stm32f103 timer input capture
Hi,
I am trying to read some square signal with timer input capture for both falling and rising edge times. When i start the code, the interrupt routine works once, even if there is no signal. I am using the code below for setup timer and interrupt routine. Any advise ?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
// Time base configuration
TIM_TimeBaseStructure.TIM_Period = 0xFFFE;
TIM_TimeBaseStructure.TIM_Prescaler = 15;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
// Input capture configuration
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
// Enable the TIM1 global interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM1, TIM_IT_CC3, ENABLE);
TIM_Cmd(TIM1, ENABLE);
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)
{
if(capture_value == 0)
{
capture_value = 1;
signalArray[signalIndex++] = TIM_GetCapture3(TIM1);
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling ;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
}
else
{
capture_value = 0;
signalArray[signalIndex++] = TIM_GetCapture3(TIM1);
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
}
TIM_ClearITPendingBit(TIM1, TIM_IT_CC3);
}
}