cancel
Showing results for 
Search instead for 
Did you mean: 

Inpute Capture

mosine
Associate II
Posted on September 12, 2013 at 08:54

Hi,

I'm working with STM3220G board.I am interested to understand input capture and I'm buildinga small project to capture input of Key button, but it doesn't work. This is my code:

#include ''stm32f2xx.h''
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Private function prototypes -----------------------------------------------*/
void TIM_Config(void);
void GPIONVIC_Config(void);
int main(void)
{
GPIONVIC_Config();
TIM_Config();
while (1);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET) {
//TODO 
}
}
void TIM_Config(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) (((SystemCoreClock / 1000000)/2) - 1); 
TIM_TimeBaseStructure.TIM_Period = 500-1; 
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; //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 CC1 Interrupt Request */
TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
}
void GPIONVIC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* GPIOG clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
/*PG.15 configuration - Key Button */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOG, &GPIO_InitStructure);
/* Connect TIM3 pins to AF2 */
GPIO_PinAFConfig(GPIOG, GPIO_PinSource15, GPIO_AF_TIM3);
/* 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);
}

Thanks.
6 REPLIES 6
Posted on September 12, 2013 at 13:45

but it doesn't work.

Doesn't work how?

The timer doesn't tick, you don't get an interrupt? What exactly?

PG15 relates to TIM3_CH1 how?

You might want to check if you need a pull-up on the button.

You will want to clear the interrupt if you expect to leave the IRQHandler

You probably want a maximal period on the time base.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mosine
Associate II
Posted on September 12, 2013 at 14:04

Hi clive,

I would expect that when I press the button was enabled TIM3 interrupt on CC1 to compute the delta of values latched in CCRx; but nothing happens.  

Posted on September 12, 2013 at 15:29

But you seem to have made some random association between PG15 and TIM3_CH1, does the datasheet indicate that's a viable connection?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mosine
Associate II
Posted on September 12, 2013 at 16:36

Thanks clive1,

now it works. I read datasheet and the right association Wakeup Button (GPIOA0) and TIM5 Channel1.

mosine
Associate II
Posted on September 13, 2013 at 09:47

Hi clive1,

I have a doubt about CCR1 register. The value of this register, is the time is the elapsed time between two interrupts? Because I read this values: 0x01D1 - 0x011B - 0x0014. Does this register need a reset after reading?

Thanks 

Posted on September 13, 2013 at 12:48

If you remember the last reading you can compute the delta between the current, and thus the period. In this case you've made life harder for yourself, and more limiting, by using a 500 tick timebase, which will require you do modulo 500 math.

There is a PWM mode of the timer that does reset, and permits a period and duty measurement.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..