cancel
Showing results for 
Search instead for 
Did you mean: 

Input capture goes to infinite loop

arunl4g
Associate II
Posted on June 09, 2015 at 13:29

hej all,

my program works when i measure but the problem is its goes to the infite loop. i would like to measure and return the value . but i cant write program in that way .

if i change the below program then it wont work .

can anyone help me to get out of this.

thank you ...

i am using stm32f407 discovery board.

the below one is intialization.

/new one

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Prescaler =168 - 1;

TIM_TimeBaseStructure.TIM_Period = 65535;

TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

GPIO_InitTypeDef GPIO_IniStructure;

NVIC_InitTypeDef NVIC_IniStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

GPIO_IniStructure.GPIO_Pin = GPIO_Pin_11;

GPIO_IniStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_IniStructure.GPIO_Speed = GPIO_Speed_100MHz;

GPIO_IniStructure.GPIO_OType = GPIO_OType_PP;

GPIO_IniStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOE, &GPIO_IniStructure);

GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1); //timer alternative function mapping

NVIC_IniStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1 Capture Compare Interrupt

NVIC_IniStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_IniStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_IniStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_IniStructure);

TIM_ICInitTypeDef TIM_ICInitStructure;

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

the below one has to give me proper value but i am not measuring the proper signal .

void TIM1_CC_IRQHandler(void)

{

if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET)

{

TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);

if(CaptureNumber == 0)

{

//sprintf(processcommand, ''%d'', IC3ReadValue2);

//printf( ''IC3ReadValue2 %d \n'', IC3ReadValue1 );

//usart_SEND (USART6,processcommand);

IC3ReadValue1 = TIM_GetCapture2(TIM1);

CaptureNumber = 1;

}

else if(CaptureNumber == 1)

{

IC3ReadValue2 = TIM_GetCapture2(TIM1);

//printf( ''IC3ReadValue2 %d \n'', IC3ReadValue2 );

if (IC3ReadValue2 > IC3ReadValue1)

{

Capture = 0;

Capture = (IC3ReadValue2 - IC3ReadValue1);

}

else if (IC3ReadValue2 < IC3ReadValue1)

{

Capture = 0;

Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);

}

else

{

Capture = 0;

}

TIM1Freq = (uint32_t) SystemCoreClock / Capture;

sprintf(processcommand, ''%d'', TIM1Freq);

usart_SEND (USART6,processcommand);

usart_SEND (USART6,''\n\r'');

//TIM1Freq = (uint32_t) SystemCoreClock / Capture1;

CaptureNumber = 0;

}

}

}

the below diagram shows where it goes in to while loop

2 REPLIES 2
Posted on June 09, 2015 at 14:30

Probably because it fires more frequently than the USART or your debugging can separate.

volatile uint32_t TIM1Freq;
uint16_t Capture, ReadValue1, Readvalue2;
void TIM1_CC_IRQHandler(void)
{
static int CaptureNumber = 0;
if (TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
if (CaptureNumber == 0)
{
ReadValue1 = TIM_GetCapture2(TIM1);
CaptureNumber = 1;
}
else if(CaptureNumber == 1)
{
ReadValue2 = TIM_GetCapture2(TIM1);
Capture = (ReadValue2 - ReadValue1); // Understand unsigned 16-bit math
if (Capture) // Catch division by zero
TIM1Freq = SystemCoreClock / (uint32_t)Capture;
else
TIM1Freq = 0;
// DON'T print here, read TIM1Freq in main() loop
CaptureNumber = 0;
}
else
CaptureNumber = 0;
}
}
while(1)
{
TIM1Freq = 0;
while(TIM1Freq == 0);
printf(''%d
'', TIM1Freq);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arunl4g
Associate II
Posted on June 11, 2015 at 13:28

Thank you clive.