cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100RB PWM Input Capture

Sean Mutlu
Associate II
Posted on July 08, 2017 at 23:04

Attempts to capture external PWM signal with TIM.h library have been unsuccessful. I figured using semihosting to print values to console would be helpful in debugging however the values refuse to change although I know the output is changing. I have reviewed code many times with no luck finding errors. Any guidance would be greatly appreciated. Below I attach printout in semihosting window as well as my source code. Thank you in advance. 

Semihosting Console:

0690X00000607TJQAY.png

Source Code:

&sharpinclude <stm32f10x_rcc.h>

&sharpinclude <stm32f10x_gpio.h>

&sharpinclude <stm32f10x_tim.h>

void main(void)

{

uint16_t TIM_Prescaler = 0;

uint16_t TIM_Period = 0;

int DutyCycle,Frequency,IC2Value;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

GPIO_InitTypeDef GPIO_InitStructure;

TIM_ICInitTypeDef TIM_ICInitStructure;

/* TIM3/GPIO A clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);

/* TIM3 chennel2 configuration : PA.07 */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_2 ;

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 = 0;

TIM_TimeBaseStructure.TIM_Period = TIM_Period;

TIM_TimeBaseStructure.TIM_Prescaler = TIM_Prescaler;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

/* Select the TIM3 Input Trigger: TI2FP2 */

TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

/* Select the slave Mode: Reset Mode */

TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

/* TIM enable counter */

TIM_Cmd(TIM3, ENABLE);

TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);

while (1)

{

IC2Value = TIM_GetCapture2(TIM3);

if (IC2Value != 0)

{

/* Duty cycle computation */

DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value;

Frequency = IC2Value;

}

else

{

DutyCycle = 0;

Frequency = 0;

}

printf(''DutyCycle: %d\r\n Frequency: %d\r\n'' +DutyCycle +Frequency);

}

}

#coocox #gpio.h #stm32f100rb #input-capture #pwm #tim.h
1 REPLY 1
Posted on July 09, 2017 at 00:55

You want the Period to be maximal, as all the TIM here are 16-bit, that would be 0xFFFF

You might want to select a prescaler, depending on the frequency you are trying to measure, so the TIM clocks at 1 MHz rather than 36 MHz or 72 MHz. At 1MHz the tick units would be microseconds.

Should avoid enabling the CCx interrupt unless you manage it in an IRQHandler.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..