AnsweredAssumed Answered

How to detect falling/rising edge or high/low pulse in STM32F405

Question asked by Abhinav Rawat on Jun 27, 2017
Latest reply on Jun 27, 2017 by waclawek.jan

Hello

I am working on STM32F405. I need to detect falling/rising edge or high/low pulse using timer capture mode. Basically I am trying to interface an IR remote which works on RC5 protocol with STM32. But before that I just want to know how to detect high/low pulses. For this, I am using Timer8 Channel 3 which is on PC8. I have a connected gpio to this PC8 and toggling this GPIO with 500ms. I want to capture this pulse with 500ms high and 500ms low. Below is the code I am using :

 

/***** GPIO PIN *******/

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef GPIO;
GPIO.GPIO_Pin = GPIO_Pin_10;
GPIO.GPIO_Speed = GPIO_Speed_100MHz;
GPIO.GPIO_Mode = GPIO_Mode_OUT;
GPIO.GPIO_OType = GPIO_OType_PP;
GPIO.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD,&GPIO);

 

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
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(GPIOC, &GPIO_InitStructure);


GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM8);

NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

TIM_ICInitTypeDef TIM_ICInitStructure;


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(TIM8, &TIM_ICInitStructure);

TIM_Cmd(TIM8, ENABLE);

TIM_ITConfig(TIM8, TIM_IT_CC2, ENABLE);

 

I have put the GPIO toggling code inside while(1) with the delay of 500ms.

Below is the interrupt

 

void TIM8_CC_IRQHandler(void)

{

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

     {

         //it enters in this if condition but failed to capture 

         int temp = TIM_GetCapture2(TIM8); //value of temp remains 0

     }

}

 

What is wrong in the code.? Please help.

Outcomes