cancel
Showing results for 
Search instead for 
Did you mean: 

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

Abhinav Rawat
Associate
Posted on June 27, 2017 at 13:33

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.

#timer #stm32-f #input-capture
3 REPLIES 3
Posted on June 27, 2017 at 13:48

TIM_ICInitStructure.TIM_Channel = TIM_Channel_

3

;

TIM_ITConfig(TIM8, TIM_IT_CC

2

, ENABLE);

Also in the ISR

JW

Abhinav Rawat
Associate
Posted on June 27, 2017 at 14:07

Waclawek.Jan

‌ Sorry for the mistake. Other than this, is the timers and other configurations correct.?

Posted on June 27, 2017 at 15:18

I don't know, sorry, I don't use 'libraries'.

If still does not work as expected, read out and post the timer registers content.

JW