AnsweredAssumed Answered

STM32F100RB PWM Input Capture

Question asked by Sean Mutlu on Jul 8, 2017
Latest reply on Jul 9, 2017 by Clive One

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:

 

 

Source Code:

#include <stm32f10x_rcc.h>
#include <stm32f10x_gpio.h>
#include <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);

}

}

Outcomes