AnsweredAssumed Answered

Inpute Capture

Question asked by marchetti.simone on Sep 12, 2013
Latest reply on Sep 13, 2013 by Clive One
Hi,
I'm working with STM3220G board. I am interested to understand input capture and I'm building a 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.

Outcomes