cancel
Showing results for 
Search instead for 
Did you mean: 

PWMInput problem

jdpv
Associate II
Posted on August 19, 2014 at 02:26

Hi,

I am trying to get the signal from a RC receiver (typical servo's signal) but i am not getting any reads even though the timer interrupt is working. PWMI - PB10 -CH3 Signal: 50 HZ (Duty 5 - 10 %)

void
TIM2_Config (
void
){
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef timerInitStructure; 
//Timer info
/* Time base configuration */
timerInitStructure.TIM_Prescaler = 30;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 65535;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure);
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOB clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* TIM2 chennel3 configuration : PB.10 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
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_UP ;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Connect TIM pin to AF2 */
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_TIM2);
/* Enable the TIM4 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); 
/* TIM Configuration */
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_PWMIConfig(TIM2, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
TIM_Cmd(TIM2, ENABLE);
/* Enable the CC3 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC3, ENABLE);
}
void
TIM2_IRQHandler(
void
)
{
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
/* Get the Input Capture value */
IC2Value = TIM_GetCapture2(TIM2);
if
(IC2Value != 0)
{
/* Duty cycle computation */
DutyCycle = (TIM_GetCapture1(TIM2) * ) / IC2Value;
/* Frequency computation 
TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
Frequency = ((RCC_Clocks.HCLK_Frequency)/60) / IC2Value;
sprintf
(&output, 
''F= %d''
, Frequency) ; 
TM_ILI9341_Puts(10, 50, &output, &TM_Font_11x18, ILI9341_COLOR_WHITE, ILI9341_COLOR_BLACK);
sprintf
(&output, 
''D= %.2f''
, DutyCycle) ; 
TM_ILI9341_Puts(10, 80, &output, &TM_Font_11x18, ILI9341_COLOR_WHITE, ILI9341_COLOR_BLACK);
}
else
{
DutyCycle = 0;
Frequency = 0;
GPIO_SetBits(GPIOG, GPIO_Pin_13);
}
}

I tried the example and it worked. STM32F429 - DISCO. Thanks. #discovery #input #stm32f4 #pwm
5 REPLIES 5
Posted on August 19, 2014 at 03:08

You must use Channel 1 or 2 for PWMInput. If you want to do multiple channels you'd have to use Input Capture and measure both edges.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jdpv
Associate II
Posted on August 19, 2014 at 15:16

Thank you clive, helpful as always.

Do you think the multiple channel(4) reading can be done with only one timer?

thanks.

Posted on August 19, 2014 at 16:33

Do you think the multiple channel(4) reading can be done with only one timer?

Yes, 50 Hz, Input Capture, Both Edges, strikes me as achievable.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jdpv
Associate II
Posted on August 20, 2014 at 21:12

Hi,

I was able to read the signal with the IC method instead of the PWMI one. Now i'm trying to add the remaining channels but i've encountered a problem. I'm using the Pin A1 with the channel 2 but it is not working(line 58 unccomented). If i enable the Channel1 interrupt source(with the code below), it triggers itself with a frequency of 2 Mhz! when my input clock is 500KHz. I think i'm not using PORTA in any other peripheral. i activated the SPI5 to use the TFT but nothing else.


void
TIM2_Config (
void
){


TIM_ICInitTypeDef TIM_ICInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

TIM_TimeBaseInitTypeDef timerInitStructure; 
//Timer info


/* TIM2 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);


/* GPIOB clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);


/* Pin configuration : A */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
// | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;

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

GPIO_Init(GPIOA, &GPIO_InitStructure);


/* Connect TIM pin to AF2 */

// GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM2);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);

// GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2);

// GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_TIM2);


/* Time base configuration */

timerInitStructure.TIM_Prescaler = 180-1;
//(SYSCLK/2)/0.5E6 - 1 = 0.5MHz

timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

timerInitStructure.TIM_Period = 65535;

timerInitStructure.TIM_ClockDivision = 0;

timerInitStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM2, &timerInitStructure);


/* Enable the TIM2 global Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure); 


/* TIM Configuration */

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; 
//TIM_Channel_1 | TIM_Channel_2 | TIM_Channel_3 | TIM_Channel_4;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0x0;


TIM_ICInit(TIM2, &TIM_ICInitStructure);


/* TIM enable counter */

TIM_Cmd(TIM2, ENABLE);


// //Enable TIM2 Channel1 interrupt source

TIM_ITConfig(TIM2, TIM_IT_CC1,ENABLE);

// //Enable TIM2 Channel2 interrupt sources

//TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);

// //Enable TIM2 Channel3 interrupt source

// TIM_ITConfig(TIM2, TIM_IT_CC3,ENABLE);

// //Enable TIM2 Channel4 interrupt sources

// TIM_ITConfig(TIM2, TIM_IT_CC4,ENABLE);

}

http://www.sciencezero.org/index.php?title=File:STM32F427_AF_mapping0.jpg Any idea? If this problem wasn't there, do you think this code will work? Thanks.